docs.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. use std::{ffi::OsString, fs, io::Write as _, process::Command};
  2. use anyhow::{Context as _, Result};
  3. use cargo_metadata::Metadata;
  4. use indoc::{indoc, writedoc};
  5. use xtask::exec;
  6. pub fn docs(metadata: Metadata) -> Result<()> {
  7. const PACKAGE_TO_DESCRIPTION: &[(&str, &str)] = &[
  8. ("aya", "User-space BPF program loading and manipulation"),
  9. (
  10. "aya-ebpf",
  11. "Kernel-space BPF program implementation toolkit",
  12. ),
  13. ("aya-log-ebpf", "Kernel-space logging from BPF programs"),
  14. (
  15. "aya-log",
  16. "User-space consumption of logs from BPF programs",
  17. ),
  18. ];
  19. let Metadata {
  20. workspace_root,
  21. target_directory,
  22. ..
  23. } = metadata;
  24. exec(
  25. Command::new("cargo")
  26. .current_dir(&workspace_root)
  27. .args(["clean", "--doc"]),
  28. )?;
  29. let tmp = tempfile::tempdir().context("create tempdir")?;
  30. let header = tmp.path().join("header.html");
  31. fs::write(&header, r#"<meta name="robots" content="noindex">"#).context("write header.html")?;
  32. let mut rustdocflags = OsString::new();
  33. rustdocflags.push("--cfg docsrs --html-in-header ");
  34. rustdocflags.push(header);
  35. rustdocflags.push(" -D warnings");
  36. exec(
  37. Command::new("cargo")
  38. .current_dir(&workspace_root)
  39. .env("RUSTDOCFLAGS", rustdocflags)
  40. .args(["+nightly", "doc", "--no-deps", "--all-features"])
  41. .args(
  42. PACKAGE_TO_DESCRIPTION
  43. .iter()
  44. .flat_map(|(package, _)| ["--package", package]),
  45. ),
  46. )?;
  47. let site = workspace_root.join("site");
  48. match fs::remove_dir_all(&site) {
  49. Ok(()) => {}
  50. Err(err) => {
  51. if err.kind() != std::io::ErrorKind::NotFound {
  52. return Err(err).context(format!("remove {site:?}"));
  53. }
  54. }
  55. }
  56. let doc = target_directory.join("doc");
  57. fs::rename(&doc, &site).with_context(|| format!("rename {doc:?} to {site:?}"))?;
  58. exec(Command::new("sh").current_dir(&site).args([
  59. "-c",
  60. "grep -FRl crabby.svg | xargs sed -i s/crabby.svg/crabby_dev.svg/g",
  61. ]))?;
  62. fs::write(
  63. site.join("robots.txt"),
  64. indoc! {r"
  65. User-Agent:*
  66. Disallow: /
  67. "},
  68. )
  69. .context("can't write robots.txt")?;
  70. let mut index = fs::File::create(site.join("index.html"))
  71. .with_context(|| format!("create {site:?}/index.html"))?;
  72. writedoc! {&mut index, r#"
  73. <html>
  74. <meta name="robots" content="noindex">
  75. <body>
  76. <ul>
  77. "#}
  78. .context("write to index.html")?;
  79. for (package, description) in PACKAGE_TO_DESCRIPTION {
  80. let package = package.replace('-', "_");
  81. writedoc! {&mut index, r#"
  82. <li><a href="{package}/index.html">Aya {description}</a></li>
  83. "#}
  84. .context("write to string")?;
  85. }
  86. writedoc! {&mut index, r#"
  87. </ul>
  88. </body>
  89. </html>
  90. "#}
  91. .context("write to index.html")?;
  92. Ok(())
  93. }