Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finish build new template project #54

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions zork++/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
use std::{fs::File, io::Read};

use clap::Parser;
use env_logger::Target;
use zork::{config_cli::CliArgs, utils::logger::config_logger};
use zork::{
config_cli::CliArgs,
config_file::ZorkConfigFile,
utils::{logger::config_logger, template::create_templated_project},
};

fn main() {
let parser_cli = CliArgs::parse_from([""]);
let cli_args = CliArgs::parse_from(vec!["", "-vv"]);
config_logger(cli_args.verbose, Target::Stdout).expect("Error configure logger");

if cli_args.new_template {
create_templated_project(&cli_args);
}

// TODO Impl the program's main logic
build_project(&cli_args)
}

config_logger(parser_cli.verbose, Target::Stdout).expect("Error configure logger");
/// Computes the logic written for the build process
fn build_project(_cli_args: &CliArgs) {
let mut buffer = String::new();
let mut file = File::open(".") // TODO Extract from cli_args alternative path?
.expect("Error open file");
file.read_to_string(&mut buffer).expect("Error read");
let _zork_conf: ZorkConfigFile = toml::from_str(&buffer).expect("Can deserialize config file");

log::warn!("warn");
log::info!("info");
log::error!("error");
log::info!("Finished procces"); // TODO Time calculations at info level
}
17 changes: 11 additions & 6 deletions zork++/src/lib/config_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ pub struct CliArgs {
pub verbose: u8,

#[arg(short, long, group = "base_option", help = "Create Project template")]
pub new_template: Option<String>,
#[arg(
long,
help = "To generate a C++ modules project or a classical one with headers, can you use with template project"
)]
pub legacy: bool,
pub new_template: bool,
#[arg(
long,
help = "Initializes a new local git repo, can you use with template project"
Expand Down Expand Up @@ -71,3 +66,13 @@ pub enum CppCompiler {
MSVC,
GCC,
}

impl CppCompiler {
pub fn get_default_extesion(&self) -> &str {
match *self {
CppCompiler::CLANG => "cppm",
CppCompiler::MSVC => "ixx",
CppCompiler::GCC => todo!("GCC is still not supported yet by Zork++"),
}
}
}
2 changes: 1 addition & 1 deletion zork++/src/lib/config_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use self::{
/// ZorkConfigFile,
/// compiler::CppCompiler
/// };
///
///
/// const CONFIG_FILE_MOCK: &str = r#"
/// [project]
/// name = 'Zork++ serde tests'
Expand Down
92 changes: 92 additions & 0 deletions zork++/src/lib/utils/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/// Provides constant values for the elements that conforms the
/// autogenerated C++ example project
pub mod autogenerated_example {
pub const ROOT_PATH_NAME: &str = "example";
pub const PROJECT_NAME: &str = "calculator";
pub const CONFIG_FILE_NAME: &str = "zork.conf"; // zork.toml

pub const IFC_MOD_FILE: &str = "
export module math;
export namespace math {
int sum(int num1, int num2);
int multiply(int num1, int num2);
int substract(int num1, int num2);
int divide(int num1, int num2);
}
";

pub const SRC_MOD_FILE: &str = "
module math;
// Implementation of the definitions on the module unit interface
// for the sum and multiply math operations
namespace math {
int sum(int num1, int num2) {
return num1 + num2;
}
int multiply(int num1, int num2) {
return num1 * num2;
}
}
";

pub const SRC_MOD_FILE_2: &str = "
module math;
// Implementation of the definitions on the module unit interface
// for the substract and divide math operations
namespace math {
int substract(int num1, int num2) {
return num1 - num2;
}
int divide(int num1, int num2) {
return num1 / num2;
}
}
";

pub const MAIN: &str = r#"
import std;
import math;
int main() {
std::cout << "Hello from an autogenerated Zork project!" << std::endl;
std::cout << "RESULT '+': " << math::sum(2, 8) << std::endl;
std::cout << "RESULT '-': " << math::substract(8, 2) << std::endl;
std::cout << "RESULT '*': " << math::multiply(2, 8) << std::endl;
std::cout << "RESULT '/': " << math::divide(2, 2) << std::endl;
return 0;
}
"#;

/// TODO Replace it in the next PR for the Zork++ toml example
pub const CONFIG_FILE: &str = r#"
#This file it's autogenerated as an example of a Zork config file
[project]
name = "calculator"
authors = [ "Zero Day Code" ] # Replace this for the real authors
[compiler]
cpp_compiler = "clang"
cpp_standard = "20"
std_lib = "libcpp"
[build]
output_dir = "./out"
[executable]
executable_name = "<autogenerated_executable>"
sources = [
"*.cpp"
]
auto_execute = true
[tests]
tests_executable_name = "zork_proj_tests"
sources = [
"*.cpp"
]
auto_run_tests = true
[modules]
base_ifcs_dir = "<project_name>/ifc/"
interfaces = [ "*.cppm" ]
base_impls_dir = "<project_name>/src/"
implementations = [
"math.cpp",
"math2.cpp=[math]"
]
"#;
}
2 changes: 2 additions & 0 deletions zork++/src/lib/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod constants;
pub mod logger;
pub mod template;
101 changes: 101 additions & 0 deletions zork++/src/lib/utils/template.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use crate::config_cli::{CliArgs, CppCompiler};
use crate::utils::constants::autogenerated_example;
use log::info;
use std::io::Write;
use std::process::Command;
use std::{
fs::{DirBuilder, File},
path::Path,
};

/// Generates a new C++ standarized empty base project
/// with a pre-designed structure to organize the
/// user code in a modern fashion way.
///
/// Base template for the project files and folders:
/// - ./ifc/<project_name>
/// - math.<extension>
/// - ./src/<project_name>
/// - math.<extension>
/// - math2.<extension>
/// - main.cpp
/// - test
/// - dependencies
pub fn create_templated_project(cli_args: &CliArgs) {
let example_dir_name = Path::new(autogenerated_example::ROOT_PATH_NAME);
info!("Creating the autogenerated template project");

let compiler = cli_args.compiler.as_ref().unwrap_or(&CppCompiler::CLANG);
let path_ifc = example_dir_name.join("ifc");
let path_src = example_dir_name.join("src");
let path_test = example_dir_name.join("test");
let path_dependencies = example_dir_name.join("deps");

create_directory(example_dir_name);
create_directory(&path_ifc);
create_directory(&path_src);
create_directory(&path_test);
create_directory(&path_dependencies);

create_file(
&path_ifc,
&format!("{}.{}", "math", compiler.get_default_extesion()),
autogenerated_example::IFC_MOD_FILE.as_bytes(),
);
create_file(
&path_src,
"main.cpp", // TODO from constants
autogenerated_example::MAIN.as_bytes(),
);
create_file(
&path_src,
"math.cpp",
autogenerated_example::SRC_MOD_FILE.as_bytes(),
);
create_file(
&path_src,
"math2.cpp",
autogenerated_example::SRC_MOD_FILE_2.as_bytes(),
);

// TODO The replaces must dissapear in the next PR
let mut zork_conf = autogenerated_example::CONFIG_FILE
.replace("<project_name>", autogenerated_example::PROJECT_NAME)
.replace("<autog_test>", autogenerated_example::PROJECT_NAME)
.replace(
"<autogenerated_executable>",
autogenerated_example::PROJECT_NAME,
);

if cfg!(windows) {
zork_conf = zork_conf.replace("libcpp", "stdlib")
}
create_file(
Path::new(autogenerated_example::ROOT_PATH_NAME),
autogenerated_example::CONFIG_FILE_NAME,
zork_conf.as_bytes(),
);

if cli_args.git {
Command::new("git")
.current_dir(autogenerated_example::ROOT_PATH_NAME)
.arg("init")
.spawn()
.expect("Error initializing a new GIT repository");
}
}

fn create_file<'a>(path: &Path, filename: &'a str, buff_write: &'a [u8]) {
let mut file = File::create(path.join(filename))
.unwrap_or_else(|_| panic!("Error creating the example file: {filename}",));

file.write_all(buff_write)
.unwrap_or_else(|_| panic!("Error writting the example file: {filename}",));
}

fn create_directory(path_create: &Path) {
DirBuilder::new()
.recursive(true)
.create(path_create)
.unwrap_or_else(|_| panic!("Error creating directory: {:?}", path_create.as_os_str()))
}
Empty file added zork++/zork.conf
Empty file.