use std::fmt;
use std::path::{Path, PathBuf};
use cuno_core::{CunoError, ExitClass, Shell};
use cuno_manifest::valid_package_name;
use cuno_toolchain::flags::{Edition, Lang};
#[derive(Debug)]
pub struct NewArgs {
pub path: PathBuf,
pub lang: Option<Lang>,
pub edition: Option<Edition>,
pub name: Option<String>,
pub vcs: Vcs,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Vcs {
Git,
None,
}
#[derive(Debug)]
pub enum NewError {
ReservedName {
name: String,
},
InvalidName {
name: String,
from_directory: bool,
},
Exists {
path: PathBuf,
},
EditionMismatch {
edition: Edition,
lang: Lang,
},
Write {
path: PathBuf,
reason: String,
},
}
fn lang_name(lang: Lang) -> &'static str {
match lang {
Lang::C => "C",
Lang::Cxx => "C++",
}
}
impl fmt::Display for NewError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NewError::InvalidName { name, .. } => {
write!(f, "`{name}` is not a valid package name")
}
NewError::ReservedName { name } => {
write!(f, "`{name}` is a reserved name on Windows")
}
NewError::Exists { path } => write!(f, "`{}` already exists", path.display()),
NewError::EditionMismatch { edition, lang } => write!(
f,
"`{}` is not a standard of {}",
edition.as_str(),
lang_name(*lang)
),
NewError::Write { path, reason } => {
write!(f, "cannot write {}: {reason}", path.display())
}
}
}
}
impl std::error::Error for NewError {}
impl CunoError for NewError {
fn exit_class(&self) -> ExitClass {
match self {
NewError::InvalidName { .. }
| NewError::ReservedName { .. }
| NewError::Exists { .. }
| NewError::EditionMismatch { .. } => ExitClass::Usage,
NewError::Write { .. } => ExitClass::Setup,
}
}
fn help(&self) -> Option<String> {
match self {
NewError::InvalidName { from_directory, .. } => {
let rule =
"a package name starts with a letter and has only letters, digits, - and _";
Some(if *from_directory {
format!("{rule}\nhelp: name the package with --name")
} else {
rule.to_owned()
})
}
NewError::EditionMismatch { edition, lang } => {
let editions: Vec<&str> = Edition::ALL
.iter()
.filter(|(e, _)| e.lang() == *lang)
.map(|(_, text)| *text)
.collect();
Some(format!(
"use --lang {}, or an edition of {}: {}",
edition.lang().as_str(),
lang_name(*lang),
editions.join(", ")
))
}
NewError::ReservedName { .. } => Some("choose another name".to_owned()),
NewError::Exists { .. } | NewError::Write { .. } => None,
}
}
}
pub fn reserved_name(name: &str) -> bool {
let lower = name.to_ascii_lowercase();
let numbered = |prefix: &str| {
lower
.strip_prefix(prefix)
.is_some_and(|n| n.len() == 1 && n.as_bytes()[0].is_ascii_digit() && n != "0")
};
["con", "prn", "aux", "nul"].contains(&lower.as_str()) || numbered("com") || numbered("lpt")
}
pub fn resolve(lang: Option<Lang>, edition: Option<Edition>) -> Result<(Lang, Edition), NewError> {
match (lang, edition) {
(Some(lang), Some(edition)) if edition.lang() != lang => {
Err(NewError::EditionMismatch { edition, lang })
}
(_, Some(edition)) => Ok((edition.lang(), edition)),
(lang, None) => {
let lang = lang.unwrap_or(Lang::Cxx);
Ok((lang, lang.default_edition()))
}
}
}
pub fn inside_repository(dir: &Path) -> bool {
dir.ancestors().any(|d| d.join(".git").exists())
}
pub fn files(name: &str, lang: Lang, edition: Edition) -> Vec<(&'static str, String)> {
let manifest = format!(
"[package]\nname = \"{name}\"\nversion = \"0.1.0\"\nlang = \"{}\"\nedition = \"{}\"\n",
lang.as_str(),
edition.as_str()
);
let mut files = vec![(cuno_manifest::MANIFEST, manifest)];
match lang {
Lang::Cxx => {
let calc = match edition {
Edition::Cxx17 => include_str!("../templates/cxx/calc17.cpp"),
Edition::Cxx20 | Edition::Cxx23 => include_str!("../templates/cxx/calc.cpp"),
Edition::C11 | Edition::C17 | Edition::C23 => {
unreachable!("a C edition with the C++ language")
}
};
let main = match edition {
Edition::Cxx17 | Edition::Cxx20 => include_str!("../templates/cxx/main.cpp"),
Edition::Cxx23 => include_str!("../templates/cxx/main23.cpp"),
Edition::C11 | Edition::C17 | Edition::C23 => {
unreachable!("a C edition with the C++ language")
}
};
files.push((lang.default_entry(), main.into()));
files.push(("src/calc.h", include_str!("../templates/cxx/calc.h").into()));
files.push(("src/calc.cpp", calc.into()));
}
Lang::C => {
files.push((
lang.default_entry(),
include_str!("../templates/c/main.c").into(),
));
files.push(("src/calc.h", include_str!("../templates/c/calc.h").into()));
files.push(("src/calc.c", include_str!("../templates/c/calc.c").into()));
}
}
files
}
const GITIGNORE: &str = include_str!("../templates/gitignore");
pub fn new(args: NewArgs, cwd: &Path, shell: &mut Shell) -> Result<(), NewError> {
let dir = cuno_core::paths::lexical(&cwd.join(&args.path));
let (name, from_directory) = match &args.name {
Some(name) => (name.clone(), false),
None => (
dir.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default(),
true,
),
};
if !valid_package_name(&name) {
return Err(NewError::InvalidName {
name,
from_directory,
});
}
if reserved_name(&name) {
return Err(NewError::ReservedName { name });
}
let (lang, edition) = resolve(args.lang, args.edition)?;
if dir.exists() {
return Err(NewError::Exists { path: args.path });
}
let write = |relative: &str, content: &str| -> Result<(), NewError> {
let path = dir.join(relative);
let made = path
.parent()
.map_or(Ok(()), std::fs::create_dir_all)
.and_then(|()| std::fs::write(&path, content));
made.map_err(|e| NewError::Write {
path,
reason: e.to_string(),
})
};
let in_repository = inside_repository(&dir);
for (relative, content) in files(&name, lang, edition) {
write(relative, &content)?;
}
if args.vcs == Vcs::Git {
if in_repository {
write(".gitignore", GITIGNORE)?;
} else {
match std::process::Command::new("git")
.args(["init", "--quiet"])
.current_dir(&dir)
.output()
{
Ok(output) if output.status.success() => write(".gitignore", GITIGNORE)?,
Ok(output) => {
let said = String::from_utf8_lossy(&output.stderr);
shell.warning(format!("git init failed: {}", said.trim()));
}
Err(_) => {
shell.warning("git was not found; the package is not under version control")
}
}
}
}
shell.status("Created", format!("`{name}` package"));
Ok(())
}
#[derive(Debug)]
pub struct InitArgs {
pub path: PathBuf,
pub lang: Option<Lang>,
pub edition: Option<Edition>,
pub name: Option<String>,
pub vcs: Vcs,
}
#[derive(Debug)]
pub enum InitError {
NoDirectory {
path: PathBuf,
},
NotADirectory {
path: PathBuf,
},
AlreadyPackage {
path: PathBuf,
},
SourcesElsewhere {
found: Vec<String>,
more: usize,
},
NoExecutable,
Exists {
path: String,
},
Gitignore {
reason: String,
},
}
impl fmt::Display for InitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
InitError::NoDirectory { path } => write!(f, "`{}` does not exist", path.display()),
InitError::NotADirectory { path } => {
write!(f, "`{}` is not a directory", path.display())
}
InitError::AlreadyPackage { path } => {
write!(f, "`{}` already has a Cuno.toml", path.display())
}
InitError::SourcesElsewhere { found, more } => {
write!(f, "sources found outside src/: {}", found.join(", "))?;
if *more > 0 {
write!(f, " and {more} more")?;
}
Ok(())
}
InitError::NoExecutable => f.write_str("src/ has no executable to build"),
InitError::Exists { path } => write!(f, "{path} already exists"),
InitError::Gitignore { reason } => write!(f, "cannot add to .gitignore: {reason}"),
}
}
}
impl std::error::Error for InitError {}
impl CunoError for InitError {
fn exit_class(&self) -> ExitClass {
ExitClass::Usage
}
fn help(&self) -> Option<String> {
let help = match self {
InitError::NoDirectory { .. } => "use cuno new to create a package in a new directory",
InitError::NotADirectory { .. } | InitError::AlreadyPackage { .. } => return None,
InitError::SourcesElsewhere { .. } => {
"move them under src/, with the program's main in src/main.cpp (or src/main.c)"
}
InitError::NoExecutable => {
"put the program's main in src/main.cpp (or src/main.c), or each program in src/bin/"
}
InitError::Exists { .. } => {
"cuno init writes no file that is there; move it aside, or add Cuno.toml by hand"
}
InitError::Gitignore { .. } => "make it a text file in UTF-8, or use --vcs none",
};
Some(help.to_owned())
}
}
const SHOWN_SOURCES: usize = 5;
fn sources_in(dir: &Path, case: cuno_core::NameCase) -> (Vec<String>, Vec<String>) {
fn walk(dir: &Path, from: &str, case: cuno_core::NameCase, out: &mut Vec<String>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
let mut entries: Vec<_> = entries.flatten().collect();
entries.sort_by_key(|e| e.file_name());
for entry in entries {
let name = entry.file_name().to_string_lossy().into_owned();
let relative = if from.is_empty() {
name.clone()
} else {
format!("{from}/{name}")
};
let Ok(kind) = entry.file_type() else {
continue;
};
if kind.is_dir() {
let skipped = name.starts_with('.')
|| (from.is_empty() && name == "target")
|| !matches!(
cuno_manifest::manifest_in(&entry.path(), case),
cuno_manifest::ManifestIn::None
);
if !skipped {
walk(&entry.path(), &relative, case, out);
}
} else if kind.is_file() && cuno_build::sources::language(&entry.path()).is_some() {
out.push(relative);
}
}
}
let mut all = Vec::new();
walk(dir, "", case, &mut all);
all.into_iter().partition(|s| {
s.split_once('/')
.is_some_and(|(top, _)| case.same(top, "src"))
})
}
enum Gitignore {
Make,
Add(String),
Keep,
}
fn plan_gitignore(path: &Path) -> Result<Gitignore, String> {
if cuno_tools::write::absent(path).is_ok() {
return Ok(Gitignore::Make);
}
cuno_tools::write::appendable(path).map_err(|refusal| refusal.to_string())?;
let held = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
let text = held.strip_prefix('\u{feff}').unwrap_or(&held);
let have: Vec<&str> = text.lines().map(str::trim).collect();
let missing: Vec<&str> = GITIGNORE
.lines()
.map(str::trim)
.filter(|l| !l.is_empty() && !l.starts_with('#') && !have.contains(l))
.collect();
if missing.is_empty() {
return Ok(Gitignore::Keep);
}
let newline = if text.contains("\r\n") { "\r\n" } else { "\n" };
let mut added = String::new();
if !text.is_empty() && !text.ends_with('\n') {
added += newline;
}
for line in missing {
added += line;
added += newline;
}
Ok(Gitignore::Add(added))
}
fn blocked(dir: &Path, relative: &str) -> Option<String> {
let parts: Vec<&str> = relative.split('/').collect();
for end in 1..=parts.len() {
let prefix = parts[..end].join("/");
let path = dir.join(&prefix);
let last = end == parts.len();
let in_the_way = if last {
cuno_tools::write::absent(&path).is_err()
} else {
std::fs::symlink_metadata(&path).is_ok_and(|m| !m.file_type().is_dir())
};
if in_the_way {
return Some(prefix);
}
}
None
}
pub fn init(
args: InitArgs,
cwd: &Path,
host: cuno_toolchain::HostOs,
shell: &mut Shell,
) -> Result<(), Box<dyn CunoError>> {
let boxed = |e: InitError| Box::new(e) as Box<dyn CunoError>;
let dir = cuno_core::paths::lexical(&cwd.join(&args.path));
match std::fs::metadata(&dir) {
Err(_) => return Err(boxed(InitError::NoDirectory { path: args.path })),
Ok(meta) if !meta.is_dir() => {
return Err(boxed(InitError::NotADirectory { path: args.path }));
}
Ok(_) => {}
}
match cuno_manifest::manifest_in(&dir, host.name_case()) {
cuno_manifest::ManifestIn::Found(_) => {
return Err(boxed(InitError::AlreadyPackage { path: args.path }));
}
cuno_manifest::ManifestIn::WrongCase(file) => {
return Err(Box::new(cuno_manifest::wrong_case(&file)));
}
cuno_manifest::ManifestIn::None => {}
}
let (name, from_directory) = match &args.name {
Some(name) => (name.clone(), false),
None => (
dir.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default(),
true,
),
};
if !valid_package_name(&name) {
return Err(Box::new(NewError::InvalidName {
name,
from_directory,
}));
}
if reserved_name(&name) {
return Err(Box::new(NewError::ReservedName { name }));
}
let (in_src, elsewhere) = sources_in(&dir, host.name_case());
if in_src.is_empty() && !elsewhere.is_empty() {
return Err(boxed(InitError::SourcesElsewhere {
more: elsewhere.len().saturating_sub(SHOWN_SOURCES),
found: elsewhere.into_iter().take(SHOWN_SOURCES).collect(),
}));
}
let lang = args.lang.or_else(|| {
(args.edition.is_none() && !in_src.is_empty()).then(|| {
let cxx = in_src
.iter()
.any(|s| cuno_build::sources::language(Path::new(s)) == Some(Lang::Cxx));
if cxx { Lang::Cxx } else { Lang::C }
})
});
let (lang, edition) =
resolve(lang, args.edition).map_err(|e| Box::new(e) as Box<dyn CunoError>)?;
let mut written = files(&name, lang, edition);
if !in_src.is_empty() {
written.truncate(1);
let manifest =
cuno_manifest::parse_manifest(&written[0].1, &dir.join(cuno_manifest::MANIFEST), &dir)
.map_err(|e| Box::new(e) as Box<dyn CunoError>)?
.value;
match cuno_build::sources::classify(&dir, &manifest, &dir.join("target"), host) {
Ok(_) => {}
Err(cuno_build::BuildError::NoExecutable { .. }) => {
return Err(boxed(InitError::NoExecutable));
}
Err(error) => return Err(Box::new(error)),
}
}
for (relative, _) in &written {
if let Some(path) = blocked(&dir, relative) {
return Err(boxed(InitError::Exists { path }));
}
}
let gitignore_path = dir.join(".gitignore");
let gitignore = match args.vcs {
Vcs::Git => Some(
plan_gitignore(&gitignore_path)
.map_err(|reason| boxed(InitError::Gitignore { reason }))?,
),
Vcs::None => None,
};
let failed = |path: PathBuf, e: std::io::Error| {
Box::new(NewError::Write {
path,
reason: e.to_string(),
}) as Box<dyn CunoError>
};
let create = |path: &Path, content: &str| -> std::io::Result<()> {
use std::io::Write;
path.parent().map_or(Ok(()), std::fs::create_dir_all)?;
std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(path)?
.write_all(content.as_bytes())
};
let in_repository = inside_repository(&dir);
for (relative, content) in &written {
let path = dir.join(relative);
create(&path, content).map_err(|e| failed(path, e))?;
}
if let Some(gitignore) = gitignore {
let repository = in_repository || {
match std::process::Command::new("git")
.args(["init", "--quiet"])
.current_dir(&dir)
.output()
{
Ok(output) if output.status.success() => true,
Ok(output) => {
let said = String::from_utf8_lossy(&output.stderr);
shell.warning(format!("git init failed: {}", said.trim()));
false
}
Err(_) => {
shell.warning("git was not found; the package is not under version control");
false
}
}
};
if repository {
let path = gitignore_path;
let done = match gitignore {
Gitignore::Make => create(&path, GITIGNORE),
Gitignore::Add(added) => {
use std::io::Write;
std::fs::OpenOptions::new()
.append(true)
.open(&path)
.and_then(|mut f| f.write_all(added.as_bytes()))
}
Gitignore::Keep => Ok(()),
};
done.map_err(|e| failed(path, e))?;
}
}
shell.status("Created", format!("`{name}` package"));
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn language_and_edition_agree() {
assert_eq!(resolve(None, None).unwrap(), (Lang::Cxx, Edition::Cxx20));
assert_eq!(
resolve(Some(Lang::C), None).unwrap(),
(Lang::C, Edition::C17)
);
assert_eq!(
resolve(None, Some(Edition::C11)).unwrap(),
(Lang::C, Edition::C11)
);
assert_eq!(
resolve(Some(Lang::Cxx), Some(Edition::Cxx17)).unwrap(),
(Lang::Cxx, Edition::Cxx17)
);
let err = resolve(Some(Lang::Cxx), Some(Edition::C17)).unwrap_err();
assert_eq!(err.to_string(), "`c17` is not a standard of C++");
assert_eq!(
err.help().as_deref(),
Some("use --lang c, or an edition of C++: c++17, c++20, c++23")
);
assert_eq!(err.exit_class(), ExitClass::Usage);
let c = resolve(Some(Lang::C), Some(Edition::Cxx17)).unwrap_err();
assert_eq!(c.to_string(), "`c++17` is not a standard of C");
assert_eq!(
c.help().as_deref(),
Some("use --lang c++, or an edition of C: c11, c17, c23")
);
}
#[test]
fn the_files_fit_the_language_and_edition() {
let cxx = files("app", Lang::Cxx, Edition::Cxx20);
assert_eq!(
cxx[0],
(
"Cuno.toml",
"[package]\nname = \"app\"\nversion = \"0.1.0\"\nlang = \"c++\"\nedition = \"c++20\"\n"
.to_owned()
)
);
let names: Vec<&str> = cxx.iter().map(|(n, _)| *n).collect();
assert_eq!(
names,
["Cuno.toml", "src/main.cpp", "src/calc.h", "src/calc.cpp"]
);
assert!(cxx[3].1.contains("<cuno/test.hpp>"));
let cxx17 = files("app", Lang::Cxx, Edition::Cxx17);
assert!(cxx17[3].1.contains("<cuno/test.h>"));
for edition in [Edition::Cxx17, Edition::Cxx20] {
let main = &files("app", Lang::Cxx, edition)[1].1;
assert!(
main.contains("std::cout << ") && main.contains("<< '\\n';"),
"{main}"
);
assert!(
!main.contains("std::endl") && !main.contains("println"),
"{main}"
);
}
let main = &files("app", Lang::Cxx, Edition::Cxx23)[1].1;
assert!(
main.contains("#include <print>") && main.contains("std::println("),
"{main}"
);
let c = files("app", Lang::C, Edition::C17);
let names: Vec<&str> = c.iter().map(|(n, _)| *n).collect();
assert_eq!(
names,
["Cuno.toml", "src/main.c", "src/calc.h", "src/calc.c"]
);
}
#[test]
fn a_repository_above_counts() {
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir(dir.path().join(".git")).unwrap();
assert!(inside_repository(&dir.path().join("a").join("app")));
}
#[test]
fn device_names_are_reserved() {
for name in ["con", "PRN", "Aux", "nul", "com1", "COM9", "lpt5"] {
assert!(reserved_name(name), "{name}");
}
for name in ["console", "com0", "com10", "lpt", "nulls", "auxiliary"] {
assert!(!reserved_name(name), "{name}");
}
}
#[test]
fn the_errors_read_as_the_spec_says() {
let named = NewError::InvalidName {
name: "1app".into(),
from_directory: false,
};
assert_eq!(named.to_string(), "`1app` is not a valid package name");
assert_eq!(
named.help().as_deref(),
Some("a package name starts with a letter and has only letters, digits, - and _")
);
let from_dir = NewError::InvalidName {
name: "my game".into(),
from_directory: true,
};
assert!(
from_dir
.help()
.unwrap()
.ends_with("\nhelp: name the package with --name")
);
let exists = NewError::Exists {
path: PathBuf::from("app"),
};
assert_eq!(exists.to_string(), "`app` already exists");
for e in [named, from_dir, exists] {
assert_eq!(e.exit_class(), ExitClass::Usage);
}
let reserved = NewError::ReservedName { name: "con".into() };
assert_eq!(reserved.to_string(), "`con` is a reserved name on Windows");
assert_eq!(reserved.help().as_deref(), Some("choose another name"));
assert_eq!(reserved.exit_class(), ExitClass::Usage);
let write = NewError::Write {
path: PathBuf::from("x"),
reason: "denied".into(),
};
assert_eq!(write.to_string(), "cannot write x: denied");
assert_eq!(write.exit_class(), ExitClass::Setup);
}
#[test]
fn init_refuses_a_manifest_in_another_case() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("cuno.toml"), "").unwrap();
let env = cuno_core::Env::from_pairs([], cuno_core::NameCase::host());
let mut shell = Shell::new(Box::new(std::io::sink()), None, &env, false);
let args = InitArgs {
path: PathBuf::from("."),
lang: None,
edition: None,
name: Some("app".to_owned()),
vcs: Vcs::None,
};
let err = init(args, dir.path(), cuno_toolchain::HostOs::Other, &mut shell).unwrap_err();
assert!(err.to_string().starts_with("found cuno.toml in "), "{err}");
let names: Vec<String> = std::fs::read_dir(dir.path())
.unwrap()
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
.collect();
assert_eq!(names, ["cuno.toml"]);
}
}