Skip to content

Latest commit

 

History

History
117 lines (80 loc) · 3.36 KB

File metadata and controls

117 lines (80 loc) · 3.36 KB

mruby Pre-Commit Framework Implementation

Overview

Successfully implemented a lightweight, cross-platform pre-commit validation framework for mruby-based projects. This framework replaces the need for Python-based pre-commit tools and integrates seamlessly with the existing dotfiles system.

Files Created

Core Framework

  • lib/pre_commit.rb - Main framework module with Config and Runner classes
  • lib/pre_commit/validators/whitespace.rb - Whitespace cleaning validator
  • lib/pre_commit/validators/line_length.rb - Line length enforcement validator
  • lib/pre_commit/validators/syntax.rb - Basic Ruby syntax validator

Integration Scripts

  • bin/pre-commit - Git pre-commit hook script
  • bin/setup-pre-commit - Hook installation/uninstallation script
  • lib/pre_commit_example.rb - Usage examples
  • test_pre_commit.rb - Test script

Documentation

  • lib/pre_commit/README.md - Comprehensive documentation
  • PRE_COMMIT_FRAMEWORK.md - This summary document

Key Features Implemented

1. Cross-Platform Compatibility

  • Works on Windows, macOS, and Linux
  • Handles different line ending conventions
  • Platform-aware file operations

2. Built-in Validators

  • Whitespace Validator: Removes trailing whitespace, converts line endings, ensures final newline
  • Line Length Validator: Enforces maximum line length with smart exclusions
  • Syntax Validator: Basic Ruby syntax checking within mruby constraints

3. Extensible Architecture

  • Easy to add custom validators
  • Configuration-based approach
  • Automatic loading of .gitignore patterns

4. Git Integration

  • Seamless hook installation
  • Automatic staging area detection
  • Proper exit codes for git workflow

Usage

Installation

ruby bin/setup-pre-commit install

Manual Testing

ruby bin/pre-commit

Custom Configuration

PreCommit.configure do |config|
  config.add_validator(PreCommit::Validators::Whitespace.new)
  config.add_validator(PreCommit::Validators::LineLength.new(79))
  config.add_validator(PreCommit::Validators::Syntax.new)

  config.exclude('vendor/**/*')
  config.exclude('*.min.js')
end

Integration with Build System

This framework is designed to be part of the mruby-based build process:

  1. Pre-commit hooks - Automatic validation before commits
  2. Build validation - Can be called during build process
  3. CI/CD integration - Can be used in continuous integration

Test Results

The framework has been successfully tested:

  • ✅ Whitespace cleaning works correctly
  • ✅ Automatic fixes are applied
  • ✅ Error reporting functions properly
  • ✅ Cross-platform file operations work

Next Steps

  1. Integrate with build system - Add to the main mruby build process
  2. Add more validators - Consider adding validators for specific file types
  3. Configuration files - Support for .pre-commit-config files
  4. Performance optimization - Optimize for large codebases

Benefits Over Python pre-commit

  • Native mruby integration - No external dependencies
  • Lighter weight - Minimal resource usage
  • Better cross-platform support - No Python version conflicts
  • Seamless dotfiles integration - Part of the existing ecosystem

This framework successfully addresses the original requirement for a pre-commit system while maintaining the mruby-based architecture of the dotfiles project.