Skip to content

Commit

Permalink
Merge pull request #5 from abdellatif-temsamani/feature/templates
Browse files Browse the repository at this point in the history
Feature/templates
  • Loading branch information
abdellatif-temsamani authored Nov 7, 2023
2 parents 6084e1e + 4043e8f commit 498ad27
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 73 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
name: Test run

on:
pull_request:
branches: ["main", "develop"]

env:
CARGO_TERM_COLOR: always

jobs:
run-linux:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: ./install_linux.sh

- name: run
run: cargo run -- --name testrun

run-windows:
runs-on: windows-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: .\install_win.bat

- name: run
run: cargo run -- --name testrun

run-macos:
runs-on: macos-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: ./install_macos.sh

- name: run
run: cargo run -- --name testrun
36 changes: 24 additions & 12 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
name: Rust
---
name: Test build

on:
pull_request:
branches: [ "main", "develop" ]
pull_request:
branches: ["main", "develop"]

env:
CARGO_TERM_COLOR: always
CARGO_TERM_COLOR: always

jobs:
build:
build-linux:
runs-on: ubuntu-latest

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose --release
build-windows:
runs-on: windows-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
build-macos:
runs-on: macos-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose --release
56 changes: 56 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"

[dependencies]
argmap = "1.1.2"
struct-field-names-as-array = "0.3.0"
17 changes: 17 additions & 0 deletions install_linux.sh
Original file line number Diff line number Diff line change
@@ -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!"
17 changes: 17 additions & 0 deletions install_macos.sh
Original file line number Diff line number Diff line change
@@ -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 "$HOME/.local/bin/"
cp -r ./templates "$HOME/Library/Application Support/cmake-init/"

echo "export PATH=$PATH:~/.local/bin" >> ~/.bashrc

echo "Installation complete!"
15 changes: 15 additions & 0 deletions install_win.bat
Original file line number Diff line number Diff line change
@@ -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%"
121 changes: 65 additions & 56 deletions src/args/args.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::{collections::HashMap, process::exit};
use struct_field_names_as_array::FieldNamesAsArray;

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.
/// The struct is then passed to the `init` function to create the project.
#[derive(Debug)]
#[derive(Debug, PartialEq, FieldNamesAsArray)]
pub struct Args {
pub name: String,
pub cmake_min_version: String,
pub lang: Languages,
pub templates_dir: String,
}

impl Args {
Expand All @@ -21,13 +23,10 @@ impl Args {
/// * `argv` - The argument map.
pub fn from(argv: HashMap<String, Vec<String>>) -> Args {
Args::validate_args(&argv);
let known_args = Args::FIELD_NAMES_AS_ARRAY;

#[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" {
if !known_args.contains(&key.as_str()) {
eprintln!("Unknown argument: {}", key);
exit(1);
}
Expand All @@ -37,9 +36,63 @@ 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.
/// The default template directory is platform dependent.
///
/// # Arguments
///
/// * `argv` - The argument map.
fn get_template(argv: &HashMap<String, Vec<String>>) -> 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.
///
Expand Down Expand Up @@ -76,7 +129,7 @@ impl Args {
/// * `argv` - The argument map.
fn validate_args(argv: &HashMap<String, Vec<String>>) {
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();
Expand All @@ -93,9 +146,10 @@ impl Args {
println!("Usage: cmake-init --name=<name>");
println!();
println!("Options:");
println!(" --name=<name> The name of the project.");
println!(" --cmake-version=<version> The minimum version of CMake to use.");
println!(" --lang=<version> The language chosen for the project(cpp, c).");
println!(" --name <name> The name of the project.");
println!(" --cmake-version <version> The minimum version of CMake to use.");
println!(" --lang <version> The language chosen for the project(cpp, c).");
println!(" --templates-dir <dir> The directory containing the templates.");
println!(" --help | -h Print this help message.");
println!(" --version | -v Print the version of cmake-init.");
}
Expand All @@ -106,48 +160,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);
}
}
6 changes: 3 additions & 3 deletions src/args/languages.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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"),
}
}
}
Loading

0 comments on commit 498ad27

Please sign in to comment.