A static site generator written in Rust
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commands.rs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. use std::sync::mpsc::channel;
  2. use std::time::Duration;
  3. use std::{env, fs, path};
  4. use fs_extra::dir;
  5. use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
  6. use toml::Value;
  7. use config::Config;
  8. use post::{parse_post, read_posts_dir};
  9. use write::{write_post, write_post_listing};
  10. pub fn build(include_drafts: bool) {
  11. let cwd = env::current_dir().expect("Couldn't read current directory");
  12. let config = match fs::read_to_string(cwd.join("tlon.toml")) {
  13. Ok(contents) => match contents.parse::<Value>() {
  14. Ok(config) => Config {
  15. site_name: String::from(config["site_name"].as_str().unwrap()),
  16. },
  17. Err(_) => panic!("Invalid tlon.toml"),
  18. },
  19. Err(_) => {
  20. panic!("Can't find tlon.toml");
  21. }
  22. };
  23. match fs::read_dir(cwd.join("public")) {
  24. Ok(_) => {
  25. fs::remove_dir_all(cwd.join("public")).unwrap();
  26. }
  27. Err(_) => {}
  28. }
  29. fs::create_dir(cwd.join("public")).expect("Couldn't create public directory");
  30. let layout_template = fs::read_to_string(&cwd.join("templates").join("layout.html"))
  31. .expect("Couldn't find layout template");
  32. let post_template = fs::read_to_string(cwd.join("templates").join("post.html"))
  33. .expect("Couldn't find post template");
  34. let post_listing_template = fs::read_to_string(cwd.join("templates").join("post_listing.html"))
  35. .expect("Couldn't find post listing item template");
  36. let post_item_template =
  37. fs::read_to_string(cwd.join("templates").join("post_listing_item.html"))
  38. .expect("Couldn't find post listing item template");
  39. let post_paths = match include_drafts {
  40. true => {
  41. let mut posts = read_posts_dir(&cwd.join("posts"));
  42. posts.append(&mut read_posts_dir(&cwd.join("drafts")));
  43. posts
  44. }
  45. false => read_posts_dir(&cwd.join("posts")),
  46. };
  47. let posts = post_paths
  48. .into_iter()
  49. .map(|entry| {
  50. let post = parse_post(entry.path());
  51. write_post(&cwd, &layout_template, &post_template, &post, &config);
  52. post
  53. }).collect();
  54. write_post_listing(
  55. &cwd,
  56. &layout_template,
  57. &post_listing_template,
  58. &post_item_template,
  59. &posts,
  60. &config,
  61. );
  62. fs_extra::copy_items(
  63. &vec![cwd.join("css"), cwd.join("js")],
  64. cwd.join("public"),
  65. &dir::CopyOptions::new(),
  66. ).expect("Couldn't copy css/js directories");
  67. }
  68. pub fn new(name: &str) {
  69. let cwd = env::current_dir().expect("Couldn't read current directory");
  70. let project_path = cwd.join(name);
  71. fs::create_dir(&project_path).expect(&format!("Couldn't create directory '{}'", &name));
  72. fs::write(
  73. project_path.join("tlon.toml"),
  74. format!("site_name = \"{}\"", &name),
  75. ).expect("Could not create tlon.toml");
  76. for dir in &["drafts", "posts", "public", "templates", "css", "js"] {
  77. fs::create_dir(&project_path.join(&dir))
  78. .expect(&format!("Couldn't create {} directory", &dir));
  79. }
  80. fs::write(project_path.join("css").join("style.css"), "")
  81. .expect("Couldn't create css/style.css");
  82. fs::write(project_path.join("js").join("index.js"), "").expect("Couldn't create js/index.js");
  83. for file in &["layout", "post_listing", "post", "post_listing_item"] {
  84. fs::write(
  85. &project_path
  86. .join("templates")
  87. .join(format!("{}.html", file)),
  88. "",
  89. ).expect(&format!("Couldn't write templates/{}.html", file));
  90. }
  91. }
  92. fn should_rebuild(cwd: &path::PathBuf, path: &path::PathBuf) -> bool {
  93. let path_string = path.to_str().unwrap().to_string();
  94. let change_is_from_public = path_string.contains(cwd.join("public").to_str().unwrap());
  95. let change_is_from_git = path_string.contains(cwd.join(".git").to_str().unwrap());
  96. !change_is_from_public && !change_is_from_git
  97. }
  98. pub fn watch(include_drafts: bool) -> notify::Result<()> {
  99. let cwd = env::current_dir().expect("Couldn't read current directory");
  100. let (tx, rx) = channel();
  101. let mut watcher: RecommendedWatcher = try!(Watcher::new(tx, Duration::from_secs(2)));
  102. try!(watcher.watch(&cwd, RecursiveMode::Recursive));
  103. println!("Watching {}", cwd.to_str().unwrap());
  104. let handle_event = |path: &path::PathBuf| {
  105. if should_rebuild(&cwd, &path) {
  106. println!("Rebuilding");
  107. build(include_drafts);
  108. }
  109. };
  110. loop {
  111. match rx.recv() {
  112. Ok(e) => match e {
  113. DebouncedEvent::Create(path) => {
  114. handle_event(&path);
  115. }
  116. DebouncedEvent::Write(path) => {
  117. handle_event(&path);
  118. }
  119. _ => {}
  120. },
  121. Err(e) => println!("watch error: {:?}", e),
  122. }
  123. }
  124. }
  125. #[cfg(test)]
  126. mod tests {
  127. #[allow(unused_imports)]
  128. use super::*;
  129. #[allow(unused_imports)]
  130. use uuid::Uuid;
  131. #[test]
  132. fn test_new() {
  133. let temp_dir = env::temp_dir();
  134. env::set_current_dir(&temp_dir).unwrap();
  135. let uuid = Uuid::new_v4().to_string();
  136. let project_dir = temp_dir.join(&uuid);
  137. new(&uuid);
  138. for dir in &["public", "posts", "templates"] {
  139. fs::read_dir(&project_dir.join(dir)).unwrap();
  140. }
  141. assert_eq!(
  142. "",
  143. fs::read_to_string(&project_dir.join("templates").join("post_listing.html")).unwrap()
  144. );
  145. assert_eq!(
  146. "",
  147. fs::read_to_string(&project_dir.join("templates").join("layout.html")).unwrap()
  148. );
  149. assert_eq!(
  150. "",
  151. fs::read_to_string(&project_dir.join("templates").join("post_listing_item.html"))
  152. .unwrap()
  153. );
  154. assert_eq!(
  155. "",
  156. fs::read_to_string(&project_dir.join("templates").join("post.html")).unwrap()
  157. );
  158. assert_eq!(
  159. "",
  160. fs::read_to_string(&project_dir.join("css").join("style.css")).unwrap()
  161. );
  162. assert_eq!(
  163. "",
  164. fs::read_to_string(&project_dir.join("js").join("index.js")).unwrap()
  165. );
  166. assert_eq!(
  167. format!("site_name = \"{}\"", &uuid),
  168. fs::read_to_string(&project_dir.join("tlon.toml")).unwrap()
  169. );
  170. fs::remove_dir_all(project_dir).unwrap();
  171. }
  172. #[test]
  173. fn test_build() {
  174. let temp_dir = env::temp_dir();
  175. let uuid = Uuid::new_v4().to_string();
  176. let project_dir = temp_dir.join(&uuid);
  177. fs::create_dir(&project_dir).unwrap();
  178. env::set_current_dir(&project_dir).unwrap();
  179. fs::create_dir(project_dir.join("posts")).unwrap();
  180. fs::create_dir(project_dir.join("public")).unwrap();
  181. fs::create_dir(project_dir.join("templates")).unwrap();
  182. fs::create_dir(project_dir.join("css")).unwrap();
  183. fs::create_dir(project_dir.join("js")).unwrap();
  184. fs::write(
  185. project_dir.join("css").join("style.css"),
  186. "body { background: blue; }",
  187. ).unwrap();
  188. fs::write(
  189. project_dir.join("js").join("index.js"),
  190. "window.onload = function () { alert() }",
  191. ).unwrap();
  192. fs::write(
  193. project_dir.join("templates").join("layout.html"),
  194. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
  195. ).unwrap();
  196. fs::write(
  197. project_dir.join("templates").join("post.html"),
  198. "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
  199. ).unwrap();
  200. fs::write(
  201. project_dir.join("templates").join("post_listing.html"),
  202. "<ul>{{ post_listing }}</ul>",
  203. ).unwrap();
  204. fs::write(
  205. project_dir.join("templates").join("post_listing_item.html"),
  206. "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
  207. ).unwrap();
  208. fs::write(
  209. project_dir.join("posts").join("first-post.md"),
  210. "# First post\n\nThis is the first post\n\nIt has multiple paragraphs",
  211. ).unwrap();
  212. fs::write(project_dir.join("tlon.toml"), "site_name = \"Test Site\"").unwrap();
  213. build(false);
  214. assert_eq!(
  215. "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/fir\
  216. st-post\">First post</a></li></ul></body></html>",
  217. fs::read_to_string(project_dir.join("public").join("index.html")).unwrap(),
  218. );
  219. assert_eq!(
  220. "<html><head><title>First post | Test Site</title></head><body><article><h1>First pos\
  221. t</h1><div><p>This is the first post</p><p>It has multiple paragra\
  222. phs</p></div></article></body></html>",
  223. fs::read_to_string(
  224. project_dir
  225. .join("public")
  226. .join("first-post")
  227. .join("index.html")
  228. ).unwrap()
  229. .replace("\n", ""),
  230. );
  231. assert_eq!(
  232. "body { background: blue; }",
  233. fs::read_to_string(project_dir.join("public").join("css").join("style.css")).unwrap()
  234. );
  235. assert_eq!(
  236. "window.onload = function () { alert() }",
  237. fs::read_to_string(project_dir.join("public").join("js").join("index.js")).unwrap()
  238. );
  239. fs::remove_dir_all(project_dir).unwrap();
  240. }
  241. #[test]
  242. fn test_build_drafts() {
  243. let temp_dir = env::temp_dir();
  244. let uuid = Uuid::new_v4().to_string();
  245. let project_dir = temp_dir.join(&uuid);
  246. fs::create_dir(&project_dir).unwrap();
  247. env::set_current_dir(&project_dir).unwrap();
  248. fs::create_dir(project_dir.join("drafts")).unwrap();
  249. fs::create_dir(project_dir.join("posts")).unwrap();
  250. fs::create_dir(project_dir.join("public")).unwrap();
  251. fs::create_dir(project_dir.join("templates")).unwrap();
  252. fs::create_dir(project_dir.join("css")).unwrap();
  253. fs::create_dir(project_dir.join("js")).unwrap();
  254. fs::write(
  255. project_dir.join("css").join("style.css"),
  256. "body { background: blue; }",
  257. ).unwrap();
  258. fs::write(
  259. project_dir.join("js").join("index.js"),
  260. "window.onload = function () { alert() }",
  261. ).unwrap();
  262. fs::write(
  263. project_dir.join("templates").join("layout.html"),
  264. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
  265. ).unwrap();
  266. fs::write(
  267. project_dir.join("templates").join("post.html"),
  268. "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
  269. ).unwrap();
  270. fs::write(
  271. project_dir.join("templates").join("post_listing.html"),
  272. "<ul>{{ post_listing }}</ul>",
  273. ).unwrap();
  274. fs::write(
  275. project_dir.join("templates").join("post_listing_item.html"),
  276. "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
  277. ).unwrap();
  278. fs::write(
  279. project_dir.join("posts").join("first-post.md"),
  280. "# First post\n\nThis is the first post",
  281. ).unwrap();
  282. fs::write(
  283. project_dir.join("drafts").join("first-draft.md"),
  284. "# First draft\n\nThis is the first draft",
  285. ).unwrap();
  286. fs::write(project_dir.join("tlon.toml"), "site_name = \"Test Site\"").unwrap();
  287. build(true);
  288. assert_eq!(
  289. "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/first-post\">First post</a></li><li><a href=\"/first-draft\">First draft</a></li></ul></body></html>",
  290. fs::read_to_string(project_dir.join("public").join("index.html")).unwrap().replace("\n", ""),
  291. );
  292. assert_eq!(
  293. "<html><head><title>First post | Test Site</title></head><body><article><h1>First post</h1><div><p>This is the first post</p></div></article></body></html>",
  294. fs::read_to_string(
  295. project_dir
  296. .join("public")
  297. .join("first-post")
  298. .join("index.html")
  299. ).unwrap()
  300. .replace("\n", ""),
  301. );
  302. assert_eq!(
  303. "<html><head><title>First draft | Test Site</title></head><body><article><h1>First draft</h1><div><p>This is the first draft</p></div></article></body></html>",
  304. fs::read_to_string(
  305. project_dir
  306. .join("public")
  307. .join("first-draft")
  308. .join("index.html")
  309. ).unwrap()
  310. .replace("\n", ""),
  311. );
  312. fs::remove_dir_all(project_dir).unwrap();
  313. }
  314. #[test]
  315. fn test_should_rebuild() {
  316. let cwd = env::current_dir().unwrap();
  317. assert_eq!(
  318. false,
  319. should_rebuild(&cwd, &cwd.join("public").join("index.html"))
  320. );
  321. assert_eq!(
  322. false,
  323. should_rebuild(&cwd, &cwd.join(".git").join("index.html"))
  324. );
  325. assert_eq!(
  326. true,
  327. should_rebuild(&cwd, &cwd.join("posts").join("test.md"))
  328. );
  329. assert_eq!(
  330. true,
  331. should_rebuild(&cwd, &cwd.join("drafts").join("test.md"))
  332. );
  333. assert_eq!(
  334. true,
  335. should_rebuild(&cwd, &cwd.join("css").join("style.css"))
  336. );
  337. assert_eq!(true, should_rebuild(&cwd, &cwd.join("js").join("index.js")));
  338. }
  339. }