Add new project template

This commit is contained in:
vh-zuka 2022-11-08 08:35:58 +07:00 committed by Lucas
parent 01e91b9fe5
commit 3faed5c980
1 changed files with 71 additions and 5 deletions

View File

@ -1,5 +1,6 @@
use miette::IntoDiagnostic; use miette::IntoDiagnostic;
use std::fs; use std::fs;
use std::io::Write;
use std::path::PathBuf; use std::path::PathBuf;
#[derive(clap::Args)] #[derive(clap::Args)]
@ -9,11 +10,76 @@ pub struct Args {
name: PathBuf, name: PathBuf,
} }
pub fn exec(Args { name }: Args) -> miette::Result<()> { pub struct Creator {
if !name.exists() { root: PathBuf,
fs::create_dir_all(name.join("lib")).into_diagnostic()?; src: PathBuf,
fs::create_dir_all(name.join("policies")).into_diagnostic()?; project_name: String,
fs::create_dir_all(name.join("scripts")).into_diagnostic()?; }
impl Creator {
fn new(args: Args) -> Self {
let root = args.name;
let src = root.join("src");
let project_name = root.clone().into_os_string().into_string().unwrap();
Self{
root,
src,
project_name
}
}
fn run(&self) -> miette::Result<()> {
fs::create_dir_all(&self.src).into_diagnostic()?;
self.aiken_toml()?;
self.always_true_script()?;
Ok(())
}
fn always_true_script(&self) -> miette::Result<()> {
write(
self.src.join("always_true.ak"),
"
pub fn spend() -> Bool {
true
}
"
)
}
fn aiken_toml(&self) -> miette::Result<()> {
write(
self.root.join("aiken.toml"),
&format!(
r#"name = "{name}"
version = "0.1.0"
licences = ["Apache-2.0"]
description = "Aiken contracts"
[dependencies]
aiken = "~> {aiken}"
"#,
name = self.project_name,
aiken = "0.0.24",
),
)
}
}
fn write(path: PathBuf, contents: &str) -> miette::Result<()> {
let mut f = fs::File::create(&path).into_diagnostic()?;
f.write_all(contents.as_bytes()).into_diagnostic()?;
Ok(())
}
pub fn exec(args: Args) -> miette::Result<()> {
if !args.name.exists() {
let creator = Creator::new(args);
creator.run()?;
} }
Ok(()) Ok(())