1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use std::io::Write;
type Result<T, E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
pub struct Project {
    name: String,
    src_dir: String,
    cargo_toml: String,
    main_rs: String,
    package_json: String,
    readme: String,
    gitignore: String,
    templates: String,
    static_dir: String,
    template_components: String,
    template_layouts: String,
    template_pages: String,
    static_css: String,
    static_js: String,
    index_js: String,
    static_images: String,
    config: String,
    config_env: String,
    config_dev_env: String,
    config_prod_env: String,
    config_test_env: String,
    config_default_env: String,
    config_database: String,
    config_dev_db: String,
    config_prod_db: String,
    config_test_db: String,
    routes: String,
    controllers: String,
    models: String,
    migrations: String,
    seeders: String,
    tests: String,
    config_initializers: String,
    config_initializers_assets: String,
    config_initializers_db: String,
    config_initializers_default: String,
    config_initializers_middleware: String,
    config_initializers_routes: String,
    index_html: String,
    styles_css: String,
    not_found_html: String,
    server_error_html: String,
    favicon_ico: String,
    robots_txt: String,
    login_page_html: String,
    signup_page_html: String,
    reset_password_page_html: String,
    forgot_password_page_html: String,
    dashboard_page_html: String,
    user_controller: String,
    user_model: String,
    user_migration: String,
    user_seeder: String,
    user_test: String,
    user_routes: String,
}
impl Project {
    pub fn new(name: String) -> Project {
        let name = name.trim().to_string();
        let src_dir = format!("{}/src", name);
        let cargo_toml = format!("{}/Cargo.toml", name);
        let main_rs = format!("{}/src/main.rs", name);
        let package_json = format!("{}/package.json", name);
        let readme = format!("{}/README.md", name);
        let gitignore = format!("{}/.gitignore", name);
        let templates = format!("{}/templates", name);
        let static_dir = format!("{}/static", name);
        let template_components = format!("{}/templates/components", name);
        let template_layouts = format!("{}/templates/layouts", name);
        let template_pages = format!("{}/templates/pages", name);
        let static_css = format!("{}/static/css", name);
        let static_js = format!("{}/static/js", name);
        let index_js = format!("{}/static/js/index.js", name);
        let static_images = format!("{}/static/images", name);
        let config = format!("{}/config", name);
        let config_env = format!("{}/config/environments", name);
        let config_dev_env = format!("{}/config/environments/dev.env", name);
        let config_prod_env = format!("{}/config/environments/prod.env", name);
        let config_test_env = format!("{}/config/environments/test.env", name);
        let config_default_env = format!("{}/config/environments/default.env", name);
        let config_database = format!("{}/config/database", name);
        let config_dev_db = format!("{}/config/database/dev.db", name);
        let config_prod_db = format!("{}/config/database/prod.db", name);
        let config_test_db = format!("{}/config/database/test.db", name);
        let routes = format!("{}/routes", name);
        let controllers = format!("{}/controllers", name);
        let models = format!("{}/models", name);
        let migrations = format!("{}/migrations", name);
        let seeders = format!("{}/seeders", name);
        let tests = format!("{}/tests", name);
        let config_initializers = format!("{}/config/initializers", name);
        let config_initializers_assets = format!("{}/config/initializers/assets.rs", name);
        let config_initializers_db = format!("{}/config/initializers/db.rs", name);
        let config_initializers_default = format!("{}/config/initializers/default.rs", name);
        let config_initializers_middleware = format!("{}/config/initializers/middleware.rs", name);
        let config_initializers_routes = format!("{}/config/initializers/routes.rs", name);
        let index_html = format!("{}/templates/pages/index.html.tera", name);
        let styles_css = format!("{}/static/css/styles.css", name);
        let not_found_html = format!("{}/templates/pages/404.html.tera", name);
        let server_error_html = format!("{}/templates/pages/500.html.tera", name);
        let favicon_ico = format!("{}/static/images/favicon.ico", name);
        let robots_txt = format!("{}/static/robots.txt", name);
        let login_page_html = format!("{}/templates/pages/login.html.tera", name);
        let signup_page_html = format!("{}/templates/pages/signup.html.tera", name);
        let reset_password_page_html = format!("{}/templates/pages/reset_password.html.tera", name);
        let forgot_password_page_html =
            format!("{}/templates/pages/forgot_password.html.tera", name);
        let dashboard_page_html = format!("{}/templates/pages/dashboard.html.tera", name);
        let user_controller = format!("{}/controllers/user.rs", name);
        let user_model = format!("{}/models/user.rs", name);
        let user_migration = format!("{}/migrations/00000000000000_create_users_table.rs", name);
        let user_seeder = format!("{}/seeders/00000000000000_create_users_table.rs", name);
        let user_test = format!("{}/tests/user.rs", name);
        let user_routes = format!("{}/routes/user.rs", name);
        Project {
            name,
            src_dir,
            cargo_toml,
            main_rs,
            package_json,
            readme,
            gitignore,
            templates,
            static_dir,
            template_components,
            template_layouts,
            template_pages,
            static_css,
            static_js,
            index_js,
            static_images,
            config,
            config_env,
            config_dev_env,
            config_prod_env,
            config_test_env,
            config_default_env,
            config_database,
            config_dev_db,
            config_prod_db,
            config_test_db,
            routes,
            controllers,
            models,
            migrations,
            seeders,
            tests,
            config_initializers,
            config_initializers_assets,
            config_initializers_db,
            config_initializers_default,
            config_initializers_middleware,
            config_initializers_routes,
            index_html,
            styles_css,
            not_found_html,
            server_error_html,
            favicon_ico,
            robots_txt,
            login_page_html,
            signup_page_html,
            reset_password_page_html,
            forgot_password_page_html,
            dashboard_page_html,
            user_controller,
            user_model,
            user_migration,
            user_seeder,
            user_test,
            user_routes,
        }
    }
    pub fn create_directories(&self) -> Result<()> {
        std::fs::create_dir(&self.name).expect("Failed to create directory");
        std::fs::create_dir(&self.src_dir).expect("Failed to create src directory");
std::fs::create_dir(&self.templates).expect("Failed to create templates directory");
        std::fs::create_dir(&self.static_dir).expect("Failed to create static directory");
        std::fs::create_dir(&self.template_components)
            .expect("Failed to create template components directory");
        std::fs::create_dir(&self.template_layouts)
            .expect("Failed to create template layouts directory");
        std::fs::create_dir(&self.template_pages)
            .expect("Failed to create template pages directory");
        std::fs::create_dir(&self.static_css).expect("Failed to create static css directory");
        std::fs::create_dir(&self.static_js).expect("Failed to create static js directory");
        std::fs::create_dir(&self.static_images)
            .expect("Failed to create static images directory");
        std::fs::create_dir(&self.config).expect("Failed to create config directory");
        std::fs::create_dir(&self.config_env).expect("Failed to create config environments directory");
        std::fs::create_dir(&self.config_database)
            .expect("Failed to create config database directory");
        std::fs::create_dir(&self.routes).expect("Failed to create routes directory");
        std::fs::create_dir(&self.controllers).expect("Failed to create controllers directory");
        std::fs::create_dir(&self.models).expect("Failed to create models directory");
        std::fs::create_dir(&self.migrations).expect("Failed to create migrations directory");
        std::fs::create_dir(&self.seeders).expect("Failed to create seeders directory");
        std::fs::create_dir(&self.tests).expect("Failed to create tests directory");
        std::fs::create_dir(&self.config_initializers)
            .expect("Failed to create config initializers directory");
        std::fs::create_dir(&self.user_controller).expect("Failed to create user controller directory");
        std::fs::create_dir(&self.user_model).expect("Failed to create user model directory");
        std::fs::create_dir(&self.user_migration)
            .expect("Failed to create user migration directory");
        std::fs::create_dir(&self.user_seeder).expect("Failed to create user seeder directory");
        std::fs::create_dir(&self.user_test).expect("Failed to create user test directory");
        std::fs::create_dir(&self.user_routes).expect("Failed to create user routes directory");
        Ok(())
    }
    pub fn create_files(&self) -> Result<()> {
        std::fs::File::create(&self.cargo_toml).expect("Failed to create Cargo.toml");
        std::fs::File::create(&self.main_rs).expect("Failed to create main.rs");
        std::fs::File::create(&self.package_json).expect("Failed to create package.json");
        std::fs::File::create(&self.readme).expect("Failed to create README.md");
        std::fs::File::create(&self.gitignore).expect("Failed to create .gitignore");
        std::fs::File::create(&self.index_html).expect("Failed to create index.html.tera");
        std::fs::File::create(&self.styles_css).expect("Failed to create styles.css");
        std::fs::File::create(&self.not_found_html).expect("Failed to create 404.html.tera");
        std::fs::File::create(&self.server_error_html).expect("Failed to create 500.html.tera");
        std::fs::File::create(&self.favicon_ico).expect("Failed to create favicon.ico");
        std::fs::File::create(&self.robots_txt).expect("Failed to create robots.txt");
        std::fs::File::create(&self.login_page_html).expect("Failed to create login.html.tera");
        std::fs::File::create(&self.signup_page_html).expect("Failed to create signup.html.tera");
        std::fs::File::create(&self.reset_password_page_html)
            .expect("Failed to create reset_password.html.tera");
        std::fs::File::create(&self.forgot_password_page_html)
            .expect("Failed to create forgot_password.html.tera");
        std::fs::File::create(&self.dashboard_page_html)
            .expect("Failed to create dashboard.html.tera");
        std::fs::File::create(&self.user_controller).expect("Failed to create user.rs");
        std::fs::File::create(&self.user_model).expect("Failed to create user.rs");
        std::fs::File::create(&self.user_migration).expect("Failed to create user.rs");
        std::fs::File::create(&self.user_seeder).expect("Failed to create user.rs");
        std::fs::File::create(&self.user_test).expect("Failed to create user.rs");
        std::fs::File::create(&self.user_routes).expect("Failed to create user.rs");
        std::fs::File::create(&self.config_dev_env).expect("Failed to create dev.env");
        std::fs::File::create(&self.config_prod_env).expect("Failed to create prod.env");
        std::fs::File::create(&self.config_test_env).expect("Failed to create test.env");
        std::fs::File::create(&self.config_dev_db).expect("Failed to create dev.db");
        std::fs::File::create(&self.config_prod_db).expect("Failed to create prod.db");
        std::fs::File::create(&self.config_test_db).expect("Failed to create test.db");
        std::fs::File::create(&self.config_initializers_db)
            .expect("Failed to create db.rs");
        std::fs::File::create(&self.config_initializers_routes)
            .expect("Failed to create routes.rs");
        Ok(())
    }
    fn write_to_main_rs(&self) -> Result<()> {
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .append(true)
            .open(&self.main_rs)
            .expect("Failed to open main.rs");
        file.write_all(
            b"
#[macro_use] extern crate rocket;
#[get(\"/\")]
fn index() -> &'static str {
    \"Hello, world!\"
}
#[launch]
fn rocket() -> _ {
    rocket::build().mount(\"/\", routes![index])
}",
        )
        .expect("Failed to write to main.rs");
        Ok(())
    }
    fn write_to_toml(&self) -> Result<()> {
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .append(true)
            .open(&self.cargo_toml)
            .expect("Failed to open Cargo.toml");
        file.write_all(
            format!(
                "[package]
name = \"{}\"
version = \"0.1.0\"
authors = [\"RustyRocket\"]
edition = \"2021\"
[dependencies]
rocket = \"0.5.0-rc.1\"",
                self.name
            )
            .as_bytes(),
        )
        .expect("Failed to write to Cargo.toml");
        Ok(())
    }
    fn write_to_package_json(&self) -> Result<()> {
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .append(true)
            .open(format!("{}/package.json", &self.package_json))
            .expect("Failed to open package.json");
        file.write_all(
            format!(
                "{{
  \"name\": \"rustyrocket\",
  \"version\": \"1.0.0\",
  \"main\": \"index.js\",
  \"repository\": \"https://github.com/Riley-Seaburg/RustyRocket.git\",
  \"author\": \"Riley Seaburg <riley@rileyseaburg.com>\",
  \"license\": \"MIT\",
  \"scripts\": {{
    \"server\": \"cargo run\",
    \"tailwind:dev\": \"npx tailwindcss -i ./src/tailwind.css -o ./static/styles.css --watch\",
    \"tailwind:build\": \"npx tailwindcss -i ./src/tailwind.css -o ./static/styles.css --minify\",
    \"dev\": \"concurrently \\\"yarn tailwind:dev\\\" \\\" yarn server\\\"\"
  }},
  \"devDependencies\": {{
    \"@tailwindcss/forms\": \"^0.5.3\",
    \"concurrently\": \"^7.6.0\",
    \"tailwindcss\": \"^3.2.4\"
  }}
}}"
            )
            .as_bytes(),
        )
        .expect("Failed to write to package.json");
        Ok(())
    }
    fn write_to_readme(&self) -> Result<()> {
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .append(true)
            .open(format!("{}/README.md", &self.readme))
            .expect("Failed to open README.md");
        file.write_all(
            format!(
                "# {}
This project was created using Rusty Roadster. Rusty Roadster is Rails for Rust. It is a CLI tool that allows you to create a new Rust project with a few commands. It also comes with TailwindCSS and Rocket pre-installed.
## Getting Started
### Configure TailwindCSS
```bash
npx tailwindcss init -p
```
To get started, run `yarn dev` to start the server and watch for changes to your TailwindCSS files.
## Contributing
If you would like to contribute to this project, please fork the repository and submit a pull request.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details."
                , self.name
            )
                .as_bytes(),
        )
            .expect("Failed to write to README.md");
        Ok(())
    }
    fn write_to_gitignore(&self) -> Result<()> {
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .append(true)
            .open(format!("{}/.gitignore", &self.gitignore))
            .expect("Failed to open .gitignore");
        file.write_all(
            b"target/
Cargo.lock
.DS_Store
.env
.db
node_modules/
static/styles.css
",
        )
        .expect("Failed to write to .gitignore");
        Ok(())
    }
    fn write_to_indexjs(&self) -> Result<()> {
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .append(true)
            .open(format!("{}/index.js", &self.index_js))
            .expect("Failed to open index.js");
        file.write_all(
            format!(
                "// Rusty Roadster
class RustyRoadster {{
    constructor() {{
        this.name = \"{}\";
function greet() {{
    console.log(\"Welcome to {} powered by Rusty Roadster\");
}}
    }}
}}
const rustyroadster = new RustyRoadster();
rustyroadster.greet();
",
                self.name, self.name
            )
            .as_bytes(),
        )
        .expect("Failed to write to index.js");
        Ok(())
    }
    fn write_to_dev_dot_env(&self) -> Result<()> {
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .append(true)
            .open(format!("{}/dev.env", &self.config_dev_env))
            .expect("Failed to open dev.env");
        file.write_all(
            b"ROCKET_ENV=dev
            ROCKET_ADDRESS=
            ROCKET_PORT=8000
            ROCKET_LOG=normal
            ROCKET_WORKERS=1
            ROCKET_SECRET_KEY=
            ROCKET_TEMPLATES=
            ROCKET_DATABASES=
            ROCKET_TLS=
            ROCKET_TLS_CERTS=
",
        )
        .expect("Failed to write to dev.env");
        Ok(())
    }
    fn write_to_prod_dot_env(&self) -> Result<()> {
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .append(true)
            .open(format!("{}/prod.env", &self.config_prod_env))
            .expect("Failed to open prod.env");
        file.write_all(
            b"ROCKET_ENV=prod
            ROCKET_ADDRESS=
            ROCKET_PORT=8000
            ROCKET_LOG=normal
            ROCKET_WORKERS=1
            ROCKET_SECRET_KEY=
            ROCKET_TEMPLATES=
            ROCKET_DATABASES=
            ROCKET_TLS=
            ROCKET_TLS_CERTS=
",
        )
        .expect("Failed to write to prod.env");
        Ok(())
    }
    fn create_new_project() {
        println!("What would you like to name your project?");
        let mut project_name_user_input = String::new();
        std::io::stdin()
            .read_line(&mut project_name_user_input)
            .expect("Failed to read line");
        let project = Self::new(project_name_user_input);
        writeln!(std::io::stdout(), "Creating project {}", project.name).unwrap();
        Self::create_directories(&project).expect("Failed to create directories");
        Self::create_files(&project).expect("Failed to create files");
        Self::write_to_toml(&project).expect("Failed to write to Cargo.toml");
        Self::write_to_main_rs(&project).expect("Failed to write to main.rs");
        Self::write_to_package_json(&project).expect("Failed to write to package.json");
        Self::write_to_readme(&project).expect("Failed to write to README.md");
        println!("Project {} created!", &project.name);
    }
    fn create_new_route() {
        println!("What would you like to name your route?");
    }
    pub fn initial_prompt() -> Result<(), Box<dyn std::error::Error>> {
        println!("What would you like to do?");
        println!("1. Create a new project");
        println!("2. Create a route");
        println!("3. CLI help");
        println!("4. Exit");
        let mut project_name = String::new();
        std::io::stdin()
            .read_line(&mut project_name)
            .expect("Failed to read line");
        let project_name: u32 = match project_name.trim().parse() {
            Ok(num) => num,
            Err(_) => 0,
        };
        match project_name {
            1 => Ok(Self::create_new_project()),
            2 => Ok(Self::create_new_route()),
            3 => Ok(println!("Helping you...")),
            4 => Ok(Self::exit_program()),
            _ => Ok(println!("Invalid project_name")),
        }
    }
    pub fn exit_program() {
        println!("Exiting...");
        std::process::exit(0);
    }
}