Skip to content

Commit

Permalink
feat(script): basic templating
Browse files Browse the repository at this point in the history
  • Loading branch information
reo101 committed Mar 7, 2024
1 parent 7dce0b4 commit 5e1c552
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 17 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread" ] }
wasmer = "3.1.0"
thiserror = "1.0.47"
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
indoc = "2.0.4"

# replay tool
function_name = "0.3.0"
Expand Down
30 changes: 13 additions & 17 deletions src/script.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::{
env, fs,
path::Path,
process::{Command, Stdio},
};

use eyre::{bail, eyre, OptionExt, Result};
use eyre::{OptionExt, Result};

use crate::{util, ScriptNewConfig};

mod templates;

pub async fn new(config: ScriptNewConfig) -> Result<()> {
println!("Adding new script: {:?}", config);

Expand All @@ -16,7 +17,7 @@ pub async fn new(config: ScriptNewConfig) -> Result<()> {
&util::discover_project_root_from_path(cwd)?.ok_or_eyre("Could not find Cargo.toml")?;
println!("Found to project root: {}", project_root.display());

let script_dir: &Path = &project_root.join("scripts");
let script_dir: &Path = &project_root.join("scripts").join(&config.path);

fs::create_dir_all(script_dir)?;
env::set_current_dir(script_dir)?;
Expand All @@ -26,20 +27,15 @@ pub async fn new(config: ScriptNewConfig) -> Result<()> {
script_dir.display()
);

let mut cmd = Command::new("cargo");
cmd.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.arg("new")
.arg("--bin")
.arg(config.path);

let output = cmd
.output()
.map_err(|e| eyre!("failed to execute cargo new command: {e}"))?;

if !output.status.success() {
bail!("Create new script failed: {:?}", output);
}
let template = templates::rust::basic_template(
config
.path
.to_str()
.ok_or_eyre("Could not convert path to string")?
.to_string(),
script_dir,
)?;
templates::realise(template)?;

Ok(())
}
25 changes: 25 additions & 0 deletions src/script/templates/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::{fs, path::PathBuf};

use eyre::{Context, OptionExt, Result};

pub mod rust;

/// Type alias for templates - file names paired up with their respective content
pub type Template = Vec<(PathBuf, String)>;

/// Bring a template to life in a given directory
pub fn realise(template: Template) -> Result<()> {
template.into_iter().try_for_each(|(path, contents)| {
fs::create_dir_all(
path.parent()
.ok_or_eyre(format!("Could not get parent of {}", path.display()))?,
)
.wrap_err(format!(
"Could not create needed directories for {}",
path.display()
))?;
fs::write(&path, contents).wrap_err(format!("Failed to write file {}", path.display()))?;
println!("Created file {}", path.display());
Ok(())
})
}
61 changes: 61 additions & 0 deletions src/script/templates/rust/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#![allow(clippy::useless_format)]

use std::path::Path;

use eyre::Result;
use indoc::indoc;

use super::Template;

pub fn basic_template(name: String, base: impl AsRef<Path>) -> Result<Template> {
let base = base.as_ref();

Ok(vec![
(
base.join("Cargo.toml"),
format!(
indoc! {r#"
[package]
name = "{}"
version = "0.1.0"
edition = "2021"
[dependencies]
alloy-primitives = "0.4.0"
eyre = "0.6.12"
"#},
name
),
),
(
base.join("src/main.rs"),
format!(
indoc! {r#"
use eyre::Result;
pub fn main() -> Result<()> {{
println!("{{}}", "{}");
Ok(())
}}
"#},
"Hello"
),
),
(
base.join("Makefile"),
// NOTE: beware of the tab literals
format!(
indoc! {r#"
run: build
cargo run
build:
cargo build
love:
@echo "not war"
"#}
),
),
])
}

0 comments on commit 5e1c552

Please sign in to comment.