-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from metacall/metassr-create
complete: metassr-create
- Loading branch information
Showing
18 changed files
with
463 additions
and
592 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,6 @@ build = "build.rs" | |
|
||
[build-dependencies] | ||
walkdir = "2.5.0" | ||
|
||
[dependencies] | ||
anyhow = "1.0.86" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,114 @@ | ||
pub fn add(left: usize, right: usize) -> usize { | ||
left + right | ||
include!(concat!(env!("OUT_DIR"), "/templates.rs")); | ||
|
||
use std::{ | ||
fs::{create_dir_all, File}, | ||
io::Write, | ||
path::PathBuf, | ||
}; | ||
|
||
use anyhow::{anyhow, Result}; | ||
use templates::Template; | ||
|
||
mod templates; | ||
|
||
pub mod tags { | ||
pub const VERSION: &str = "%VER%"; | ||
pub const NAME: &str = "%NAME%"; | ||
pub const DESC: &str = "%DESC%"; | ||
} | ||
|
||
pub struct Creator { | ||
project_name: String, | ||
version: String, | ||
description: String, | ||
template: Template, | ||
} | ||
impl Creator { | ||
pub fn new(project_name: &str, version: &str, desc: &str, template: &str) -> Self { | ||
Self { | ||
project_name: project_name.to_string(), | ||
version: version.to_string(), | ||
description: desc.to_string(), | ||
template: Template::from(template), | ||
} | ||
} | ||
pub fn generate(&self) -> Result<()> { | ||
let template = self.template.load(&self)?; | ||
Check warning on line 36 in crates/metassr-create/src/lib.rs
|
||
let root = PathBuf::from(&self.project_name); | ||
|
||
if root.exists() { | ||
return Err(anyhow!("Path already exists.")); | ||
} | ||
for (file, buf) in template { | ||
let path = root.join(&file); | ||
create_dir_all(path.parent().unwrap())?; | ||
|
||
let _ = File::create(path)?.write(&buf)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
mod test { | ||
use crate::{tags, Creator}; | ||
use anyhow::Result; | ||
use std::{ | ||
env::set_current_dir, | ||
fs::create_dir, | ||
path::Path, | ||
str::from_utf8, | ||
time::{SystemTime, UNIX_EPOCH}, | ||
}; | ||
|
||
fn init_test_dir() -> Result<()> { | ||
let path = Path::new("tests"); | ||
if !path.exists() { | ||
create_dir(path)?; | ||
} | ||
set_current_dir(path)?; | ||
Ok(()) | ||
} | ||
|
||
include!(concat!(env!("OUT_DIR"), "/templates.rs")); | ||
#[test] | ||
fn load_template() { | ||
dbg!(&from_utf8( | ||
load_templates() | ||
.get("typescript") | ||
.unwrap() | ||
.get("src/_head.tsx") | ||
.unwrap() | ||
) | ||
.unwrap() | ||
.replace(tags::VERSION, "1.0.0") | ||
.replace(tags::NAME, "MetaSSR")); | ||
} | ||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
fn generate_templates() -> Result<()> { | ||
init_test_dir()?; | ||
let project_name = SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.unwrap() | ||
.as_secs() | ||
.to_string(); | ||
dbg!(&project_name); | ||
Creator::new( | ||
&format!("{}-javascript", project_name), | ||
"1.0.0", | ||
"Hello World!", | ||
"js", | ||
) | ||
.generate()?; | ||
Creator::new( | ||
&format!("{}-typescript", project_name), | ||
"1.0.0", | ||
"Hello World!", | ||
"ts", | ||
) | ||
.generate()?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::{load_templates, tags, Creator}; | ||
use anyhow::Result; | ||
use std::{collections::HashMap, str::from_utf8}; | ||
|
||
pub enum Template { | ||
Javascript, | ||
Typescript, | ||
} | ||
|
||
impl From<&str> for Template { | ||
fn from(value: &str) -> Self { | ||
match value.to_lowercase().as_str() { | ||
"javascript" | "js" => Self::Javascript, | ||
"typescript" | "ts" => Self::Typescript, | ||
_ => unreachable!("Template isn't detected."), | ||
} | ||
} | ||
} | ||
|
||
impl ToString for Template { | ||
fn to_string(&self) -> String { | ||
match *self { | ||
Self::Javascript => "javascript", | ||
Self::Typescript => "typescript", | ||
} | ||
.to_string() | ||
} | ||
} | ||
Check warning on line 28 in crates/metassr-create/src/templates.rs
|
||
|
||
impl Template { | ||
pub fn load(&self, creator: &Creator) -> Result<HashMap<String, Vec<u8>>> { | ||
let template = load_templates(); | ||
let template = template.get(&self.to_string()).unwrap(); | ||
let mut template = template.clone(); | ||
let package_json = from_utf8(template.get("package.json").unwrap())? | ||
.replace(tags::NAME, &creator.project_name) | ||
.replace(tags::VERSION, &creator.version) | ||
.replace(tags::DESC, &creator.description) | ||
.as_bytes() | ||
.to_vec(); | ||
template.insert("package.json".to_string(), package_json); | ||
let ext = match *self { | ||
Template::Javascript => "jsx", | ||
Template::Typescript => "tsx", | ||
}; | ||
let head = from_utf8(template.get(&format!("src/_head.{ext}")).unwrap())? | ||
.replace(tags::NAME, &creator.project_name) | ||
.replace(tags::VERSION, &creator.version) | ||
.as_bytes() | ||
.to_vec(); | ||
template.insert(format!("src/_head.{ext}"), head); | ||
|
||
Ok(template) | ||
} | ||
} |
Oops, something went wrong.