Browse Source

Move commands into module

master
Dylan Baker 5 years ago
parent
commit
ab1c0df8f6
2 changed files with 232 additions and 229 deletions
  1. 230
    0
      src/commands.rs
  2. 2
    229
      src/main.rs

+ 230
- 0
src/commands.rs View File

@@ -0,0 +1,230 @@
1
+use std::{env, fs};
2
+
3
+use fs_extra::dir;
4
+use toml::Value;
5
+
6
+use config::Config;
7
+use post::{parse_post, read_posts_dir};
8
+use write::{write_post, write_post_listing};
9
+
10
+pub fn build() {
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
+
24
+    match fs::read_dir(cwd.join("public")) {
25
+        Ok(_) => {
26
+            fs::remove_dir_all(cwd.join("public")).unwrap();
27
+        }
28
+        Err(_) => {}
29
+    }
30
+
31
+    fs::create_dir(cwd.join("public")).expect("Couldn't create public directory");
32
+
33
+    let layout_template = fs::read_to_string(&cwd.join("templates").join("layout.html"))
34
+        .expect("Couldn't find layout template");
35
+    let post_template = fs::read_to_string(cwd.join("templates").join("post.html"))
36
+        .expect("Couldn't find post template");
37
+    let post_listing_template = fs::read_to_string(cwd.join("templates").join("post_listing.html"))
38
+        .expect("Couldn't find post listing item template");
39
+    let post_item_template =
40
+        fs::read_to_string(cwd.join("templates").join("post_listing_item.html"))
41
+            .expect("Couldn't find post listing item template");
42
+
43
+    let post_paths = read_posts_dir(&cwd.join("posts"));
44
+    let posts = post_paths
45
+        .into_iter()
46
+        .map(|path| match path {
47
+            Ok(p) => {
48
+                let post = parse_post(p.path());
49
+                write_post(&cwd, &layout_template, &post_template, &post, &config);
50
+                post
51
+            }
52
+            Err(err) => panic!(err),
53
+        }).collect();
54
+
55
+    write_post_listing(
56
+        &cwd,
57
+        &layout_template,
58
+        &post_listing_template,
59
+        &post_item_template,
60
+        &posts,
61
+        &config,
62
+    );
63
+
64
+    fs_extra::copy_items(
65
+        &vec![cwd.join("css"), cwd.join("js")],
66
+        cwd.join("public"),
67
+        &dir::CopyOptions::new(),
68
+    ).expect("Couldn't copy css/js directories");
69
+}
70
+
71
+pub fn new(name: &str) {
72
+    let cwd = env::current_dir().expect("Couldn't read current directory");
73
+    let project_path = cwd.join(name);
74
+
75
+    fs::create_dir(&project_path).expect(&format!("Couldn't create directory '{}'", &name));
76
+    fs::write(
77
+        project_path.join("tlon.toml"),
78
+        format!("site_name = \"{}\"", &name),
79
+    ).expect("Could not create tlon.toml");
80
+
81
+    for dir in &["posts", "public", "templates", "css", "js"] {
82
+        fs::create_dir(&project_path.join(&dir))
83
+            .expect(&format!("Couldn't create {} directory", &dir));
84
+    }
85
+
86
+    fs::write(project_path.join("css").join("style.css"), "")
87
+        .expect("Couldn't create css/style.css");
88
+    fs::write(project_path.join("js").join("index.js"), "").expect("Couldn't create js/index.js");
89
+
90
+    for file in &["layout", "post_listing", "post", "post_listing_item"] {
91
+        fs::write(
92
+            &project_path
93
+                .join("templates")
94
+                .join(format!("{}.html", file)),
95
+            "",
96
+        ).expect(&format!("Couldn't write templates/{}.html", file));
97
+    }
98
+}
99
+
100
+#[cfg(test)]
101
+mod tests {
102
+    #[allow(unused_imports)]
103
+    use super::*;
104
+    #[allow(unused_imports)]
105
+    use uuid::Uuid;
106
+
107
+    #[test]
108
+    fn test_new() {
109
+        let temp_dir = env::temp_dir();
110
+        env::set_current_dir(&temp_dir).unwrap();
111
+
112
+        let uuid = Uuid::new_v4().to_string();
113
+        let project_dir = temp_dir.join(&uuid);
114
+        new(&uuid);
115
+
116
+        for dir in &["public", "posts", "templates"] {
117
+            fs::read_dir(&project_dir.join(dir)).unwrap();
118
+        }
119
+
120
+        assert_eq!(
121
+            "",
122
+            fs::read_to_string(&project_dir.join("templates").join("post_listing.html")).unwrap()
123
+        );
124
+        assert_eq!(
125
+            "",
126
+            fs::read_to_string(&project_dir.join("templates").join("layout.html")).unwrap()
127
+        );
128
+        assert_eq!(
129
+            "",
130
+            fs::read_to_string(&project_dir.join("templates").join("post_listing_item.html"))
131
+                .unwrap()
132
+        );
133
+        assert_eq!(
134
+            "",
135
+            fs::read_to_string(&project_dir.join("templates").join("post.html")).unwrap()
136
+        );
137
+        assert_eq!(
138
+            "",
139
+            fs::read_to_string(&project_dir.join("css").join("style.css")).unwrap()
140
+        );
141
+        assert_eq!(
142
+            "",
143
+            fs::read_to_string(&project_dir.join("js").join("index.js")).unwrap()
144
+        );
145
+        assert_eq!(
146
+            format!("site_name = \"{}\"", &uuid),
147
+            fs::read_to_string(&project_dir.join("tlon.toml")).unwrap()
148
+        );
149
+
150
+        fs::remove_dir_all(project_dir).unwrap();
151
+    }
152
+
153
+    #[test]
154
+    fn test_build() {
155
+        let temp_dir = env::temp_dir();
156
+        let uuid = Uuid::new_v4().to_string();
157
+        let project_dir = temp_dir.join(&uuid);
158
+        fs::create_dir(&project_dir).unwrap();
159
+        env::set_current_dir(&project_dir).unwrap();
160
+
161
+        fs::create_dir(project_dir.join("posts")).unwrap();
162
+        fs::create_dir(project_dir.join("public")).unwrap();
163
+        fs::create_dir(project_dir.join("templates")).unwrap();
164
+        fs::create_dir(project_dir.join("css")).unwrap();
165
+        fs::create_dir(project_dir.join("js")).unwrap();
166
+
167
+        fs::write(
168
+            project_dir.join("css").join("style.css"),
169
+            "body { background: blue; }",
170
+        ).unwrap();
171
+        fs::write(
172
+            project_dir.join("js").join("index.js"),
173
+            "window.onload = function () { alert() }",
174
+        ).unwrap();
175
+        fs::write(
176
+            project_dir.join("templates").join("layout.html"),
177
+            "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
178
+        ).unwrap();
179
+        fs::write(
180
+            project_dir.join("templates").join("post.html"),
181
+            "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
182
+        ).unwrap();
183
+        fs::write(
184
+            project_dir.join("templates").join("post_listing.html"),
185
+            "<ul>{{ post_listing }}</ul>",
186
+        ).unwrap();
187
+        fs::write(
188
+            project_dir.join("templates").join("post_listing_item.html"),
189
+            "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
190
+        ).unwrap();
191
+        fs::write(
192
+            project_dir.join("posts").join("first-post.md"),
193
+            "# First post\n\nThis is the first post\n\nIt has multiple paragraphs",
194
+        ).unwrap();
195
+        fs::write(project_dir.join("tlon.toml"), "site_name = \"Test Site\"").unwrap();
196
+
197
+        build();
198
+
199
+        assert_eq!(
200
+            "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/fir\
201
+             st-post\">First post</a></li></ul></body></html>",
202
+            fs::read_to_string(project_dir.join("public").join("index.html")).unwrap(),
203
+        );
204
+
205
+        assert_eq!(
206
+            "<html><head><title>First post | Test Site</title></head><body><article><h1>First pos\
207
+             t</h1><div><p>This is the first post</p><p>It has multiple paragra\
208
+             phs</p></div></article></body></html>",
209
+            fs::read_to_string(
210
+                project_dir
211
+                    .join("public")
212
+                    .join("first-post")
213
+                    .join("index.html")
214
+            ).unwrap()
215
+            .replace("\n", ""),
216
+        );
217
+
218
+        assert_eq!(
219
+            "body { background: blue; }",
220
+            fs::read_to_string(project_dir.join("public").join("css").join("style.css")).unwrap()
221
+        );
222
+
223
+        assert_eq!(
224
+            "window.onload = function () { alert() }",
225
+            fs::read_to_string(project_dir.join("public").join("js").join("index.js")).unwrap()
226
+        );
227
+
228
+        fs::remove_dir_all(project_dir).unwrap();
229
+    }
230
+}

+ 2
- 229
src/main.rs View File

@@ -7,111 +7,16 @@ extern crate regex;
7 7
 extern crate toml;
8 8
 extern crate uuid;
9 9
 
10
-use std::{env, fs};
11
-
12 10
 use clap::{App, Arg};
13
-use fs_extra::dir;
14
-use toml::Value;
15 11
 
16
-use config::Config;
17
-use post::{parse_post, read_posts_dir};
18
-use write::{write_post, write_post_listing};
12
+use commands::{build, new};
19 13
 
14
+mod commands;
20 15
 mod config;
21 16
 mod post;
22 17
 mod render;
23 18
 mod write;
24 19
 
25
-fn build() {
26
-    let cwd = env::current_dir().expect("Couldn't read current directory");
27
-    let config = match fs::read_to_string(cwd.join("tlon.toml")) {
28
-        Ok(contents) => match contents.parse::<Value>() {
29
-            Ok(config) => Config {
30
-                site_name: String::from(config["site_name"].as_str().unwrap()),
31
-            },
32
-            Err(_) => panic!("Invalid tlon.toml"),
33
-        },
34
-        Err(_) => {
35
-            panic!("Can't find tlon.toml");
36
-        }
37
-    };
38
-
39
-    match fs::read_dir(cwd.join("public")) {
40
-        Ok(_) => {
41
-            fs::remove_dir_all(cwd.join("public")).unwrap();
42
-        }
43
-        Err(_) => {}
44
-    }
45
-
46
-    fs::create_dir(cwd.join("public")).expect("Couldn't create public directory");
47
-
48
-    let layout_template = fs::read_to_string(&cwd.join("templates").join("layout.html"))
49
-        .expect("Couldn't find layout template");
50
-    let post_template = fs::read_to_string(cwd.join("templates").join("post.html"))
51
-        .expect("Couldn't find post template");
52
-    let post_listing_template = fs::read_to_string(cwd.join("templates").join("post_listing.html"))
53
-        .expect("Couldn't find post listing item template");
54
-    let post_item_template =
55
-        fs::read_to_string(cwd.join("templates").join("post_listing_item.html"))
56
-            .expect("Couldn't find post listing item template");
57
-
58
-    let post_paths = read_posts_dir(&cwd.join("posts"));
59
-    let posts = post_paths
60
-        .into_iter()
61
-        .map(|path| match path {
62
-            Ok(p) => {
63
-                let post = parse_post(p.path());
64
-                write_post(&cwd, &layout_template, &post_template, &post, &config);
65
-                post
66
-            }
67
-            Err(err) => panic!(err),
68
-        }).collect();
69
-
70
-    write_post_listing(
71
-        &cwd,
72
-        &layout_template,
73
-        &post_listing_template,
74
-        &post_item_template,
75
-        &posts,
76
-        &config,
77
-    );
78
-
79
-    fs_extra::copy_items(
80
-        &vec![cwd.join("css"), cwd.join("js")],
81
-        cwd.join("public"),
82
-        &dir::CopyOptions::new(),
83
-    ).expect("Couldn't copy css/js directories");
84
-}
85
-
86
-fn new(name: &str) {
87
-    let cwd = env::current_dir().expect("Couldn't read current directory");
88
-    let project_path = cwd.join(name);
89
-
90
-    fs::create_dir(&project_path).expect(&format!("Couldn't create directory '{}'", &name));
91
-    fs::write(
92
-        project_path.join("tlon.toml"),
93
-        format!("site_name = \"{}\"", &name),
94
-    ).expect("Could not create tlon.toml");
95
-
96
-    for dir in &["posts", "public", "templates", "css", "js"] {
97
-        fs::create_dir(&project_path.join(&dir))
98
-            .expect(&format!("Couldn't create {} directory", &dir));
99
-    }
100
-
101
-    fs::write(project_path.join("css").join("style.css"), "")
102
-        .expect("Couldn't create css/style.css");
103
-    fs::write(project_path.join("js").join("index.js"), "").expect("Couldn't create js/index.js");
104
-
105
-    for file in &["layout", "post_listing", "post", "post_listing_item"] {
106
-        fs::write(
107
-            &project_path
108
-                .join("templates")
109
-                .join(format!("{}.html", file)),
110
-            "",
111
-        ).expect(&format!("Couldn't write templates/{}.html", file));
112
-    }
113
-}
114
-
115 20
 fn main() {
116 21
     let matches = App::new("tlon")
117 22
         .version("0.1.0")
@@ -135,135 +40,3 @@ fn main() {
135 40
         new(&matches.value_of("name").unwrap());
136 41
     }
137 42
 }
138
-
139
-#[cfg(test)]
140
-mod tests {
141
-    #[allow(unused_imports)]
142
-    use super::*;
143
-    #[allow(unused_imports)]
144
-    use uuid::Uuid;
145
-
146
-    #[test]
147
-    fn test_new() {
148
-        let temp_dir = env::temp_dir();
149
-        env::set_current_dir(&temp_dir).unwrap();
150
-
151
-        let uuid = Uuid::new_v4().to_string();
152
-        let project_dir = temp_dir.join(&uuid);
153
-        new(&uuid);
154
-
155
-        for dir in &["public", "posts", "templates"] {
156
-            fs::read_dir(&project_dir.join(dir)).unwrap();
157
-        }
158
-
159
-        assert_eq!(
160
-            "",
161
-            fs::read_to_string(&project_dir.join("templates").join("post_listing.html")).unwrap()
162
-        );
163
-        assert_eq!(
164
-            "",
165
-            fs::read_to_string(&project_dir.join("templates").join("layout.html")).unwrap()
166
-        );
167
-        assert_eq!(
168
-            "",
169
-            fs::read_to_string(&project_dir.join("templates").join("post_listing_item.html"))
170
-                .unwrap()
171
-        );
172
-        assert_eq!(
173
-            "",
174
-            fs::read_to_string(&project_dir.join("templates").join("post.html")).unwrap()
175
-        );
176
-        assert_eq!(
177
-            "",
178
-            fs::read_to_string(&project_dir.join("css").join("style.css")).unwrap()
179
-        );
180
-        assert_eq!(
181
-            "",
182
-            fs::read_to_string(&project_dir.join("js").join("index.js")).unwrap()
183
-        );
184
-        assert_eq!(
185
-            format!("site_name = \"{}\"", &uuid),
186
-            fs::read_to_string(&project_dir.join("tlon.toml")).unwrap()
187
-        );
188
-
189
-        fs::remove_dir_all(project_dir).unwrap();
190
-    }
191
-
192
-    #[test]
193
-    fn test_build() {
194
-        let temp_dir = env::temp_dir();
195
-        let uuid = Uuid::new_v4().to_string();
196
-        let project_dir = temp_dir.join(&uuid);
197
-        fs::create_dir(&project_dir).unwrap();
198
-        env::set_current_dir(&project_dir).unwrap();
199
-
200
-        fs::create_dir(project_dir.join("posts")).unwrap();
201
-        fs::create_dir(project_dir.join("public")).unwrap();
202
-        fs::create_dir(project_dir.join("templates")).unwrap();
203
-        fs::create_dir(project_dir.join("css")).unwrap();
204
-        fs::create_dir(project_dir.join("js")).unwrap();
205
-
206
-        fs::write(
207
-            project_dir.join("css").join("style.css"),
208
-            "body { background: blue; }",
209
-        ).unwrap();
210
-        fs::write(
211
-            project_dir.join("js").join("index.js"),
212
-            "window.onload = function () { alert() }",
213
-        ).unwrap();
214
-        fs::write(
215
-            project_dir.join("templates").join("layout.html"),
216
-            "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
217
-        ).unwrap();
218
-        fs::write(
219
-            project_dir.join("templates").join("post.html"),
220
-            "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
221
-        ).unwrap();
222
-        fs::write(
223
-            project_dir.join("templates").join("post_listing.html"),
224
-            "<ul>{{ post_listing }}</ul>",
225
-        ).unwrap();
226
-        fs::write(
227
-            project_dir.join("templates").join("post_listing_item.html"),
228
-            "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
229
-        ).unwrap();
230
-        fs::write(
231
-            project_dir.join("posts").join("first-post.md"),
232
-            "# First post\n\nThis is the first post\n\nIt has multiple paragraphs",
233
-        ).unwrap();
234
-        fs::write(project_dir.join("tlon.toml"), "site_name = \"Test Site\"").unwrap();
235
-
236
-        build();
237
-
238
-        assert_eq!(
239
-            "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/fir\
240
-             st-post\">First post</a></li></ul></body></html>",
241
-            fs::read_to_string(project_dir.join("public").join("index.html")).unwrap(),
242
-        );
243
-
244
-        assert_eq!(
245
-            "<html><head><title>First post | Test Site</title></head><body><article><h1>First pos\
246
-             t</h1><div><p>This is the first post</p><p>It has multiple paragra\
247
-             phs</p></div></article></body></html>",
248
-            fs::read_to_string(
249
-                project_dir
250
-                    .join("public")
251
-                    .join("first-post")
252
-                    .join("index.html")
253
-            ).unwrap()
254
-            .replace("\n", ""),
255
-        );
256
-
257
-        assert_eq!(
258
-            "body { background: blue; }",
259
-            fs::read_to_string(project_dir.join("public").join("css").join("style.css")).unwrap()
260
-        );
261
-
262
-        assert_eq!(
263
-            "window.onload = function () { alert() }",
264
-            fs::read_to_string(project_dir.join("public").join("js").join("index.js")).unwrap()
265
-        );
266
-
267
-        fs::remove_dir_all(project_dir).unwrap();
268
-    }
269
-}

Loading…
Cancel
Save