Skip to content

Commit 0b6b46c

Browse files
committed
Add add_essential_files function
1 parent 0277c2a commit 0b6b46c

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

src/bin/cratesfyi.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ pub fn main() {
8787
.arg(Arg::with_name("CRATE_VERSION")
8888
.index(2)
8989
.required(true)
90-
.help("Version of crate"))))
90+
.help("Version of crate")))
91+
.subcommand(SubCommand::with_name("add-essential-files")
92+
.about("Adds essential files for rustc")))
9193
.subcommand(SubCommand::with_name("database")
9294
.about("Database operations")
9395
.subcommand(SubCommand::with_name("init")
@@ -164,6 +166,8 @@ pub fn main() {
164166
docbuilder.build_package(matches.value_of("CRATE_NAME").unwrap(),
165167
matches.value_of("CRATE_VERSION").unwrap())
166168
.expect("Building documentation failed");
169+
} else if let Some(_) = matches.subcommand_matches("add-essential-files") {
170+
docbuilder.add_essential_files().expect("Failed to add essential files");
167171
}
168172

169173
docbuilder.save_cache().expect("Failed to save cache");

src/docbuilder/chroot_builder.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,100 @@ impl DocBuilder {
331331
package.manifest().version()));
332332
add_path_into_database(conn, &prefix, crate_doc_path)
333333
}
334+
335+
336+
/// This function will build an empty crate and will add essential documentation files.
337+
///
338+
/// It is required to run after every rustc update. cratesfyi is not keeping this files
339+
/// for every crate to avoid duplications.
340+
///
341+
/// List of the files:
342+
///
343+
/// * rustdoc.css (with rustc version)
344+
/// * main.css (with rustc version)
345+
/// * main.js (with rustc version)
346+
/// * jquery.js (with rustc version)
347+
/// * playpen.js (with rustc version)
348+
/// * normalize.css
349+
/// * FiraSans-Medium.woff
350+
/// * FiraSans-Regular.woff
351+
/// * Heuristica-Italic.woff
352+
/// * SourceCodePro-Regular.woff
353+
/// * SourceCodePro-Semibold.woff
354+
/// * SourceSerifPro-Bold.woff
355+
/// * SourceSerifPro-Regular.woff
356+
pub fn add_essential_files(&self) -> Result<(), DocBuilderError> {
357+
use std::fs::{copy, create_dir_all};
358+
359+
// acme-client-0.0.0 is an empty library crate and it will always build
360+
let pkg = try!(get_package("acme-client", Some("=0.0.0")));
361+
let res = self.build_package_in_chroot(&pkg);
362+
let rustc_version = parse_rustc_version(&res.rustc_version);
363+
364+
if !res.build_success {
365+
return Err(DocBuilderError::GenericError(format!("Failed to build empty crate for: {}",
366+
res.rustc_version)));
367+
}
368+
369+
info!("Copying essential files for: {}", res.rustc_version);
370+
371+
let files = (
372+
// files require rustc version subfix
373+
["rustdoc.css",
374+
"main.css",
375+
"main.js",
376+
"jquery.js",
377+
"playpen.js"],
378+
// files doesn't require rustc version subfix
379+
["normalize.css",
380+
"FiraSans-Medium.woff",
381+
"normalize.css",
382+
"FiraSans-Medium.woff",
383+
"FiraSans-Regular.woff",
384+
"Heuristica-Italic.woff",
385+
"SourceCodePro-Regular.woff",
386+
"SourceCodePro-Semibold.woff",
387+
"SourceSerifPro-Bold.woff",
388+
"SourceSerifPro-Regular.woff",
389+
],
390+
);
391+
392+
let source = PathBuf::from(&self.options.chroot_path)
393+
.join("home")
394+
.join(&self.options.chroot_user)
395+
.join(canonical_name(&pkg))
396+
.join("doc");
397+
398+
// use copy_documentation destination directory so self.clean can remove it when
399+
// we are done
400+
let destination = PathBuf::from(&self.options.destination)
401+
.join(format!("{}/{}",
402+
pkg.manifest().name(),
403+
pkg.manifest().version()));
404+
try!(create_dir_all(&destination));
405+
406+
for file in files.0.iter() {
407+
let source_path = source.join(file);
408+
let destination_path = {
409+
let spl: Vec<&str> = file.split('.').collect();
410+
destination.join(format!("{}-{}.{}", spl[0], rustc_version, spl[1]))
411+
};
412+
try!(copy(source_path, destination_path));
413+
}
414+
415+
for file in files.1.iter() {
416+
let source_path = source.join(file);
417+
let destination_path = destination.join(file);
418+
try!(copy(source_path, destination_path));
419+
}
420+
421+
let conn = try!(connect_db());
422+
try!(add_path_into_database(&conn, "", destination));
423+
424+
try!(self.clean(&pkg));
425+
426+
Ok(())
427+
}
334428
}
335429

336430

@@ -416,4 +510,14 @@ mod test {
416510
assert_eq!(parse_rustc_version("cratesfyi 0.2.0 (ba9ae23 2016-05-26)"),
417511
"20160526-0.2.0-ba9ae23");
418512
}
513+
514+
#[test]
515+
#[ignore]
516+
fn test_add_essential_files() {
517+
let _ = env_logger::init();
518+
let options = DocBuilderOptions::from_prefix(PathBuf::from("../cratesfyi-prefix"));
519+
let docbuilder = DocBuilder::new(options);
520+
521+
docbuilder.add_essential_files().unwrap();
522+
}
419523
}

0 commit comments

Comments
 (0)