/
rename_template.rb
executable file
·298 lines (234 loc) · 8.39 KB
/
rename_template.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env ruby
require "bundler/inline"
require "fileutils"
require "io/console"
require "open3"
gemfile do
source "https://rubygems.org"
gem "octokit", "~> 4.14"
end
def main # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
assert_git_repo!
git_meta = read_git_data
plugin_name = ask("Plugin name?", default: git_meta[:origin_repo_name].sub(/^tomo-plugin-/, ""))
plugin_name.sub!(/^tomo-plugin-/, "")
gem_name = "tomo-plugin-#{plugin_name}"
gem_summary = ask(
"Gem summary (< 60 chars)?", default: "#{plugin_name} tasks for tomo"
)
author_email = ask("Author email?", default: git_meta[:user_email])
author_name = ask("Author name?", default: git_meta[:user_name])
github_repo = ask("GitHub repository?", default: git_meta[:origin_repo_path])
if ask_yes_or_no("Create GitHub labels?", default: "Y")
puts
puts "I need to ask for your GitHub credentials in order to create labels."
puts "Don't worry, your GitHub credentials will NOT be saved."
puts
login = ask("GitHub username?", default: github_repo.split("/").first)
password = ask("GitHub password?", echo: false)
client = authenticate_github(login, password)
create_labels(
client,
github_repo,
["⚠️ Breaking", "d12d1b", "Introduces a backwards-incompatible change"],
["🐛 Bug Fix", "c0fc80", "Fixes a bug"],
["📚 Docs", "bfdadc", "Improves documentation"],
["✨ Feature", "ba1ecc", "Adds a new feature"],
["🏠 Housekeeping", "ccccff", "Non-user facing cleanup and maintenance"]
)
end
git "mv", ".github/workflows/push.yml.dist", ".github/workflows/push.yml"
FileUtils.mkdir_p "lib/#{as_path(gem_name)}"
FileUtils.mkdir_p "test/#{as_path(gem_name)}"
ensure_executable "bin/console"
ensure_executable "bin/setup"
replace_in_file "LICENSE.txt",
"Example Owner" => author_name
replace_in_file "Rakefile",
"example.gemspec" => "#{gem_name}.gemspec",
"mattbrictson/tomo-plugin" => github_repo
replace_in_file "README.md",
"mattbrictson/tomo-plugin" => github_repo,
"example" => plugin_name,
"plugin_name" => plugin_name.tr("-", "_"),
"replace_with_gem_name" => gem_name,
/\A.*<!-- END FRONT MATTER -->\n+/m => ""
replace_in_file "CHANGELOG.md",
"mattbrictson/tomo-plugin" => github_repo
replace_in_file "CODE_OF_CONDUCT.md",
"owner@example.com" => author_email
replace_in_file "bin/console", "tomo/plugin/example" => as_path(gem_name)
replace_in_file "example.gemspec",
"mattbrictson/tomo-plugin" => github_repo,
'"Example Owner"' => author_name.inspect,
'"owner@example.com"' => author_email.inspect,
'"example"' => gem_name.inspect,
"example/version" => "#{as_path(plugin_name)}/version",
"Example::VERSION" => "#{as_module(plugin_name)}::VERSION",
/summary\s*=\s*("")/ => gem_summary.inspect
git "mv", "example.gemspec", "#{gem_name}.gemspec"
replace_in_file "lib/tomo/plugin/example.rb",
"example" => as_path(plugin_name).sub(%r{^.*/}, ""),
"plugin_name" => plugin_name.tr("-", "_"),
"Example" => as_module(plugin_name)
git "mv", "lib/tomo/plugin/example.rb", "lib/#{as_path(gem_name)}.rb"
replace_in_file "lib/tomo/plugin/example/version.rb", <<~MODULE => ""
module Tomo
module Plugin
end
end
MODULE
%w[helpers tasks version].each do |file|
replace_in_file "lib/tomo/plugin/example/#{file}.rb",
"Example" => as_module(plugin_name),
"example" => plugin_name
git "mv", "lib/tomo/plugin/example/#{file}.rb", "lib/#{as_path(gem_name)}/#{file}.rb"
end
reindent_module "lib/#{as_path(gem_name)}.rb"
reindent_module "lib/#{as_path(gem_name)}/version.rb"
replace_in_file "test/tomo/plugin/example_test.rb",
"Example" => as_module(plugin_name)
git "mv", "test/tomo/plugin/example_test.rb", "test/#{as_path(gem_name)}_test.rb"
%w[helpers_test tasks_test].each do |file|
replace_in_file "test/tomo/plugin/example/#{file}.rb",
"Example" => as_module(plugin_name)
replace_in_file "test/tomo/plugin/example/#{file}.rb",
"example" => plugin_name
git "mv", "test/tomo/plugin/example/#{file}.rb", "test/#{as_path(gem_name)}/#{file}.rb"
end
replace_in_file "test/test_helper.rb",
'require "tomo/plugin/example"' =>
%Q(require "#{as_path(gem_name)}")
git "rm", "rename_template.rb"
puts <<~MESSAGE
All set!
The project has been renamed to "#{gem_name}".
Review the changes and then run:
git commit && git push
MESSAGE
end
def assert_git_repo!
return if File.file?(".git/config")
warn("This doesn't appear to be a git repo. Can't continue. :(")
exit(1)
end
def git(*args)
sh! "git", *args
end
def ensure_executable(path)
return if File.executable?(path)
FileUtils.chmod 0o755, path
git "add", path
end
def sh!(*args)
puts ">>>> #{args.join(' ')}"
stdout, status = Open3.capture2(*args)
raise("Failed to execute: #{args.join(' ')}") unless status.success?
stdout
end
def remove_line(file, pattern)
text = IO.read(file)
text = text.lines.filter.grep_v(pattern).join
IO.write(file, text)
git "add", file
end
def ask(question, default: nil, echo: true)
prompt = "#{question} "
prompt << "[#{default}] " unless default.nil?
print prompt
answer = if echo
$stdin.gets.chomp
else
$stdin.noecho(&:gets).tap { $stdout.print "\n" }.chomp
end
answer.to_s.strip.empty? ? default : answer
end
def ask_yes_or_no(question, default: "N")
default = default == "Y" ? "Y/n" : "y/N"
answer = ask(question, default: default)
answer != "y/N" && answer.match?(/^y/i)
end
def read_git_data
return {} unless git("remote", "-v").match?(/^origin/)
origin_url = git("remote", "get-url", "origin").chomp
origin_repo_path = origin_url[%r{[:/]([^/]+/[^/]+)(?:\.git)$}, 1]
{
origin_repo_name: origin_repo_path.split("/").last,
origin_repo_path: origin_repo_path,
user_email: git("config", "user.email").chomp,
user_name: git("config", "user.name").chomp
}
end
def replace_in_file(path, replacements)
contents = IO.read(path)
replacements.each do |regexp, text|
contents.gsub!(regexp) do |match|
next text if Regexp.last_match(1).nil?
match[regexp, 1] = text
match
end
end
IO.write(path, contents)
git "add", path
end
def as_path(gem_name)
gem_name.tr("-", "/")
end
def as_module(gem_name)
parts = gem_name.split("-")
parts.map do |part|
part.gsub(/^[a-z]|_[a-z]/) { |str| str.chars.last.upcase }
end.join("::")
end
def reindent_module(path)
contents = IO.read(path)
preamble = contents[/\A(.*)^(?:module|class)/m, 1]
contents.sub!(preamble, "") if preamble
namespace_mod = contents[/(?:module|class) (\S+)/, 1]
return unless namespace_mod.include?("::")
contents.sub!(namespace_mod, namespace_mod.split("::").last)
namespace_mod.split("::")[0...-1].reverse_each do |mod|
contents = "module #{mod}\n#{contents.gsub(/^/, ' ')}end\n"
end
contents.gsub!(/^\s+$/, "")
IO.write(path, [preamble, contents].join)
git "add", path
end
def authenticate_github(login, password)
octokit = Octokit::Client.new(login: login, password: password, netrc: false)
octokit.user
GithubClient.new(octokit)
rescue Octokit::OneTimePasswordRequired
token = ask("2FA token?")
GithubClient.new(octokit, otp_token: token)
end
def create_labels(client, github_repo, *labels)
labels.each do |name, color, description|
client.add_label(github_repo, name, color, description)
end
puts "Created labels: #{labels.map(&:first).join(', ')}"
end
class GithubClient
def initialize(octokit, otp_token: nil)
@octokit = octokit
@otp_token = otp_token
end
def add_label(repo, name, color, description)
octokit.add_label(
repo,
name,
color,
auth_options.merge(
description: description,
accept: "application/vnd.github.symmetra-preview+json"
)
)
end
private
attr_reader :octokit, :otp_token
def auth_options
return {} if otp_token.nil?
{ headers: { "X-GitHub-OTP" => otp_token } }
end
end
main if $PROGRAM_NAME == __FILE__