class Ahnnotate::Cli

Public Class Methods

new(name:) click to toggle source
# File lib/ahnnotate/cli.rb, line 6
def initialize(name:)
  @name = name
  @options = Options.new(fix: false)
end

Public Instance Methods

run(argv, config = nil) click to toggle source
# File lib/ahnnotate/cli.rb, line 11
def run(argv, config = nil)
  argv =
    if argv.is_a?(String)
      require "shellwords"
      Shellwords.split(argv)
    else
      argv.dup
    end

  debug_options = argv.delete("--debug-opts") || argv.delete("--debug-options")

  parser.parse(argv)

  if debug_options
    puts @options
  end

  if @options.exit?
    return
  end

  root = Pathname.new(Dir.pwd)
  config ||= Config.load(root: root)

  if @options.remove?
    Function::Niam.new(root, @options, config).call
  else
    Function::Main.new(root, @options, config).call
  end
end

Private Instance Methods

parser() click to toggle source
# File lib/ahnnotate/cli.rb, line 44
    def parser
      if instance_variable_defined?(:@parser)
        return @parser
      end

      @parser = OptionParser.new do |opts|
        opts.banner = "Usage: #{@name} [options]"
        opts.separator ""
        opts.separator "Command line options:"
        opts.separator ""

        opts.on("--[no-]fix", "Actually modify files") do |fix|
          @options.fix = fix
        end

        opts.on("--remove", "Remove annotations") do |remove|
          @options.remove = remove
        end

        opts.on("-h", "--help", "Prints this help message") do
          @options.exit = true
          puts opts
        end

        opts.on("--version", "Print version") do
          @options.exit = true
          puts Ahnnotate::VERSION
        end

        opts.separator ""
        opts.separator ""
        opts.separator "Configuration file:"
        opts.separator ""

        # The gsub converts all non-consecutive newlines into a space.
        # Consecutive newlines are left alone.
        configuration_file_help = <<-MSG.gsub(/(?<!\n)\n(?!\n)/, " ")
The configuration file (`.ahnnotate.yml`) must be placed at the root of your
project, or wherever you will be calling this script. Any unset config option
will fall back to the following default configuration:

%{default}

In Rails projects (projects which explicitly use the `Rails` gem), ahnnotate
merges the following configs in with the defaults. This should allow ahnnotate
to work out of the box.

%{rails_additions}

(It should generally be possible to speed up the "boot" process by only loading
ActiveRecord, custom inflections, etc. Note though that the actual models do
not need to be loaded into the runtime; ahnnotate will read them as needed)
        MSG

        output = format(
          configuration_file_help,
          default: yaml_dump_and_indent(Ahnnotate::Config.default, indent: 4),
          rails_additions: yaml_dump_and_indent(Ahnnotate::Config.rails_additions, indent: 4)
        )

        opts.separator wrap_and_indent(text: output, width: 72 + 4, indent: 4)
      end

      @parser
    end
wrap_and_indent(text:, width:, indent:) click to toggle source
# File lib/ahnnotate/cli.rb, line 114
def wrap_and_indent(text:, width:, indent:)
  GemTextWrapper.format_text(text, width, indent)
end
yaml_dump_and_indent(object, indent:) click to toggle source
# File lib/ahnnotate/cli.rb, line 110
def yaml_dump_and_indent(object, indent:)
  YAML.dump(object).gsub(/^/, " " * indent)
end