Skip to content

Commit 3b904e9

Browse files
Clean code a bit
1 parent 465bf7b commit 3b904e9

File tree

1 file changed

+155
-166
lines changed

1 file changed

+155
-166
lines changed

src/docbuilder/rustwide_builder.rs

Lines changed: 155 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -476,31 +476,19 @@ impl RustwideBuilder {
476476
Ok(())
477477
}
478478

479-
fn get_coverage(
479+
fn make_build_object<T, F: Fn(Vec<&str>, Vec<String>, LogStorage) -> Result<T>>(
480480
&self,
481481
target: &str,
482-
build: &Build,
483482
metadata: &Metadata,
484483
limits: &Limits,
485-
) -> Option<(i32, i32)> {
486-
let rustdoc_flags: Vec<String> = vec![
487-
"-Z".to_string(),
488-
"unstable-options".to_string(),
489-
"--static-root-path".to_string(),
490-
"/".to_string(),
491-
"--cap-lints".to_string(),
492-
"warn".to_string(),
493-
"--output-format".to_string(),
494-
"json".to_string(),
495-
"--show-coverage".to_string(),
496-
];
497-
484+
f: F,
485+
) -> Result<T> {
498486
let mut cargo_args = vec!["doc", "--lib", "--no-deps"];
499487
if target != HOST_TARGET {
500488
// If the explicit target is not a tier one target, we need to install it.
501489
if !TARGETS.contains(&target) {
502490
// This is a no-op if the target is already installed.
503-
self.toolchain.add_target(&self.workspace, target).ok()?;
491+
self.toolchain.add_target(&self.workspace, target)?;
504492
}
505493
cargo_args.push("--target");
506494
cargo_args.push(target);
@@ -528,52 +516,78 @@ impl RustwideBuilder {
528516
let mut storage = LogStorage::new(LevelFilter::Info);
529517
storage.set_max_size(limits.max_log_size());
530518

531-
let mut json = String::new();
532-
if build
533-
.cargo()
534-
.timeout(Some(limits.timeout()))
535-
.no_output_timeout(None)
536-
.env(
537-
"RUSTFLAGS",
538-
metadata
539-
.rustc_args
540-
.as_ref()
541-
.map(|args| args.join(" "))
542-
.unwrap_or_default(),
543-
)
544-
.env("RUSTDOCFLAGS", rustdoc_flags.join(" "))
545-
// For docs.rs detection from build script:
546-
// https://github.com/rust-lang/docs.rs/issues/147
547-
.env("DOCS_RS", "1")
548-
.args(&cargo_args)
549-
.log_output(false)
550-
.process_lines(&mut |line, _| {
551-
if line.starts_with('{') && line.ends_with('}') {
552-
json = line.to_owned();
553-
}
554-
})
555-
.run()
556-
.is_ok()
557-
{
558-
match serde_json::from_str(&json).expect("conversion failed...") {
559-
Value::Object(m) => {
560-
let mut total = 0;
561-
let mut documented = 0;
562-
for entry in m.values() {
563-
if let Some(Value::Number(n)) = entry.get("total") {
564-
total += n.as_i64().unwrap_or(0) as i32;
519+
let rustdoc_flags: Vec<String> = vec![
520+
"-Z".to_string(),
521+
"unstable-options".to_string(),
522+
"--static-root-path".to_string(),
523+
"/".to_string(),
524+
"--cap-lints".to_string(),
525+
"warn".to_string(),
526+
];
527+
528+
f(cargo_args, rustdoc_flags, storage)
529+
}
530+
531+
fn get_coverage(
532+
&self,
533+
target: &str,
534+
build: &Build,
535+
metadata: &Metadata,
536+
limits: &Limits,
537+
) -> Result<Option<(i32, i32)>> {
538+
self.make_build_object(
539+
target,
540+
metadata,
541+
limits,
542+
|cargo_args, mut rustdoc_flags, _| {
543+
rustdoc_flags.extend(vec![
544+
"--output-format".to_string(),
545+
"json".to_string(),
546+
"--show-coverage".to_string(),
547+
]);
548+
let mut doc_coverage_json = None;
549+
build
550+
.cargo()
551+
.timeout(Some(limits.timeout()))
552+
.no_output_timeout(None)
553+
.env(
554+
"RUSTFLAGS",
555+
metadata
556+
.rustc_args
557+
.as_ref()
558+
.map(|args| args.join(" "))
559+
.unwrap_or_default(),
560+
)
561+
.env("RUSTDOCFLAGS", rustdoc_flags.join(" "))
562+
// For docs.rs detection from build script:
563+
// https://github.com/rust-lang/docs.rs/issues/147
564+
.env("DOCS_RS", "1")
565+
.args(&cargo_args)
566+
.log_output(false)
567+
.process_lines(&mut |line, _| {
568+
if line.starts_with('{') && line.ends_with('}') {
569+
doc_coverage_json = Some(line.to_owned());
565570
}
566-
if let Some(Value::Number(n)) = entry.get("with_docs") {
567-
documented += n.as_i64().unwrap_or(0) as i32;
571+
})
572+
.run()
573+
.ok();
574+
if let Some(json) = doc_coverage_json {
575+
if let Ok(Value::Object(m)) = serde_json::from_str(&json) {
576+
let (mut total, mut documented) = (0, 0);
577+
for entry in m.values() {
578+
if let Some(Value::Number(n)) = entry.get("total") {
579+
total += n.as_i64().unwrap_or(0) as i32;
580+
}
581+
if let Some(Value::Number(n)) = entry.get("with_docs") {
582+
documented += n.as_i64().unwrap_or(0) as i32;
583+
}
568584
}
585+
return Ok(Some((total, documented)));
569586
}
570-
Some((total, documented))
571587
}
572-
_ => None,
573-
}
574-
} else {
575-
None
576-
}
588+
Ok(None)
589+
},
590+
)
577591
}
578592

579593
fn execute_build(
@@ -584,118 +598,93 @@ impl RustwideBuilder {
584598
limits: &Limits,
585599
metadata: &Metadata,
586600
) -> Result<FullBuildResult> {
587-
let cargo_metadata =
588-
CargoMetadata::load(&self.workspace, &self.toolchain, &build.host_source_dir())?;
589-
590-
let mut rustdoc_flags: Vec<String> = vec![
591-
"-Z".to_string(),
592-
"unstable-options".to_string(),
593-
"--resource-suffix".to_string(),
594-
format!("-{}", parse_rustc_version(&self.rustc_version)?),
595-
"--static-root-path".to_string(),
596-
"/".to_string(),
597-
"--cap-lints".to_string(),
598-
"warn".to_string(),
599-
];
600-
for dep in &cargo_metadata.root_dependencies() {
601-
rustdoc_flags.push("--extern-html-root-url".to_string());
602-
rustdoc_flags.push(format!(
603-
"{}=https://docs.rs/{}/{}",
604-
dep.name.replace("-", "_"),
605-
dep.name,
606-
dep.version
607-
));
608-
}
609-
if let Some(package_rustdoc_args) = &metadata.rustdoc_args {
610-
rustdoc_flags.append(&mut package_rustdoc_args.iter().map(|s| s.to_owned()).collect());
611-
}
612-
let mut cargo_args = vec!["doc", "--lib", "--no-deps"];
613-
if target != HOST_TARGET {
614-
// If the explicit target is not a tier one target, we need to install it.
615-
if !TARGETS.contains(&target) {
616-
// This is a no-op if the target is already installed.
617-
self.toolchain.add_target(&self.workspace, target)?;
618-
}
619-
cargo_args.push("--target");
620-
cargo_args.push(target);
621-
};
622-
623-
let tmp_jobs;
624-
if let Some(cpu_limit) = self.cpu_limit {
625-
tmp_jobs = format!("-j{}", cpu_limit);
626-
cargo_args.push(&tmp_jobs);
627-
}
628-
629-
let tmp;
630-
if let Some(features) = &metadata.features {
631-
cargo_args.push("--features");
632-
tmp = features.join(" ");
633-
cargo_args.push(&tmp);
634-
}
635-
if metadata.all_features {
636-
cargo_args.push("--all-features");
637-
}
638-
if metadata.no_default_features {
639-
cargo_args.push("--no-default-features");
640-
}
601+
self.make_build_object(
602+
target,
603+
metadata,
604+
limits,
605+
|cargo_args, mut rustdoc_flags, storage| {
606+
rustdoc_flags.extend(vec![
607+
"--resource-suffix".to_string(),
608+
format!("-{}", parse_rustc_version(&self.rustc_version)?),
609+
]);
610+
let cargo_metadata = CargoMetadata::load(
611+
&self.workspace,
612+
&self.toolchain,
613+
&build.host_source_dir(),
614+
)?;
641615

642-
let mut storage = LogStorage::new(LevelFilter::Info);
643-
storage.set_max_size(limits.max_log_size());
616+
for dep in &cargo_metadata.root_dependencies() {
617+
rustdoc_flags.push("--extern-html-root-url".to_string());
618+
rustdoc_flags.push(format!(
619+
"{}=https://docs.rs/{}/{}",
620+
dep.name.replace("-", "_"),
621+
dep.name,
622+
dep.version
623+
));
624+
}
625+
if let Some(package_rustdoc_args) = &metadata.rustdoc_args {
626+
rustdoc_flags
627+
.append(&mut package_rustdoc_args.iter().map(|s| s.to_owned()).collect());
628+
}
644629

645-
let successful = logging::capture(&storage, || {
646-
build
647-
.cargo()
648-
.timeout(Some(limits.timeout()))
649-
.no_output_timeout(None)
650-
.env(
651-
"RUSTFLAGS",
652-
metadata
653-
.rustc_args
654-
.as_ref()
655-
.map(|args| args.join(" "))
656-
.unwrap_or_default(),
657-
)
658-
.env("RUSTDOCFLAGS", rustdoc_flags.join(" "))
659-
// For docs.rs detection from build script:
660-
// https://github.com/rust-lang/docs.rs/issues/147
661-
.env("DOCS_RS", "1")
662-
.args(&cargo_args)
663-
.run()
664-
.is_ok()
665-
});
666-
let mut total_items = None;
667-
let mut documented_items = None;
668-
if successful {
669-
if let Some((total, documented)) = self.get_coverage(target, build, metadata, limits) {
670-
total_items = Some(total);
671-
documented_items = Some(documented);
672-
}
673-
}
674-
// If we're passed a default_target which requires a cross-compile,
675-
// cargo will put the output in `target/<target>/doc`.
676-
// However, if this is the default build, we don't want it there,
677-
// we want it in `target/doc`.
678-
if target != HOST_TARGET && is_default_target {
679-
// mv target/$target/doc target/doc
680-
let target_dir = build.host_target_dir();
681-
let old_dir = target_dir.join(target).join("doc");
682-
let new_dir = target_dir.join("doc");
683-
debug!("rename {} to {}", old_dir.display(), new_dir.display());
684-
std::fs::rename(old_dir, new_dir)?;
685-
}
630+
let successful = logging::capture(&storage, || {
631+
build
632+
.cargo()
633+
.timeout(Some(limits.timeout()))
634+
.no_output_timeout(None)
635+
.env(
636+
"RUSTFLAGS",
637+
metadata
638+
.rustc_args
639+
.as_ref()
640+
.map(|args| args.join(" "))
641+
.unwrap_or_default(),
642+
)
643+
.env("RUSTDOCFLAGS", rustdoc_flags.join(" "))
644+
// For docs.rs detection from build script:
645+
// https://github.com/rust-lang/docs.rs/issues/147
646+
.env("DOCS_RS", "1")
647+
.args(&cargo_args)
648+
.run()
649+
.is_ok()
650+
});
651+
let mut total_items = None;
652+
let mut documented_items = None;
653+
if successful {
654+
if let Some((total, documented)) =
655+
self.get_coverage(target, build, metadata, limits)?
656+
{
657+
total_items = Some(total);
658+
documented_items = Some(documented);
659+
}
660+
}
661+
// If we're passed a default_target which requires a cross-compile,
662+
// cargo will put the output in `target/<target>/doc`.
663+
// However, if this is the default build, we don't want it there,
664+
// we want it in `target/doc`.
665+
if target != HOST_TARGET && is_default_target {
666+
// mv target/$target/doc target/doc
667+
let target_dir = build.host_target_dir();
668+
let old_dir = target_dir.join(target).join("doc");
669+
let new_dir = target_dir.join("doc");
670+
debug!("rename {} to {}", old_dir.display(), new_dir.display());
671+
std::fs::rename(old_dir, new_dir)?;
672+
}
686673

687-
Ok(FullBuildResult {
688-
result: BuildResult {
689-
build_log: storage.to_string(),
690-
rustc_version: self.rustc_version.clone(),
691-
docsrs_version: format!("docsrs {}", crate::BUILD_VERSION),
692-
successful,
693-
total_items,
694-
documented_items,
674+
Ok(FullBuildResult {
675+
result: BuildResult {
676+
build_log: storage.to_string(),
677+
rustc_version: self.rustc_version.clone(),
678+
docsrs_version: format!("docsrs {}", crate::BUILD_VERSION),
679+
successful,
680+
total_items,
681+
documented_items,
682+
},
683+
cargo_metadata,
684+
target: target.to_string(),
685+
})
695686
},
696-
cargo_metadata,
697-
target: target.to_string(),
698-
})
687+
)
699688
}
700689

701690
fn copy_docs(

0 commit comments

Comments
 (0)