primer_view_components 0.0.38 → 0.0.43

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +283 -27
  3. data/app/assets/javascripts/primer_view_components.js +1 -1
  4. data/app/assets/javascripts/primer_view_components.js.map +1 -1
  5. data/app/components/primer/auto_complete.rb +4 -2
  6. data/app/components/primer/auto_complete/item.rb +1 -1
  7. data/app/components/primer/avatar_component.rb +22 -3
  8. data/app/components/primer/avatar_stack_component.rb +8 -5
  9. data/app/components/primer/base_button.rb +1 -1
  10. data/app/components/primer/base_component.rb +24 -13
  11. data/app/components/primer/blankslate_component.html.erb +1 -0
  12. data/app/components/primer/blankslate_component.rb +68 -47
  13. data/app/components/primer/border_box_component.rb +1 -1
  14. data/app/components/primer/box_component.rb +1 -1
  15. data/app/components/primer/breadcrumb_component.rb +1 -1
  16. data/app/components/primer/button_component.html.erb +9 -0
  17. data/app/components/primer/button_component.rb +39 -5
  18. data/app/components/primer/button_group.rb +3 -3
  19. data/app/components/primer/button_marketing_component.rb +1 -1
  20. data/app/components/primer/clipboard_copy.rb +1 -1
  21. data/app/components/primer/clipboard_copy_component.js +17 -8
  22. data/app/components/primer/clipboard_copy_component.ts +19 -8
  23. data/app/components/primer/close_button.rb +4 -2
  24. data/app/components/primer/component.rb +22 -3
  25. data/app/components/primer/counter_component.rb +6 -1
  26. data/app/components/primer/details_component.rb +1 -1
  27. data/app/components/primer/dropdown/menu_component.rb +1 -1
  28. data/app/components/primer/dropdown_component.rb +1 -1
  29. data/app/components/primer/flash_component.rb +3 -3
  30. data/app/components/primer/flex_component.rb +28 -1
  31. data/app/components/primer/flex_item_component.rb +20 -1
  32. data/app/components/primer/heading_component.rb +12 -19
  33. data/app/components/primer/hidden_text_expander.rb +4 -4
  34. data/app/components/primer/icon_button.rb +65 -0
  35. data/app/components/primer/image.rb +46 -0
  36. data/app/components/primer/image_crop.d.ts +1 -0
  37. data/app/components/primer/image_crop.html.erb +12 -0
  38. data/app/components/primer/image_crop.js +1 -0
  39. data/app/components/primer/image_crop.rb +36 -0
  40. data/app/components/primer/image_crop.ts +1 -0
  41. data/app/components/primer/label_component.rb +7 -3
  42. data/app/components/primer/layout_component.rb +1 -1
  43. data/app/components/primer/link_component.rb +1 -1
  44. data/app/components/primer/local_time.d.ts +1 -0
  45. data/app/components/primer/local_time.js +1 -0
  46. data/app/components/primer/local_time.rb +59 -0
  47. data/app/components/primer/local_time.ts +1 -0
  48. data/app/components/primer/{markdown_component.rb → markdown.rb} +12 -7
  49. data/app/components/primer/menu_component.rb +1 -1
  50. data/app/components/primer/navigation/tab_component.rb +8 -1
  51. data/app/components/primer/octicon_component.html.erb +7 -0
  52. data/app/components/primer/octicon_component.rb +53 -19
  53. data/app/components/primer/octicon_symbols_component.html.erb +3 -0
  54. data/app/components/primer/octicon_symbols_component.rb +61 -0
  55. data/app/components/primer/popover_component.rb +1 -1
  56. data/app/components/primer/primer.d.ts +2 -0
  57. data/app/components/primer/primer.js +2 -0
  58. data/app/components/primer/primer.ts +2 -0
  59. data/app/components/primer/progress_bar_component.rb +1 -1
  60. data/app/components/primer/spinner_component.rb +3 -3
  61. data/app/components/primer/state_component.rb +1 -1
  62. data/app/components/primer/subhead_component.rb +34 -4
  63. data/app/components/primer/tab_container_component.rb +1 -1
  64. data/app/components/primer/tab_nav_component.html.erb +5 -1
  65. data/app/components/primer/tab_nav_component.rb +63 -10
  66. data/app/components/primer/text_component.rb +6 -3
  67. data/app/components/primer/time_ago_component.rb +1 -1
  68. data/app/components/primer/timeline_item_component.rb +1 -1
  69. data/app/components/primer/{tooltip_component.rb → tooltip.rb} +11 -9
  70. data/app/components/primer/truncate.rb +7 -3
  71. data/app/components/primer/underline_nav_component.html.erb +1 -1
  72. data/app/components/primer/underline_nav_component.rb +18 -2
  73. data/app/lib/primer/classify.rb +27 -39
  74. data/app/lib/primer/classify/cache.rb +35 -15
  75. data/app/lib/primer/classify/flex.rb +111 -0
  76. data/app/lib/primer/classify/functional_border_colors.rb +1 -2
  77. data/app/lib/primer/classify/grid.rb +45 -0
  78. data/app/lib/primer/fetch_or_fallback_helper.rb +2 -2
  79. data/app/lib/primer/octicon/cache.rb +42 -0
  80. data/app/lib/primer/tabbed_component_helper.rb +2 -2
  81. data/app/lib/primer/view_helper.rb +2 -1
  82. data/lib/primer/view_components.rb +1 -1
  83. data/lib/primer/view_components/engine.rb +2 -0
  84. data/lib/primer/view_components/linters.rb +3 -0
  85. data/lib/primer/view_components/linters/button_component_migration_counter.rb +16 -0
  86. data/lib/primer/view_components/linters/flash_component_migration_counter.rb +16 -0
  87. data/lib/primer/view_components/linters/helpers.rb +89 -0
  88. data/lib/primer/view_components/version.rb +1 -1
  89. data/lib/tasks/coverage.rake +14 -0
  90. data/lib/tasks/docs.rake +372 -0
  91. data/lib/tasks/statuses.rake +12 -0
  92. data/lib/yard/docs_helper.rb +57 -0
  93. data/static/statuses.json +54 -1
  94. metadata +62 -11
  95. data/app/components/primer/button_component.rb.orig +0 -138
  96. data/app/components/primer/foo_bar.d.ts +0 -1
  97. data/app/components/primer/foo_bar.js +0 -1
@@ -20,10 +20,10 @@ module Primer
20
20
  with_panel ? :div : :nav
21
21
  end
22
22
 
23
- def wrapper
23
+ def wrapper(**system_arguments)
24
24
  return yield unless @with_panel
25
25
 
26
- render Primer::TabContainerComponent.new do
26
+ render Primer::TabContainerComponent.new(**system_arguments) do
27
27
  yield
28
28
  end
29
29
  end
@@ -8,7 +8,8 @@ module Primer
8
8
  HELPERS = {
9
9
  octicon: "Primer::OcticonComponent",
10
10
  heading: "Primer::HeadingComponent",
11
- time_ago: "Primer::TimeAgoComponent"
11
+ time_ago: "Primer::TimeAgoComponent",
12
+ image: "Primer::Image"
12
13
  }.freeze
13
14
 
14
15
  HELPERS.each do |name, component|
@@ -27,7 +27,7 @@ module Primer
27
27
  statuses = generate_statuses
28
28
 
29
29
  File.open(File.join(path, DEFAULT_STATUS_FILE_NAME), "w") do |f|
30
- f.write(statuses.to_json)
30
+ f.write(JSON.pretty_generate(statuses))
31
31
  f.write($INPUT_RECORD_SEPARATOR)
32
32
  end
33
33
  end
@@ -13,7 +13,9 @@ module Primer
13
13
  ]
14
14
 
15
15
  config.primer_view_components = ActiveSupport::OrderedOptions.new
16
+
16
17
  config.primer_view_components.force_functional_colors = true
18
+ config.primer_view_components.force_system_arguments = false
17
19
  config.primer_view_components.silence_deprecations = false
18
20
 
19
21
  initializer "primer_view_components.assets" do |app|
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ Dir[File.join(__dir__, "linters", "*.rb")].sort.each { |file| require file }
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "helpers"
4
+
5
+ module ERBLint
6
+ module Linters
7
+ # Counts the number of times a HTML button is used instead of the component.
8
+ class ButtonComponentMigrationCounter < Linter
9
+ include Helpers
10
+
11
+ TAGS = %w[button summary a].freeze
12
+ CLASS = "btn"
13
+ MESSAGE = "We are migrating buttons to use [Primer::ButtonComponent](https://primer.style/view-components/components/button), please try to use that instead of raw HTML."
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "helpers"
4
+
5
+ module ERBLint
6
+ module Linters
7
+ # Counts the number of times a HTML flash is used instead of the component.
8
+ class FlashComponentMigrationCounter < Linter
9
+ include Helpers
10
+
11
+ TAGS = %w[div].freeze
12
+ CLASS = "flash"
13
+ MESSAGE = "We are migrating flashes to use [Primer::FlashComponent](https://primer.style/view-components/components/flash), please try to use that instead of raw HTML."
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "openssl"
5
+
6
+ module ERBLint
7
+ module Linters
8
+ # Helper methods for linting ERB.
9
+ module Helpers
10
+ def self.included(base)
11
+ base.include(LinterRegistry)
12
+
13
+ define_method "run" do |processed_source|
14
+ tags(processed_source).each do |tag|
15
+ next if tag.closing?
16
+ next unless self.class::TAGS&.include?(tag.name)
17
+
18
+ classes = tag.attributes["class"]&.value&.split(" ")
19
+
20
+ next unless !self.class::CLASS || classes&.include?(self.class::CLASS)
21
+
22
+ generate_offense(self.class, processed_source, tag, self.class::MESSAGE)
23
+ end
24
+
25
+ counter_correct?(processed_source)
26
+ end
27
+
28
+ define_method "autocorrect" do |processed_source, offense|
29
+ return unless offense.context
30
+
31
+ lambda do |corrector|
32
+ if processed_source.file_content.include?("erblint:counter #{self.class.name.demodulize}")
33
+ # update the counter if exists
34
+ corrector.replace(offense.source_range, offense.context)
35
+ else
36
+ # add comment with counter if none
37
+ corrector.insert_before(processed_source.source_buffer.source_range, "#{offense.context}\n")
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def tags(processed_source)
46
+ processed_source.parser.nodes_with_type(:tag).map { |tag_node| BetterHtml::Tree::Tag.from_node(tag_node) }
47
+ end
48
+
49
+ def counter_correct?(processed_source)
50
+ comment_node = nil
51
+ expected_count = 0
52
+ rule_name = self.class.name.match(/:?:?(\w+)\Z/)[1]
53
+ offenses_count = @offenses.length
54
+
55
+ processed_source.parser.ast.descendants(:erb).each do |node|
56
+ indicator_node, _, code_node, = *node
57
+ indicator = indicator_node&.loc&.source
58
+ comment = code_node&.loc&.source&.strip
59
+
60
+ if indicator == "#" && comment.start_with?("erblint:count") && comment.match(rule_name)
61
+ comment_node = code_node
62
+ expected_count = comment.match(/\s(\d+)\s?$/)[1].to_i
63
+ end
64
+ end
65
+
66
+ if offenses_count.zero?
67
+ add_offense(processed_source.to_source_range(comment_node.loc), "Unused erblint:count comment for #{rule_name}") if comment_node
68
+ return
69
+ end
70
+
71
+ first_offense = @offenses[0]
72
+
73
+ if comment_node.nil?
74
+ add_offense(processed_source.to_source_range(first_offense.source_range), "#{rule_name}: If you must, add <%# erblint:counter #{rule_name} #{offenses_count} %> to bypass this check.", "<%# erblint:counter #{rule_name} #{offenses_count} %>")
75
+ else
76
+ clear_offenses
77
+ add_offense(processed_source.to_source_range(comment_node.loc), "Incorrect erblint:counter number for #{rule_name}. Expected: #{expected_count}, actual: #{offenses_count}.", " erblint:counter #{rule_name} #{offenses_count} ") if expected_count != offenses_count
78
+ end
79
+ end
80
+
81
+ def generate_offense(klass, processed_source, tag, message = nil, replacement = nil)
82
+ message ||= klass::MESSAGE
83
+ klass_name = klass.name.split("::")[-1]
84
+ offense = ["#{klass_name}:#{message}", tag.node.loc.source].join("\n")
85
+ add_offense(processed_source.to_source_range(tag.loc), offense, replacement)
86
+ end
87
+ end
88
+ end
89
+ end
@@ -5,7 +5,7 @@ module Primer
5
5
  module VERSION
6
6
  MAJOR = 0
7
7
  MINOR = 0
8
- PATCH = 38
8
+ PATCH = 43
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH].join(".")
11
11
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :coverage do
4
+ task :report do
5
+ require "simplecov"
6
+ require "simplecov-console"
7
+
8
+ SimpleCov.minimum_coverage 100
9
+
10
+ SimpleCov.collate Dir["simplecov-resultset-*/.resultset.json"], "rails" do
11
+ formatter SimpleCov::Formatter::Console
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,372 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :docs do
4
+ task :livereload do
5
+ require "listen"
6
+
7
+ Rake::Task["docs:build"].execute
8
+
9
+ puts "Listening for changes to documentation..."
10
+
11
+ listener = Listen.to("app") do |modified, added, removed|
12
+ puts "modified absolute path: #{modified}"
13
+ puts "added absolute path: #{added}"
14
+ puts "removed absolute path: #{removed}"
15
+
16
+ Rake::Task["docs:build"].execute
17
+ end
18
+ listener.start # not blocking
19
+ sleep
20
+ end
21
+
22
+ task :build do
23
+ registry = generate_yard_registry
24
+
25
+ puts "Converting YARD documentation to Markdown files."
26
+
27
+ # Rails controller for rendering arbitrary ERB
28
+ view_context = ApplicationController.new.tap { |c| c.request = ActionDispatch::TestRequest.create }.view_context
29
+ components = [
30
+ Primer::Image,
31
+ Primer::LocalTime,
32
+ Primer::OcticonSymbolsComponent,
33
+ Primer::ImageCrop,
34
+ Primer::IconButton,
35
+ Primer::AutoComplete,
36
+ Primer::AutoComplete::Item,
37
+ Primer::AvatarComponent,
38
+ Primer::AvatarStackComponent,
39
+ Primer::BaseButton,
40
+ Primer::BlankslateComponent,
41
+ Primer::BorderBoxComponent,
42
+ Primer::BoxComponent,
43
+ Primer::BreadcrumbComponent,
44
+ Primer::ButtonComponent,
45
+ Primer::ButtonGroup,
46
+ Primer::ButtonMarketingComponent,
47
+ Primer::ClipboardCopy,
48
+ Primer::CloseButton,
49
+ Primer::CounterComponent,
50
+ Primer::DetailsComponent,
51
+ Primer::DropdownComponent,
52
+ Primer::DropdownMenuComponent,
53
+ Primer::FlashComponent,
54
+ Primer::FlexComponent,
55
+ Primer::FlexItemComponent,
56
+ Primer::HeadingComponent,
57
+ Primer::HiddenTextExpander,
58
+ Primer::LabelComponent,
59
+ Primer::LayoutComponent,
60
+ Primer::LinkComponent,
61
+ Primer::Markdown,
62
+ Primer::MenuComponent,
63
+ Primer::Navigation::TabComponent,
64
+ Primer::OcticonComponent,
65
+ Primer::PopoverComponent,
66
+ Primer::ProgressBarComponent,
67
+ Primer::StateComponent,
68
+ Primer::SpinnerComponent,
69
+ Primer::SubheadComponent,
70
+ Primer::TabContainerComponent,
71
+ Primer::TabNavComponent,
72
+ Primer::TextComponent,
73
+ Primer::TimeAgoComponent,
74
+ Primer::TimelineItemComponent,
75
+ Primer::Tooltip,
76
+ Primer::Truncate,
77
+ Primer::UnderlineNavComponent
78
+ ]
79
+
80
+ js_components = [
81
+ Primer::LocalTime,
82
+ Primer::ImageCrop,
83
+ Primer::AutoComplete,
84
+ Primer::ClipboardCopy,
85
+ Primer::TabContainerComponent,
86
+ Primer::TabNavComponent,
87
+ Primer::TimeAgoComponent,
88
+ Primer::UnderlineNavComponent
89
+ ]
90
+
91
+ all_components = Primer::Component.descendants - [Primer::BaseComponent]
92
+ components_needing_docs = all_components - components
93
+
94
+ components_without_examples = []
95
+ args_for_components = []
96
+ classes_found_in_examples = []
97
+
98
+ components.each do |component|
99
+ documentation = registry.get(component.name)
100
+
101
+ # Primer::AvatarComponent => Avatar
102
+ short_name = component.name.gsub(/Primer|::|Component/, "")
103
+
104
+ path = Pathname.new("docs/content/components/#{short_name.downcase}.md")
105
+ path.dirname.mkdir unless path.dirname.exist?
106
+ File.open(path, "w") do |f|
107
+ f.puts("---")
108
+ f.puts("title: #{short_name}")
109
+ f.puts("status: #{component.status.to_s.capitalize}")
110
+ f.puts("source: https://github.com/primer/view_components/tree/main/app/components/primer/#{component.to_s.demodulize.underscore}.rb")
111
+ f.puts("storybook: https://primer.style/view-components/stories/?path=/story/primer-#{short_name.underscore.dasherize}-component")
112
+ f.puts("---")
113
+ f.puts
114
+ f.puts("import Example from '../../src/@primer/gatsby-theme-doctocat/components/example'")
115
+
116
+ if js_components.include?(component)
117
+ f.puts("import RequiresJSFlash from '../../src/@primer/gatsby-theme-doctocat/components/requires-js-flash'")
118
+ f.puts
119
+ f.puts("<RequiresJSFlash />")
120
+ end
121
+
122
+ f.puts
123
+ f.puts("<!-- Warning: AUTO-GENERATED file, do not edit. Add code comments to your Ruby instead <3 -->")
124
+ f.puts
125
+ f.puts(view_context.render(inline: documentation.base_docstring))
126
+
127
+ if documentation.tags(:accessibility).any?
128
+ f.puts
129
+ f.puts("## Accessibility")
130
+ documentation.tags(:accessibility).each do |tag|
131
+ f.puts
132
+ f.puts view_context.render(inline: tag.text)
133
+ end
134
+ end
135
+
136
+ if documentation.tags(:deprecated).any?
137
+ f.puts
138
+ f.puts("## Deprecation")
139
+ documentation.tags(:deprecated).each do |tag|
140
+ f.puts
141
+ f.puts view_context.render(inline: tag.text)
142
+ end
143
+ end
144
+
145
+ initialize_method = documentation.meths.find(&:constructor?)
146
+
147
+ if initialize_method.tags(:example).any?
148
+ f.puts
149
+ f.puts("## Examples")
150
+ else
151
+ components_without_examples << component
152
+ end
153
+
154
+ initialize_method.tags(:example).each do |tag|
155
+ name = tag.name
156
+ description = nil
157
+ code = nil
158
+
159
+ if tag.text.include?("@description")
160
+ splitted = tag.text.split(/@description|@code/)
161
+ description = splitted.second.gsub(/^[ \t]{2}/, "").strip
162
+ code = splitted.last.gsub(/^[ \t]{2}/, "").strip
163
+ else
164
+ code = tag.text
165
+ end
166
+
167
+ f.puts
168
+ f.puts("### #{name}")
169
+ if description
170
+ f.puts
171
+ f.puts(description)
172
+ end
173
+ f.puts
174
+ html = view_context.render(inline: code)
175
+ html.scan(/class="([^"]*)"/) do |classnames|
176
+ classes_found_in_examples.concat(classnames[0].split(" ").reject { |c| c.starts_with?("octicon", "js", "my-") }.map { ".#{_1}"})
177
+ end
178
+ f.puts("<Example src=\"#{html.tr('"', "\'").delete("\n")}\" />")
179
+ f.puts
180
+ f.puts("```erb")
181
+ f.puts(code.to_s)
182
+ f.puts("```")
183
+ end
184
+
185
+ params = initialize_method.tags(:param)
186
+ if params.any?
187
+ f.puts
188
+ f.puts("## Arguments")
189
+ f.puts
190
+ f.puts("| Name | Type | Default | Description |")
191
+ f.puts("| :- | :- | :- | :- |")
192
+
193
+ args = []
194
+ params.each do |tag|
195
+ params = tag.object.parameters.find { |param| [tag.name.to_s, tag.name.to_s + ":"].include?(param[0]) }
196
+
197
+ default =
198
+ if params && params[1]
199
+ constant_name = "#{component.name}::#{params[1]}"
200
+ constant_value = constant_name.safe_constantize
201
+ if constant_value.nil?
202
+ pretty_value(params[1])
203
+ else
204
+ pretty_value(constant_value)
205
+ end
206
+ else
207
+ "N/A"
208
+ end
209
+
210
+ args << {
211
+ "name" => tag.name,
212
+ "type" => tag.types.join(", "),
213
+ "default" => default,
214
+ "description" => view_context.render(inline: tag.text)
215
+ }
216
+
217
+ f.puts("| `#{tag.name}` | `#{tag.types.join(', ')}` | #{default} | #{view_context.render(inline: tag.text)} |")
218
+ end
219
+
220
+ component_args = {
221
+ "component" => short_name,
222
+ "source" => "https://github.com/primer/view_components/tree/main/app/components/primer/#{component.to_s.demodulize.underscore}.rb",
223
+ "parameters" => args
224
+ }
225
+
226
+ args_for_components << component_args
227
+ end
228
+
229
+ # Slots V2 docs
230
+ slot_v2_methods = documentation.meths.select { |x| x[:renders_one] || x[:renders_many] }
231
+
232
+ if slot_v2_methods.any?
233
+ f.puts
234
+ f.puts("## Slots")
235
+
236
+ slot_v2_methods.each do |slot_documentation|
237
+ f.puts
238
+ f.puts("### `#{slot_documentation.name.to_s.capitalize}`")
239
+
240
+ if slot_documentation.base_docstring.present?
241
+ f.puts
242
+ f.puts(view_context.render(inline: slot_documentation.base_docstring))
243
+ end
244
+
245
+ param_tags = slot_documentation.tags(:param)
246
+ if param_tags.any?
247
+ f.puts
248
+ f.puts("| Name | Type | Default | Description |")
249
+ f.puts("| :- | :- | :- | :- |")
250
+ end
251
+
252
+ param_tags.each do |tag|
253
+ params = tag.object.parameters.find { |param| [tag.name.to_s, tag.name.to_s + ":"].include?(param[0]) }
254
+
255
+ default =
256
+ if params && params[1]
257
+ "`#{params[1]}`"
258
+ else
259
+ "N/A"
260
+ end
261
+
262
+ f.puts("| `#{tag.name}` | `#{tag.types.join(', ')}` | #{default} | #{view_context.render(inline: tag.text)} |")
263
+ end
264
+ end
265
+ end
266
+ end
267
+ end
268
+
269
+ File.open("static/classes.yml", "w") do |f|
270
+ f.puts YAML.dump(classes_found_in_examples.sort.uniq)
271
+ end
272
+
273
+ File.open("static/arguments.yml", "w") do |f|
274
+ f.puts YAML.dump(args_for_components)
275
+ end
276
+
277
+ # Build system arguments docs from BaseComponent
278
+ documentation = registry.get(Primer::BaseComponent.name)
279
+ File.open("docs/content/system-arguments.md", "w") do |f|
280
+ f.puts("---")
281
+ f.puts("title: System arguments")
282
+ f.puts("---")
283
+ f.puts
284
+ f.puts("<!-- Warning: AUTO-GENERATED file, do not edit. Add code comments to your Ruby instead <3 -->")
285
+ f.puts
286
+ f.puts(documentation.base_docstring)
287
+ f.puts
288
+
289
+ initialize_method = documentation.meths.find(&:constructor?)
290
+
291
+ f.puts(view_context.render(inline: initialize_method.base_docstring))
292
+ end
293
+
294
+ puts "Markdown compiled."
295
+
296
+ if components_without_examples.any?
297
+ puts
298
+ puts "The following components have no examples defined: #{components_without_examples.map(&:name).join(', ')}. Consider adding an example?"
299
+ end
300
+
301
+ if components_needing_docs.any?
302
+ puts
303
+ puts "The following components needs docs. Care to contribute them? #{components_needing_docs.map(&:name).join(', ')}"
304
+ end
305
+ end
306
+
307
+ task :preview do
308
+ registry = generate_yard_registry
309
+
310
+ components = Primer::Component.descendants
311
+
312
+ # Generate previews from documentation examples
313
+ components.each do |component|
314
+ documentation = registry.get(component.name)
315
+ short_name = component.name.gsub(/Primer|::/, "")
316
+ initialize_method = documentation.meths.find(&:constructor?)
317
+
318
+ next unless initialize_method.tags(:example).any?
319
+
320
+ yard_example_tags = initialize_method.tags(:example)
321
+
322
+ path = Pathname.new("demo/test/components/previews/primer/docs/#{short_name.underscore}_preview.rb")
323
+ path.dirname.mkdir unless path.dirname.exist?
324
+
325
+ File.open(path, "w") do |f|
326
+ f.puts("module Primer")
327
+ f.puts(" module Docs")
328
+ f.puts(" class #{short_name}Preview < ViewComponent::Preview")
329
+
330
+ yard_example_tags.each_with_index do |tag, index|
331
+ method_name = tag.name.split("|").first.downcase.parameterize.underscore
332
+ f.puts(" def #{method_name}; end")
333
+ f.puts unless index == yard_example_tags.size - 1
334
+ path = Pathname.new("demo/test/components/previews/primer/docs/#{short_name.underscore}_preview/#{method_name}.html.erb")
335
+ path.dirname.mkdir unless path.dirname.exist?
336
+ File.open(path, "w") do |view_file|
337
+ view_file.puts(tag.text.to_s)
338
+ end
339
+ end
340
+
341
+ f.puts(" end")
342
+ f.puts(" end")
343
+ f.puts("end")
344
+ end
345
+ end
346
+ end
347
+
348
+ def generate_yard_registry
349
+ require File.expand_path("./../../demo/config/environment.rb", __dir__)
350
+ require "primer/view_components"
351
+ require "yard/docs_helper"
352
+ require "view_component/test_helpers"
353
+ include ViewComponent::TestHelpers
354
+ include Primer::ViewHelper
355
+ include YARD::DocsHelper
356
+
357
+ Dir["./app/components/primer/**/*.rb"].sort.each { |file| require file }
358
+
359
+ YARD::Rake::YardocTask.new
360
+
361
+ # Custom tags for yard
362
+ YARD::Tags::Library.define_tag("Accessibility", :accessibility)
363
+ YARD::Tags::Library.define_tag("Deprecation", :deprecation)
364
+
365
+ puts "Building YARD documentation."
366
+ Rake::Task["yard"].execute
367
+
368
+ registry = YARD::RegistryStore.new
369
+ registry.load!(".yardoc")
370
+ registry
371
+ end
372
+ end