From 6d9243e8957bbae61944cccf8dac029ea290382c Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Wed, 17 May 2023 17:46:25 -0500 Subject: [PATCH] added: new flag to create a skeleton directory --- src/lib.rs | 37 +++++++++++++++++++++++++++++ src/main.rs | 6 ++++- src/options/arg_parser.rs | 3 +++ src/options/opt_struct.rs | 3 +++ test_siet/build/hello_world.html | 11 +++++++++ test_siet/pattern/base/default.meta | 5 ++++ test_siet/pattern/body/default.meta | 3 +++ test_siet/pattern/foot/default.meta | 2 ++ test_siet/pattern/head/default.meta | 3 +++ test_siet/source/hello_world.meta | 3 +++ 10 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 test_siet/build/hello_world.html create mode 100644 test_siet/pattern/base/default.meta create mode 100644 test_siet/pattern/body/default.meta create mode 100644 test_siet/pattern/foot/default.meta create mode 100644 test_siet/pattern/head/default.meta create mode 100644 test_siet/source/hello_world.meta diff --git a/src/lib.rs b/src/lib.rs index d7443ce..dd757de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ pub use parser::*; use clap::Parser; use color_eyre::Result; +use std::fs; pub fn get_opts() -> Result { let opts = Options::try_from(Opts::parse())?; @@ -38,3 +39,39 @@ pub fn build_dir(opts: &Options) -> Result<()> { source.build_dir() } + +pub fn new_site(opts: &Options) -> Result<()> { + macro_rules! exist_or_build( + ($p:expr) => { + if !$p.exists() { + fs::create_dir_all(&$p)?; + } + }; + ); + + macro_rules! write_default ( + ($p:expr, $m:literal) => { + let path = opts.pattern.join($p).join("default.meta"); + fs::write(path, $m)?; + }; + ); + + exist_or_build!(opts.root); + exist_or_build!(opts.source); + exist_or_build!(opts.pattern); + + exist_or_build!(opts.pattern.join("base")); + exist_or_build!(opts.pattern.join("body")); + exist_or_build!(opts.pattern.join("head")); + exist_or_build!(opts.pattern.join("foot")); + + write_default!("base", "\n&{head}\n&{body}\n&{foot}\n\n"); + write_default!("body", "\n&{SOURCE}\n\n"); + write_default!("head", "\nHELLO WORLD\n\n"); + write_default!("foot", "\n${footer}\n\n"); + + let path = opts.source.join("hello_world.meta"); + fs::write(path, "${ footer = 'made using metaforge' }\n# it works\ncall `metaforge -h` for help, or read the [readme](https://huck.website/code/metaforge)\n")?; + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 6304640..760e9c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,5 +3,9 @@ fn main() -> color_eyre::Result<()> { let opts = metaforge::get_opts()?; - metaforge::build_dir(&opts) + if opts.new { + metaforge::new_site(&opts) + } else { + metaforge::build_dir(&opts) + } } diff --git a/src/options/arg_parser.rs b/src/options/arg_parser.rs index 868fdbd..ec829d2 100644 --- a/src/options/arg_parser.rs +++ b/src/options/arg_parser.rs @@ -18,6 +18,9 @@ pub struct Opts { /// Pattern directory [CURRENT_DIR/pattern] #[arg(short, long, value_name = "PATTERN_DIR")] pub pattern: Option, + /// Create a new skeleton directory + #[arg(long, default_value_t = false)] + pub new: bool, /// Enable extra output. /// Repeated flags give more info #[arg(short, long, action = clap::ArgAction::Count)] diff --git a/src/options/opt_struct.rs b/src/options/opt_struct.rs index e37aa63..49d0031 100644 --- a/src/options/opt_struct.rs +++ b/src/options/opt_struct.rs @@ -13,6 +13,7 @@ pub struct Options { pub undefined: bool, pub clean: bool, pub no_pandoc: bool, + pub new: bool, } impl Options { @@ -28,6 +29,7 @@ impl Options { undefined: false, clean: false, no_pandoc: false, + new: false, } } } @@ -43,6 +45,7 @@ impl TryFrom for Options { options.undefined = value.undefined; options.clean = value.clean; options.no_pandoc = value.no_pandoc; + options.new = value.new; if let Some(root) = value.root.as_deref() { options.root = PathBuf::from(root).canonicalize()?; diff --git a/test_siet/build/hello_world.html b/test_siet/build/hello_world.html new file mode 100644 index 0000000..02b4aa0 --- /dev/null +++ b/test_siet/build/hello_world.html @@ -0,0 +1,11 @@ + + +HELLO WORLD + + +

it works

+

call metaforge -h for help, or read the readme

+ +made using metaforge + diff --git a/test_siet/pattern/base/default.meta b/test_siet/pattern/base/default.meta new file mode 100644 index 0000000..a0b16bc --- /dev/null +++ b/test_siet/pattern/base/default.meta @@ -0,0 +1,5 @@ + +&{head} +&{body} +&{foot} + diff --git a/test_siet/pattern/body/default.meta b/test_siet/pattern/body/default.meta new file mode 100644 index 0000000..c91a409 --- /dev/null +++ b/test_siet/pattern/body/default.meta @@ -0,0 +1,3 @@ + +&{SOURCE} + diff --git a/test_siet/pattern/foot/default.meta b/test_siet/pattern/foot/default.meta new file mode 100644 index 0000000..336bf8d --- /dev/null +++ b/test_siet/pattern/foot/default.meta @@ -0,0 +1,2 @@ +${footer} + diff --git a/test_siet/pattern/head/default.meta b/test_siet/pattern/head/default.meta new file mode 100644 index 0000000..bed94d1 --- /dev/null +++ b/test_siet/pattern/head/default.meta @@ -0,0 +1,3 @@ + +HELLO WORLD + diff --git a/test_siet/source/hello_world.meta b/test_siet/source/hello_world.meta new file mode 100644 index 0000000..718d479 --- /dev/null +++ b/test_siet/source/hello_world.meta @@ -0,0 +1,3 @@ +${ footer = 'made using metaforge' } +# it works +call `metaforge -h` for help, or read the [readme](https://huck.website/code/metaforge) -- 2.45.2