Skip to content

Commit

Permalink
feat: create CMakeLists.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
abdellatif-temsamani committed Nov 7, 2023
1 parent 4043e8f commit 281f01a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/args/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
57 changes: 55 additions & 2 deletions src/template.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
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,
pwd: std::env::current_dir().unwrap(),
}
}

/// 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();
Expand All @@ -28,15 +46,50 @@ 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
.pwd
.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()
),
]
}
}

0 comments on commit 281f01a

Please sign in to comment.