about summary refs log tree commit diff
path: root/src/librustdoc/html/render
diff options
context:
space:
mode:
authorJoshua Nelson <jyn514@gmail.com>2021-03-25 12:46:35 -0400
committerJoshua Nelson <jyn514@gmail.com>2021-03-25 20:03:44 -0400
commitf77ebd4ffaea7fc5af49425cafefe141e7458cc3 (patch)
tree53c07eb79f74b270df4a919dd6c65aaa1ccfba8a /src/librustdoc/html/render
parent7c89cc4a6f68a9c544ff1b000c8f7ef1c3535278 (diff)
downloadrust-f77ebd4ffaea7fc5af49425cafefe141e7458cc3.tar.gz
rust-f77ebd4ffaea7fc5af49425cafefe141e7458cc3.zip
Add unstable option to only emit shared/crate-specific files
The intended use case is for docs.rs, which can now copy exactly the
files it cares about, rather than having to guess based on whether they
have a resource suffix or not. In particular, some files have a resource
suffix but cannot be shared between crates: https://github.com/rust-lang/docs.rs/pull/1312#issuecomment-798783688

The end goal is to fix https://github.com/rust-lang/docs.rs/issues/1327
by reverting https://github.com/rust-lang/docs.rs/pull/1324.

This obsoletes `--print=unversioned-files`, which I plan to remove as
soon as docs.rs stops using it.
Diffstat (limited to 'src/librustdoc/html/render')
-rw-r--r--src/librustdoc/html/render/context.rs5
-rw-r--r--src/librustdoc/html/render/write_shared.rs47
2 files changed, 40 insertions, 12 deletions
diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs
index 468bd9997a6..17cedeb5a51 100644
--- a/src/librustdoc/html/render/context.rs
+++ b/src/librustdoc/html/render/context.rs
@@ -288,6 +288,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
     ) -> Result<(Self, clean::Crate), Error> {
         // need to save a copy of the options for rendering the index page
         let md_opts = options.clone();
+        let emit_crate = options.should_emit_crate();
         let RenderOptions {
             output,
             external_html,
@@ -393,7 +394,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
 
         let dst = output;
         scx.ensure_dir(&dst)?;
-        krate = sources::render(&dst, &mut scx, krate)?;
+        if emit_crate {
+            krate = sources::render(&dst, &mut scx, krate)?;
+        }
 
         // Build our search index
         let index = build_index(&krate, &mut cache, tcx);
diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs
index 2ab423c238c..af346bb8225 100644
--- a/src/librustdoc/html/render/write_shared.rs
+++ b/src/librustdoc/html/render/write_shared.rs
@@ -13,7 +13,7 @@ use serde::Serialize;
 
 use super::{collect_paths_for_type, ensure_trailing_slash, Context, BASIC_KEYWORDS};
 use crate::clean::Crate;
-use crate::config::RenderOptions;
+use crate::config::{EmitType, RenderOptions};
 use crate::docfs::PathError;
 use crate::error::Error;
 use crate::formats::FormatRenderer;
@@ -72,6 +72,18 @@ impl SharedResource<'_> {
             SharedResource::CrateSpecific { basename } => cx.suffix_path(basename),
         }
     }
+
+    fn should_emit(&self, emit: &[EmitType]) -> bool {
+        if emit.is_empty() {
+            return true;
+        }
+        let kind = match self {
+            SharedResource::Unversioned { .. } => EmitType::Unversioned,
+            SharedResource::ToolchainSpecific { .. } => EmitType::Toolchain,
+            SharedResource::CrateSpecific { .. } => EmitType::CrateSpecific,
+        };
+        emit.contains(&kind)
+    }
 }
 
 impl Context<'_> {
@@ -86,9 +98,17 @@ impl Context<'_> {
         self.dst.join(&filename)
     }
 
-    fn write_shared<C: AsRef<[u8]>>(&self, resource: SharedResource<'_>, contents: C) -> Result<(), Error>
-    {
-        self.shared.fs.write(resource.path(self), contents)
+    fn write_shared<C: AsRef<[u8]>>(
+        &self,
+        resource: SharedResource<'_>,
+        contents: C,
+        emit: &[EmitType],
+    ) -> Result<(), Error> {
+        if resource.should_emit(emit) {
+            self.shared.fs.write(resource.path(self), contents)
+        } else {
+            Ok(())
+        }
     }
 
     fn write_minify(
@@ -96,6 +116,7 @@ impl Context<'_> {
         resource: SharedResource<'_>,
         contents: &str,
         minify: bool,
+        emit: &[EmitType],
     ) -> Result<(), Error> {
         let tmp;
         let contents = if minify {
@@ -111,7 +132,7 @@ impl Context<'_> {
             contents.as_bytes()
         };
 
-        self.write_shared(resource, contents)
+        self.write_shared(resource, contents, emit)
     }
 }
 
@@ -133,10 +154,14 @@ pub(super) fn write_shared(
             SharedResource::ToolchainSpecific { basename: p },
             c,
             options.enable_minification,
+            &options.emit,
         )
     };
-    let write_toolchain =
-        |p: &_, c: &_| cx.write_shared(SharedResource::ToolchainSpecific { basename: p }, c);
+    let write_toolchain = |p: &_, c: &_| {
+        cx.write_shared(SharedResource::ToolchainSpecific { basename: p }, c, &options.emit)
+    };
+    let write_crate =
+        |p, c: &_| cx.write_shared(SharedResource::CrateSpecific { basename: p }, c, &options.emit);
 
     // Add all the static files. These may already exist, but we just
     // overwrite them anyway to make sure that they're fresh and up-to-date.
@@ -214,7 +239,7 @@ pub(super) fn write_shared(
     }
     write_minify("normalize.css", static_files::NORMALIZE_CSS)?;
     for (name, contents) in &*FILES_UNVERSIONED {
-        cx.write_shared(SharedResource::Unversioned { name }, contents)?;
+        cx.write_shared(SharedResource::Unversioned { name }, contents, &options.emit)?;
     }
 
     fn collect(path: &Path, krate: &str, key: &str) -> io::Result<(Vec<String>, Vec<String>)> {
@@ -354,7 +379,7 @@ pub(super) fn write_shared(
             "var N = null;var sourcesIndex = {{}};\n{}\ncreateSourceSidebar();\n",
             all_sources.join("\n")
         );
-        cx.write_shared(SharedResource::CrateSpecific { basename: "source-files.js" }, v)?;
+        write_crate("source-files.js", &v)?;
     }
 
     // Update the search index and crate list.
@@ -371,12 +396,12 @@ pub(super) fn write_shared(
         let mut v = String::from("var searchIndex = JSON.parse('{\\\n");
         v.push_str(&all_indexes.join(",\\\n"));
         v.push_str("\\\n}');\ninitSearch(searchIndex);");
-        cx.write_shared(SharedResource::CrateSpecific { basename: "search-index.js" }, v)?;
+        write_crate("search-index.js", &v)?;
     }
 
     let crate_list =
         format!("window.ALL_CRATES = [{}];", krates.iter().map(|k| format!("\"{}\"", k)).join(","));
-    cx.write_shared(SharedResource::CrateSpecific { basename: "crates.js" }, crate_list)?;
+    write_crate("crates.js", &crate_list)?;
 
     if options.enable_index_page {
         if let Some(index_page) = options.index_page.clone() {