Skip to content

Commit f89ea08

Browse files
committed
Split TOML postprocessing into a separate function
1 parent 82756fd commit f89ea08

File tree

1 file changed

+114
-99
lines changed

1 file changed

+114
-99
lines changed

src/bootstrap/src/core/config/config.rs

Lines changed: 114 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,15 @@ impl Config {
468468
let (mut toml, toml_path) = load_toml_config(&config.src, flags_config, &get_toml);
469469
config.config = toml_path.clone();
470470

471+
postprocess_toml(
472+
&mut toml,
473+
&config.src,
474+
toml_path,
475+
config.exec_ctx(),
476+
&flags_set,
477+
&get_toml,
478+
);
479+
471480
// Set flags.
472481
config.paths = std::mem::take(&mut flags_paths);
473482

@@ -534,105 +543,6 @@ impl Config {
534543
build.cargo = build.cargo.take().or(std::env::var_os("CARGO").map(|p| p.into()));
535544
}
536545

537-
if config.git_info(false, &config.src).is_from_tarball() && toml.profile.is_none() {
538-
toml.profile = Some("dist".into());
539-
}
540-
541-
// Reverse the list to ensure the last added config extension remains the most dominant.
542-
// For example, given ["a.toml", "b.toml"], "b.toml" should take precedence over "a.toml".
543-
//
544-
// This must be handled before applying the `profile` since `include`s should always take
545-
// precedence over `profile`s.
546-
for include_path in toml.include.clone().unwrap_or_default().iter().rev() {
547-
let include_path = toml_path
548-
.as_ref()
549-
.expect("include found in default TOML config")
550-
.parent()
551-
.unwrap()
552-
.join(include_path);
553-
554-
let included_toml = get_toml(&include_path).unwrap_or_else(|e| {
555-
eprintln!("ERROR: Failed to parse '{}': {e}", include_path.display());
556-
exit!(2);
557-
});
558-
toml.merge(
559-
Some(include_path),
560-
&mut Default::default(),
561-
included_toml,
562-
ReplaceOpt::IgnoreDuplicate,
563-
);
564-
}
565-
566-
if let Some(include) = &toml.profile {
567-
// Allows creating alias for profile names, allowing
568-
// profiles to be renamed while maintaining back compatibility
569-
// Keep in sync with `profile_aliases` in bootstrap.py
570-
let profile_aliases = HashMap::from([("user", "dist")]);
571-
let include = match profile_aliases.get(include.as_str()) {
572-
Some(alias) => alias,
573-
None => include.as_str(),
574-
};
575-
let mut include_path = config.src.clone();
576-
include_path.push("src");
577-
include_path.push("bootstrap");
578-
include_path.push("defaults");
579-
include_path.push(format!("bootstrap.{include}.toml"));
580-
let included_toml = get_toml(&include_path).unwrap_or_else(|e| {
581-
eprintln!(
582-
"ERROR: Failed to parse default config profile at '{}': {e}",
583-
include_path.display()
584-
);
585-
exit!(2);
586-
});
587-
toml.merge(
588-
Some(include_path),
589-
&mut Default::default(),
590-
included_toml,
591-
ReplaceOpt::IgnoreDuplicate,
592-
);
593-
}
594-
595-
let mut override_toml = TomlConfig::default();
596-
for option in flags_set.iter() {
597-
fn get_table(option: &str) -> Result<TomlConfig, toml::de::Error> {
598-
toml::from_str(option).and_then(|table: toml::Value| TomlConfig::deserialize(table))
599-
}
600-
601-
let mut err = match get_table(option) {
602-
Ok(v) => {
603-
override_toml.merge(
604-
None,
605-
&mut Default::default(),
606-
v,
607-
ReplaceOpt::ErrorOnDuplicate,
608-
);
609-
continue;
610-
}
611-
Err(e) => e,
612-
};
613-
// We want to be able to set string values without quotes,
614-
// like in `configure.py`. Try adding quotes around the right hand side
615-
if let Some((key, value)) = option.split_once('=')
616-
&& !value.contains('"')
617-
{
618-
match get_table(&format!(r#"{key}="{value}""#)) {
619-
Ok(v) => {
620-
override_toml.merge(
621-
None,
622-
&mut Default::default(),
623-
v,
624-
ReplaceOpt::ErrorOnDuplicate,
625-
);
626-
continue;
627-
}
628-
Err(e) => err = e,
629-
}
630-
}
631-
eprintln!("failed to parse override `{option}`: `{err}");
632-
exit!(2)
633-
}
634-
toml.merge(None, &mut Default::default(), override_toml, ReplaceOpt::Override);
635-
636546
config.change_id = toml.change_id.inner;
637547

638548
let Build {
@@ -2337,3 +2247,108 @@ fn load_toml_config(
23372247
(TomlConfig::default(), None)
23382248
}
23392249
}
2250+
2251+
fn postprocess_toml(
2252+
toml: &mut TomlConfig,
2253+
src_dir: &Path,
2254+
toml_path: Option<PathBuf>,
2255+
exec_ctx: &ExecutionContext,
2256+
override_set: &[String],
2257+
get_toml: &impl Fn(&Path) -> Result<TomlConfig, toml::de::Error>,
2258+
) {
2259+
let git_info = GitInfo::new(false, src_dir, exec_ctx);
2260+
2261+
if git_info.is_from_tarball() && toml.profile.is_none() {
2262+
toml.profile = Some("dist".into());
2263+
}
2264+
2265+
// Reverse the list to ensure the last added config extension remains the most dominant.
2266+
// For example, given ["a.toml", "b.toml"], "b.toml" should take precedence over "a.toml".
2267+
//
2268+
// This must be handled before applying the `profile` since `include`s should always take
2269+
// precedence over `profile`s.
2270+
for include_path in toml.include.clone().unwrap_or_default().iter().rev() {
2271+
let include_path = toml_path
2272+
.as_ref()
2273+
.expect("include found in default TOML config")
2274+
.parent()
2275+
.unwrap()
2276+
.join(include_path);
2277+
2278+
let included_toml = get_toml(&include_path).unwrap_or_else(|e| {
2279+
eprintln!("ERROR: Failed to parse '{}': {e}", include_path.display());
2280+
exit!(2);
2281+
});
2282+
toml.merge(
2283+
Some(include_path),
2284+
&mut Default::default(),
2285+
included_toml,
2286+
ReplaceOpt::IgnoreDuplicate,
2287+
);
2288+
}
2289+
2290+
if let Some(include) = &toml.profile {
2291+
// Allows creating alias for profile names, allowing
2292+
// profiles to be renamed while maintaining back compatibility
2293+
// Keep in sync with `profile_aliases` in bootstrap.py
2294+
let profile_aliases = HashMap::from([("user", "dist")]);
2295+
let include = match profile_aliases.get(include.as_str()) {
2296+
Some(alias) => alias,
2297+
None => include.as_str(),
2298+
};
2299+
let mut include_path = PathBuf::from(src_dir);
2300+
include_path.push("src");
2301+
include_path.push("bootstrap");
2302+
include_path.push("defaults");
2303+
include_path.push(format!("bootstrap.{include}.toml"));
2304+
let included_toml = get_toml(&include_path).unwrap_or_else(|e| {
2305+
eprintln!(
2306+
"ERROR: Failed to parse default config profile at '{}': {e}",
2307+
include_path.display()
2308+
);
2309+
exit!(2);
2310+
});
2311+
toml.merge(
2312+
Some(include_path.into()),
2313+
&mut Default::default(),
2314+
included_toml,
2315+
ReplaceOpt::IgnoreDuplicate,
2316+
);
2317+
}
2318+
2319+
let mut override_toml = TomlConfig::default();
2320+
for option in override_set.iter() {
2321+
fn get_table(option: &str) -> Result<TomlConfig, toml::de::Error> {
2322+
toml::from_str(option).and_then(|table: toml::Value| TomlConfig::deserialize(table))
2323+
}
2324+
2325+
let mut err = match get_table(option) {
2326+
Ok(v) => {
2327+
override_toml.merge(None, &mut Default::default(), v, ReplaceOpt::ErrorOnDuplicate);
2328+
continue;
2329+
}
2330+
Err(e) => e,
2331+
};
2332+
// We want to be able to set string values without quotes,
2333+
// like in `configure.py`. Try adding quotes around the right hand side
2334+
if let Some((key, value)) = option.split_once('=')
2335+
&& !value.contains('"')
2336+
{
2337+
match get_table(&format!(r#"{key}="{value}""#)) {
2338+
Ok(v) => {
2339+
override_toml.merge(
2340+
None,
2341+
&mut Default::default(),
2342+
v,
2343+
ReplaceOpt::ErrorOnDuplicate,
2344+
);
2345+
continue;
2346+
}
2347+
Err(e) => err = e,
2348+
}
2349+
}
2350+
eprintln!("failed to parse override `{option}`: `{err}");
2351+
exit!(2)
2352+
}
2353+
toml.merge(None, &mut Default::default(), override_toml, ReplaceOpt::Override);
2354+
}

0 commit comments

Comments
 (0)