Module: GemContribute::CLI

Defined in:
lib/gem_contribute/cli.rb,
lib/gem_contribute/cli/fix.rb,
lib/gem_contribute/cli/auth.rb,
lib/gem_contribute/cli/fork.rb,
lib/gem_contribute/cli/init.rb,
lib/gem_contribute/cli/scan.rb,
lib/gem_contribute/cli/config.rb,
lib/gem_contribute/cli/issues.rb,
lib/gem_contribute/cli/submit.rb,
lib/gem_contribute/cli/workflow.rb,
lib/gem_contribute/cli/platform_tools.rb,
lib/gem_contribute/cli/issue_announcer.rb,
lib/gem_contribute/cli/post_clone_hooks.rb,
lib/gem_contribute/cli/rate_limit_footer.rb

Defined Under Namespace

Modules: IssueAnnouncer, PlatformTools, RateLimitFooter, Workflow Classes: Auth, Config, Fix, Fork, Init, Issues, PostCloneHooks, Scan, Submit

Constant Summary collapse

USAGE =
<<~USAGE
  Usage: gem-contribute <command> [options]

  Commands:
    init                     One-time interactive setup (sets clone_root).
    scan [path]              Summarize the contributable surface of a Gemfile.lock.
                             Path defaults to ./Gemfile.lock.
    issues <gem|all>         List open "good first issue" issues for a gem (or all gems).
    config set <key> <val>   Persist a configuration value.
    config get <key>         Print a configuration value.
    config list              Print all configuration values.
    auth login               Authenticate with GitHub via OAuth device flow.
    auth status              Show whether you're authenticated.
    auth logout              Remove the cached token for github.com.
    fork <gem|owner/repo>    Fork (and clone) any GitHub repo. Pass a gem name
                             to look it up on RubyGems, or `owner/repo` for any
                             GitHub project (e.g. `rubyevents/rubyevents`).
                             Lands on the default branch.
                             Flags: -e (editor), -a (AI tool).
    fix <gem>/<issue#>       Fork the gem's repo, clone the fork, branch from main.
                             Flags: -e (editor), -a (AI tool), --no-comment.
    submit                   Push the current branch and open a pre-filled
                             PR compare page in the browser. Run from inside
                             a clone created by `fix`.

  Global options:
    --refresh                Invalidate caches before running.
    -h, --help               Show this help.
    --version                Print the version and exit.
USAGE
COMMANDS =
{
  "init" => ->(o, e) { Init.new(stdout: o, stderr: e) },
  "scan" => ->(o, e) { Scan.new(stdout: o, stderr: e, adapter: github_adapter) },
  "issues" => ->(o, e) { Issues.new(stdout: o, stderr: e, adapter: github_adapter) },
  "config" => ->(o, e) { Config.new(stdout: o, stderr: e) },
  "auth" => ->(o, e) { Auth.new(stdout: o, stderr: e) },
  "fork" => ->(o, e) { Fork.new(stdout: o, stderr: e, clone_root: GemContribute::Config.new.clone_root) },
  "fix" => lambda { |o, e|
    config = GemContribute::Config.new
    Fix.new(stdout: o, stderr: e, clone_root: config.clone_root, config: config)
  },
  "submit" => ->(o, e) { Submit.new(stdout: o, stderr: e) }
}.freeze

Class Method Summary collapse

Class Method Details

.dispatch(command, argv, stdout:, stderr:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/gem_contribute/cli.rb', line 48

def dispatch(command, argv, stdout:, stderr:)
  builder = COMMANDS[command]
  if builder.nil?
    return print_help(stdout) if [nil, "help", "-h", "--help"].include?(command)

    return unknown_command(command, stderr)
  end

  builder.call(stdout, stderr).run(argv)
end

.github_adapterObject



84
85
86
87
# File 'lib/gem_contribute/cli.rb', line 84

def github_adapter
  token = TokenStore.new.token_for("github.com")
  HostAdapters::GitHubAdapter.new(token: token)
end

.handle_global_flags!(argv, stdout:) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gem_contribute/cli.rb', line 89

def handle_global_flags!(argv, stdout:)
  if argv.include?("--version")
    stdout.puts "gem-contribute #{GemContribute::VERSION}"
    exit 0
  end

  return unless argv.delete("--refresh")

  Cache.new.clear!
  stdout.puts "Cache cleared at #{Cache.default_root}"
end


73
74
75
76
# File 'lib/gem_contribute/cli.rb', line 73

def print_help(stdout)
  stdout.puts USAGE
  0
end

.run(argv, stdout: $stdout, stderr: $stderr) ⇒ Object

Entry point for exe/gem-contribute. Returns an integer exit status so the caller can ‘exit GemContribute::CLI.run(ARGV)`.



42
43
44
45
46
# File 'lib/gem_contribute/cli.rb', line 42

def run(argv, stdout: $stdout, stderr: $stderr)
  argv = argv.dup
  handle_global_flags!(argv, stdout: stdout)
  dispatch(argv.shift, argv, stdout: stdout, stderr: stderr)
end

.unknown_command(command, stderr) ⇒ Object



78
79
80
81
82
# File 'lib/gem_contribute/cli.rb', line 78

def unknown_command(command, stderr)
  stderr.puts "gem-contribute: unknown command #{command.inspect}"
  stderr.puts USAGE
  2
end