]> git.huck.website - metaforge.git/commitdiff
added: new flag to create a skeleton directory
authorHuck Boles <huck@huck.website>
Wed, 17 May 2023 22:46:25 +0000 (17:46 -0500)
committerHuck Boles <huck@huck.website>
Wed, 17 May 2023 22:46:25 +0000 (17:46 -0500)
src/lib.rs
src/main.rs
src/options/arg_parser.rs
src/options/opt_struct.rs
test_siet/build/hello_world.html [new file with mode: 0644]
test_siet/pattern/base/default.meta [new file with mode: 0644]
test_siet/pattern/body/default.meta [new file with mode: 0644]
test_siet/pattern/foot/default.meta [new file with mode: 0644]
test_siet/pattern/head/default.meta [new file with mode: 0644]
test_siet/source/hello_world.meta [new file with mode: 0644]

index d7443ce1765fb29ccba126a7a534c4540b395b22..dd757de1d035699c935ced3633abec6fdf9f8688 100644 (file)
@@ -14,6 +14,7 @@ pub use parser::*;
 
 use clap::Parser;
 use color_eyre::Result;
+use std::fs;
 
 pub fn get_opts() -> Result<Options> {
     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", "<html>\n&{head}\n&{body}\n&{foot}\n</html>\n");
+    write_default!("body", "<body>\n&{SOURCE}\n</body>\n");
+    write_default!("head", "<head>\n<title>HELLO WORLD</title>\n</head>\n");
+    write_default!("foot", "<foot>\n${footer}\n</foot>\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(())
+}
index 63046405cfe6ed0236cdf642ef193984fef19f71..760e9c729dfd70df32fc7c3e0dadf14fe9e05fb3 100644 (file)
@@ -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)
+    }
 }
index 868fdbd43fa978cad0c3a31d4f44d036fdc5d2b0..ec829d211b7ada240987b5a2f25b927ab12ba931 100644 (file)
@@ -18,6 +18,9 @@ pub struct Opts {
     /// Pattern directory [CURRENT_DIR/pattern]
     #[arg(short, long, value_name = "PATTERN_DIR")]
     pub pattern: Option<String>,
+    /// 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)]
index e37aa63b579fa1fc6fa522ee26864da05f9170b4..49d0031829939407a036dddb9c87c4a16a475e09 100644 (file)
@@ -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<crate::Opts> 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 (file)
index 0000000..02b4aa0
--- /dev/null
@@ -0,0 +1,11 @@
+<html>
+<head>
+<title>HELLO WORLD</title>
+</head>
+<body>
+<h1 id="it-works">it works</h1>
+<p>call <code>metaforge -h</code> for help, or read the <a
+href="https://huck.website/code/metaforge">readme</a></p>
+</body>
+<foot>made using metaforge</foot>
+</html>
diff --git a/test_siet/pattern/base/default.meta b/test_siet/pattern/base/default.meta
new file mode 100644 (file)
index 0000000..a0b16bc
--- /dev/null
@@ -0,0 +1,5 @@
+<html>
+&{head}
+&{body}
+&{foot}
+</html>
diff --git a/test_siet/pattern/body/default.meta b/test_siet/pattern/body/default.meta
new file mode 100644 (file)
index 0000000..c91a409
--- /dev/null
@@ -0,0 +1,3 @@
+<body>
+&{SOURCE}
+</body>
diff --git a/test_siet/pattern/foot/default.meta b/test_siet/pattern/foot/default.meta
new file mode 100644 (file)
index 0000000..336bf8d
--- /dev/null
@@ -0,0 +1,2 @@
+<foot>${footer}
+</foot>
diff --git a/test_siet/pattern/head/default.meta b/test_siet/pattern/head/default.meta
new file mode 100644 (file)
index 0000000..bed94d1
--- /dev/null
@@ -0,0 +1,3 @@
+<head>
+<title>HELLO WORLD</title>
+</head>
diff --git a/test_siet/source/hello_world.meta b/test_siet/source/hello_world.meta
new file mode 100644 (file)
index 0000000..718d479
--- /dev/null
@@ -0,0 +1,3 @@
+${ footer = 'made using metaforge' }
+# it works
+call `metaforge -h` for help, or read the [readme](https://huck.website/code/metaforge)