Skip to content
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
159 changes: 159 additions & 0 deletions IMPROVEMENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# Project Improvements Summary

This document summarizes the major improvements made to the `initcpp.sh` C++ project initialization script.

## 🚨 Critical Issues Fixed

### 1. GTest Integration Failure
**Problem**: Script generated CMakeLists.txt files that referenced `GTest::GTest` and `GTest::Main` targets even when GTest wasn't available, causing build failures.

**Solution**:
- Added proper GTest detection with `find_package(GTest QUIET)`
- Added graceful fallback when GTest is not found
- Tests are only enabled when GTest is actually available
- Clear warning messages inform users about missing GTest

### 2. Command-Line Parsing Bug
**Problem**: The script had a `-c` option in the `getopts` string but no handler in the case statement, causing parsing errors.

**Solution**:
- Added proper `-c` option handler for C++ standard selection
- Updated usage documentation to reflect the new option
- Added examples showing how to use different C++ standards

### 3. Directory Validation Issue
**Problem**: Script checked if directory was empty using `ls -A ${PROJECT_DIR}` but failed when the directory didn't exist yet.

**Solution**:
- Added proper existence check before testing if directory is empty
- Improved error messages with clearer feedback
- Added proper error handling with `/dev/null` redirection

### 4. Non-Standard Header Usage
**Problem**: Used `stdafx.hpp` which is Microsoft Visual Studio specific and not standard C++.

**Solution**:
- Replaced with `common.hpp` following standard C++ practices
- Added common standard library includes
- Improved header organization and documentation

## πŸ”§ Major Refactoring Improvements

### 1. Sanitizer Flag Organization
**Before**: Extremely long, unreadable lines with all sanitizer flags concatenated
**After**: Organized flags in CMake lists for better maintainability and readability

### 2. Cross-Platform Package Manager Support
**Before**: Only supported `apt-get` (Ubuntu/Debian)
**After**: Added support for:
- `apt-get` (Ubuntu/Debian)
- `dnf` (Fedora/RHEL)
- `pacman` (Arch Linux)

### 3. Enhanced Error Handling
**Improvements**:
- Added project name validation function
- Better error messages throughout the script
- Proper exit codes and error handling
- Warning messages for potential issues

### 4. Code Organization
**Improvements**:
- Broke large script into logical functions
- Added helper functions for validation
- Better separation of concerns
- Improved code readability and maintainability

### 5. Modern Git Integration
**Improvements**:
- Initialize with `main` branch by default
- Automatic commit with descriptive message
- Proper fallback for older Git versions
- Better Git configuration handling

### 6. Enhanced User Experience
**Improvements**:
- Clear success messages with emojis
- Step-by-step instructions after project creation
- Helpful examples in usage documentation
- Better feedback during script execution

## πŸ“š Documentation Improvements

### 1. README.md Enhancements
- Added comprehensive feature list with emojis
- Detailed usage examples
- Complete command-line option documentation
- Project structure visualization
- "What's New" section highlighting improvements

### 2. Improved CLI Help
- Detailed help message with all options
- Multiple usage examples
- Clear parameter descriptions
- Better formatting and readability

## πŸ§ͺ Testing and Validation

### Tests Performed
1. βœ… Basic project creation with default settings
2. βœ… C++ standard selection (`-c` option)
3. βœ… Custom directory path (`-p` option)
4. βœ… Help functionality (`-h` option)
5. βœ… Project builds and runs successfully
6. βœ… Git initialization works properly
7. βœ… Error handling for invalid inputs
8. βœ… Script syntax validation

### Edge Cases Tested
- Projects with special characters in names
- Non-existent directories
- Missing dependencies (GTest)
- Different C++ standards (17, 20)
- Various command-line option combinations

## 🎯 Benefits of Improvements

### For Users
- **Reliability**: Script no longer fails on missing dependencies
- **Flexibility**: Support for different C++ standards and platforms
- **Clarity**: Better error messages and success feedback
- **Guidance**: Clear next steps after project creation

### For Maintainers
- **Readability**: Better organized and documented code
- **Maintainability**: Modular functions and clear structure
- **Extensibility**: Easy to add new features and options
- **Standards**: Follows modern shell scripting best practices

## πŸ“Š Script Statistics

### Before Improvements
- Lines of code: ~313
- Functions: 2 (usage, install_deps)
- Platform support: Ubuntu/Debian only
- Error handling: Minimal
- Documentation: Basic

### After Improvements
- Lines of code: ~380+ (better organized)
- Functions: 4 (usage, validate_project_name, install_deps, + main logic)
- Platform support: Multiple Linux distributions
- Error handling: Comprehensive
- Documentation: Extensive with examples

## πŸš€ Future Improvement Opportunities

While the current improvements significantly enhance the script, potential future enhancements could include:

1. **Configuration Files**: Support for project templates and configuration files
2. **More Build Systems**: Support for Bazel, Meson, etc.
3. **IDE Integration**: Generate VS Code/CLion configurations
4. **Package Managers**: Support for Conan, vcpkg integration
5. **Testing Frameworks**: Support for Catch2, doctest alternatives
6. **CI/CD**: Generate GitHub Actions, GitLab CI configurations
7. **Windows Support**: PowerShell version for Windows users

## ✨ Conclusion

These improvements transform the script from a basic project generator into a robust, professional-grade tool that follows modern C++ and shell scripting best practices. The script now provides a much better user experience while being more maintainable and extensible for future enhancements.
71 changes: 66 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ When starting a new C++ project, certain tasks are repetitive and time-consuming
As someone who regularly studies C++ and creates new projects, I understand the importance of being able to focus on the code rather than the project structure. That is why I created this script to automate the process of creating a new project.

### Features
- Create a project directory structure
- Generate CMakeLists.txt file
- By default sanitize and enable warnings in the CMakeLists.txt file
- Add a sample test file
- **πŸ—οΈ Automated Project Setup**: Create complete C++ project directory structure
- **βš™οΈ Modern CMake Configuration**: Generate optimized CMakeLists.txt with best practices
- **πŸ›‘οΈ Security & Quality**: Enable sanitizers and comprehensive warning flags by default
- **βœ… Testing Ready**: Automatic GTest integration when available
- **πŸ”§ Configurable**: Support for different C++ standards and custom project paths
- **🌐 Cross-Platform**: Support for multiple Linux package managers (apt, dnf, pacman)
- **πŸ“‹ Modern Git Integration**: Initialize with main branch and proper .gitignore

Enabling sanitizers and warning flags in C++ offers significant benefits. Sanitizers detect common errors reducing security vulnerabilities. Warning flags promote best practices and code quality. By incorporating these tools, developers can enhance the security, reliability, and maintainability of their C++ codebases.
Since sanitizers run at runtime, it is essential to have automated tests in the codebase.
Expand Down Expand Up @@ -49,8 +52,66 @@ cp initcpp.sh ~/.local/bin/initcpp
```

### Usage
To create a new C++ project, run the script with the project name as an argument. The script will create a new directory with the project name and set up the project structure and files.
Create a new C++ project with the project name as an argument:

```bash
initcpp <project-name>
```

#### Command Line Options

- `-h`: Display help message with usage examples
- `-c <standard>`: Set C++ standard version (11, 14, 17, 20, 23) - default is 20
- `-p <path>`: Custom directory path for the project
- `-i`: Install required dependencies (cmake, git, ninja, gtest)
- `-f`: Force script to continue even on errors
- `-d`: Enable debug mode with verbose output

#### Examples

```bash
# Create project with default settings (C++20)
initcpp my_awesome_project

# Create project with C++17 standard
initcpp -c 17 legacy_project

# Create project in custom directory
initcpp -p /home/user/projects/my_project awesome_project

# Install dependencies first, then create project
initcpp -i
initcpp my_project

# Create project with debug output
initcpp -d debug_project
```

#### Project Structure

The script creates the following structure:
```
my_project/
β”œβ”€β”€ CMakeLists.txt # Main build configuration
β”œβ”€β”€ .gitignore # Git ignore patterns
β”œβ”€β”€ include/
β”‚ └── common.hpp # Common headers
β”œβ”€β”€ src/
β”‚ β”œβ”€β”€ main.cpp # Application entry point
β”‚ └── lib.cpp # Library implementation
β”œβ”€β”€ tests/
β”‚ β”œβ”€β”€ CMakeLists.txt # Test configuration
β”‚ └── test.cpp # Sample tests (if GTest available)
└── build/ # Build directory (configured)
```

### What's New

Recent improvements include:
- **πŸ”§ Enhanced CLI**: Better command-line parsing with C++ standard selection
- **πŸ› οΈ Cross-Platform**: Support for multiple Linux distributions (Ubuntu/Debian, Fedora/RHEL, Arch Linux)
- **πŸ—οΈ Robust Build**: Improved CMake configuration with better error handling
- **πŸ“š Better Documentation**: Comprehensive README with examples and project structure
- **πŸ§ͺ Smart Testing**: Automatic GTest detection with graceful fallback
- **🎯 Modern Practices**: Main branch initialization and organized sanitizer flags
- **⚑ Better UX**: Clear success messages and next-step instructions
Loading