forked from primer/view_components
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_status_migrator.thor
209 lines (166 loc) · 5.43 KB
/
component_status_migrator.thor
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
# frozen_string_literal: true
require "thor"
require "active_support/core_ext/string/inflections"
# Migrates components to their new namespace.
#
# Usage:
#
# bundle exec thor component_status_migrator MyComponentName
# bundle exec thor component_status_migrator MyComponentName --status beta
class ComponentStatusMigrator < Thor::Group
include Thor::Actions
STATUSES = %w[alpha beta deprecated stable experimental].freeze
# Define arguments and options
argument :name
class_option :status, default: "alpha", desc: "Status of the component. Valid values: #{STATUSES.join(', ')}", required: true, type: :string
def self.source_root
File.dirname(__FILE__)
end
def validate_status
raise "Invalid status: #{status}" unless STATUSES.include?(status)
end
def move_controller
move_file("controller", controller_path, controller_path_with_status)
end
def move_template
move_file("template", template_path, template_path_with_status)
end
def copy_test
move_file("test", test_path, test_path_with_status)
end
def add_module
return if stable?
insert_into_file(controller_path_with_status, " module #{class_status}\n", after: "module Primer\n")
insert_into_file(controller_path_with_status, " end\n", before: /^end$/, force: true)
end
def remove_suffix
if name == name_without_suffix
puts "No change needed - class suffix not removed"
else
gsub_file(controller_path_with_status, "class #{name}", "class #{name_without_suffix}")
end
end
def rename_test_class
gsub_file(test_path_with_status, /class .*Test </, "class Primer#{class_status}#{name_without_suffix.gsub('::', '')}Test <")
end
def rename_nav_entry
nav_file = "docs/src/@primer/gatsby-theme-doctocat/nav.yml"
gsub_file(nav_file, "title: #{name}", "title: #{name_without_suffix}")
gsub_file(nav_file, "url: \"/components/#{name_without_suffix.downcase}\"", "url: \"/components/#{status_url}#{name_without_suffix.downcase}\"")
end
def update_all_references
exclude_files = [
".git",
"CHANGELOG.md",
test_path
]
exclude_folders = [
".cache",
".yardoc",
"builds",
"log",
"node_modules",
"tmp",
"vendor"
]
cmd = ["grep -rl #{name} ."]
cmd << exclude_files.join(" --exclude=")
cmd << "--exclude-dir={#{exclude_folders.join(',')}}"
cmd << "| xargs sed -i '' 's/Primer::#{name}/Primer::#{status_module}#{name_without_suffix}/g'"
run(cmd.join(" "))
end
def add_alias
return if controller_path == controller_path_with_status
remove_file(controller_path)
create_file(
controller_path,
"# frozen_string_literal: true\n\nmodule Primer\n\tclass #{name} < Primer::#{status_module}#{name_without_suffix}\n\t\tstatus :deprecated\n\tend\nend"
)
end
def add_to_deprecated_component_helper
insert_into_file(
"lib/primer/deprecations.rb",
"\"Primer::#{name}\" => \"Primer::#{status_module}#{name_without_suffix}\",\n",
after: "DEPRECATED_COMPONENTS = {\n"
)
end
def add_to_ignored_component_test
insert_into_file(
"test/components/component_test.rb",
"\"Primer::#{name}\",\n",
after: "ignored_components = [\n"
)
end
def run_rubocop
run("bundle exec rubocop -a")
end
def generate_docs
run("bundle exec rake docs:build")
end
def dump_static_files
run("bundle exec rake static:dump")
end
private
def class_status
@class_status ||= status.capitalize unless stable?
end
def controller_path
@controller_path ||= "app/components/primer/#{name.underscore}.rb"
end
def controller_path_with_status
@controller_path_with_status ||= "app/components/primer/#{status_folder_name}#{name_without_suffix.underscore}.rb"
end
def move_file(file_type, old_path, new_path)
if old_path == new_path
puts "No change needed - #{file_type} file not moved"
elsif File.exist?(old_path)
copy_file(old_path, new_path)
remove_file(old_path)
else
puts "Nothing moved. #{file_type.capitalize} file not found: #{new_path}"
end
end
def stable?
@stable ||= status == "stable"
end
def status_folder_name
@status_folder_name ||= "#{status}/" unless stable?
end
def status_module
@status_module ||= "#{class_status}::" unless stable?
end
def template_path
@template_path ||= "app/components/primer/#{name.underscore}.html.erb"
end
def template_path_with_status
@template_path_with_status ||= "app/components/primer/#{status_folder_name}#{name_without_suffix.underscore}.html.erb"
end
def test_path
@test_path ||= "test/components/#{name.underscore}_test.rb"
end
def test_path_with_status
@test_path_with_status ||= "test/components/#{status_folder_name}#{name_without_suffix.underscore}_test.rb"
end
def docs_path
@docs_path ||= "/components/#{short_name}.md"
end
def docs_path_with_status
@docs_path_with_status ||= "/components/#{status_folder_name}#{short_name}.md"
end
def status
@status ||= options[:status].downcase
end
def status_url
@status_url ||= "#{status}/" unless stable?
end
def name_without_suffix
@name_without_suffix ||= name.gsub("Component", "")
end
def short_name
@short_name ||= begin
name_with_status = name.gsub(/Primer::|Component/, "")
m = name_with_status.match(/(?<status>Beta|Alpha|Deprecated)?(?<_colons>::)?(?<name>.*)/)
m[:name].gsub("::", "").downcase
end
end
end