mod.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. use crate::utils::exec;
  2. use anyhow::{Context as _, Result};
  3. use std::{
  4. path::{Path, PathBuf},
  5. process::Command,
  6. };
  7. use std::{fs, io, io::Write};
  8. use indoc::indoc;
  9. pub fn docs() -> Result<()> {
  10. let current_dir = PathBuf::from(".");
  11. let header_path = current_dir.join("header.html");
  12. let mut header = fs::File::create(&header_path).expect("can't create header.html");
  13. header
  14. .write_all(r#"<meta name="robots" content="noindex">"#.as_bytes())
  15. .expect("can't write header.html contents");
  16. header.flush().expect("couldn't flush contents");
  17. let abs_header_path = fs::canonicalize(&header_path).unwrap();
  18. build_docs(&current_dir.join("aya"), &abs_header_path)?;
  19. build_docs(&current_dir.join("bpf/aya-bpf"), &abs_header_path)?;
  20. copy_dir_all("./target/doc".as_ref(), "./site/user".as_ref())?;
  21. copy_dir_all(
  22. "./target/bpfel-unknown-none/doc".as_ref(),
  23. "./site/bpf".as_ref(),
  24. )?;
  25. let mut robots = fs::File::create("site/robots.txt").expect("can't create robots.txt");
  26. robots
  27. .write_all(
  28. indoc! {r#"
  29. User-Agent:*
  30. Disallow: /
  31. "#}
  32. .as_bytes(),
  33. )
  34. .expect("can't write robots.txt");
  35. let mut index = fs::File::create("site/index.html").expect("can't create index.html");
  36. index
  37. .write_all(
  38. indoc! {r#"
  39. <html>
  40. <meta name="robots" content="noindex">
  41. <body>
  42. <ul>
  43. <li><a href="user/aya/index.html">Aya User-space Development Documentation</a></li>
  44. <li><a href="bpf/aya_bpf/index.html">Aya Kernel-space Development Documentation</a></li>
  45. </ul>
  46. </body>
  47. </html>
  48. "#}
  49. .as_bytes(),
  50. )
  51. .expect("can't write index.html");
  52. Ok(())
  53. }
  54. fn build_docs(working_dir: &Path, abs_header_path: &Path) -> Result<()> {
  55. exec(Command::new("sed").current_dir(working_dir).args([
  56. "-i.bak",
  57. "s/crabby.svg/crabby_dev.svg/",
  58. "src/lib.rs",
  59. ]))?;
  60. exec(
  61. Command::new("cargo")
  62. .current_dir(working_dir)
  63. .env(
  64. "RUSTDOCFLAGS",
  65. format!(
  66. "--cfg docsrs --html-in-header {} -D warnings",
  67. abs_header_path.to_str().unwrap()
  68. ),
  69. )
  70. .args(["+nightly", "doc", "--no-deps", "--all-features"]),
  71. )?;
  72. fs::rename(
  73. working_dir.join("src/lib.rs.bak"),
  74. working_dir.join("src/lib.rs"),
  75. )
  76. .context("Failed to rename lib.rs.bak to lib.rs")
  77. }
  78. fn copy_dir_all(src: &Path, dst: &Path) -> io::Result<()> {
  79. fs::create_dir_all(dst)?;
  80. for entry in fs::read_dir(src)? {
  81. let entry = entry?;
  82. let ty = entry.file_type()?;
  83. let src = entry.path();
  84. let src = src.as_path();
  85. let dst = dst.join(entry.file_name());
  86. let dst = dst.as_path();
  87. if ty.is_dir() {
  88. copy_dir_all(src, dst)?;
  89. } else if !dst.exists() {
  90. fs::copy(src, dst)?;
  91. }
  92. }
  93. Ok(())
  94. }