From 1a5f521133e1039848fd3ca2c92302359b80b53d Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 17 Nov 2022 13:13:12 +0100 Subject: [PATCH 1/5] Add dependency on `include_dir` to `fj-app` --- Cargo.lock | 20 ++++++++++++++++++++ crates/fj-app/Cargo.toml | 1 + 2 files changed, 21 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 36ede137e..da863f29a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1164,6 +1164,7 @@ dependencies = [ "fj-viewer", "fj-window", "ignore", + "include_dir", "serde", "tar", "tracing-subscriber", @@ -1829,6 +1830,25 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "include_dir" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18762faeff7122e89e0857b02f7ce6fcc0d101d5e9ad2ad7846cc01d61b7f19e" +dependencies = [ + "include_dir_macros", +] + +[[package]] +name = "include_dir_macros" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b139284b5cf57ecfa712bcc66950bb635b31aff41c188e8a4cfc758eca374a3f" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "indexmap" version = "1.9.1" diff --git a/crates/fj-app/Cargo.toml b/crates/fj-app/Cargo.toml index 59c7bf3de..0affc5e92 100644 --- a/crates/fj-app/Cargo.toml +++ b/crates/fj-app/Cargo.toml @@ -29,6 +29,7 @@ fj-math.workspace = true fj-operations.workspace = true fj-viewer.workspace = true fj-window.workspace = true +include_dir = "0.7.3" [dependencies.clap] version = "4.0.23" From 20a210cd4da11ddf4ffcefa784ed2957854901f1 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 17 Nov 2022 13:28:00 +0100 Subject: [PATCH 2/5] Use `include_dir` to bundle model template --- crates/fj-app/build.rs | 36 -------------------------------- crates/fj-app/src/model_crate.rs | 8 +++---- 2 files changed, 4 insertions(+), 40 deletions(-) delete mode 100644 crates/fj-app/build.rs diff --git a/crates/fj-app/build.rs b/crates/fj-app/build.rs deleted file mode 100644 index 7821a1972..000000000 --- a/crates/fj-app/build.rs +++ /dev/null @@ -1,36 +0,0 @@ -use ignore::WalkBuilder; -use std::{collections::HashSet, env, ffi::OsStr, fs::File, path::Path}; - -static EXTRA_IGNORED_FILES: &[&str] = &["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 new_model_path = Path::new(&manifest_dir).join("model-template"); - - let extra_ignored_files = EXTRA_IGNORED_FILES - .iter() - .map(OsStr::new) - .collect::>(); - - 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(); -} diff --git a/crates/fj-app/src/model_crate.rs b/crates/fj-app/src/model_crate.rs index 95b6de17e..2f5edcd6e 100644 --- a/crates/fj-app/src/model_crate.rs +++ b/crates/fj-app/src/model_crate.rs @@ -1,12 +1,12 @@ +use include_dir::{include_dir, Dir}; use std::{fs, path::Path}; -use tar::Archive; -static NEW_MODEL_TAR: &[u8] = - include_bytes!(concat!(env!("OUT_DIR"), "/new_model.tar")); +static MODEL_TEMPLATE: Dir = include_dir!("$CARGO_MANIFEST_DIR/model-template"); pub fn create(model_name: &str) -> anyhow::Result<()> { let path = Path::new(model_name); - Archive::new(NEW_MODEL_TAR).unpack(path)?; + fs::create_dir_all(path)?; + MODEL_TEMPLATE.extract(path)?; postprocess_model_files(path, model_name)?; println!("Model '{model_name}' created"); Ok(()) From 32809296d54b5f1348534954392caef52c1e49e6 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 17 Nov 2022 13:29:36 +0100 Subject: [PATCH 3/5] Update function name --- crates/fj-app/src/model_crate.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-app/src/model_crate.rs b/crates/fj-app/src/model_crate.rs index 2f5edcd6e..41d835279 100644 --- a/crates/fj-app/src/model_crate.rs +++ b/crates/fj-app/src/model_crate.rs @@ -7,12 +7,12 @@ pub fn create(model_name: &str) -> anyhow::Result<()> { let path = Path::new(model_name); fs::create_dir_all(path)?; MODEL_TEMPLATE.extract(path)?; - postprocess_model_files(path, model_name)?; + post_process_model_files(path, model_name)?; println!("Model '{model_name}' created"); Ok(()) } -fn postprocess_model_files( +fn post_process_model_files( path: &Path, model_name: &str, ) -> anyhow::Result<()> { From f18ce8ff69506884ce9579c21e1de15525990563 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 17 Nov 2022 13:32:17 +0100 Subject: [PATCH 4/5] Remove unused dependencies from `fj-app` --- Cargo.lock | 52 ---------------------------------------- crates/fj-app/Cargo.toml | 12 ---------- 2 files changed, 64 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index da863f29a..186ea225d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -346,15 +346,6 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" -[[package]] -name = "bstr" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" -dependencies = [ - "memchr", -] - [[package]] name = "bumpalo" version = "3.11.1" @@ -1163,10 +1154,8 @@ dependencies = [ "fj-operations", "fj-viewer", "fj-window", - "ignore", "include_dir", "serde", - "tar", "tracing-subscriber", ] @@ -1520,19 +1509,6 @@ version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d" -[[package]] -name = "globset" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a1e17342619edbc21a964c2afbeb6c820c6a2560032872f397bb97ea127bd0a" -dependencies = [ - "aho-corasick", - "bstr", - "fnv", - "log", - "regex", -] - [[package]] name = "glow" version = "0.11.2" @@ -1812,24 +1788,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "ignore" -version = "0.4.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "713f1b139373f96a2e0ce3ac931cd01ee973c3c5dd7c40c0c2efe96ad2b6751d" -dependencies = [ - "crossbeam-utils", - "globset", - "lazy_static", - "log", - "memchr", - "regex", - "same-file", - "thread_local", - "walkdir", - "winapi-util", -] - [[package]] name = "include_dir" version = "0.7.3" @@ -3593,16 +3551,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "tar" -version = "0.4.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b55807c0344e1e6c04d7c965f5289c39a8d94ae23ed5c0b57aabac549f871c6" -dependencies = [ - "filetime", - "libc", -] - [[package]] name = "tempfile" version = "3.3.0" diff --git a/crates/fj-app/Cargo.toml b/crates/fj-app/Cargo.toml index 0affc5e92..aad12bb28 100644 --- a/crates/fj-app/Cargo.toml +++ b/crates/fj-app/Cargo.toml @@ -10,14 +10,6 @@ license.workspace = true keywords.workspace = true categories.workspace = true -[build-dependencies.ignore] -version = "0.4.18" -default-features = false - -[build-dependencies.tar] -version = "0.4.35" -default-features = false - [dependencies] anyhow = "1.0.66" fj.workspace = true @@ -46,7 +38,3 @@ features = ["derive"] [dependencies.tracing-subscriber] version = "0.3.16" features = ["env-filter", "fmt"] - -[dependencies.tar] -version = "0.4.35" -default-features = false From 4301e06373e7fa618044e3340865ffce7896382a Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 17 Nov 2022 13:40:45 +0100 Subject: [PATCH 5/5] Improve README of generated models --- crates/fj-app/model-template/README.md | 11 +++++++++-- crates/fj-app/src/model_crate.rs | 5 ++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/crates/fj-app/model-template/README.md b/crates/fj-app/model-template/README.md index 29281129f..c43f25115 100644 --- a/crates/fj-app/model-template/README.md +++ b/crates/fj-app/model-template/README.md @@ -1,3 +1,10 @@ -# Fornjot - Model Template +# Model Template -This template is compiled into the `fj-app` binary, where it is used to generate new models. +This is a Fornjot model that has been freshly generated! [**Fornjot**][Fornjot] is a next-generation, code-first CAD application. [**Check out the website**][Fornjot] to learn more! + +Fornjot models are written in Rust, and a Fornjot model is just a Cargo package. If you don't fully understand what's going on here, check out the Rust documentation: [**https://www.rust-lang.org/learn**](https://www.rust-lang.org/learn) + +Once you feel ready to dive in, get your favorite editor and check out `src/lib.rs`, which contains the source code for this model. + + +[Fornjot]: https://www.fornjot.app/ diff --git a/crates/fj-app/src/model_crate.rs b/crates/fj-app/src/model_crate.rs index 41d835279..738fa216b 100644 --- a/crates/fj-app/src/model_crate.rs +++ b/crates/fj-app/src/model_crate.rs @@ -30,7 +30,10 @@ fn post_process_model_files( ), ], )?; - fs::write(path.join("README.md"), format!("# {model_name}\n"))?; + replace_in_file( + &path.join("README.md"), + [("# Model Template".to_string(), format!("# {model_name}"))], + )?; Ok(()) }