Reiki refactored

I’ve been putting in some time on side projects every morning while building up the motivation to work on more overwhelming things. Recently the focus was on Reiki, my tool for running Rake tasks with fuzzy matching and humane argument parsing.

[paywall The details]
### Multi-Shell Support

  • Automatic Shell Detection: Reiki now automatically detects your shell (Bash, Zsh, Fish, Ksh, Dash, Ash) when sourced and creates the appropriate wrapper function.
  • Native Integration: For Bash and Zsh, the script creates a native shell function that leverages all internal functionality for optimal performance.
  • Fish Shell Support: Dedicated Fish function wrapper with intelligent path detection.
  • Universal Compatibility: Falls back gracefully for unknown shells, ensuring broad compatibility.

Automatic Tab Completion

  • Zero Configuration: Tab completion is automatically enabled when you source r.bash in Bash or Zsh with no additional setup required.
  • Smart Cache Management: The completion function automatically finds your Rakefile (in current directory or git repository root) and manages the task cache.
  • Legacy-Free: The separate reiki.completion.bash file is no longer needed.

Backwards Compatibilitye

  • Bash 3.0+ Support: All code has been refactored to avoid Bash 4.x-only features (like mapfile), ensuring compatibility with older systems including legacy macOS installations.

Robust Argument Parsing

  • Colon in Quotes: The script now correctly parses colons (:) inside quoted arguments. This means you can run commands like r draft "Reiki: Updates and Refactoring" without worrying about misinterpretation. You can still run a task like rake test:auth without a colon, e.g. r t a or r test auth.
  • Exact Task Matching: I improved the matching logic so that task subtask (with a space) is now treated as an exact match for task:subtask, not just as a fuzzy match for task_subtask. This makes it easier to run nested or namespaced Rake tasks intuitively.

Modular and Maintainable Code

  • The script was refactored into more modular functions for argument parsing, task matching, command building, validation, and user interaction.
  • Error handling is now consistent and informative, with clear messages for invalid input, missing tasks, or command failures.
  • Better documentation for functions with clear descriptions, parameters, and return values.

Enhanced Matching and Retry Logic

  • If no matching task is found, the script automatically regenerates the completions cache and retries the match, reducing manual intervention.
  • Whitespace and formatting issues in task matching are now handled gracefully, preventing common Bash syntax errors.

Configuration via ~/.rrc and Environment Variables

  • You can now customize Reiki behavior through a configuration file at ~/.rrc (or custom path via $R_CONFIG). An example config file is provided in the repo.
  • Environment Variable Support: All settings can be overridden via environment variables:
    • R_CONFIG – Custom config file location
    • R_VERIFY_TASK – Force verification
    • R_AUTO_TIMEOUT – Verification timeout
    • R_DEBUG – Debug mode
    • R_QUIET – Quiet mode
    • R_BUNDLE – Use “bundle exec” prefix

Enhanced Cache Management

  • Cache Versioning: Cache files now include version markers to prevent compatibility issues.
  • Atomic Cache Updates: Cache regeneration uses atomic writes to prevent corruption.
  • Smart Validation: Checks Rakefile for changes before accepting cached data.

Sourced function or direct execution

Reiki also now intelligently determines whether it’s being sourced or executed directly:

  • When sourced: Sets up the appropriate shell wrapper function
  • When executed: Runs the task directly with all arguments

[endpaywall]

I should probably port this to Make at some point, but my Makefiles are never as complex as my Rakefiles. NPM already does a pretty good job of the kind of matching Reiki does.

Anyway, this refactor makes Reiki into a modern, multi-shell-compatible tool that works seamlessly across more environments. With automatic shell detection, zero-config tab completion, backwards compatibility to Bash 3.0, comprehensive configuration options, and robust error handling, you can work faster regardless of your shell preference.

Check it out on the project page.


Leave a Reply

Your email address will not be published. Required fields are marked *