gempilot 0.3.5 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.claude/skills/using-command-kit/generator-pattern.rb +2 -2
- data/.ruby-version +1 -1
- data/CLAUDE.md +12 -0
- data/Rakefile +3 -2
- data/data/templates/gem/Gemfile.erb +1 -0
- data/data/templates/gem/Rakefile.erb +5 -2
- data/data/templates/gem/bin/setup.erb +1 -0
- data/data/templates/gem/dotfiles/github/workflows/ci.yml.erb +2 -0
- data/data/templates/gem/dotfiles/gitignore +1 -0
- data/lib/gempilot/cli/commands/bump.rb +4 -5
- data/lib/gempilot/cli/commands/console.rb +1 -3
- data/lib/gempilot/cli/commands/create.rb +32 -165
- data/lib/gempilot/cli/commands/destroy.rb +9 -8
- data/lib/gempilot/cli/commands/release.rb +5 -9
- data/lib/gempilot/cli/gem_builder.rb +2 -31
- data/lib/gempilot/cli/gem_scaffold.rb +188 -0
- data/lib/gempilot/cli/generator.rb +11 -11
- data/lib/gempilot/github_release.rb +8 -10
- data/lib/gempilot/origin.rb +6 -10
- data/lib/gempilot/rbs_task.rb +87 -0
- data/lib/gempilot/shell/command_failed.rb +17 -0
- data/lib/gempilot/shell/gateway.rb +45 -0
- data/lib/gempilot/shell/invocation.rb +16 -0
- data/lib/gempilot/shell/result.rb +14 -0
- data/lib/gempilot/shell/status.rb +20 -0
- data/lib/gempilot/shell/stub_gateway.rb +65 -0
- data/lib/gempilot/shell/unregistered_command.rb +23 -0
- data/lib/gempilot/shell.rb +66 -0
- data/lib/gempilot/version.rb +1 -1
- data/lib/gempilot/version_tag.rb +13 -17
- data/lib/gempilot/zeitwerk_task.rb +2 -2
- data/lib/gempilot.rb +1 -3
- data/rbs_collection.lock.yaml +188 -0
- data/rbs_collection.yaml +19 -0
- metadata +14 -3
- data/lib/gempilot/strict_shell.rb +0 -18
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
module Gempilot
|
|
2
|
+
class CLI
|
|
3
|
+
## Writes a new gem's files from the same options as +gempilot create+,
|
|
4
|
+
## without running any commands.
|
|
5
|
+
##
|
|
6
|
+
## Gempilot::CLI::GemScaffold.new.main(["--test", "rspec", "--no-git", "my_gem"])
|
|
7
|
+
## # create my_gem/my_gem.gemspec
|
|
8
|
+
## # create my_gem/Gemfile
|
|
9
|
+
## # ...
|
|
10
|
+
class GemScaffold < Command
|
|
11
|
+
using String::Inflectable
|
|
12
|
+
|
|
13
|
+
include Generator
|
|
14
|
+
include GemBuilder
|
|
15
|
+
|
|
16
|
+
template_dir File.join(Gempilot::ROOT, "data", "templates", "gem")
|
|
17
|
+
|
|
18
|
+
option :summary, value: { type: String },
|
|
19
|
+
desc: "Gem summary"
|
|
20
|
+
|
|
21
|
+
option :author, value: {
|
|
22
|
+
type: String,
|
|
23
|
+
default: -> { Shell.capture("git", "config", "user.name").output.strip },
|
|
24
|
+
},
|
|
25
|
+
desc: "Author name"
|
|
26
|
+
|
|
27
|
+
option :email, value: {
|
|
28
|
+
type: String,
|
|
29
|
+
default: -> { Shell.capture("git", "config", "user.email").output.strip },
|
|
30
|
+
},
|
|
31
|
+
desc: "Author email"
|
|
32
|
+
|
|
33
|
+
option :ruby_version, value: {
|
|
34
|
+
type: String,
|
|
35
|
+
default: RUBY_VERSION,
|
|
36
|
+
},
|
|
37
|
+
desc: "Minimum Ruby version"
|
|
38
|
+
|
|
39
|
+
option :test, value: {
|
|
40
|
+
type: { "minitest" => :minitest, "rspec" => :rspec },
|
|
41
|
+
},
|
|
42
|
+
desc: "Test framework"
|
|
43
|
+
|
|
44
|
+
option :exe, long: "--[no-]exe", desc: "Create an executable"
|
|
45
|
+
|
|
46
|
+
option :git, long: "--[no-]git", desc: "Initialize git repo"
|
|
47
|
+
|
|
48
|
+
option :branch, value: { type: String },
|
|
49
|
+
desc: "Git branch name"
|
|
50
|
+
|
|
51
|
+
argument :gem_name, required: false,
|
|
52
|
+
desc: "Name of the gem"
|
|
53
|
+
|
|
54
|
+
def run(gem_name = nil)
|
|
55
|
+
puts colors.bold("Creating gem...")
|
|
56
|
+
puts
|
|
57
|
+
collect_options(gem_name)
|
|
58
|
+
print_header
|
|
59
|
+
render_gem
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def collect_options(gem_name)
|
|
65
|
+
collect_gem_name(gem_name)
|
|
66
|
+
derive_naming
|
|
67
|
+
collect_author_info
|
|
68
|
+
collect_summary
|
|
69
|
+
collect_build_options
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def render_gem
|
|
73
|
+
create_directories
|
|
74
|
+
render_core_templates
|
|
75
|
+
render_test_templates
|
|
76
|
+
render_dev_files
|
|
77
|
+
render_config_files
|
|
78
|
+
render_executable
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
GEM_NAME_PATTERN = /\A[a-z][a-z0-9]*([_-][a-z0-9]+)*\z/
|
|
82
|
+
private_constant :GEM_NAME_PATTERN
|
|
83
|
+
|
|
84
|
+
def collect_gem_name(gem_name)
|
|
85
|
+
@gem_name = gem_name || begin
|
|
86
|
+
puts colors.bright_black("The gem name determines the directory, require path, and gemspec name.")
|
|
87
|
+
ask(colors.green("Gem name"), required: true)
|
|
88
|
+
end
|
|
89
|
+
validate_gem_name
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def validate_gem_name
|
|
93
|
+
return if @gem_name.match?(GEM_NAME_PATTERN)
|
|
94
|
+
|
|
95
|
+
puts colors.red("Invalid gem name '#{@gem_name}'.")
|
|
96
|
+
puts colors.red("Use lowercase letters, digits, hyphens, or underscores (e.g., my_gem or my-gem).")
|
|
97
|
+
exit 1
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def derive_naming
|
|
101
|
+
@require_path = @gem_name.tr("-", "/")
|
|
102
|
+
@module_name = @require_path.camelize
|
|
103
|
+
@module_parts = @module_name.split("::")
|
|
104
|
+
@base_module = @module_parts.first
|
|
105
|
+
@hyphenated = @gem_name.include?("-")
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def collect_author_info
|
|
109
|
+
@author = option_or_ask(:author, "The author name for the gemspec and LICENSE.", "Author name")
|
|
110
|
+
@email = option_or_ask(:email, "The contact email for the gemspec.", "Author email")
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def option_or_ask(key, hint, label)
|
|
114
|
+
options[key].then { |v| v.to_s.empty? ? nil : v } || begin
|
|
115
|
+
puts colors.bright_black(hint)
|
|
116
|
+
ask(colors.green(label), required: true)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def collect_summary
|
|
121
|
+
@summary = options[:summary] || begin
|
|
122
|
+
puts
|
|
123
|
+
puts colors.bright_black("A short (one line) summary of what your gem does.")
|
|
124
|
+
ask(colors.green("Summary"), default: "A new Ruby gem")
|
|
125
|
+
end
|
|
126
|
+
@ruby_version = options[:ruby_version]
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def collect_build_options
|
|
130
|
+
collect_test_framework
|
|
131
|
+
collect_exe_option
|
|
132
|
+
collect_git_options
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def collect_test_framework
|
|
136
|
+
@test_framework = options[:test] || begin
|
|
137
|
+
puts
|
|
138
|
+
puts colors.bright_black("Choose a test framework for your gem:")
|
|
139
|
+
puts " * #{colors.bold("Minitest")} - https://github.com/minitest/minitest"
|
|
140
|
+
puts " * #{colors.bold("RSpec")} - https://rspec.info"
|
|
141
|
+
ask_multiple_choice(colors.green("Test framework"), { "minitest" => :minitest, "rspec" => :rspec })
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def collect_exe_option
|
|
146
|
+
return if options.key?(:exe)
|
|
147
|
+
|
|
148
|
+
puts
|
|
149
|
+
puts colors.bright_black("An executable in exe/ lets users run your gem from the command line.")
|
|
150
|
+
prompt = colors.green("Create an executable")
|
|
151
|
+
options[:exe] = ask_yes_or_no(prompt, default: false)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def collect_git_options
|
|
155
|
+
collect_git_flag
|
|
156
|
+
collect_branch if options[:git]
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def collect_git_flag
|
|
160
|
+
return if options.key?(:git)
|
|
161
|
+
|
|
162
|
+
puts
|
|
163
|
+
puts colors.bright_black("Initialize a git repository with an initial commit.")
|
|
164
|
+
prompt = colors.green("Initialize git repo")
|
|
165
|
+
options[:git] = ask_yes_or_no(prompt, default: true)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def collect_branch
|
|
169
|
+
@branch = options[:branch] || begin
|
|
170
|
+
puts
|
|
171
|
+
puts colors.bright_black("The default branch name for the new git repository.")
|
|
172
|
+
ask(colors.green("Branch name"), default: "master")
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def print_header
|
|
177
|
+
styled_name = colors.bold(colors.cyan(@gem_name))
|
|
178
|
+
puts
|
|
179
|
+
puts colors.bright_white("Creating gem '") + styled_name + colors.bright_white("'...")
|
|
180
|
+
puts
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def minor_version_for(version)
|
|
184
|
+
version.split(".")[0..1].join(".")
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
@@ -48,12 +48,12 @@ module Gempilot
|
|
|
48
48
|
|
|
49
49
|
def mkdir(path)
|
|
50
50
|
print_action "mkdir", path
|
|
51
|
-
|
|
51
|
+
mkdir_p(path)
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
def touch(path)
|
|
55
55
|
print_action "touch", path
|
|
56
|
-
|
|
56
|
+
super
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
def create_file(path, content)
|
|
@@ -63,12 +63,12 @@ module Gempilot
|
|
|
63
63
|
|
|
64
64
|
def chmod(mode, path)
|
|
65
65
|
print_action "chmod", path
|
|
66
|
-
|
|
66
|
+
super
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
def cp(source, dest)
|
|
70
70
|
print_action "cp", dest, source: source
|
|
71
|
-
|
|
71
|
+
super(File.join(@template_dir, source), dest)
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
def erb(source, dest = nil)
|
|
@@ -78,13 +78,13 @@ module Gempilot
|
|
|
78
78
|
super(source_path, dest)
|
|
79
79
|
end
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
81
|
+
## Prints and runs a command outside gempilot's bundle.
|
|
82
|
+
##
|
|
83
|
+
## run_command "bin/setup"
|
|
84
|
+
## # run bin/setup
|
|
85
|
+
def run_command(*command)
|
|
86
|
+
print_action "run", command.join(" ")
|
|
87
|
+
Shell.run(*command, unbundled: true)
|
|
88
88
|
end
|
|
89
89
|
end
|
|
90
90
|
end
|
|
@@ -2,8 +2,6 @@ module Gempilot
|
|
|
2
2
|
## Manages GitHub releases for a version tag. Tags naming a prerelease
|
|
3
3
|
## version (e.g. +v1.2.4.dev1+) are created as GitHub prereleases.
|
|
4
4
|
class GithubRelease
|
|
5
|
-
include StrictShell
|
|
6
|
-
|
|
7
5
|
attr_reader :tag
|
|
8
6
|
|
|
9
7
|
def initialize(tag)
|
|
@@ -11,20 +9,20 @@ module Gempilot
|
|
|
11
9
|
end
|
|
12
10
|
|
|
13
11
|
def create
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
Shell.run("gh", "release", "create",
|
|
13
|
+
"--generate-notes", "--fail-on-no-commits",
|
|
14
|
+
*prerelease_flag,
|
|
15
|
+
tag)
|
|
18
16
|
end
|
|
19
17
|
|
|
20
18
|
def destroy
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
Shell.run("gh", "release", "delete",
|
|
20
|
+
"--yes", "--cleanup-tag",
|
|
21
|
+
tag)
|
|
24
22
|
end
|
|
25
23
|
|
|
26
24
|
def list
|
|
27
|
-
|
|
25
|
+
Shell.run("gh", "release", "list")
|
|
28
26
|
end
|
|
29
27
|
|
|
30
28
|
private
|
data/lib/gempilot/origin.rb
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
require "open3"
|
|
2
|
-
|
|
3
1
|
module Gempilot
|
|
4
2
|
## Pushes the current branch and a release tag to the branch's git remote.
|
|
5
3
|
## Backs the +release:source_control_push+ task. Idempotent: pushing an
|
|
@@ -9,8 +7,6 @@ module Gempilot
|
|
|
9
7
|
## HEAD, such as a CI checkout of the tag, there is no branch to push, so
|
|
10
8
|
## only the tag goes to +origin+.
|
|
11
9
|
class Origin
|
|
12
|
-
include StrictShell
|
|
13
|
-
|
|
14
10
|
attr_reader :tag
|
|
15
11
|
|
|
16
12
|
def initialize(tag)
|
|
@@ -18,8 +14,8 @@ module Gempilot
|
|
|
18
14
|
end
|
|
19
15
|
|
|
20
16
|
def push
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
Shell.run("git", "push", remote, "refs/heads/#{branch}") if branch
|
|
18
|
+
Shell.run("git", "push", remote, "refs/tags/#{tag}")
|
|
23
19
|
end
|
|
24
20
|
|
|
25
21
|
private
|
|
@@ -31,8 +27,8 @@ module Gempilot
|
|
|
31
27
|
end
|
|
32
28
|
|
|
33
29
|
def attached_branch
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
result = Shell.capture("git", "symbolic-ref", "--quiet", "--short", "HEAD")
|
|
31
|
+
result.output.strip if result.success?
|
|
36
32
|
end
|
|
37
33
|
|
|
38
34
|
def remote
|
|
@@ -42,8 +38,8 @@ module Gempilot
|
|
|
42
38
|
def configured_remote
|
|
43
39
|
return unless branch
|
|
44
40
|
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
result = Shell.capture("git", "config", "--get", "branch.#{branch}.remote")
|
|
42
|
+
result.output.strip if result.success?
|
|
47
43
|
end
|
|
48
44
|
end
|
|
49
45
|
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
require "rake/clean"
|
|
2
|
+
require "rake/tasklib"
|
|
3
|
+
require_relative "../gempilot"
|
|
4
|
+
|
|
5
|
+
module Gempilot
|
|
6
|
+
## Defines rake tasks that install and validate a gem's RBS collection.
|
|
7
|
+
##
|
|
8
|
+
## # Rakefile
|
|
9
|
+
## require "gempilot/rbs_task"
|
|
10
|
+
## Gempilot::RBSTask.new
|
|
11
|
+
##
|
|
12
|
+
## $ rake rbs:validate
|
|
13
|
+
## # It's done! 27 gems' RBSs now installed.
|
|
14
|
+
class RBSTask < Rake::TaskLib
|
|
15
|
+
attr_reader :root
|
|
16
|
+
|
|
17
|
+
def initialize(root: Dir.pwd)
|
|
18
|
+
super()
|
|
19
|
+
@root = Pathname(root)
|
|
20
|
+
define_tasks
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def collection_yaml = root / "rbs_collection.yaml"
|
|
24
|
+
|
|
25
|
+
def collection_dir = root / ".gem_rbs_collection"
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def define_tasks
|
|
30
|
+
namespace :rbs do
|
|
31
|
+
define_setup_task
|
|
32
|
+
define_clobber_task
|
|
33
|
+
define_validate_task
|
|
34
|
+
namespace(:collection) { define_collection_tasks }
|
|
35
|
+
end
|
|
36
|
+
define_top_level_hooks
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def define_top_level_hooks
|
|
40
|
+
desc "Prepare the development environment" unless Rake::Task.task_defined?(:setup)
|
|
41
|
+
task setup: ["rbs:setup"]
|
|
42
|
+
task clobber: ["rbs:clobber"]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def define_setup_task
|
|
46
|
+
desc "Install the rbs collection rbs_collection.lock.yaml pins"
|
|
47
|
+
task setup: ["rbs:collection:install"]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def define_clobber_task
|
|
51
|
+
dir = collection_dir
|
|
52
|
+
desc "Remove the installed rbs collection"
|
|
53
|
+
task(:clobber) { rm_rf dir }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def define_validate_task
|
|
57
|
+
desc "Validate sig/ against the installed collection"
|
|
58
|
+
task(validate: ["rbs:setup"]) { rbs("-I", "sig", "validate") }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def define_collection_tasks
|
|
62
|
+
define_init_task
|
|
63
|
+
define_collection_command_task(:install, "Install the gem RBS files the collection lists",
|
|
64
|
+
["rbs:collection:init"])
|
|
65
|
+
define_collection_command_task(:update, "Re-resolve the collection and rewrite rbs_collection.lock.yaml")
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def define_init_task
|
|
69
|
+
file(collection_yaml) { rbs_collection "init" }
|
|
70
|
+
desc "Write rbs_collection.yaml if it is missing"
|
|
71
|
+
task init: [collection_yaml]
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def define_collection_command_task(name, description, prerequisites = [])
|
|
75
|
+
desc description
|
|
76
|
+
task(name => prerequisites) { rbs_collection name.to_s }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def rbs_collection(subcommand)
|
|
80
|
+
rbs("collection", subcommand)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def rbs(*args)
|
|
84
|
+
Shell.run("bundle", "exec", "rbs", "--collection", collection_yaml.to_s, *args, chdir: root)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module Gempilot
|
|
2
|
+
module Shell
|
|
3
|
+
## Raised by Shell.run when a command fails.
|
|
4
|
+
##
|
|
5
|
+
## Gempilot::Shell.run("git", "checkout", "no-such-branch")
|
|
6
|
+
## # Gempilot::Shell::CommandFailed: Command failed with status (1): [git checkout no-such-branch]
|
|
7
|
+
class CommandFailed < Error
|
|
8
|
+
attr_reader :invocation, :status
|
|
9
|
+
|
|
10
|
+
def initialize(invocation, status)
|
|
11
|
+
@invocation = invocation
|
|
12
|
+
@status = status
|
|
13
|
+
super("Command failed with status (#{status.exitstatus.inspect}): [#{invocation}]")
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require "bundler"
|
|
2
|
+
require "open3"
|
|
3
|
+
|
|
4
|
+
module Gempilot
|
|
5
|
+
module Shell
|
|
6
|
+
## Runs commands as real processes. Shell uses it unless a test replaces it.
|
|
7
|
+
class Gateway
|
|
8
|
+
def run(invocation)
|
|
9
|
+
in_environment(invocation) do
|
|
10
|
+
begin
|
|
11
|
+
pid = Process.spawn(invocation.env, *invocation.command, **spawn_options(invocation))
|
|
12
|
+
Status.from_process(Process.wait2(pid).last)
|
|
13
|
+
rescue SystemCallError
|
|
14
|
+
Status.exited(127)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def capture(invocation)
|
|
20
|
+
in_environment(invocation) do
|
|
21
|
+
begin
|
|
22
|
+
output, status = Open3.capture2(invocation.env, *invocation.command, **spawn_options(invocation))
|
|
23
|
+
Result.new(output:, status: Status.from_process(status))
|
|
24
|
+
rescue SystemCallError
|
|
25
|
+
Result.new(output: "", status: Status.exited(127))
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def exec(invocation)
|
|
31
|
+
in_environment(invocation) { Kernel.exec(invocation.env, *invocation.command, **spawn_options(invocation)) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def in_environment(invocation, &)
|
|
37
|
+
invocation.unbundled ? Bundler.with_unbundled_env(&) : yield
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def spawn_options(invocation)
|
|
41
|
+
invocation.chdir ? { chdir: invocation.chdir.to_s } : {}
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require "shellwords"
|
|
2
|
+
|
|
3
|
+
module Gempilot
|
|
4
|
+
module Shell
|
|
5
|
+
## A command and how to run it.
|
|
6
|
+
##
|
|
7
|
+
## Gempilot::Shell::Invocation.new(command: ["git", "commit", "-m", "Bump version"],
|
|
8
|
+
## chdir: nil, env: {}, unbundled: false).to_s
|
|
9
|
+
## # => "git commit -m Bump\\ version"
|
|
10
|
+
Invocation = Data.define(:command, :chdir, :env, :unbundled) do
|
|
11
|
+
def to_s
|
|
12
|
+
command.shelljoin
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module Gempilot
|
|
2
|
+
module Shell
|
|
3
|
+
## A captured command's standard output and exit status.
|
|
4
|
+
##
|
|
5
|
+
## result = Gempilot::Shell.capture("git", "config", "--get", "no.such.key")
|
|
6
|
+
## result.output # => ""
|
|
7
|
+
## result.success? # => false
|
|
8
|
+
Result = Data.define(:output, :status) do
|
|
9
|
+
def success?
|
|
10
|
+
status.success?
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Gempilot
|
|
2
|
+
module Shell
|
|
3
|
+
## How a command exited.
|
|
4
|
+
##
|
|
5
|
+
## Gempilot::Shell::Status.exited(1).success? # => false
|
|
6
|
+
Status = Data.define(:exitstatus, :success) do
|
|
7
|
+
def self.exited(exitstatus)
|
|
8
|
+
new(exitstatus:, success: exitstatus.zero?)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.from_process(status)
|
|
12
|
+
new(exitstatus: status.exitstatus, success: status.success?)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def success?
|
|
16
|
+
success
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
module Gempilot
|
|
2
|
+
module Shell
|
|
3
|
+
## Fakes commands in tests, the way WebMock fakes HTTP. Stubbed commands
|
|
4
|
+
## return what you give them; any other command raises UnregisteredCommand.
|
|
5
|
+
##
|
|
6
|
+
## stubs = Gempilot::Shell::StubGateway.new
|
|
7
|
+
## stubs.stub("gh", "release", "create", "v1.0.0").to_return(exitstatus: 0)
|
|
8
|
+
##
|
|
9
|
+
## Gempilot::Shell.with_gateway(stubs) { Gempilot::GithubRelease.new("v1.0.0").create }
|
|
10
|
+
## stubs.ran?("gh", "release", "create", "v1.0.0") # => true
|
|
11
|
+
class StubGateway
|
|
12
|
+
## A stubbed command's result. Succeeds with no output unless given +to_return+.
|
|
13
|
+
##
|
|
14
|
+
## stubs.stub("bundle", "exec", "rake", "release").to_return(output: "", exitstatus: 1)
|
|
15
|
+
class Stub
|
|
16
|
+
attr_reader :result
|
|
17
|
+
|
|
18
|
+
def initialize
|
|
19
|
+
to_return
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def to_return(output: "", exitstatus: 0)
|
|
23
|
+
@result = Result.new(output:, status: Status.exited(exitstatus))
|
|
24
|
+
self
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
attr_reader :invocations
|
|
29
|
+
|
|
30
|
+
def initialize
|
|
31
|
+
@stubs = {}
|
|
32
|
+
@invocations = []
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def stub(*command)
|
|
36
|
+
@stubs[command] = Stub.new
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def ran?(*command)
|
|
40
|
+
invocations.any? { it.command == command }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def run(invocation)
|
|
44
|
+
result = respond_to(invocation)
|
|
45
|
+
$stdout.write(result.output)
|
|
46
|
+
result.status
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def capture(invocation)
|
|
50
|
+
respond_to(invocation)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def exec(invocation)
|
|
54
|
+
respond_to(invocation).status
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def respond_to(invocation)
|
|
60
|
+
invocations << invocation
|
|
61
|
+
@stubs.fetch(invocation.command) { raise UnregisteredCommand, invocation }.result
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Gempilot
|
|
2
|
+
module Shell
|
|
3
|
+
## Raised by a StubGateway when a command was not stubbed.
|
|
4
|
+
##
|
|
5
|
+
## Gempilot::Shell.with_gateway(Gempilot::Shell::StubGateway.new) do
|
|
6
|
+
## Gempilot::Shell.run("gh", "release", "create", "v1.0.1")
|
|
7
|
+
## end
|
|
8
|
+
## # Gempilot::Shell::UnregisteredCommand: Unregistered command: [gh release create v1.0.1]
|
|
9
|
+
## #
|
|
10
|
+
## # Stub it with:
|
|
11
|
+
## #
|
|
12
|
+
## # stub("gh", "release", "create", "v1.0.1")
|
|
13
|
+
class UnregisteredCommand < Error
|
|
14
|
+
attr_reader :invocation
|
|
15
|
+
|
|
16
|
+
def initialize(invocation)
|
|
17
|
+
@invocation = invocation
|
|
18
|
+
stub_call = "stub(#{invocation.command.map(&:inspect).join(", ")})"
|
|
19
|
+
super("Unregistered command: [#{invocation}]\n\nStub it with:\n\n #{stub_call}")
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
module Gempilot
|
|
2
|
+
## Runs external commands for gempilot.
|
|
3
|
+
##
|
|
4
|
+
## Gempilot::Shell.run("git", "--version")
|
|
5
|
+
## # git version 2.55.0
|
|
6
|
+
## # => #<data Gempilot::Shell::Status exitstatus=0, success=true>
|
|
7
|
+
##
|
|
8
|
+
## Gempilot::Shell.capture("git", "branch", "--show-current").output
|
|
9
|
+
## # => "main\n"
|
|
10
|
+
module Shell
|
|
11
|
+
class << self
|
|
12
|
+
attr_writer :gateway
|
|
13
|
+
|
|
14
|
+
## The gateway commands run through: Gateway, unless replaced with +with_gateway+.
|
|
15
|
+
def gateway
|
|
16
|
+
@gateway ||= Gateway.new
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
## Runs a command, streaming its output. Raises CommandFailed if it fails.
|
|
20
|
+
##
|
|
21
|
+
## Gempilot::Shell.run("git", "checkout", "no-such-branch")
|
|
22
|
+
## # error: pathspec 'no-such-branch' did not match any file(s) known to git
|
|
23
|
+
## # Gempilot::Shell::CommandFailed: Command failed with status (1): [git checkout no-such-branch]
|
|
24
|
+
def run(*command, chdir: nil, env: {}, unbundled: false)
|
|
25
|
+
invocation = Invocation.new(command:, chdir:, env:, unbundled:)
|
|
26
|
+
status = gateway.run(invocation)
|
|
27
|
+
raise CommandFailed.new(invocation, status) unless status.success?
|
|
28
|
+
|
|
29
|
+
status
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
## Runs a command and returns its standard output. Never raises.
|
|
33
|
+
##
|
|
34
|
+
## result = Gempilot::Shell.capture("git", "config", "--get", "user.name")
|
|
35
|
+
## result.output # => "Ada Lovelace\n"
|
|
36
|
+
## result.success? # => true
|
|
37
|
+
def capture(*command, chdir: nil, env: {}, unbundled: false)
|
|
38
|
+
gateway.capture(Invocation.new(command:, chdir:, env:, unbundled:))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
## Replaces the current process with a command.
|
|
42
|
+
##
|
|
43
|
+
## Gempilot::Shell.exec("bin/console", unbundled: true)
|
|
44
|
+
def exec(*command, chdir: nil, env: {}, unbundled: false)
|
|
45
|
+
gateway.exec(Invocation.new(command:, chdir:, env:, unbundled:))
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
## Runs the block with commands going through +gateway+.
|
|
49
|
+
##
|
|
50
|
+
## stubs = Gempilot::Shell::StubGateway.new
|
|
51
|
+
## stubs.stub("gh", "release", "list").to_return(output: "v1.0.0\tLatest\n")
|
|
52
|
+
##
|
|
53
|
+
## Gempilot::Shell.with_gateway(stubs) { Gempilot::Shell.capture("gh", "release", "list").output }
|
|
54
|
+
## # => "v1.0.0\tLatest\n"
|
|
55
|
+
def with_gateway(gateway)
|
|
56
|
+
previous = self.gateway
|
|
57
|
+
begin
|
|
58
|
+
self.gateway = gateway
|
|
59
|
+
yield
|
|
60
|
+
ensure
|
|
61
|
+
self.gateway = previous
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|