mod.rs 3.6 KB

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