Skip to content

Commit

Permalink
Feture/new command structure (#4)
Browse files Browse the repository at this point in the history
* Refactor new commands c-new c-build

* refactor file

* clippy refactor

* clippy format

* new subcommands
  • Loading branch information
CALEXCO authored Dec 3, 2024
1 parent 59193fb commit 35a8208
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 145 deletions.
138 changes: 3 additions & 135 deletions README.md
Original file line number Diff line number Diff line change
@@ -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] <PROJECT_NAME>
```

**Arguments:**
- `<PROJECT_NAME>`: Name of the new C project.

**Options:**
- `--lib <FILE>`: Creates a `lib` directory with files named `<FILE>.c` and `<FILE>.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
```

---
20 changes: 10 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <FILE>.c and <FILE>.h
Expand All @@ -26,9 +26,9 @@ enum Commands {
},

/// Build ./src/*.c files and moved to ./bin/
BuildC,
Build,
/// Build and run project
BulidCRunC,
BulidRun,
}

#[derive(Clone)]
Expand All @@ -40,7 +40,7 @@ impl Cinit {
}
}

fn c_new(project_name: &String, lib: Option<String>) -> Result<(), Box<dyn std::error::Error>> {
fn new(project_name: &String, lib: Option<String>) -> Result<(), Box<dyn std::error::Error>> {
create_sub_directory(project_name, "src")?;

if let Some(lib_file_name) = lib {
Expand All @@ -54,7 +54,7 @@ fn c_new(project_name: &String, lib: Option<String>) -> Result<(), Box<dyn std::
Ok(())
}

fn build_c() -> 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"])
Expand Down Expand Up @@ -161,14 +161,14 @@ fn create_lib_folder(
fn main() -> Result<(), Box<dyn std::error::Error>> {
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"),
}
}

Expand Down

0 comments on commit 35a8208

Please sign in to comment.