Also move some associated methods to the 'Config' module
And refactor the 'new' command implementation.
This commit is contained in:
parent
0771ab24bd
commit
d4f905b1db
|
@ -1,13 +1,18 @@
|
||||||
use crate::{package_name::PackageName, Error};
|
use crate::{package_name::PackageName, Error};
|
||||||
use aiken_lang::ast::Span;
|
use aiken_lang::ast::Span;
|
||||||
use miette::NamedSource;
|
use miette::NamedSource;
|
||||||
use serde::{de::Visitor, Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{fmt::Display, fs, path::PathBuf};
|
use std::{
|
||||||
|
fmt::Display,
|
||||||
|
fs, io,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub name: PackageName,
|
pub name: PackageName,
|
||||||
pub version: String,
|
pub version: String,
|
||||||
|
pub license: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub description: String,
|
pub description: String,
|
||||||
pub repository: Option<Repository>,
|
pub repository: Option<Repository>,
|
||||||
|
@ -15,7 +20,7 @@ pub struct Config {
|
||||||
pub dependencies: Vec<Dependency>,
|
pub dependencies: Vec<Dependency>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct Repository {
|
pub struct Repository {
|
||||||
pub user: String,
|
pub user: String,
|
||||||
pub project: String,
|
pub project: String,
|
||||||
|
@ -48,6 +53,34 @@ impl Display for Platform {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
pub fn default(name: &PackageName) -> Self {
|
||||||
|
Config {
|
||||||
|
name: name.clone(),
|
||||||
|
version: "0.0.0".to_string(),
|
||||||
|
license: "Apache-2.0".to_string(),
|
||||||
|
description: format!("Aiken contracts for project '{name}'"),
|
||||||
|
repository: Some(Repository {
|
||||||
|
user: name.owner.clone(),
|
||||||
|
project: name.repo.clone(),
|
||||||
|
platform: Platform::Github,
|
||||||
|
}),
|
||||||
|
dependencies: vec![Dependency {
|
||||||
|
name: PackageName {
|
||||||
|
owner: "aiken-lang".to_string(),
|
||||||
|
repo: "stdlib".to_string(),
|
||||||
|
},
|
||||||
|
version: "main".to_string(),
|
||||||
|
source: Platform::Github,
|
||||||
|
}],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save(&self, dir: &Path) -> Result<(), io::Error> {
|
||||||
|
let aiken_toml_path = dir.join("aiken.toml");
|
||||||
|
let aiken_toml = toml::to_string_pretty(self).unwrap();
|
||||||
|
fs::write(aiken_toml_path, aiken_toml)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load(dir: PathBuf) -> Result<Config, Error> {
|
pub fn load(dir: PathBuf) -> Result<Config, Error> {
|
||||||
let config_path = dir.join("aiken.toml");
|
let config_path = dir.join("aiken.toml");
|
||||||
let raw_config = fs::read_to_string(&config_path)
|
let raw_config = fs::read_to_string(&config_path)
|
||||||
|
|
|
@ -43,10 +43,12 @@ fn create_project(args: Args, package_name: &PackageName) -> miette::Result<()>
|
||||||
create_validators_folder(&root)?;
|
create_validators_folder(&root)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
aiken_toml(&root, package_name)?;
|
|
||||||
|
|
||||||
readme(&root, &package_name.repo)?;
|
readme(&root, &package_name.repo)?;
|
||||||
|
|
||||||
|
Config::default(package_name)
|
||||||
|
.save(&root)
|
||||||
|
.into_diagnostic()?;
|
||||||
|
|
||||||
gitignore(&root)?;
|
gitignore(&root)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -84,28 +86,6 @@ fn create_validators_folder(root: &Path) -> miette::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn aiken_toml(root: &Path, package_name: &PackageName) -> miette::Result<()> {
|
|
||||||
let aiken_toml_path = root.join("aiken.toml");
|
|
||||||
|
|
||||||
fs::write(
|
|
||||||
aiken_toml_path,
|
|
||||||
formatdoc! {
|
|
||||||
r#"
|
|
||||||
name = "{name}"
|
|
||||||
version = "0.0.0"
|
|
||||||
licences = ["Apache-2.0"]
|
|
||||||
description = "Aiken contracts for project '{name}'"
|
|
||||||
|
|
||||||
dependencies = [
|
|
||||||
{{ name = "aiken-lang/stdlib", version = "main", source = "github" }},
|
|
||||||
]
|
|
||||||
"#,
|
|
||||||
name = package_name.to_string(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.into_diagnostic()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn readme(root: &Path, project_name: &str) -> miette::Result<()> {
|
fn readme(root: &Path, project_name: &str) -> miette::Result<()> {
|
||||||
fs::write(
|
fs::write(
|
||||||
root.join("README.md"),
|
root.join("README.md"),
|
||||||
|
|
Loading…
Reference in New Issue