-
-
Notifications
You must be signed in to change notification settings - Fork 119
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 #1344 from MartinKavik/model_new
Command to create a new model
- Loading branch information
Showing
6 changed files
with
164 additions
and
1 deletion.
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,43 @@ | ||
use ignore::WalkBuilder; | ||
use std::{collections::HashSet, env, ffi::OsStr, fs::File, path::Path}; | ||
|
||
static NEW_MODEL_TEMPLATE: &str = "star"; | ||
static EXTRA_IGNORED_FILES: &[&str] = &["star.png", "README.md"]; | ||
|
||
fn main() { | ||
create_new_model_tar(); | ||
} | ||
|
||
fn create_new_model_tar() { | ||
let out_dir = env::var_os("OUT_DIR").unwrap(); | ||
let file = File::create(Path::new(&out_dir).join("new_model.tar")).unwrap(); | ||
let mut tar_builder = tar::Builder::new(file); | ||
|
||
let manifest_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap(); | ||
let fornjot_root_path = Path::new(&manifest_dir) | ||
.ancestors() | ||
.nth(2) | ||
.expect("Failed to get 'fornjot_root_path' path"); | ||
|
||
let new_model_path = | ||
fornjot_root_path.join("models").join(NEW_MODEL_TEMPLATE); | ||
|
||
let extra_ignored_files = EXTRA_IGNORED_FILES | ||
.iter() | ||
.map(OsStr::new) | ||
.collect::<HashSet<_>>(); | ||
|
||
for entry in WalkBuilder::new(&new_model_path).hidden(false).build() { | ||
let path = entry.unwrap().into_path(); | ||
if path.is_dir() | ||
|| extra_ignored_files.contains(&path.file_name().unwrap()) | ||
{ | ||
continue; | ||
} | ||
let tar_path = path.strip_prefix(&new_model_path).unwrap(); | ||
tar_builder | ||
.append_file(tar_path, &mut File::open(&path).unwrap()) | ||
.unwrap(); | ||
} | ||
tar_builder.finish().unwrap(); | ||
} |
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,48 @@ | ||
use std::{fs, path::Path}; | ||
use tar::Archive; | ||
|
||
static NEW_MODEL_TEMPLATE: &str = "star"; | ||
|
||
static NEW_MODEL_TAR: &[u8] = | ||
include_bytes!(concat!(env!("OUT_DIR"), "/new_model.tar")); | ||
|
||
pub fn create(model_name: &str) -> anyhow::Result<()> { | ||
let path = Path::new(model_name); | ||
Archive::new(NEW_MODEL_TAR).unpack(path)?; | ||
postprocess_model_files(path, model_name)?; | ||
println!("Model '{model_name}' created"); | ||
Ok(()) | ||
} | ||
|
||
fn postprocess_model_files( | ||
path: &Path, | ||
model_name: &str, | ||
) -> anyhow::Result<()> { | ||
replace_in_file( | ||
&path.join("Cargo.toml"), | ||
[ | ||
( | ||
format!("name = \"{NEW_MODEL_TEMPLATE}\""), | ||
format!("name = \"{model_name}\""), | ||
), | ||
( | ||
r#"path = "../../crates/fj""#.to_owned(), | ||
["version = \"", fj::version::VERSION_PKG, "\""].concat(), | ||
), | ||
], | ||
)?; | ||
fs::write(path.join("README.md"), format!("# {model_name}\n"))?; | ||
Ok(()) | ||
} | ||
|
||
fn replace_in_file( | ||
path: &Path, | ||
replacements: impl IntoIterator<Item = (String, String)>, | ||
) -> anyhow::Result<()> { | ||
let mut content = fs::read_to_string(path)?; | ||
for (from, to) in replacements { | ||
content = content.replace(&from, &to); | ||
} | ||
fs::write(path, content)?; | ||
Ok(()) | ||
} |