Skip to content

Commit 579f83b

Browse files
authored
Merge pull request #182 from onur/rustdoc-resource-suffix
rustdoc resource suffix changes
2 parents 97b8b0a + 4763662 commit 579f83b

File tree

10 files changed

+104
-119
lines changed

10 files changed

+104
-119
lines changed

.travis.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
dist: precise
21
language: rust
3-
addons:
4-
apt:
5-
sources:
6-
- kalakris-cmake
7-
packages:
8-
- cmake
2+
cache: cargo
93
before_install:
104
- sudo apt-get update -qq
115
- sudo apt-get install -qq libmagic1 libmagic-dev
6+
script: cargo build --verbose && cargo test --verbose -- --test-threads=1

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cratesfyi"
3-
version = "0.4.2"
3+
version = "0.5.0"
44
authors = ["Onur Aslan <onuraslan@gmail.com>"]
55
readme = "README.md"
66
license = "MIT"

src/docbuilder/chroot_builder.rs

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11

22
use super::DocBuilder;
33
use super::crates::crates_from_path;
4-
use utils::{get_package, source_path, copy_dir, copy_doc_dir, update_sources};
4+
use utils::{get_package, source_path, copy_dir, copy_doc_dir,
5+
update_sources, parse_rustc_version, command_result};
56
use db::{connect_db, add_package_into_database, add_build_into_database, add_path_into_database};
67
use cargo::core::Package;
7-
use std::process::{Command, Output};
8+
use std::process::Command;
89
use std::path::PathBuf;
910
use std::fs::remove_dir_all;
1011
use postgres::Connection;
1112
use rustc_serialize::json::Json;
1213
use error::Result;
13-
use regex::Regex;
1414

1515

1616
/// List of targets supported by docs.rs
@@ -208,7 +208,7 @@ impl DocBuilder {
208208
.join(target.unwrap_or(""));
209209
copy_doc_dir(crate_doc_path,
210210
destination,
211-
parse_rustc_version(rustc_version).trim(),
211+
parse_rustc_version(rustc_version)?.trim(),
212212
target.is_some())
213213
}
214214

@@ -353,7 +353,7 @@ impl DocBuilder {
353353
// acme-client-0.0.0 is an empty library crate and it will always build
354354
let pkg = try!(get_package("acme-client", Some("=0.0.0")));
355355
let res = self.build_package_in_chroot(&pkg);
356-
let rustc_version = parse_rustc_version(&res.rustc_version);
356+
let rustc_version = parse_rustc_version(&res.rustc_version)?;
357357

358358
if !res.build_success {
359359
return Err(format!("Failed to build empty crate for: {}", res.rustc_version).into());
@@ -362,7 +362,14 @@ impl DocBuilder {
362362
info!("Copying essential files for: {}", res.rustc_version);
363363

364364
let files = (// files require rustc version subfix
365-
["rustdoc.css", "main.css", "main.js", "normalize.css"],
365+
["brush.svg",
366+
"dark.css",
367+
"main.css",
368+
"main.js",
369+
"normalize.css",
370+
"rustdoc.css",
371+
"storage.js",
372+
"theme.js"],
366373
// files doesn't require rustc version subfix
367374
["FiraSans-Medium.woff",
368375
"FiraSans-Regular.woff",
@@ -385,11 +392,10 @@ impl DocBuilder {
385392
try!(create_dir_all(&destination));
386393

387394
for file in files.0.iter() {
388-
let source_path = source.join(file);
389-
let destination_path = {
390-
let spl: Vec<&str> = file.split('.').collect();
391-
destination.join(format!("{}-{}.{}", spl[0], rustc_version, spl[1]))
392-
};
395+
let spl: Vec<&str> = file.split('.').collect();
396+
let file_name = format!("{}-{}.{}", spl[0], rustc_version, spl[1]);
397+
let source_path = source.join(&file_name);
398+
let destination_path = destination.join(&file_name);
393399
try!(copy(source_path, destination_path));
394400
}
395401

@@ -409,18 +415,6 @@ impl DocBuilder {
409415
}
410416

411417

412-
/// Simple function to capture command output
413-
fn command_result(output: Output) -> Result<String> {
414-
let mut command_out = String::from_utf8_lossy(&output.stdout).into_owned();
415-
command_out.push_str(&String::from_utf8_lossy(&output.stderr).into_owned()[..]);
416-
match output.status.success() {
417-
true => Ok(command_out),
418-
false => Err(command_out.into()),
419-
}
420-
}
421-
422-
423-
424418
/// Returns canonical name of a package.
425419
///
426420
/// It's just package-version. All directory structure used in cratesfyi is
@@ -432,20 +426,6 @@ fn canonical_name(package: &Package) -> String {
432426
}
433427

434428

435-
/// Parses rustc commit hash from rustc version string
436-
fn parse_rustc_version<S: AsRef<str>>(version: S) -> String {
437-
let version_regex = Regex::new(r" ([\w-.]+) \((\w+) (\d+)-(\d+)-(\d+)\)").unwrap();
438-
let captures = version_regex.captures(version.as_ref()).expect("Failed to parse rustc version");
439-
440-
format!("{}{}{}-{}-{}",
441-
captures.get(3).unwrap().as_str(),
442-
captures.get(4).unwrap().as_str(),
443-
captures.get(5).unwrap().as_str(),
444-
captures.get(1).unwrap().as_str(),
445-
captures.get(2).unwrap().as_str())
446-
}
447-
448-
449429
/// Runs `func` with the all crates from crates-io.index repository path.
450430
///
451431
/// First argument of func is the name of crate and
@@ -460,7 +440,6 @@ fn crates<F>(path: PathBuf, mut func: F) -> Result<()>
460440
#[cfg(test)]
461441
mod test {
462442
extern crate env_logger;
463-
use super::parse_rustc_version;
464443
use std::path::PathBuf;
465444
use {DocBuilder, DocBuilderOptions};
466445

@@ -484,14 +463,6 @@ mod test {
484463
assert!(res.is_ok());
485464
}
486465

487-
#[test]
488-
fn test_parse_rustc_version() {
489-
assert_eq!(parse_rustc_version("rustc 1.10.0-nightly (57ef01513 2016-05-23)"),
490-
"20160523-1.10.0-nightly-57ef01513");
491-
assert_eq!(parse_rustc_version("cratesfyi 0.2.0 (ba9ae23 2016-05-26)"),
492-
"20160526-0.2.0-ba9ae23");
493-
}
494-
495466
#[test]
496467
#[ignore]
497468
fn test_add_essential_files() {

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use cargo;
77
use reqwest;
88
use magic::MagicError;
99
use git2;
10+
use regex;
1011

1112

1213
error_chain! {
@@ -19,5 +20,6 @@ error_chain! {
1920
Git2Error(git2::Error);
2021
MagicError(MagicError);
2122
CargoError(Box<cargo::CargoError>);
23+
RegexError(regex::Error);
2224
}
2325
}

src/utils/build_doc.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use cargo::util::{CargoResult, Config, human, Filesystem};
1212
use cargo::sources::SourceConfigMap;
1313
use cargo::ops::{self, Packages, DefaultExecutor};
1414

15+
use utils::{get_current_versions, parse_rustc_version};
16+
use error::Result;
17+
1518
use Metadata;
1619

1720

@@ -23,7 +26,7 @@ use Metadata;
2326
// idea is to make cargo to download
2427
// and build a crate and its documentation
2528
// instead of doing it manually like in the previous version of cratesfyi
26-
pub fn build_doc(name: &str, vers: Option<&str>, target: Option<&str>) -> CargoResult<Package> {
29+
pub fn build_doc(name: &str, vers: Option<&str>, target: Option<&str>) -> Result<Package> {
2730
let config = try!(Config::default());
2831
let source_id = try!(SourceId::crates_io(&config));
2932

@@ -55,6 +58,16 @@ pub fn build_doc(name: &str, vers: Option<&str>, target: Option<&str>) -> CargoR
5558
env::set_var("RUSTFLAGS", rustc_args.join(" "));
5659
}
5760

61+
// since https://github.com/rust-lang/rust/pull/48511 we can pass --resource-suffix to
62+
// add correct version numbers to css and javascript files
63+
let mut rustdoc_args: Vec<String> =
64+
vec!["-Z".to_string(), "unstable-options".to_string(),
65+
"--resource-suffix".to_string(),
66+
format!("-{}", parse_rustc_version(get_current_versions()?.0)?)];
67+
if let Some(package_rustdoc_args) = metadata.rustdoc_args {
68+
rustdoc_args.append(&mut package_rustdoc_args.iter().map(|s| s.to_owned()).collect());
69+
}
70+
5871
let opts = ops::CompileOptions {
5972
config: &config,
6073
jobs: None,
@@ -72,7 +85,7 @@ pub fn build_doc(name: &str, vers: Option<&str>, target: Option<&str>) -> CargoR
7285
&[], false,
7386
&[], false),
7487
target_rustc_args: None,
75-
target_rustdoc_args: metadata.rustdoc_args.as_ref().map(Vec::as_slice),
88+
target_rustdoc_args: Some(rustdoc_args.as_slice()),
7689
};
7790

7891
let ws = try!(Workspace::ephemeral(pkg, &config, Some(Filesystem::new(target_dir)), false));
@@ -92,6 +105,8 @@ pub fn get_package(name: &str, vers: Option<&str>) -> CargoResult<Package> {
92105
let source_map = try!(SourceConfigMap::new(&config));
93106
let mut source = try!(source_map.load(&source_id));
94107

108+
try!(source.update());
109+
95110
let dep = try!(Dependency::parse_no_deprecated(name, vers, &source_id));
96111
let deps = try!(source.query(&dep));
97112
let pkg = try!(deps.iter().map(|p| p.package_id()).max()
@@ -128,23 +143,8 @@ pub fn source_path(pkg: &Package) -> Option<&Path> {
128143
#[cfg(test)]
129144
mod test {
130145
use std::path::Path;
131-
use std::fs::remove_dir_all;
132146
use super::*;
133147

134-
#[test]
135-
fn test_build_doc() {
136-
let doc = build_doc("rand", None, None);
137-
assert!(doc.is_ok());
138-
139-
let root_path = Path::new("cratesfyi");
140-
assert!(root_path.join("doc").join("rand").exists());
141-
142-
remove_dir_all(root_path).unwrap();
143-
144-
let doc = build_doc("SOMECRATEWICHWILLBENVEREXISTS", None, None);
145-
assert!(doc.is_err());
146-
}
147-
148148
#[test]
149149
fn test_get_package() {
150150
let pkg = get_package("rand", None);

src/utils/copy.rs

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// FIXME: There is so many PathBuf's in this module
33
// Conver them to Path
44

5-
use std::io::prelude::*;
6-
use std::io;
75
use std::path::{Path, PathBuf};
86
use std::fs;
97
use error::Result;
@@ -48,13 +46,17 @@ fn copy_files_and_handle_html(source: PathBuf,
4846
target: bool)
4947
-> Result<()> {
5048

49+
// FIXME: handle_html is useless since we started using --resource-suffix
50+
// argument with rustdoc
51+
5152
// Make sure destination directory is exists
5253
if !destination.exists() {
5354
try!(fs::create_dir_all(&destination));
5455
}
5556

56-
// Avoid copying duplicated files
57-
let dup_regex = Regex::new(r"(\.lock|\.txt|\.woff|jquery\.js|playpen\.js|main\.js|\.css)$")
57+
// Avoid copying common files
58+
let dup_regex = Regex::new(
59+
r"(\.lock|\.txt|\.woff|\.svg|\.css|main-.*\.css|main-.*\.js|normalize-.*\.js|rustdoc-.*\.css|storage-.*\.js|theme-.*\.js)$")
5860
.unwrap();
5961

6062
for file in try!(source.read_dir()) {
@@ -72,8 +74,6 @@ fn copy_files_and_handle_html(source: PathBuf,
7274
handle_html,
7375
&rustc_version,
7476
target));
75-
} else if handle_html && file.file_name().into_string().unwrap().ends_with(".html") {
76-
try!(copy_html(&file.path(), &destination_full_path, rustc_version, target));
7777
} else if handle_html && dup_regex.is_match(&file.file_name().into_string().unwrap()[..]) {
7878
continue;
7979
} else {
@@ -85,45 +85,6 @@ fn copy_files_and_handle_html(source: PathBuf,
8585
}
8686

8787

88-
fn copy_html(source: &PathBuf,
89-
destination: &PathBuf,
90-
rustc_version: &str,
91-
target: bool)
92-
-> Result<()> {
93-
94-
let source_file = try!(fs::File::open(source));
95-
let mut destination_file = try!(fs::OpenOptions::new()
96-
.write(true)
97-
.create(true)
98-
.open(destination));
99-
100-
let reader = io::BufReader::new(source_file);
101-
102-
// FIXME: We don't need to store common libraries (jquery and normalize) for the each version
103-
// of rustc. I believe storing only one version of this files should work in every
104-
// documentation page.
105-
let replace_regex =
106-
Regex::new(r#"(href|src)="(.*)(main|jquery|rustdoc|playpen|normalize)\.(css|js)""#)
107-
.unwrap();
108-
let replace_str = format!("$1=\"{}../../$2$3-{}.$4\"",
109-
if target { "../" } else { "" },
110-
rustc_version);
111-
112-
for line in reader.lines() {
113-
let mut line = try!(line);
114-
115-
// replace css links
116-
line = replace_regex.replace_all(&line[..], &replace_str[..]).into_owned();
117-
118-
try!(destination_file.write(line.as_bytes()));
119-
// need to write consumed newline
120-
try!(destination_file.write(&['\n' as u8]));
121-
}
122-
123-
Ok(())
124-
}
125-
126-
12788

12889
#[cfg(test)]
12990
mod test {

src/utils/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ pub use self::copy::{copy_dir, copy_doc_dir};
66
pub use self::github_updater::github_updater;
77
pub use self::release_activity_updater::update_release_activity;
88
pub use self::daemon::start_daemon;
9+
pub use self::rustc_version::{parse_rustc_version, get_current_versions, command_result};
910

1011
mod github_updater;
1112
mod build_doc;
1213
mod copy;
1314
mod release_activity_updater;
1415
mod daemon;
1516
mod pubsubhubbub;
17+
mod rustc_version;

0 commit comments

Comments
 (0)