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 3 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
48 changes: 42 additions & 6 deletions zork++/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
use std::{path::Path, 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_template_project, constans::{DEFAULT_CONFIG_NAME, DEFAULT_DIRECTORY},

},
};


fn main() {
let parser_cli = CliArgs::parse_from([""]);

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

log::warn!("warn");
log::info!("info");
log::error!("error");
if parser_cli.new_template {
create_template_project(DEFAULT_CONFIG_NAME,Path::new(DEFAULT_DIRECTORY), true, parser_cli.compiler);
}

if parser_cli.command.is_some() {
match parser_cli.command.unwrap() {
zork::config_cli::Command::Tests => todo!(),
}
}else{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New code hasn't been formated, as usual...

execution_procces(Path::new(".").join(DEFAULT_DIRECTORY).join(DEFAULT_CONFIG_NAME).as_path());
}


}

fn execution_procces(path: &Path) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fn execution_process() isn't documented. Probably we may know what the function is doing, but not other people involved in the project. Every new feature in the code must be documented

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheHiddenOnSystem solve the TODO's letfted on the code and request for approve again


log::info!("Find config file: {:?}",path);
let mut buffer = String::new();
let mut file = File::open(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");

if zork_conf.executable.is_some() {

}

log::info!("Finish procces");
}
7 changes: 1 addition & 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
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
86 changes: 86 additions & 0 deletions zork++/src/lib/utils/constans.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
pub const DEFAULT_DIRECTORY: &str="example";
pub const DEFAULT_CONFIG_NAME: &str = "zork.conf";

pub const INTERFACE_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;
}
"#;

pub const ZORK_CONF: &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 constans;
pub mod logger;
pub mod template;
119 changes: 119 additions & 0 deletions zork++/src/lib/utils/template.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use crate::config_cli::CppCompiler;
use crate::utils::constans::{INTERFACE_MOD_FILE, MAIN, SRC_MOD_FILE, SRC_MOD_FILE_2, ZORK_CONF};
use log::info;
use std::io::{Write};
use std::path::PathBuf;
use std::process::Command;
use std::str::FromStr;
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 design for create the project files and folders:
/// - ./ifc/<project_name>
/// - hello_zork.<mod_extension>
/// - ./src/<project_name>
/// - hello_zork.cpp
/// - test
/// - dependencies
pub fn create_template_project(name_template: &str,root_path:&Path, git: bool, compiler: Option<CppCompiler>) {
// TODO required validate or control optionals
info!("Create template project");
let resolved_compiler = compiler.unwrap_or(CppCompiler::CLANG);
let path_ifc = root_path.join("ifc");
let path_src = root_path.join("src");
let path_test = root_path.join("test");
let path_dependencies = root_path.join("dependencies");

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

let extension_ifcs = match resolved_compiler {
CppCompiler::CLANG => "cppm",
CppCompiler::MSVC => "ixx",
CppCompiler::GCC => "ixx",
};
create_file(
&path_ifc,
format!("{}.{}", "math", extension_ifcs).as_str(),
INTERFACE_MOD_FILE.as_bytes(),
);
create_file(&path_src, "main.cpp", MAIN.as_bytes());
create_file(&path_src, "math.cpp", SRC_MOD_FILE.as_bytes());
create_file(&path_src, "math2.cpp", SRC_MOD_FILE_2.as_bytes());

let mut zork_conf = ZORK_CONF
.replace("<project_name>", name_template)
.replace("<autog_test>", name_template)
.replace("<autogenerated_executable>", name_template);

if cfg!(windows) {
zork_conf = zork_conf.replace("libcpp", "stdlib")
}
create_file(
&PathBuf::from_str(root_path.to_str().expect("Error parsed file to zork.conf"))
.expect("Not valid path to generate zork.conf"),
"zork.conf",
zork_conf.as_bytes(),
);

if git {
let result_git_init = Command::new("git")
.current_dir(&root_path)
.arg("init")
.spawn();

match result_git_init {
Ok(_children) => {
log::info!("Created git repository")
}
Err(_) => {
log::error!("Error create git respotory")
}
}
}
}

fn create_file<'a>(path: &PathBuf, name_file: &'a str, buff_write: &'a [u8]) {
let mut file = File::create(path.join(name_file)).expect(
format!(
"Error create in directory: {}, file: {}",
path.to_str().expect("Error parsed path in create file"),
name_file
)
.as_str(),
);
file.write_all(buff_write).expect(
format!(
"Error write in directory: {}, file: {}",
path.to_str()
.expect("Error parsed path create file in write"),
name_file
)
.as_str(),
);
}

fn create_directory(path_create: &Path) {
DirBuilder::new()
.recursive(true)
.create(path_create)
.expect(
format!(
"Error create directory: {}",
path_create
.as_os_str()
.to_str()
.expect("Error parsed path create directory")
)
.as_str(),
)
}

Empty file added zork++/zork.conf
Empty file.