From f3ead329daa4a93f2cf34fc273ab1ab0a4c122e1 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Tue, 31 Oct 2023 00:40:11 +0100 Subject: [PATCH 01/24] feat: parsing args --- Cargo.lock | 9 ++++++ Cargo.toml | 1 + src/args.rs | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 11 ++++++- 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/args.rs diff --git a/Cargo.lock b/Cargo.lock index 425b757..17c5ba2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "argmap" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c007f456524f3f1e06e8929963425b5dadf8616d9110ea0809840c16997994e9" + [[package]] name = "cmake-init" version = "0.1.0" +dependencies = [ + "argmap", +] diff --git a/Cargo.toml b/Cargo.toml index 97ab654..f06d8d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +argmap = "1.1.2" diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..05b6b85 --- /dev/null +++ b/src/args.rs @@ -0,0 +1,91 @@ +use std::{collections::HashMap, process::exit}; + +#[derive(Debug)] +pub struct Args { + pub name: String, + pub cmake_min_version: String, + pub lang: String, +} + +impl Args { + /// Create a new Args struct from the given argument map. + /// If any of the required arguments are missing, print an error and exit. + /// + /// # Arguments + /// + /// * `argv` - The argument map. + pub fn from(argv: HashMap>) -> Args { + Args::validate_args(&argv); + + Args { + name: Args::get_arg(&argv, "name", true, None), + cmake_min_version: Args::get_arg(&argv, "cmake-version", false, Some("3.0")), + lang: Args::get_arg(&argv, "lang", false, Some("")), + } + } + + /// Get the value of the argument at the given index. + /// If the argument is not found, print an error and exit. + /// + /// # Arguments + /// + /// * `argv` - The argument map. + /// * `index` - The index of the argument to get. + fn get_arg( + argv: &HashMap>, + index: &str, + required: bool, + default: Option<&str>, + ) -> String { + let default = &default.unwrap_or("").to_string(); + + match argv.get(index).unwrap_or(&vec![]).get(0) { + Some(expr) => expr.to_owned(), + None => { + if required { + eprintln!("{} is required.", index); + exit(1); + } else { + default.to_string() + } + } + } + } + + /// Check the health of the arguments. + /// If there are no arguments, print an error and exit. + /// + /// # Arguments + /// + /// * `argv` - The argument map. + fn validate_args(argv: &HashMap>) { + if argv.len() == 0 { + eprintln!("No arguments provided."); + exit(1); + } else if argv.get("help").is_some() { + Args::print_help(); + } else if argv.get("version").is_some() { + Args::print_version(); + } + exit(0); + } + + /// Print the help message. + /// This is called when the user passes the `--help` flag. + fn print_help() { + println!("Usage: cmake-init --name="); + println!(); + println!("Options:"); + println!(" --name= The name of the project."); + println!(" --cmake-version= The minimum version of CMake to use."); + println!(" --lang= the language chosen for the project(cpp, c)."); + println!(" --help Print this help message."); + println!(" --version Print the version of cmake-init."); + } + + /// Print the version of cmake-init. + /// This is called when the user passes the `--version` flag. + pub fn print_version() { + println!("cmake-init {}", env!("CARGO_PKG_VERSION")); + } +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..7b8aef6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,12 @@ +mod args; + +use args::Args; + +use argmap; + fn main() { - println!("Hello, world!"); + let (_, argv) = argmap::parse(std::env::args()); + + let args = Args::from(argv); + println!("{:?}", args); } From e633cc83001f277f829f1ff630bd1b6dbe48c7f6 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Tue, 31 Oct 2023 00:46:42 +0100 Subject: [PATCH 02/24] feat: tests --- src/args.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/args.rs b/src/args.rs index 05b6b85..3213e18 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,5 +1,8 @@ use std::{collections::HashMap, process::exit}; +/// A struct to hold the arguments passed to cmake-init. +/// The arguments are parsed from the command line and stored in this struct. +/// The struct is then passed to the `init` function to create the project. #[derive(Debug)] pub struct Args { pub name: String, @@ -89,3 +92,47 @@ impl Args { println!("cmake-init {}", env!("CARGO_PKG_VERSION")); } } + +/// tests +#[cfg(test)] +mod arg_tests { + use std::collections::HashMap; + #[test] + fn test_args() { + let mut args = HashMap::new(); + args.insert("name".to_string(), vec!["test".to_string()]); + args.insert("cmake-version".to_string(), vec!["3.0".to_string()]); + args.insert("lang".to_string(), vec!["cpp".to_string()]); + let args = super::Args::from(args); + assert_eq!(args.name, "test"); + assert_eq!(args.cmake_min_version, "3.0"); + assert_eq!(args.lang, "cpp"); + } + + #[test] + fn test_args_default() { + let mut args = HashMap::new(); + args.insert("name".to_string(), vec!["test".to_string()]); + let args = super::Args::from(args); + assert_eq!(args.name, "test"); + assert_eq!(args.cmake_min_version, "3.0"); + assert_eq!(args.lang, ""); + } + + #[test] + #[should_panic] + fn test_args_panic() { + let args = HashMap::new(); + let _args = super::Args::from(args); + } + + #[test] + fn test_args_help() { + let mut args = HashMap::new(); + args.insert("help".to_string(), vec!["".to_string()]); + let args = super::Args::from(args); + assert_eq!(args.name, ""); + assert_eq!(args.cmake_min_version, "3.0"); + assert_eq!(args.lang, ""); + } +} From 5dfe5b562241670c847eccf071007550998e79e8 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Tue, 31 Oct 2023 01:02:37 +0100 Subject: [PATCH 03/24] feat: languages --- src/{ => args}/args.rs | 15 +++++++++------ src/args/languages.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/args/mod.rs | 5 +++++ 3 files changed, 56 insertions(+), 6 deletions(-) rename src/{ => args}/args.rs (92%) create mode 100644 src/args/languages.rs create mode 100644 src/args/mod.rs diff --git a/src/args.rs b/src/args/args.rs similarity index 92% rename from src/args.rs rename to src/args/args.rs index 3213e18..ada31cc 100644 --- a/src/args.rs +++ b/src/args/args.rs @@ -1,5 +1,7 @@ use std::{collections::HashMap, process::exit}; +use super::Languages; + /// A struct to hold the arguments passed to cmake-init. /// The arguments are parsed from the command line and stored in this struct. /// The struct is then passed to the `init` function to create the project. @@ -7,7 +9,7 @@ use std::{collections::HashMap, process::exit}; pub struct Args { pub name: String, pub cmake_min_version: String, - pub lang: String, + pub lang: Languages, } impl Args { @@ -23,7 +25,7 @@ impl Args { Args { name: Args::get_arg(&argv, "name", true, None), cmake_min_version: Args::get_arg(&argv, "cmake-version", false, Some("3.0")), - lang: Args::get_arg(&argv, "lang", false, Some("")), + lang: Languages::from_string(Args::get_arg(&argv, "lang", false, Some("c"))), } } @@ -34,7 +36,7 @@ impl Args { /// /// * `argv` - The argument map. /// * `index` - The index of the argument to get. - fn get_arg( + pub fn get_arg( argv: &HashMap>, index: &str, required: bool, @@ -96,6 +98,7 @@ impl Args { /// tests #[cfg(test)] mod arg_tests { + use super::Languages; use std::collections::HashMap; #[test] fn test_args() { @@ -106,7 +109,7 @@ mod arg_tests { let args = super::Args::from(args); assert_eq!(args.name, "test"); assert_eq!(args.cmake_min_version, "3.0"); - assert_eq!(args.lang, "cpp"); + assert_eq!(args.lang, Languages::CPP); } #[test] @@ -116,7 +119,7 @@ mod arg_tests { let args = super::Args::from(args); assert_eq!(args.name, "test"); assert_eq!(args.cmake_min_version, "3.0"); - assert_eq!(args.lang, ""); + assert_eq!(args.lang, Languages::CPP); } #[test] @@ -133,6 +136,6 @@ mod arg_tests { let args = super::Args::from(args); assert_eq!(args.name, ""); assert_eq!(args.cmake_min_version, "3.0"); - assert_eq!(args.lang, ""); + assert_eq!(args.lang, Languages::CPP); } } diff --git a/src/args/languages.rs b/src/args/languages.rs new file mode 100644 index 0000000..4d9e591 --- /dev/null +++ b/src/args/languages.rs @@ -0,0 +1,42 @@ +use std::process::exit; + +/// Enum for the languages that can be used. +/// NOTE: Currently only C and C++ are supported. +#[derive(Debug, PartialEq)] +pub enum Languages { + C, + CPP, +} + +impl Languages { + /// Create a new Languages enum from the given string. + /// If the string is not a valid language, print an error and exit. + /// + /// # Arguments + /// + /// * `name` - The name of the language to create. + pub fn from_string(name: String) -> Languages { + match &name as &str { + "c" => Languages::C, + "cpp" => Languages::CPP, + _ => { + eprintln!("{} is not a valid language.", name); + exit(1); + } + } + } +} + +impl ToString for Languages { + /// Convert the language to a string. + /// + /// # Returns + /// + /// * A string representing the language. + fn to_string(&self) -> String { + match self { + Languages::C => String::from("c"), + Languages::CPP => String::from("cpp"), + } + } +} diff --git a/src/args/mod.rs b/src/args/mod.rs new file mode 100644 index 0000000..1f049b3 --- /dev/null +++ b/src/args/mod.rs @@ -0,0 +1,5 @@ +mod args; +mod languages; + +pub use args::Args; +pub use languages::Languages; From feccfee5f2074c672e061c96dd97d6d5375e02f3 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Tue, 31 Oct 2023 01:11:47 +0100 Subject: [PATCH 04/24] fix: args and help menu --- src/args/args.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/args/args.rs b/src/args/args.rs index ada31cc..797d0a7 100644 --- a/src/args/args.rs +++ b/src/args/args.rs @@ -67,9 +67,9 @@ impl Args { if argv.len() == 0 { eprintln!("No arguments provided."); exit(1); - } else if argv.get("help").is_some() { + } else if argv.get("help").is_some() || argv.get("h").is_some() { Args::print_help(); - } else if argv.get("version").is_some() { + } else if argv.get("version").is_some() || argv.get("v").is_some() { Args::print_version(); } exit(0); @@ -83,9 +83,9 @@ impl Args { println!("Options:"); println!(" --name= The name of the project."); println!(" --cmake-version= The minimum version of CMake to use."); - println!(" --lang= the language chosen for the project(cpp, c)."); - println!(" --help Print this help message."); - println!(" --version Print the version of cmake-init."); + println!(" --lang= The language chosen for the project(cpp, c)."); + println!(" --help | -h Print this help message."); + println!(" --version | -v Print the version of cmake-init."); } /// Print the version of cmake-init. From bddd9a588a34bc19cb11f48d3cc63c63871f91d4 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Wed, 1 Nov 2023 18:26:27 +0100 Subject: [PATCH 05/24] hotfix: exit 0 --- src/args/args.rs | 3 ++- src/main.rs | 4 ++-- templates/c.c | 7 +++++++ templates/cpp.cpp | 6 ++++++ 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 templates/c.c create mode 100644 templates/cpp.cpp diff --git a/src/args/args.rs b/src/args/args.rs index 797d0a7..9f2137c 100644 --- a/src/args/args.rs +++ b/src/args/args.rs @@ -69,10 +69,11 @@ impl Args { exit(1); } else if argv.get("help").is_some() || argv.get("h").is_some() { Args::print_help(); + exit(0); } else if argv.get("version").is_some() || argv.get("v").is_some() { Args::print_version(); + exit(0); } - exit(0); } /// Print the help message. diff --git a/src/main.rs b/src/main.rs index 7b8aef6..3db59a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ mod args; -use args::Args; - use argmap; +use crate::args::Args; + fn main() { let (_, argv) = argmap::parse(std::env::args()); diff --git a/templates/c.c b/templates/c.c new file mode 100644 index 0000000..6ca7055 --- /dev/null +++ b/templates/c.c @@ -0,0 +1,7 @@ +#include +#include + +int main(int argc, char *argv[]) { + printf("Hello World!\n"); + return 0; +} diff --git a/templates/cpp.cpp b/templates/cpp.cpp new file mode 100644 index 0000000..ddcb539 --- /dev/null +++ b/templates/cpp.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + std::cout << "Hello World!"; + return 0; +} From 6a632832f0881ae7ad111d1da5043312e3fd0538 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Wed, 1 Nov 2023 18:42:01 +0100 Subject: [PATCH 06/24] fix: unknown args --- .github/workflows/todo.yml | 41 ++++++++++++++++++++++++++++++++++++++ src/args/args.rs | 11 ++++++++++ 2 files changed, 52 insertions(+) create mode 100644 .github/workflows/todo.yml diff --git a/.github/workflows/todo.yml b/.github/workflows/todo.yml new file mode 100644 index 0000000..a2a55ba --- /dev/null +++ b/.github/workflows/todo.yml @@ -0,0 +1,41 @@ +--- +name: Create issues from TODOs + +on: + workflow_dispatch: + inputs: + importAll: + default: false + required: false + type: boolean + description: Enable, if you want to import all TODOs. Runs on checked out branch! Only use if you're sure what you are doing. + push: + branches: + - develop + +permissions: + issues: write + repository-projects: read + contents: read + +jobs: + todos: + runs-on: ubuntu-latest + permissions: + issues: write + repository-projects: read + contents: read + + steps: + - uses: actions/checkout@v3 + + - name: Run Issue Bot + uses: derjuulsn/todo-issue@main + with: + excludePattern: "^(node_modules/)" + blobLines: 7 + blobLinesBefore: 4 + keywords: "FIXME,BUG,WONTFIX,TODO,WARN,HACK,PERF,OPTIM,NOTE" + reopenClosed: true + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/src/args/args.rs b/src/args/args.rs index 9f2137c..6b8c116 100644 --- a/src/args/args.rs +++ b/src/args/args.rs @@ -22,6 +22,17 @@ impl Args { pub fn from(argv: HashMap>) -> Args { Args::validate_args(&argv); + #[allow(unused_doc_comments)] + /** + * HACK: This is a hack to check unknown arguments. + */ + for (key, _) in &argv { + if key != "name" && key != "cmake-version" && key != "lang" { + eprintln!("Unknown argument: {}", key); + exit(1); + } + } + Args { name: Args::get_arg(&argv, "name", true, None), cmake_min_version: Args::get_arg(&argv, "cmake-version", false, Some("3.0")), From 43a5b38875e0b51018e9566683a659bf87dd7712 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Sat, 4 Nov 2023 13:33:53 +0100 Subject: [PATCH 07/24] feat: templates --- install_linux.sh | 17 ++++++++ install_macos.sh | 17 ++++++++ install_win.bat | 15 +++++++ src/args/args.rs | 75 +++++++++++++++++++++++++++++++-- src/args/languages.rs | 6 +-- src/main.rs | 19 ++++++++- templates/{c.c => main.c} | 0 templates/{cpp.cpp => main.cpp} | 0 8 files changed, 142 insertions(+), 7 deletions(-) create mode 100755 install_linux.sh create mode 100755 install_macos.sh create mode 100755 install_win.bat rename templates/{c.c => main.c} (100%) rename templates/{cpp.cpp => main.cpp} (100%) diff --git a/install_linux.sh b/install_linux.sh new file mode 100755 index 0000000..665c4e5 --- /dev/null +++ b/install_linux.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +# building cmake-init +cargo build --release + +# creating necessary directories +mkdir -p "$HOME/.local/bin/" +mkdir -p "$HOME/.local/share/cmake-init/" + + +# copying files +cp ./target/release/cmake-init ~/.local/bin/ +cp -r ./templates ~/.local/share/cmake-init/ + +echo "export PATH=$PATH:~/.local/bin" >> ~/.bashrc + +echo "Installation complete!" diff --git a/install_macos.sh b/install_macos.sh new file mode 100755 index 0000000..8fd87d0 --- /dev/null +++ b/install_macos.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +# building cmake-init +cargo build --release + +# creating necessary directories +mkdir -p "$HOME/.local/bin/" +mkdir -p "$HOME/Library/Application Support/cmake-init/templates" + + +# copying files +cp ./target/release/cmake-init ~/.local/bin/ +cp -r ./templates ~/.local/share/cmake-init/ + +echo "export PATH=$PATH:~/.local/bin" >> ~/.bashrc + +echo "Installation complete!" diff --git a/install_win.bat b/install_win.bat new file mode 100755 index 0000000..b946fb6 --- /dev/null +++ b/install_win.bat @@ -0,0 +1,15 @@ +@echo off + +cargo install --release + +mkdir "%APPDATA%\\cmake-init\\templates" +xcopy /s /y "templates" "%APPDATA%\\cmake-init\\templates" + + +REM cope target\release\cmake-init.exe to %USERPROFILE%\bin +mkdir "%USERPROFILE%\\bin" +xcopy /s /y "target\\release\\cmake-init.exe" "%USERPROFILE%\\bin\\cmake-init.exe" + + +REM add %USERPROFILE%\bin to PATH +setx PATH "%USERPROFILE%\\bin;%PATH%" diff --git a/src/args/args.rs b/src/args/args.rs index 6b8c116..e96e5f4 100644 --- a/src/args/args.rs +++ b/src/args/args.rs @@ -5,11 +5,12 @@ use super::Languages; /// A struct to hold the arguments passed to cmake-init. /// The arguments are parsed from the command line and stored in this struct. /// The struct is then passed to the `init` function to create the project. -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct Args { pub name: String, pub cmake_min_version: String, pub lang: Languages, + pub templates_dir: String, } impl Args { @@ -27,7 +28,7 @@ impl Args { * HACK: This is a hack to check unknown arguments. */ for (key, _) in &argv { - if key != "name" && key != "cmake-version" && key != "lang" { + if key != "name" && key != "cmake-version" && key != "lang" && key != "templates-dir" { eprintln!("Unknown argument: {}", key); exit(1); } @@ -37,9 +38,62 @@ impl Args { name: Args::get_arg(&argv, "name", true, None), cmake_min_version: Args::get_arg(&argv, "cmake-version", false, Some("3.0")), lang: Languages::from_string(Args::get_arg(&argv, "lang", false, Some("c"))), + templates_dir: Args::get_template(&argv), } } + /// Get the template directory path. + /// If the user has not specified a template directory, use the default. + /// + /// # Arguments + /// + /// * `argv` - The argument map. + fn get_template(argv: &HashMap>) -> String { + #[cfg(target_os = "linux")] + let templates_dir = Args::get_arg( + &argv, + "templates-dir", + false, + Some( + format!( + "{}/.local/share/cmake-init/templates", + std::env::var("HOME").unwrap() + ) + .as_str(), + ), + ); + + #[cfg(target_os = "windows")] + let templates_dir = Args::get_arg( + &argv, + "templates-dir", + false, + Some( + format!( + "{}\\cmake-init\\templates", + std::env::var("APPDATA").unwrap() + ) + .as_str(), + ), + ); + + #[cfg(target_os = "macos")] + let templates_dir = Args::get_arg( + &argv, + "templates-dir", + false, + Some( + format!( + "{}/Library/Application Support/cmake-init/templates", + std::env::var("HOME").unwrap() + ) + .as_str(), + ), + ); + + templates_dir + } + /// Get the value of the argument at the given index. /// If the argument is not found, print an error and exit. /// @@ -76,7 +130,7 @@ impl Args { /// * `argv` - The argument map. fn validate_args(argv: &HashMap>) { if argv.len() == 0 { - eprintln!("No arguments provided."); + Args::print_help(); exit(1); } else if argv.get("help").is_some() || argv.get("h").is_some() { Args::print_help(); @@ -96,6 +150,7 @@ impl Args { println!(" --name= The name of the project."); println!(" --cmake-version= The minimum version of CMake to use."); println!(" --lang= The language chosen for the project(cpp, c)."); + println!(" --templates-dir= The directory containing the templates."); println!(" --help | -h Print this help message."); println!(" --version | -v Print the version of cmake-init."); } @@ -150,4 +205,18 @@ mod arg_tests { assert_eq!(args.cmake_min_version, "3.0"); assert_eq!(args.lang, Languages::CPP); } + + #[test] + #[should_panic] + fn unknown_args() { + let mut args = HashMap::new(); + args.insert("name".to_string(), vec!["test".to_string()]); + args.insert("cmake-version".to_string(), vec!["3.0".to_string()]); + args.insert("lang".to_string(), vec!["cpp".to_string()]); + args.insert("unknown".to_string(), vec!["".to_string()]); + let args = super::Args::from(args); + assert_eq!(args.name, "test"); + assert_eq!(args.cmake_min_version, "3.0"); + assert_eq!(args.lang, Languages::CPP); + } } diff --git a/src/args/languages.rs b/src/args/languages.rs index 4d9e591..706278e 100644 --- a/src/args/languages.rs +++ b/src/args/languages.rs @@ -1,7 +1,7 @@ use std::process::exit; /// Enum for the languages that can be used. -/// NOTE: Currently only C and C++ are supported. +/** NOTE: Currently only C and C++ are supported. */ #[derive(Debug, PartialEq)] pub enum Languages { C, @@ -35,8 +35,8 @@ impl ToString for Languages { /// * A string representing the language. fn to_string(&self) -> String { match self { - Languages::C => String::from("c"), - Languages::CPP => String::from("cpp"), + Languages::C => String::from("main.c"), + Languages::CPP => String::from("main.cpp"), } } } diff --git a/src/main.rs b/src/main.rs index 3db59a7..30540bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,5 +8,22 @@ fn main() { let (_, argv) = argmap::parse(std::env::args()); let args = Args::from(argv); - println!("{:?}", args); + + let pwd = std::env::current_dir().unwrap(); + + // create src directory if it doesn't exist + let src_dir = pwd.join("src"); + if !src_dir.exists() { + std::fs::create_dir(src_dir).unwrap(); + } + + // read template file + let template_file = pwd.join(args.templates_dir).join(args.lang.to_string()); + let template = std::fs::read_to_string(template_file).unwrap(); + + // create main file + let main_file = pwd.join("src").join(args.lang.to_string()); + if !main_file.exists() { + std::fs::write(main_file, template).unwrap(); + } } diff --git a/templates/c.c b/templates/main.c similarity index 100% rename from templates/c.c rename to templates/main.c diff --git a/templates/cpp.cpp b/templates/main.cpp similarity index 100% rename from templates/cpp.cpp rename to templates/main.cpp From 196da98c3bcc7089ab494b52bdcb43c8ef9423ff Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Tue, 7 Nov 2023 22:46:56 +0100 Subject: [PATCH 08/24] feat: link to project page --- src/args/args.rs | 3 ++- src/args/mod.rs | 2 +- src/main.rs | 21 ++++----------------- src/template.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 src/template.rs diff --git a/src/args/args.rs b/src/args/args.rs index e96e5f4..4d60c7d 100644 --- a/src/args/args.rs +++ b/src/args/args.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, process::exit}; -use super::Languages; +use crate::args::languages::Languages; /// A struct to hold the arguments passed to cmake-init. /// The arguments are parsed from the command line and stored in this struct. @@ -44,6 +44,7 @@ impl Args { /// Get the template directory path. /// If the user has not specified a template directory, use the default. + /// The default template directory is platform dependent. /// /// # Arguments /// diff --git a/src/args/mod.rs b/src/args/mod.rs index 1f049b3..3a824d5 100644 --- a/src/args/mod.rs +++ b/src/args/mod.rs @@ -1,5 +1,5 @@ mod args; mod languages; +// exports pub use args::Args; -pub use languages::Languages; diff --git a/src/main.rs b/src/main.rs index 30540bd..a9a93bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,29 +1,16 @@ mod args; +mod template; use argmap; use crate::args::Args; +use crate::template::Template; fn main() { let (_, argv) = argmap::parse(std::env::args()); let args = Args::from(argv); - let pwd = std::env::current_dir().unwrap(); - - // create src directory if it doesn't exist - let src_dir = pwd.join("src"); - if !src_dir.exists() { - std::fs::create_dir(src_dir).unwrap(); - } - - // read template file - let template_file = pwd.join(args.templates_dir).join(args.lang.to_string()); - let template = std::fs::read_to_string(template_file).unwrap(); - - // create main file - let main_file = pwd.join("src").join(args.lang.to_string()); - if !main_file.exists() { - std::fs::write(main_file, template).unwrap(); - } + let template = Template::new(args); + template.create(); } diff --git a/src/template.rs b/src/template.rs new file mode 100644 index 0000000..295078b --- /dev/null +++ b/src/template.rs @@ -0,0 +1,42 @@ +use crate::args::Args; +use std::path::PathBuf; + +pub struct Template { + pub args: Args, + pub pwd: PathBuf, +} + +impl Template { + pub fn new(args: Args) -> Self { + Self { + args, + pwd: std::env::current_dir().unwrap(), + } + } + + pub fn create(&self) { + // create src directory if it doesn't exist + let template = self.get_template(); + + let src_dir = self.pwd.join("src"); + if !src_dir.exists() { + std::fs::create_dir(src_dir).unwrap(); + } + + // create main file + let main_file = self.pwd.join("src").join(self.args.lang.to_string()); + if !main_file.exists() { + std::fs::write(main_file, template).unwrap(); + } + } + + fn get_template(&self) -> String { + // read template file + let template_file = self + .pwd + .join(&self.args.templates_dir) + .join(self.args.lang.to_string()); + + std::fs::read_to_string(template_file).unwrap() + } +} From 5f226bde528d0182829b1df99019b9660066ec9f Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Tue, 7 Nov 2023 23:10:23 +0100 Subject: [PATCH 09/24] refactor: unknown args --- Cargo.lock | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/args/args.rs | 16 +++++++------- 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 17c5ba2..64af48f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,4 +13,60 @@ name = "cmake-init" version = "0.1.0" dependencies = [ "argmap", + "struct-field-names-as-array", ] + +[[package]] +name = "proc-macro2" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "struct-field-names-as-array" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ba4bae771f9cc992c4f403636c54d2ef13acde6367583e99d06bb336674dd9" +dependencies = [ + "struct-field-names-as-array-derive", +] + +[[package]] +name = "struct-field-names-as-array-derive" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2dbf8b57f3ce20e4bb171a11822b283bdfab6c4bb0fe64fa729f045f23a0938" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/Cargo.toml b/Cargo.toml index f06d8d4..bd12fbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" [dependencies] argmap = "1.1.2" +struct-field-names-as-array = "0.3.0" diff --git a/src/args/args.rs b/src/args/args.rs index 4d60c7d..e0e3921 100644 --- a/src/args/args.rs +++ b/src/args/args.rs @@ -1,11 +1,12 @@ use std::{collections::HashMap, process::exit}; +use struct_field_names_as_array::FieldNamesAsArray; use crate::args::languages::Languages; /// A struct to hold the arguments passed to cmake-init. /// The arguments are parsed from the command line and stored in this struct. /// The struct is then passed to the `init` function to create the project. -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, FieldNamesAsArray)] pub struct Args { pub name: String, pub cmake_min_version: String, @@ -23,14 +24,13 @@ impl Args { pub fn from(argv: HashMap>) -> Args { Args::validate_args(&argv); - #[allow(unused_doc_comments)] - /** - * HACK: This is a hack to check unknown arguments. - */ for (key, _) in &argv { - if key != "name" && key != "cmake-version" && key != "lang" && key != "templates-dir" { - eprintln!("Unknown argument: {}", key); - exit(1); + for known_arg in Args::FIELD_NAMES_AS_ARRAY { + if key != known_arg { + eprintln!("Unknown argument: {}", key); + eprintln!("Run `cmake-init --help` for more information."); + exit(1); + } } } From 6f90f3442f7b08772385a0dc2e7863e69ed02979 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Tue, 7 Nov 2023 23:32:54 +0100 Subject: [PATCH 10/24] rm: tests --- .github/workflows/rust.yml | 21 ++++++-------- src/args/args.rs | 59 -------------------------------------- 2 files changed, 9 insertions(+), 71 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index f305fbe..baf8a27 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,20 +1,17 @@ name: Rust on: - pull_request: - branches: [ "main", "develop" ] + pull_request: + branches: ["main", "develop"] env: - CARGO_TERM_COLOR: always + CARGO_TERM_COLOR: always jobs: - build: + build: + runs-on: ubuntu-latest - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose + steps: + - uses: actions/checkout@v3 + - name: Build + run: cargo build --verbose --release diff --git a/src/args/args.rs b/src/args/args.rs index e0e3921..d66463b 100644 --- a/src/args/args.rs +++ b/src/args/args.rs @@ -162,62 +162,3 @@ impl Args { println!("cmake-init {}", env!("CARGO_PKG_VERSION")); } } - -/// tests -#[cfg(test)] -mod arg_tests { - use super::Languages; - use std::collections::HashMap; - #[test] - fn test_args() { - let mut args = HashMap::new(); - args.insert("name".to_string(), vec!["test".to_string()]); - args.insert("cmake-version".to_string(), vec!["3.0".to_string()]); - args.insert("lang".to_string(), vec!["cpp".to_string()]); - let args = super::Args::from(args); - assert_eq!(args.name, "test"); - assert_eq!(args.cmake_min_version, "3.0"); - assert_eq!(args.lang, Languages::CPP); - } - - #[test] - fn test_args_default() { - let mut args = HashMap::new(); - args.insert("name".to_string(), vec!["test".to_string()]); - let args = super::Args::from(args); - assert_eq!(args.name, "test"); - assert_eq!(args.cmake_min_version, "3.0"); - assert_eq!(args.lang, Languages::CPP); - } - - #[test] - #[should_panic] - fn test_args_panic() { - let args = HashMap::new(); - let _args = super::Args::from(args); - } - - #[test] - fn test_args_help() { - let mut args = HashMap::new(); - args.insert("help".to_string(), vec!["".to_string()]); - let args = super::Args::from(args); - assert_eq!(args.name, ""); - assert_eq!(args.cmake_min_version, "3.0"); - assert_eq!(args.lang, Languages::CPP); - } - - #[test] - #[should_panic] - fn unknown_args() { - let mut args = HashMap::new(); - args.insert("name".to_string(), vec!["test".to_string()]); - args.insert("cmake-version".to_string(), vec!["3.0".to_string()]); - args.insert("lang".to_string(), vec!["cpp".to_string()]); - args.insert("unknown".to_string(), vec!["".to_string()]); - let args = super::Args::from(args); - assert_eq!(args.name, "test"); - assert_eq!(args.cmake_min_version, "3.0"); - assert_eq!(args.lang, Languages::CPP); - } -} From e964ebc4fa09e907fec6d6ffd9df2680862db02e Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Tue, 7 Nov 2023 23:37:52 +0100 Subject: [PATCH 11/24] ci(test) --- .github/workflows/rust.yml | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index baf8a27..030f055 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -15,3 +15,38 @@ jobs: - uses: actions/checkout@v3 - name: Build run: cargo build --verbose --release + + test_run: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build + run: cargo build --release + + - name: create test dir + run: mkdir testdir + + - name: copy binary + run: | + cp target/release/rust testdir + cd testdir + + - name: run binary + run: | + ./cmake-init --name=test + if [ "$?" -eq 0 ]; then + echo "Test passed" + exit 0 + else + echo "Test failed" + exit 1 + fi + + if [[ -f "src/main.c" ]] || [[ -f "CMakeLists.txt" ]]; then + echo "Test passed" + exit 0 + else + echo "Test failed" + exit 1 + fi From a60a88a11eca03d969bafb6235746f6fe1775ff8 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Tue, 7 Nov 2023 23:39:55 +0100 Subject: [PATCH 12/24] fix: help menu --- src/args/args.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/args/args.rs b/src/args/args.rs index d66463b..afaaf16 100644 --- a/src/args/args.rs +++ b/src/args/args.rs @@ -148,10 +148,10 @@ impl Args { println!("Usage: cmake-init --name="); println!(); println!("Options:"); - println!(" --name= The name of the project."); - println!(" --cmake-version= The minimum version of CMake to use."); - println!(" --lang= The language chosen for the project(cpp, c)."); - println!(" --templates-dir= The directory containing the templates."); + println!(" --name The name of the project."); + println!(" --cmake-version The minimum version of CMake to use."); + println!(" --lang The language chosen for the project(cpp, c)."); + println!(" --templates-dir The directory containing the templates."); println!(" --help | -h Print this help message."); println!(" --version | -v Print the version of cmake-init."); } From bed4c17b33a92d7284412878569aecd0a22c8ec4 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Tue, 7 Nov 2023 23:47:18 +0100 Subject: [PATCH 13/24] fix: help menu --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 030f055..5d07214 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -29,7 +29,7 @@ jobs: - name: copy binary run: | - cp target/release/rust testdir + cp target/release/cmake-init testdir cd testdir - name: run binary From 05b17f4b0f80290e796d76f3aebfe993635030b1 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Tue, 7 Nov 2023 23:54:48 +0100 Subject: [PATCH 14/24] fix: unknown args --- .github/workflows/rust.yml | 2 +- src/args/args.rs | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 5d07214..8d6841c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -30,10 +30,10 @@ jobs: - name: copy binary run: | cp target/release/cmake-init testdir - cd testdir - name: run binary run: | + cd testdir ./cmake-init --name=test if [ "$?" -eq 0 ]; then echo "Test passed" diff --git a/src/args/args.rs b/src/args/args.rs index afaaf16..14a8739 100644 --- a/src/args/args.rs +++ b/src/args/args.rs @@ -23,14 +23,12 @@ impl Args { /// * `argv` - The argument map. pub fn from(argv: HashMap>) -> Args { Args::validate_args(&argv); + let known_args = Args::FIELD_NAMES_AS_ARRAY; for (key, _) in &argv { - for known_arg in Args::FIELD_NAMES_AS_ARRAY { - if key != known_arg { - eprintln!("Unknown argument: {}", key); - eprintln!("Run `cmake-init --help` for more information."); - exit(1); - } + if !known_args.contains(&key.as_str()) { + eprintln!("Unknown argument: {}", key); + exit(1); } } From d0109501ba69c7eee966380606287e7eaeb374f2 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Tue, 7 Nov 2023 23:58:15 +0100 Subject: [PATCH 15/24] ci: fix test run --- .github/workflows/rust.yml | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 8d6841c..8d0d486 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -21,29 +21,23 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Build - run: cargo build --release - - - name: create test dir - run: mkdir testdir - - name: copy binary + - name: build and run cli run: | + cargo build --release + mkdir testdir cp target/release/cmake-init testdir - - name: run binary - run: | cd testdir - ./cmake-init --name=test + ./cmake-init --name test if [ "$?" -eq 0 ]; then echo "Test passed" exit 0 else echo "Test failed" - exit 1 fi - if [[ -f "src/main.c" ]] || [[ -f "CMakeLists.txt" ]]; then + if [[ -f "src/main.c" ]] ; then echo "Test passed" exit 0 else From c5be6cd4f4276cea8b3d1392421a649b1da3fea5 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Wed, 8 Nov 2023 00:00:28 +0100 Subject: [PATCH 16/24] ci: fix test run --- .github/workflows/rust.yml | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 8d0d486..d53720f 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -8,39 +8,24 @@ env: CARGO_TERM_COLOR: always jobs: - build: + build-linux: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Build run: cargo build --verbose --release - - test_run: - runs-on: ubuntu-latest + build-windows: + runs-on: windows-latest steps: - uses: actions/checkout@v3 + - name: Build + run: cargo build --verbose --release + build-macos: + runs-on: macos-latest - - name: build and run cli - run: | - cargo build --release - mkdir testdir - cp target/release/cmake-init testdir - - cd testdir - ./cmake-init --name test - if [ "$?" -eq 0 ]; then - echo "Test passed" - exit 0 - else - echo "Test failed" - fi - - if [[ -f "src/main.c" ]] ; then - echo "Test passed" - exit 0 - else - echo "Test failed" - exit 1 - fi + steps: + - uses: actions/checkout@v3 + - name: Build + run: cargo build --verbose --release From 4deb4f7ea73f62f1a32e609f192a118d7f8d15c0 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Wed, 8 Nov 2023 00:03:04 +0100 Subject: [PATCH 17/24] ci: test run --- .github/workflows/rust.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index d53720f..3f0ce31 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -15,6 +15,10 @@ jobs: - uses: actions/checkout@v3 - name: Build run: cargo build --verbose --release + + - name: run + run: cargo run -- --name testrun + build-windows: runs-on: windows-latest @@ -22,6 +26,10 @@ jobs: - uses: actions/checkout@v3 - name: Build run: cargo build --verbose --release + + - name: run + run: cargo run -- --name testrun + build-macos: runs-on: macos-latest @@ -29,3 +37,6 @@ jobs: - uses: actions/checkout@v3 - name: Build run: cargo build --verbose --release + + - name: run + run: cargo run -- --name testrun From d7a7d02d9a5c76a72aaf24c693a8ae48dcf40d49 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Wed, 8 Nov 2023 00:09:47 +0100 Subject: [PATCH 18/24] ci: test run --- .github/workflows/run.yml | 42 ++++++++++++++++++++++++++++++++++++++ .github/workflows/rust.yml | 11 ---------- 2 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/run.yml diff --git a/.github/workflows/run.yml b/.github/workflows/run.yml new file mode 100644 index 0000000..588a2fc --- /dev/null +++ b/.github/workflows/run.yml @@ -0,0 +1,42 @@ +name: Rust + +on: + pull_request: + branches: ["main", "develop"] + +env: + CARGO_TERM_COLOR: always + +jobs: + build-linux: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build + run: ./install_linux.sh + + - name: run + run: cargo run -- --name testrun + + build-windows: + runs-on: windows-latest + + steps: + - uses: actions/checkout@v3 + - name: Build + run: .\install_win.bat + + - name: run + run: cargo run -- --name testrun + + build-macos: + runs-on: macos-latest + + steps: + - uses: actions/checkout@v3 + - name: Build + run: ./install_macos.sh + + - name: run + run: cargo run -- --name testrun diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 3f0ce31..d53720f 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -15,10 +15,6 @@ jobs: - uses: actions/checkout@v3 - name: Build run: cargo build --verbose --release - - - name: run - run: cargo run -- --name testrun - build-windows: runs-on: windows-latest @@ -26,10 +22,6 @@ jobs: - uses: actions/checkout@v3 - name: Build run: cargo build --verbose --release - - - name: run - run: cargo run -- --name testrun - build-macos: runs-on: macos-latest @@ -37,6 +29,3 @@ jobs: - uses: actions/checkout@v3 - name: Build run: cargo build --verbose --release - - - name: run - run: cargo run -- --name testrun From bc3fd2b433ac65a7d09f7db13a340da959e9d02d Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Wed, 8 Nov 2023 00:15:35 +0100 Subject: [PATCH 19/24] fix: macos installation --- .github/workflows/run.yml | 3 ++- .github/workflows/rust.yml | 3 ++- install_macos.sh | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/run.yml b/.github/workflows/run.yml index 588a2fc..7c68541 100644 --- a/.github/workflows/run.yml +++ b/.github/workflows/run.yml @@ -1,4 +1,5 @@ -name: Rust +--- +name: Test run on: pull_request: diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index d53720f..3b18f7a 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,4 +1,5 @@ -name: Rust +--- +name: Test build on: pull_request: diff --git a/install_macos.sh b/install_macos.sh index 8fd87d0..37f1819 100755 --- a/install_macos.sh +++ b/install_macos.sh @@ -9,8 +9,8 @@ mkdir -p "$HOME/Library/Application Support/cmake-init/templates" # copying files -cp ./target/release/cmake-init ~/.local/bin/ -cp -r ./templates ~/.local/share/cmake-init/ +cp ./target/release/cmake-init "$HOME/.local/bin/" +cp -r ./templates "$HOME/Library/Application Support/cmake-init/" echo "export PATH=$PATH:~/.local/bin" >> ~/.bashrc From 99ae6152f2c588c7229e725adcf3f06b1430b038 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Wed, 8 Nov 2023 00:16:38 +0100 Subject: [PATCH 20/24] fix: macos installation --- .github/workflows/run.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/run.yml b/.github/workflows/run.yml index 7c68541..3616379 100644 --- a/.github/workflows/run.yml +++ b/.github/workflows/run.yml @@ -9,7 +9,7 @@ env: CARGO_TERM_COLOR: always jobs: - build-linux: + run-linux: runs-on: ubuntu-latest steps: @@ -20,7 +20,7 @@ jobs: - name: run run: cargo run -- --name testrun - build-windows: + run-windows: runs-on: windows-latest steps: @@ -31,7 +31,7 @@ jobs: - name: run run: cargo run -- --name testrun - build-macos: + run-macos: runs-on: macos-latest steps: From 0e7ed27bb637a42547c971471b26b43cc5e5bed2 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Wed, 8 Nov 2023 00:17:12 +0100 Subject: [PATCH 21/24] ci: test run --- .github/workflows/run.yml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/.github/workflows/run.yml b/.github/workflows/run.yml index 3616379..be24f19 100644 --- a/.github/workflows/run.yml +++ b/.github/workflows/run.yml @@ -14,9 +14,6 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Build - run: ./install_linux.sh - - name: run run: cargo run -- --name testrun @@ -25,9 +22,6 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Build - run: .\install_win.bat - - name: run run: cargo run -- --name testrun @@ -36,8 +30,5 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Build - run: ./install_macos.sh - - name: run run: cargo run -- --name testrun From 4043e8f3e8d5b60659a9229a062545df6d1c6db7 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Wed, 8 Nov 2023 00:18:41 +0100 Subject: [PATCH 22/24] fix: test run --- .github/workflows/run.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/run.yml b/.github/workflows/run.yml index be24f19..3616379 100644 --- a/.github/workflows/run.yml +++ b/.github/workflows/run.yml @@ -14,6 +14,9 @@ jobs: steps: - uses: actions/checkout@v3 + - name: Build + run: ./install_linux.sh + - name: run run: cargo run -- --name testrun @@ -22,6 +25,9 @@ jobs: steps: - uses: actions/checkout@v3 + - name: Build + run: .\install_win.bat + - name: run run: cargo run -- --name testrun @@ -30,5 +36,8 @@ jobs: steps: - uses: actions/checkout@v3 + - name: Build + run: ./install_macos.sh + - name: run run: cargo run -- --name testrun From 281f01a16c8441861591e2b246aa6c115b38692a Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Wed, 8 Nov 2023 00:44:29 +0100 Subject: [PATCH 23/24] feat: create CMakeLists.txt --- src/args/args.rs | 13 ++++++++--- src/template.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/args/args.rs b/src/args/args.rs index 14a8739..8932a3f 100644 --- a/src/args/args.rs +++ b/src/args/args.rs @@ -3,9 +3,16 @@ use struct_field_names_as_array::FieldNamesAsArray; use crate::args::languages::Languages; -/// A struct to hold the arguments passed to cmake-init. -/// The arguments are parsed from the command line and stored in this struct. -/// The struct is then passed to the `init` function to create the project. +/// # Args struct +/// +/// This struct is used to parse the arguments passed to the program. +/// +/// ## Fields +/// +/// * `name` - The name of the project +/// * `cmake_min_version` - The minimum version of CMake to use +/// * `lang` - The language chosen for the project +/// * `templates_dir` - The directory containing the template #[derive(Debug, PartialEq, FieldNamesAsArray)] pub struct Args { pub name: String, diff --git a/src/template.rs b/src/template.rs index 295078b..c82820b 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,12 +1,26 @@ use crate::args::Args; -use std::path::PathBuf; +use std::{path::PathBuf, process::exit}; +/// # Template struct +/// +/// This struct is used to create the template files +/// and directories for the project. +/// +/// ## Fields +/// +/// * `args` - The arguments passed to the program +/// * `pwd` - The current working directory pub struct Template { pub args: Args, pub pwd: PathBuf, } impl Template { + /// Create a new Template struct from the given arguments. + /// + /// # Arguments + /// + /// * `args` - The arguments passed to the program pub fn new(args: Args) -> Self { Self { args, @@ -14,6 +28,10 @@ impl Template { } } + /// Create the template files and directories. + /// If the src directory does not exist, create it. + /// If the main file does not exist, create it. + /// If the CMakeLists.txt file does not exist, create it. pub fn create(&self) { // create src directory if it doesn't exist let template = self.get_template(); @@ -28,8 +46,13 @@ impl Template { if !main_file.exists() { std::fs::write(main_file, template).unwrap(); } + + // create CMakeLists.txt + self.create_cmakelists(); } + /// Get the template file contents. + /// If the template file does not exist, print an error and exit. fn get_template(&self) -> String { // read template file let template_file = self @@ -37,6 +60,36 @@ impl Template { .join(&self.args.templates_dir) .join(self.args.lang.to_string()); - std::fs::read_to_string(template_file).unwrap() + std::fs::read_to_string(template_file).unwrap_or_else(|_| { + eprintln!("cannot read template file"); + exit(1); + }) + } + + /// Create the CMakeLists.txt file. + fn create_cmakelists(&self) { + let cmakelists = self.pwd.join("CMakeLists.txt"); + + std::fs::write(cmakelists, self.get_cmake_template().join("\n")).unwrap(); + } + + /// Get the CMakeLists.txt template. + /// This is a three element array of strings. + fn get_cmake_template(&self) -> [String; 3] { + [ + format!( + "project({})", + self.args.name.to_lowercase().replace("-", "_") + ), + format!( + "cmake_minimum_required(VERSION {})", + self.args.cmake_min_version + ), + format!( + "add_executable({} src/{})", + self.args.name.to_lowercase().replace("-", "_"), + self.args.lang.to_string() + ), + ] } } From 05fce8de5291938da909d84a591530539f5fcbf6 Mon Sep 17 00:00:00 2001 From: abdellatif-temsamani Date: Wed, 8 Nov 2023 00:48:15 +0100 Subject: [PATCH 24/24] fix: install win --- install_win.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install_win.bat b/install_win.bat index b946fb6..9a8972c 100755 --- a/install_win.bat +++ b/install_win.bat @@ -1,6 +1,6 @@ @echo off -cargo install --release +cargo build --release mkdir "%APPDATA%\\cmake-init\\templates" xcopy /s /y "templates" "%APPDATA%\\cmake-init\\templates"