From 35a820890b12e9f14585cc25a75e98124914dfd2 Mon Sep 17 00:00:00 2001 From: CALEXCO <82407596+CALEXCO@users.noreply.github.com> Date: Tue, 3 Dec 2024 16:54:56 +0100 Subject: [PATCH] Feture/new command structure (#4) * Refactor new commands c-new c-build * refactor file * clippy refactor * clippy format * new subcommands --- README.md | 138 ++-------------------------------------------------- src/main.rs | 20 ++++---- 2 files changed, 13 insertions(+), 145 deletions(-) diff --git a/README.md b/README.md index 9215094..63d5be9 100644 --- a/README.md +++ b/README.md @@ -1,141 +1,9 @@ +# cinit -# CInit - Command Line Tool for C Projects - -**CInit** is a command-line utility designed to simplify the workflow for creating, building, and running C projects. It provides a set of commands to automate common tasks, such as initializing project structures, compiling source files, and managing binaries. +**cinit** is a CLI tool built in **Rust** to simplify the creation of new C projects. It provides a quick and standardized way to generate the basic structure for a C project, inspired by tools like `cargo` (Rust) or `zig init` (Zig). ## Features -- Quickly create new C projects with standard folder structures. -- Build all `.c` files in the `./src/` directory. -- Move compiled binaries to the `./bin/` directory. -- Optionally, create a library directory with `.c` and `.h` files. -- Build and immediately run a project. - -## Installation - -1. Clone the repository: - ```bash - git clone https://github.com/yourusername/cinit.git - cd cinit - ``` - -2. Build the project: - ```bash - cargo build --release - ``` - -3. (Optional) Move the binary to a directory in your `$PATH`: - ```bash - mv target/release/cinit /usr/local/bin/ - ``` - -## Usage - -To see the list of available commands, run: - -```bash -cinit help -``` - -### Commands - -#### `c-new` -Create a new C project with the specified name. - -**Usage:** - -```bash -cinit c-new [OPTIONS] -``` - -**Arguments:** -- ``: Name of the new C project. - -**Options:** -- `--lib `: Creates a `lib` directory with files named `.c` and `.h`. - -**Example:** -```bash -cinit c-new my_project --lib my_library -``` - -This will create a directory `my_project` with the following structure: - -``` -my_project/ -├── src/ -├── include/ -├── lib/ -│ ├── my_library.c -│ └── my_library.h -└── bin/ -``` - -#### `build-c` -Compile all `.c` files in the `src/` directory and move the binary to the `bin/` directory. - -**Usage:** - -```bash -cinit build-c -``` - -**Description:** -- Looks for `.c` files in the `src/` directory. -- Compiles them using `gcc` with the `-Wall` flag. -- Places the compiled binary in the `bin/` directory. - -**Example:** -```bash -cinit build-c -``` - -#### `build-c-run-c` -Build and run your project in a single command. - -**Usage:** - -```bash -cinit build-c-run-c -``` - -**Description:** -- Builds the project as described in `build-c`. -- Immediately executes the resulting binary. - -#### `help` -Prints the help message for the main command or any subcommand. - -**Usage:** - -```bash -cinit help -``` - -**Example:** -To see help for a specific command, use: -```bash -cinit help c-new -``` - ---- - -## Example Workflow - -1. **Create a new project:** - ```bash - cinit c-new my_project - cd my_project - ``` - -2. **Build the project:** - ```bash - cinit build-c - ``` +- Automatic generation of the basic structure for a C project. -3. **Run the binary:** - ```bash - ./bin/main - ``` ---- \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4210766..762f4df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ struct Args { #[derive(Subcommand, Debug, Clone)] enum Commands { /// Create new C Proejct with name - CNew { + New { project_name: String, /// Create lib directory whith files named .c and .h @@ -26,9 +26,9 @@ enum Commands { }, /// Build ./src/*.c files and moved to ./bin/ - BuildC, + Build, /// Build and run project - BulidCRunC, + BulidRun, } #[derive(Clone)] @@ -40,7 +40,7 @@ impl Cinit { } } -fn c_new(project_name: &String, lib: Option) -> Result<(), Box> { +fn new(project_name: &String, lib: Option) -> Result<(), Box> { create_sub_directory(project_name, "src")?; if let Some(lib_file_name) = lib { @@ -54,7 +54,7 @@ fn c_new(project_name: &String, lib: Option) -> Result<(), Box Result<(), std::io::Error> { +fn build() -> Result<(), std::io::Error> { // Ejecutar GCC para compilar el programa let gcc_status = Command::new("gcc") .args(["src/main.c", "-Wall", "-o", "main"]) @@ -161,14 +161,14 @@ fn create_lib_folder( fn main() -> Result<(), Box> { if let Some(command) = Cinit::new().0.command { match command { - Commands::CNew { project_name, lib } => { - c_new(&project_name, lib)?; + Commands::New { project_name, lib } => { + new(&project_name, lib)?; } - Commands::BuildC => { - build_c()?; + Commands::Build => { + build()?; //println!("{:?}", std::env::current_dir()) } - Commands::BulidCRunC => eprintln!("Developing"), + Commands::BulidRun => todo!("Developing"), } }