Skip to content

Commit 3ee7f2d

Browse files
author
ProCode2
committed
initial commit (lol)
0 parents  commit 3ee7f2d

File tree

7 files changed

+414
-0
lines changed

7 files changed

+414
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

Lines changed: 193 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "dgen"
3+
version = "0.1.0"
4+
authors = ["ProCode2 <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
serde = { version = "1.0.126", features = ["derive"] }
11+
serde_json = "1.0"
12+
clap = "2.33.3"

cra.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

src/main.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
use clap::{App, Arg};
2+
use serde::{Deserialize, Serialize};
3+
use serde_json::Result;
4+
use std::env;
5+
use std::fs;
6+
use std::io::prelude::*;
7+
8+
#[derive(Serialize, Deserialize, Debug)]
9+
struct FileNode {
10+
name: String,
11+
content: String,
12+
}
13+
14+
// The core data structure
15+
#[derive(Serialize, Deserialize, Debug)]
16+
struct FolderNode {
17+
name: String,
18+
files: Vec<FileNode>,
19+
folders: Vec<FolderNode>,
20+
}
21+
22+
// the core algorithm
23+
fn dir_from_json(folder: &FolderNode, path: String) -> std::io::Result<()> {
24+
let new_path = format!("{}{}/", path, &folder.name);
25+
26+
// create the new directory
27+
println!("Generating new directory: {}", &new_path);
28+
fs::create_dir(&new_path)?;
29+
30+
// create all the files in this directory
31+
folder.files.iter().for_each(|file| {
32+
println!("Generating new file: {}{}", &new_path, &file.name);
33+
let mut file_handler = fs::File::create(format!("{}{}", &new_path, &file.name)).unwrap();
34+
let _ = file_handler.write_all(&file.content.as_bytes()).unwrap();
35+
});
36+
37+
// base case [TODO: also handle if folder key does not exist]
38+
if folder.folders.len() == 0 {
39+
return Ok(());
40+
}
41+
42+
// recursively calling gen_template
43+
folder.folders.iter().for_each(|node| {
44+
let _ = dir_from_json(&node, new_path.to_string());
45+
});
46+
47+
Ok(())
48+
}
49+
50+
fn json_from_dir(folder: &mut FolderNode, path: String) {
51+
let new_path = format!("{}{}/", path, folder.name);
52+
println!("{}", &new_path);
53+
let paths = fs::read_dir(&new_path).expect(&new_path);
54+
55+
for res_path in paths {
56+
let path = res_path.unwrap();
57+
if fs::metadata(path.path()).unwrap().is_dir() {
58+
let mut f = FolderNode {
59+
name: path
60+
.path()
61+
.file_name()
62+
.unwrap()
63+
.to_string_lossy()
64+
.to_string(),
65+
files: Vec::new(),
66+
folders: Vec::new(),
67+
};
68+
json_from_dir(&mut f, new_path.clone());
69+
folder.folders.push(f);
70+
} else {
71+
if fs::read_to_string(path.path()).is_err() {
72+
continue;
73+
}
74+
let file_content = fs::read_to_string(path.path()).unwrap();
75+
folder.files.push(FileNode {
76+
name: path
77+
.path()
78+
.file_name()
79+
.unwrap()
80+
.to_string_lossy()
81+
.to_string(),
82+
content: file_content,
83+
})
84+
}
85+
}
86+
}
87+
88+
fn generate_json() {
89+
let root_name = env::current_dir()
90+
.unwrap()
91+
.file_name()
92+
.unwrap()
93+
.to_string_lossy()
94+
.to_string();
95+
96+
let mut f = FolderNode {
97+
name: String::from(&root_name),
98+
files: Vec::new(),
99+
folders: Vec::new(),
100+
};
101+
102+
json_from_dir(&mut f, String::from("../"));
103+
let json_template = serde_json::to_string(&f).unwrap();
104+
match fs::write(format!("{}.json", root_name), &json_template) {
105+
Ok(_) => println!("Done!"),
106+
Err(_) => {
107+
println!("Failed to write json into file");
108+
println!("Printing the result to console instead:");
109+
println!("{}", &json_template);
110+
}
111+
}
112+
}
113+
114+
fn generate_dir(json_path: &str) {
115+
let json_data = fs::read_to_string(&json_path).expect("smth wrong with that file, hmm sus...");
116+
let handler: FolderNode = serde_json::from_str(&json_data).unwrap();
117+
let init_path = format!("{}/", env::current_dir().unwrap().display().to_string());
118+
let _ = dir_from_json(&handler, init_path);
119+
}
120+
121+
fn main() -> Result<()> {
122+
let matches = App::new("Dgen")
123+
.version("1.0")
124+
.author("ProCode")
125+
.about("Create your starter repositories from a single json blueprint.")
126+
.arg(
127+
Arg::with_name("Generate_JSON")
128+
.short("b")
129+
.long("blueprint")
130+
.help("Create json blueprint of the directory you are in."),
131+
)
132+
.arg(
133+
Arg::with_name("Generate_Repository")
134+
.short("g")
135+
.long("generate")
136+
.value_name("JSON blueprint name")
137+
.help("Create the directory from the json blueprint.")
138+
.takes_value(false),
139+
)
140+
.get_matches();
141+
142+
if matches.is_present("Generate_JSON") {
143+
generate_json();
144+
} else if matches.is_present("Generate_Repository") {
145+
let json_path = matches
146+
.value_of("Generate_Repository")
147+
.unwrap_or("template.json");
148+
generate_dir(json_path);
149+
}
150+
151+
Ok(())
152+
}

0 commit comments

Comments
 (0)