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
//! Suggested Projects Module
//!
//! This module contains main functions for handling project creation.

// #[macro_use]
// use super::log;
// use super::env_logger;

// use super::log::info;

use std::collections::HashMap;
use std::fs;
use std::io::{self, Write};
use std::path::Path;
use std::process::{self, Command};

/// Type for function callbacks
///
/// This type is used as value for `HashMap`. Functions that return this type are
/// project generators, which are *_handler functions.
type Callback = fn(project: &mut Project) -> Result<process::ExitStatus, io::Error>;

/// Contains all the necessary handler information
#[allow(non_snake_case)]
pub struct Project {
    AVAILABLE_PROJECTS: Vec<&'static str>,
    num_of_projects: usize,
    functions: HashMap<&'static str, Callback>,
    key: &'static str,
    project_name: String,
}

// public
impl Project {
    /// Creates the new instance of `Project` structure
    ///
    /// # Examples
    ///
    /// ```rust
    /// # fn main() {
    /// let project = Project::new();
    /// # }
    /// ```
    #[allow(non_snake_case)]
    pub fn new() -> Project {
        let num_of_projects = 5;
        let key = "";
        let project_name = String::from("");

        let AVAILABLE_PROJECTS: Vec<&str> = vec![
            "deno",
            "rust-wasm",
            "cargo-bin",
            "cargo-lib",
            "create-react-app",
        ];
        let CALLBACKS: Vec<Callback> = vec![
            Project::deno_handler,
            Project::rust_wasm_handler,
            Project::cargo_bin_handler,
            Project::cargo_lib_handler,
            Project::create_react_app_handler,
        ];

        let functions: HashMap<&str, Callback> = AVAILABLE_PROJECTS
            .into_iter()
            .zip(CALLBACKS.into_iter())
            .collect();

        Project {
            AVAILABLE_PROJECTS: vec![
                "deno",
                "rust-wasm",
                "cargo-bin",
                "cargo-lib",
                "create-react-app",
            ], // O_O
            num_of_projects,
            functions,
            key,
            project_name,
        }
    }

    /// Displays the projects to choose from
    ///
    /// Returns the instance of `Project`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # fn main() {
    /// let project = Project::new();
    /// project.display();
    /// # }
    /// ```
    // TODO: logging
    pub fn display(self) -> Project {
        // env_logger::init();
        // info!("Uh");

        io::stdout().write_all(b" Choose one project:\n").unwrap();

        let mut i = 1;
        let _ = self
            .AVAILABLE_PROJECTS
            .iter()
            .map(|x| {
                println!(" {}: {}", i, x);
                i += 1;
                x
            })
            .collect::<Vec<_>>();
        println!();

        self
    }

    /// Prompts the user to choose from the available projects
    ///
    /// Sets key to which project the user has chosen. Returns the instance of the `Project`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # fn main() {
    /// let project = Project::new();
    /// project.display().choose();
    /// # }
    /// ```
    pub fn choose(mut self) -> Project {
        let key: &str = loop {
            io::stdout().write_all(b" >> ").unwrap();
            let _ = io::stdout().flush();

            let mut input = String::new();
            io::stdin().read_line(&mut input).unwrap();

            match input.trim().parse::<usize>() {
                Ok(val) if val >= 1 && val <= self.num_of_projects => {
                    break self.AVAILABLE_PROJECTS[val - 1];
                }
                _ => {
                    println!(
                        " [!!] Input should be numbers between {} and {}.",
                        1, self.num_of_projects
                    );
                    continue;
                }
            };
        };
        self.key = key;

        self
    }

    /// Generate project
    ///
    /// Generates user chosen project.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # fn main() {
    /// let project = Project::new();
    /// project.display().choose().generate_project();
    /// # }
    /// ```
    pub fn generate_project(&mut self) {
        let _ = self.functions.get(self.key).unwrap()(self);
        Project::init_git_inside(self.get_project_name());
    }
}

// private
impl Project {
    fn set_project_name(&mut self, project_name: String) {
        self.project_name = project_name;
    }

    fn get_project_name(&self) -> String {
        let result = &self.project_name;
        result.to_string()
    }

    fn init_git_inside(project_name: String) {
        let mut cmd = Command::new("git");
        cmd.current_dir(fs::canonicalize(&project_name).unwrap());
        if let Ok(mut child) = cmd.arg("init").spawn() {
            // is dis good? :/
            match child.wait() {
                _ => (),
            }
        } else {
            eprintln!(" [!!!] Could not initialize Git inside {}", &project_name);
            eprintln!(" [!!] Exiting...");
            process::exit(1);
        }
    }

    fn rust_wasm_handler(_project: &mut Project) -> Result<process::ExitStatus, io::Error> {
        // cargo generate --git https://github.com/rustwasm/wasm-pack-template
        if let Ok(mut child) = Command::new("cargo")
            .arg("generate")
            .arg("--git")
            .arg("https://github.com/rustwasm/wasm-pack-template")
            .spawn()
        {
            child.wait()
        } else {
            eprintln!(" [!!!] Could not generate rust-wasm");
            process::exit(1);
        }
    }

    fn create_react_app_handler(project: &mut Project) -> Result<process::ExitStatus, io::Error> {
        let project_name = &Project::read_project_name();
        project.set_project_name(project_name.to_string());

        if let Ok(mut child) = Command::new("create-react-app")
            .arg(project.get_project_name())
            .spawn()
        {
            child.wait()
        } else {
            eprintln!(" [!!!] Could not create-react-app");
            process::exit(1);
        }
    }

    fn deno_handler(project: &mut Project) -> Result<process::ExitStatus, io::Error> {
        let project_name = &Project::read_project_name();
        project.set_project_name(project_name.to_string());

        let dirs = [
            "public/javascripts",
            "public/style",
            "public/res/images",
            "src/routes",
            "tests",
        ];

        let files = [
            "public/index.html",
            "public/javascripts/script.js",
            "public/style/style.css",
            "src/deps.ts",
            "src/mod.ts",
            "src/test_deps.ts",
            ".env",
            ".gitignore",
            "Dockerfile",
            "docker-compose.yml",
            "DrakeFile.ts",
            "lock.json",
            "README.md",
        ];

        // create project root directory
        let mut cmd = Command::new("mkdir");
        cmd.arg("-p")
            .arg(project_name)
            .status()
            .expect(&format!("failed to create {}", project_name)[..]);

        // create sub directories
        let mut cmd = Command::new("mkdir");
        cmd.current_dir(fs::canonicalize(project_name).unwrap());
        cmd.arg("-p").args(&dirs).status()?;

        // create files
        let mut cmd = Command::new("touch");
        cmd.current_dir(fs::canonicalize(project_name).unwrap());
        cmd.args(&files).status()?;

        let path = Path::new(".");
        let path = path.join(project_name);

        let drakefile_ts_path = path.join("DrakeFile.ts");
        let data = Project::echo_drakefile_ts();
        fs::write(drakefile_ts_path, data).expect("Unable to write to DrakeFile.ts file");

        let deps_ts_path = path.join("src").join("deps.ts");
        let data = Project::echo_deps_ts();
        fs::write(deps_ts_path, data).expect("Unable to write to deps.ts file");

        let index_html_path = path.join("public/index.html");
        let data = Project::echo_index_html();
        fs::write(index_html_path, data).expect("Unable to write to public/index.html file");

        let dot_env_path = path.join(".env");
        let data = Project::echo_dot_env();
        fs::write(dot_env_path, data).expect("Unable to write to .env file");

        let docker_compose_yml = path.join("docker-compose.yml");
        let data = Project::echo_docker_compose_yml(project_name.to_string());
        fs::write(docker_compose_yml, data).expect("Unable to write to docker-compose.yml file");

        // this is bad; executing touch cmd again just to get te return type correctly
        cmd.status()
    }

    fn echo_docker_compose_yml(project_name: String) -> String {
        format!(
            "version: '3'
services:
  api:
    container_name: {project_name}
    # image: hayd/deno:alpine-1.5.0
    environment:
      - SHELL=/bin/sh
    command: run --allow-all DrakeFile.ts start
    env_file: .env
    volumes:
      - .:/app
    working_dir: /app
    ports:
      # - \"8000:8000\"
     - \"${{PORT}}:${{PORT}}\"
    restart: always",
            project_name = project_name
        )
        .to_string()
    }

    fn echo_dot_env() -> String {
        "PORT=8000".to_string()
    }

    fn echo_drakefile_ts() -> String {
        "import { desc, task, sh, run } from \"./src/deps.ts\";

desc(\"start app\");
task(\"start\", [], async function () {
  // Add additional permissions
  await sh(
    \"deno run src/mod.ts\",
  );
});

run();"
            .to_string()
    }

    fn echo_deps_ts() -> String {
        "// Standard library dependencies
export * as log from \"https://deno.land/std/log/mod.ts\";

// Third party dependencies
export { desc, task, sh, run } from \"https://deno.land/x/drake/mod.ts\";
export { config } from \"https://deno.land/x/dotenv/mod.ts\";
"
        .to_string()
    }

    fn echo_index_html() -> String {
        "<!DOCTYPE html>
<html lang=\"en\">
<head>
  <meta charset=\"UTF-8\">
  <title>Deno Project</title>
  <link rel=\"stylesheet\" href=\"style/style.css\">
</head>
<body>
  <h1>Deno Project</h1>
  <script src=\"javascripts/script.js\"></script>
</body>
</html>
"
        .to_string()
    }

    fn cargo_bin_handler(project: &mut Project) -> Result<process::ExitStatus, io::Error> {
        let project_name = &Project::read_project_name();
        project.set_project_name(project_name.to_string());

        let flag = "--bin";
        Project::cargo_handler(flag, project_name)
    }

    fn cargo_lib_handler(project: &mut Project) -> Result<process::ExitStatus, io::Error> {
        let project_name = &Project::read_project_name();
        project.set_project_name(project_name.to_string());

        let flag = "--lib";
        Project::cargo_handler(flag, project_name)
    }

    fn cargo_handler(flag: &str, project_name: &String) -> Result<process::ExitStatus, io::Error> {
        if let Ok(mut child) = Command::new("cargo")
            .args(&["new", flag, project_name])
            .spawn()
        {
            child.wait()
        } else {
            eprintln!(" [!!!] Could not generate cargo-bin");
            process::exit(1);
        }
    }

    fn read_project_name() -> String {
        // TODO: filter user input
        io::stdout().write_all(b" Project Name: ").unwrap();
        let _ = io::stdout().flush();

        let mut project_name = String::new();
        io::stdin().read_line(&mut project_name).unwrap();

        project_name.trim().to_string()
    }
}