From a3591cc7dcddd01a2d95cc2fa9f898da14d5d43f Mon Sep 17 00:00:00 2001 From: KtorZ Date: Wed, 21 Dec 2022 00:19:24 +0100 Subject: [PATCH] Fix 'aiken new', now require project name in specific format. --- crates/cli/src/cmd/error.rs | 13 +++++++------ crates/cli/src/cmd/new.rs | 11 ++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/crates/cli/src/cmd/error.rs b/crates/cli/src/cmd/error.rs index 29cce983..98c5ef44 100644 --- a/crates/cli/src/cmd/error.rs +++ b/crates/cli/src/cmd/error.rs @@ -4,7 +4,7 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum Error { - #[error("{} is not a valid project name. {}", name, reason.to_string())] + #[error("'{}' is not a valid project name: {}", name, reason.to_string())] InvalidProjectName { name: String, reason: InvalidProjectNameReason, @@ -21,15 +21,16 @@ pub enum InvalidProjectNameReason { impl fmt::Display for InvalidProjectNameReason { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - InvalidProjectNameReason::AikenPrefix => write!(f, "It is a reserved word in Aiken."), + InvalidProjectNameReason::AikenPrefix => write!(f, "it's a reserved word in Aiken."), InvalidProjectNameReason::AikenReservedModule => { - write!(f, "It is a reserved module name in Aiken.") + write!(f, "it's a reserved module name in Aiken.") } InvalidProjectNameReason::Format => write!( f, - "It does not have the correct format. Project names \ - must start with a lowercase letter and may only contain lowercase letters, \ - numbers and underscores." + "it is malformed.\n\nProjects must be named as:\n\n\t\ + {{repository}}/{{project}}\n\nEach part must start with a lowercase letter \ + and may only contain lowercase letters, numbers, hyphens or underscores.\ + \nFor example,\n\n\taiken-lang/stdlib" ), } } diff --git a/crates/cli/src/cmd/new.rs b/crates/cli/src/cmd/new.rs index fe43dd5b..0d186a2a 100644 --- a/crates/cli/src/cmd/new.rs +++ b/crates/cli/src/cmd/new.rs @@ -23,7 +23,7 @@ pub struct Creator { impl Creator { fn new(args: Args, project_name: String) -> Self { - let root = args.name; + let root = PathBuf::from(args.name.file_name().unwrap()); let lib = root.join("lib"); let validators = root.join("validators"); let project_name = project_name; @@ -124,9 +124,7 @@ Find more on the [Aiken's user manual](https://aiken-lang.org). version = "0.0.0" licences = ["Apache-2.0"] description = "Aiken contracts for project '{name}'" - - [dependencies] - + dependencies = [] "#, name = self.project_name, }, @@ -136,7 +134,6 @@ Find more on the [Aiken's user manual](https://aiken-lang.org). 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(()) } @@ -152,8 +149,8 @@ fn validate_name(name: &str) -> Result<(), Error> { name: name.to_string(), reason: InvalidProjectNameReason::AikenReservedModule, }) - } else if !regex::Regex::new("^[a-z][a-z0-9_]*$") - .expect("new name regex could not be compiled") + } else if !regex::Regex::new("^[a-z0-9_-]+/[a-z0-9_-]+$") + .expect("regex could not be compiled") .is_match(name) { Err(Error::InvalidProjectName {