-
Notifications
You must be signed in to change notification settings - Fork 1
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 #1 from refcell/refcell/cli-impl
feat: v0.1.2
- Loading branch information
Showing
13 changed files
with
399 additions
and
6 deletions.
There are no files selected for viewing
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,28 @@ | ||
use std::{fs, io}; | ||
|
||
fn main() { | ||
let out_dir = std::env::var("OUT_DIR").unwrap(); | ||
|
||
fs::create_dir_all(format!("{}/templates/bin/", out_dir)) | ||
.expect("unable to create templates bin directory"); | ||
fs::create_dir_all(format!("{}/templates/lib/", out_dir)) | ||
.expect("unable to create templates lib directory"); | ||
|
||
copy_content(&out_dir, "templates/bin/Cargo.toml"); | ||
copy_content(&out_dir, "templates/bin/main.rs"); | ||
copy_content(&out_dir, "templates/lib/Cargo.toml"); | ||
copy_content(&out_dir, "templates/lib/lib.rs"); | ||
copy_content(&out_dir, "templates/Cargo.toml"); | ||
} | ||
|
||
fn copy_content(out: &str, source: &str) { | ||
let out_path = format!("{}/{}", out, source); | ||
let mut out_file = fs::OpenOptions::new() | ||
.append(true) | ||
.create(true) | ||
.open(out_path) | ||
.expect("unable to open/create data file"); | ||
if let Ok(mut source_file) = fs::File::open(source) { | ||
io::copy(&mut source_file, &mut out_file).expect("failed to copy data after opening"); | ||
} | ||
} |
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,118 @@ | ||
use std::io::Write; | ||
use std::path::Path; | ||
|
||
use eyre::Result; | ||
use ptree::TreeBuilder; | ||
use tracing::instrument; | ||
|
||
/// Creates a new bin crate. | ||
#[instrument(name = "bin", skip(dir, name, dry, tree))] | ||
pub(crate) fn create( | ||
dir: &Path, | ||
name: impl AsRef<str>, | ||
dry: bool, | ||
mut tree: Option<&mut TreeBuilder>, | ||
) -> Result<()> { | ||
tracing::info!("Creating binary crate"); | ||
|
||
let project_path_buf = dir.join(name.as_ref()); | ||
let cargo_toml_path_buf = project_path_buf.join("Cargo.toml"); | ||
let src_path_buf = project_path_buf.join("src"); | ||
let main_rs_path_buf = project_path_buf.join("src").join("main.rs"); | ||
|
||
if !dry { | ||
tracing::debug!("Creating bin directory at {:?}", dir); | ||
std::fs::create_dir_all(dir)?; | ||
} | ||
tree.as_deref_mut() | ||
.map(|t| t.begin_child("bin".to_string())); | ||
|
||
if !dry { | ||
tracing::debug!("Creating crate directory at {:?}", project_path_buf); | ||
std::fs::create_dir_all(&project_path_buf)?; | ||
} | ||
tree.as_deref_mut() | ||
.map(|t| t.begin_child(name.as_ref().to_string())); | ||
|
||
if !dry { | ||
tracing::debug!( | ||
"Creating crate Cargo.toml file as {:?}", | ||
cargo_toml_path_buf | ||
); | ||
let mut cargo_toml = std::fs::File::create(&cargo_toml_path_buf)?; | ||
cargo_toml.write_all(include_bytes!(concat!( | ||
env!("OUT_DIR"), | ||
"/templates/bin/Cargo.toml" | ||
)))?; | ||
} | ||
tree.as_deref_mut() | ||
.map(|t| t.add_empty_child("Cargo.toml".to_string())); | ||
|
||
if !dry { | ||
tracing::debug!("Creating crate src directory at {:?}", src_path_buf); | ||
std::fs::create_dir_all(&src_path_buf)?; | ||
} | ||
tree.as_deref_mut() | ||
.map(|t| t.begin_child("src".to_string())); | ||
|
||
if !dry { | ||
tracing::debug!("Creating main.rs file as {:?}", main_rs_path_buf); | ||
let mut main_rs = std::fs::File::create(&main_rs_path_buf)?; | ||
main_rs.write_all(include_bytes!(concat!( | ||
env!("OUT_DIR"), | ||
"/templates/bin/main.rs" | ||
)))?; | ||
} | ||
tree.as_deref_mut() | ||
.map(|t| t.add_empty_child("main.rs".to_string())); | ||
|
||
tree.as_deref_mut().map(|t| t.end_child()); // <- src/ | ||
tree.as_deref_mut().map(|t| t.end_child()); // <- <name>/ | ||
tree.map(|t| t.end_child()); // <- bin/ | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs::File; | ||
use std::io::Read; | ||
use tempfile::tempdir; | ||
|
||
#[test] | ||
fn test_create() { | ||
let dir = tempdir().unwrap(); | ||
let dir_path_buf = dir.path().to_path_buf(); | ||
let bin_path_buf = dir_path_buf.join("bin"); | ||
let project_name = "example"; | ||
let project_path = bin_path_buf.join(project_name); | ||
create(&bin_path_buf, project_name, false, None).unwrap(); | ||
|
||
assert!(project_path.exists()); | ||
assert!(project_path.join("src").exists()); | ||
assert!(project_path.join("src").join("main.rs").exists()); | ||
assert!(project_path.join("Cargo.toml").exists()); | ||
|
||
let mut main_rs = File::open(project_path.join("src").join("main.rs")).unwrap(); | ||
let mut main_rs_contents = String::new(); | ||
main_rs.read_to_string(&mut main_rs_contents).unwrap(); | ||
let expected_contents = "fn main() {\n println!(\"Hello World!\");\n}\n"; | ||
assert_eq!(main_rs_contents, expected_contents); | ||
} | ||
|
||
#[test] | ||
fn test_create_dry_run() { | ||
let dir = tempdir().unwrap(); | ||
let dir_path_buf = dir.path().to_path_buf(); | ||
let bin_path_buf = dir_path_buf.join("bin"); | ||
let project_name = "example"; | ||
let project_path = bin_path_buf.join(project_name); | ||
create(&bin_path_buf, project_name, true, None).unwrap(); | ||
|
||
assert!(!project_path.exists()); | ||
assert!(!project_path.join("src").exists()); | ||
assert!(!project_path.join("src").join("main.rs").exists()); | ||
assert!(!project_path.join("Cargo.toml").exists()); | ||
} | ||
} |
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
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,117 @@ | ||
use std::io::Write; | ||
use std::path::Path; | ||
|
||
use eyre::Result; | ||
use ptree::TreeBuilder; | ||
use tracing::instrument; | ||
|
||
/// Creates a new lib crate. | ||
#[instrument(name = "lib", skip(dir, name, dry, tree))] | ||
pub(crate) fn create( | ||
dir: &Path, | ||
name: impl AsRef<str>, | ||
dry: bool, | ||
mut tree: Option<&mut TreeBuilder>, | ||
) -> Result<()> { | ||
tracing::info!("Creating lib crate"); | ||
|
||
let lib_path_buf = dir.join(name.as_ref()); | ||
let src_path_buf = lib_path_buf.join("src"); | ||
let cargo_toml_path_buf = lib_path_buf.join("Cargo.toml"); | ||
let lib_rs_path_buf = lib_path_buf.join("src").join("lib.rs"); | ||
|
||
if !dry { | ||
tracing::debug!("Creating crates directory at {:?}", dir); | ||
std::fs::create_dir_all(dir)?; | ||
} | ||
tree.as_deref_mut() | ||
.map(|t| t.begin_child("crates".to_string())); | ||
|
||
if !dry { | ||
tracing::debug!("Creating crate directory at {:?}", lib_path_buf); | ||
std::fs::create_dir_all(&lib_path_buf)?; | ||
} | ||
tree.as_deref_mut() | ||
.map(|t| t.begin_child(name.as_ref().to_string())); | ||
|
||
if !dry { | ||
tracing::debug!( | ||
"Creating crate Cargo.toml file as {:?}", | ||
cargo_toml_path_buf | ||
); | ||
let mut cargo_toml = std::fs::File::create(&cargo_toml_path_buf)?; | ||
cargo_toml.write_all(include_bytes!(concat!( | ||
env!("OUT_DIR"), | ||
"/templates/lib/Cargo.toml" | ||
)))?; | ||
} | ||
tree.as_deref_mut() | ||
.map(|t| t.add_empty_child("Cargo.toml".to_string())); | ||
|
||
if !dry { | ||
tracing::debug!("Creating crate src directory at {:?}", src_path_buf); | ||
std::fs::create_dir_all(&src_path_buf)?; | ||
} | ||
tree.as_deref_mut() | ||
.map(|t| t.begin_child("src".to_string())); | ||
|
||
if !dry { | ||
tracing::debug!("Creating lib.rs file as {:?}", lib_rs_path_buf); | ||
let mut lib_rs = std::fs::File::create(&lib_rs_path_buf)?; | ||
lib_rs.write_all(include_bytes!(concat!( | ||
env!("OUT_DIR"), | ||
"/templates/lib/lib.rs" | ||
)))?; | ||
} | ||
tree.as_deref_mut() | ||
.map(|t| t.add_empty_child("main.rs".to_string())); | ||
|
||
tree.as_deref_mut().map(|t| t.end_child()); // <- src/ | ||
tree.as_deref_mut().map(|t| t.end_child()); // <- <name>/ | ||
tree.map(|t| t.end_child()); // <- crates | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs::File; | ||
use std::io::Read; | ||
use tempfile::tempdir; | ||
|
||
#[test] | ||
fn test_create() { | ||
let dir = tempdir().unwrap(); | ||
let dir_path_buf = dir.path().to_path_buf(); | ||
let crates_path_buf = dir_path_buf.join("crates"); | ||
let project_name = "example"; | ||
let project_path = crates_path_buf.join(project_name); | ||
create(&crates_path_buf, project_name, false, None).unwrap(); | ||
|
||
assert!(project_path.exists()); | ||
assert!(project_path.join("src").exists()); | ||
assert!(project_path.join("src").join("lib.rs").exists()); | ||
assert!(project_path.join("Cargo.toml").exists()); | ||
|
||
let mut lib_rs = File::open(project_path.join("src").join("lib.rs")).unwrap(); | ||
let mut lib_rs_contents = String::new(); | ||
lib_rs.read_to_string(&mut lib_rs_contents).unwrap(); | ||
assert!(lib_rs_contents.len() > 0); | ||
} | ||
|
||
#[test] | ||
fn test_create_dry_run() { | ||
let dir = tempdir().unwrap(); | ||
let dir_path_buf = dir.path().to_path_buf(); | ||
let crates_path_buf = dir_path_buf.join("crates"); | ||
let project_name = "example"; | ||
let project_path = crates_path_buf.join(project_name); | ||
create(&crates_path_buf, project_name, true, None).unwrap(); | ||
|
||
assert!(!project_path.exists()); | ||
assert!(!project_path.join("src").exists()); | ||
assert!(!project_path.join("src").join("lib.rs").exists()); | ||
assert!(!project_path.join("Cargo.toml").exists()); | ||
} | ||
} |
Oops, something went wrong.