forked from primer/view_components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent_status_migrator.thor
163 lines (124 loc) · 4.12 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
# 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].freeze
# Define arguments and options
argument :name
class_option :status, default: "alpha", desc: "Status of the component. Either alpha, beta or deprecated", required: true, type: :string
def self.source_root
File.dirname(__FILE__)
end
def validate_status
raise unless STATUSES.include?(status)
end
def move_controller
raise unless File.exist?(controller_path)
copy_file(controller_path, controller_path_with_status)
remove_file(controller_path)
end
def move_template
if File.exist?(template_path)
copy_file(template_path, template_path_with_status)
remove_file(template_path)
else
puts "No template found"
end
end
def copy_test
raise unless File.exist?(test_path)
copy_file(test_path, test_path_with_status)
end
def move_story
raise unless File.exist?(story_path)
copy_file(story_path, story_path_with_status)
remove_file(story_path)
end
def add_module
insert_into_file(controller_path_with_status, " module #{status.capitalize}\n", after: "module Primer\n")
insert_into_file(controller_path_with_status, " end\n", before: /^end$/, force: true)
end
def remove_suffix
gsub_file(controller_path_with_status, "class #{name}", "class #{name_without_suffix}")
end
def rename_test_class
gsub_file(test_path_with_status, /class .*Test </, "class Primer#{status.capitalize}#{name_without_suffix.gsub('::', '')}Test <")
end
def add_require_to_story
insert_into_file(story_path_with_status, "require \"primer/#{status}/#{name_without_suffix.underscore}\"\n", after: "# frozen_string_literal: true\n")
end
def rename_nav_entry
gsub_file("docs/src/@primer/gatsby-theme-doctocat/nav.yml", "class #{name}", name_without_suffix)
end
def update_all_references
run("grep -rl #{name} . --exclude=CHANGELOG.md --exclude=#{test_path} | xargs sed -i 's/Primer::#{name}/Primer::#{status.capitalize}::#{name_without_suffix}/g'")
end
def add_alias
insert_into_file(controller_path_with_status, "\nPrimer::#{name} = Primer::#{status.capitalize}::#{name_without_suffix}\n")
end
def add_to_linter
insert_into_file(
"lib/rubocop/cop/primer/component_name_migration.rb",
"\"Primer::#{name}\" => \"Primer::#{status.capitalize}::#{name_without_suffix}\",\n",
after: "DEPRECATIONS = {\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 controller_path
"app/components/primer/#{name.underscore}.rb"
end
def controller_path_with_status
"app/components/primer/#{status}/#{name_without_suffix.underscore}.rb"
end
def template_path
"app/components/primer/#{name.underscore}.html.erb"
end
def template_path_with_status
"app/components/primer/#{status}/#{name_without_suffix.underscore}.html.erb"
end
def test_path
"test/components/#{name.underscore}_test.rb"
end
def test_path_with_status
"test/components/#{status}/#{name_without_suffix.underscore}_test.rb"
end
def story_path
"stories/primer/#{name.underscore}_stories.rb"
end
def story_path_with_status
"stories/primer/#{status}/#{name_without_suffix.underscore}_stories.rb"
end
def docs_path
"/components/#{short_name}.md"
end
def docs_path_with_status
"/components/#{status}/#{short_name}.md"
end
def status
options[:status].downcase
end
def name_without_suffix
name.gsub("Component", "")
end
def short_name
name_with_status = name.gsub(/Primer::|Component/, "")
m = name_with_status.match(/(?<status>Beta|Alpha|Deprecated)?(?<_colons>::)?(?<name>.*)/)
m[:name].gsub("::", "").downcase
end
end