amber_component 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3fe50a4aaf1482572196e8ae5fd9fd7e2f04ef1ee07b9b5275b0454623e54a6d
4
- data.tar.gz: 17c1e2848263efac38a029621d26a8d2eae5b4b3d47d2c9eaf8e040b14fe3eae
3
+ metadata.gz: 46dc65dbef14224da8e0ce373eb07528188c399444eb2b90968c1cf3dc049864
4
+ data.tar.gz: d547837963b2e6148a0724d961f3ba9fa1f540365a711d40d483a24ccdcf8909
5
5
  SHA512:
6
- metadata.gz: e566cb7199d6d8c890081557ed20ac02410085992b036b6a603e909a62ac6fb6af0e853910ab02ac087fd8e3bcaf398d59496e7172ac1233d843206785b15509
7
- data.tar.gz: 3b8bb6257ba09f3f6fbf98c68a128ab99adfa289e8ee3d674de744052b4b69f7b06c0e5f43a5e7bb4ae54533a78728fea15ba2534b947b4e730869b5e09b8932
6
+ metadata.gz: 9f060bd9c1a97475c0678ddc5c0df166bf6f05001cde7f43a41bdc130c068b2ea24c15518d384016aeef2e8ef592b2928c305a41aaaa24dcebf038783a96b57b
7
+ data.tar.gz: 8f2f8029094be9ac3eccbb55b1c46a3c1ed43bb5e7d4c55263bf9bf0a2501862589f7644b8c357ceaa1d8762ea2370125f29c85bce7299925588570ba5118860
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- amber_component (1.1.0)
4
+ amber_component (1.1.1)
5
5
  actionview (>= 6)
6
6
  activemodel (>= 6)
7
7
  activesupport (>= 6)
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Maintainability](https://api.codeclimate.com/v1/badges/ad84af499e9791933a87/maintainability)](https://codeclimate.com/github/amber-ruby/amber_component/maintainability)
4
4
  [![CI badge](https://github.com/amber-ruby/amber_component/actions/workflows/ci_ruby.yml/badge.svg)](https://github.com/amber-ruby/amber_component/actions/workflows/ci_ruby.yml)
5
5
  [![Coverage Badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/Verseth/6a095c79278b074d79feaa4f8ceeb2a8/raw/amber_component__heads_main.json)](https://github.com/amber-ruby/amber_component/actions/workflows/ci_ruby.yml)
6
- [![Downloads](https://ruby-gem-downloads-badge.herokuapp.com/amber_component)]((https://rubygems.org/gems/amber_component))
6
+ <!-- [![Downloads](https://ruby-gem-downloads-badge.herokuapp.com/amber_component)]((https://rubygems.org/gems/amber_component)) -->
7
7
 
8
8
  <img src="banner.png" width="500px" style="margin-bottom: 2rem;"/>
9
9
 
@@ -54,6 +54,13 @@ Every component consists of:
54
54
  - a style file (css, scss, sass etc.)
55
55
  - [optional] a JavaScript file with a Stimulus controller (if you installed the gem with `--stimulus`)
56
56
 
57
+ `amber_component` automatically detects what kind of view and stylesheet formats your app is configured to use.
58
+
59
+ So if you've got `haml-rails`, components will be generated with `haml`. When your app uses `slim-rails`, components will be generated with `slim`. When your `Gemfile` contains `sassc-rails`, components will use `scss` etc.
60
+
61
+ All of these formats can be overridden in
62
+ an initializer or by adding arguments to the component generator.
63
+
57
64
  ```
58
65
  app/components/
59
66
  ├─ [name]_component.rb
@@ -197,13 +204,20 @@ This will generate a new component in `app/components/[name]_component.rb` along
197
204
  app/components/
198
205
  ├─ [name]_component.rb
199
206
  └─ [name]_component/
200
- ├─ style.css
201
- ├─ view.html.erb
207
+ ├─ style.css # may be `.scss` or `.sass`
208
+ ├─ view.html.erb # may be `.haml` or `.slim`
202
209
  └─ controller.js # if stimulus is configured
203
210
  test/components/
204
211
  └─ [name]_component_test.rb
205
212
  ```
206
213
 
214
+ View and stylesheet formats can be overridden by providing options.
215
+
216
+ ```
217
+ -v, [--view=VIEW] # Indicate what type of view should be generated eg. [:erb, :haml, :slim]
218
+ --styles, -c, [--css=CSS] # Indicate what type of styles should be generated eg. [:css, :scss, :sass]
219
+ ```
220
+
207
221
  ### Component properties
208
222
 
209
223
  There is a neat prop DSL.
@@ -411,6 +425,21 @@ This makes component views very flexible and convenient.
411
425
  <% end %>
412
426
  ```
413
427
 
428
+ ### Configuration
429
+
430
+ This gem can be configured in an initializer.
431
+ If you used the installer generator it should already be present.
432
+
433
+ ```ruby
434
+ # config/initializers/amber_component.rb
435
+
436
+ ::AmberComponent.configure do |c|
437
+ c.stimulus = nil # [nil, :importmap, :webpacker, :jsbundling, :webpack, :esbuild, :rollup]
438
+ c.stylesheet_format = :css # [:css, :scss, :sass]
439
+ c.view_format = :erb # [:erb, :haml, :slim]
440
+ end
441
+ ```
442
+
414
443
  ### Testing Components
415
444
 
416
445
  ### Rails
@@ -4,18 +4,36 @@ module ::AmberComponent
4
4
  # Object which stores configuration options
5
5
  # for this gem.
6
6
  class Configuration
7
- # @return [Array<Symbol>]
8
- STIMULUS_INTEGRATIONS = %i[importmap jsbundling webpack esbuild rollup].freeze
7
+ # @return [Set<Symbol>]
8
+ STIMULUS_INTEGRATIONS = ::Set[nil, :importmap, :webpacker, :jsbundling, :webpack, :esbuild, :rollup]
9
+ # @return [Set<Symbol>]
10
+ ALLOWED_STYLES = ::Set.new(%i[css scss sass])
11
+ # @return [Set<Symbol>]
12
+ ALLOWED_VIEWS = ::Set.new(%i[erb haml slim])
9
13
 
10
14
  # How Stimulus.js is bundled in this app.
11
- # Possible values: `[nil, :importmap, :jsbundling, :webpack, :esbuild, :rollup]`
15
+ # Possible values: `[nil, :importmap, :webpacker, :jsbundling, :webpack, :esbuild, :rollup]`
12
16
  # `nil` indicates that stimulus should not be used (default behaviour).
13
17
  #
14
18
  # @return [Symbol, nil]
15
19
  attr_reader :stimulus
16
20
 
21
+ # The default format that the generators will use
22
+ # for the view/template file of a component.
23
+ # Possible values: `[nil, :erb, :haml, :slim]`
24
+ #
25
+ # @return [Symbol, nil]
26
+ attr_reader :view_format
27
+
28
+ # The default format that the generators will use
29
+ # for the stylesheets of a component.
30
+ # Possible values: `[nil, :css, :scss, :sass]`
31
+ #
32
+ # @return [Symbol, nil]
33
+ attr_reader :stylesheet_format
34
+
17
35
  # How Stimulus.js is bundled in this app.
18
- # Possible values: `[nil, :importmap, :jsbundling, :webpack, :esbuild, :rollup]`
36
+ # Possible values: `[nil, :importmap, :webpacker, :jsbundling, :webpack, :esbuild, :rollup]`
19
37
  # `nil` indicates that stimulus should not be used (default behaviour).
20
38
  #
21
39
  # @param val [Symbol, String, nil]
@@ -23,13 +41,37 @@ module ::AmberComponent
23
41
  val = val&.to_sym
24
42
  unless val.nil? || STIMULUS_INTEGRATIONS.include?(val)
25
43
  raise(::ArgumentError,
26
- "Invalid value for `stimulus` bundling. " \
44
+ "Invalid value for `#{__method__}` bundling. " \
27
45
  "Received #{val.inspect}, expected one of #{STIMULUS_INTEGRATIONS.inspect}")
28
46
  end
29
47
 
30
48
  @stimulus = val
31
49
  end
32
50
 
51
+ # @param val [Symbol, String, nil]
52
+ def stylesheet_format=(val)
53
+ val = val&.to_sym
54
+ unless val.nil? || ALLOWED_STYLES.include?(val)
55
+ raise(::ArgumentError,
56
+ "Invalid value for `#{__method__}`. " \
57
+ "Received #{val.inspect}, expected one of #{ALLOWED_STYLES.inspect}")
58
+ end
59
+
60
+ @stylesheet_format = val
61
+ end
62
+
63
+ # @param val [Symbol, String, nil]
64
+ def view_format=(val)
65
+ val = val&.to_sym
66
+ unless val.nil? || ALLOWED_VIEWS.include?(val)
67
+ raise(::ArgumentError,
68
+ "Invalid value for `#{__method__}`. " \
69
+ "Received #{val.inspect}, expected one of #{ALLOWED_VIEWS.inspect}")
70
+ end
71
+
72
+ @view_format = val
73
+ end
74
+
33
75
  # @return [Boolean]
34
76
  def stimulus?
35
77
  !@stimulus.nil?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ::AmberComponent
4
- VERSION = '1.1.0'
4
+ VERSION = '1.1.1'
5
5
  end
@@ -8,7 +8,7 @@ module ::AmberComponent
8
8
  # @return [Set<Symbol>]
9
9
  VIEW_TYPES_WITH_RUBY = ::Set[:erb, :haml, :slim].freeze
10
10
  # @return [Set<Symbol>]
11
- ALLOWED_VIEW_TYPES = ::Set[:erb, :haml, :slim, :html, :md, :markdown].freeze
11
+ ALLOWED_VIEW_TYPES = ::Set[:erb, :haml, :slim, :html].freeze
12
12
  # @return [Regexp]
13
13
  VIEW_FILE_REGEXP = /^view\./.freeze
14
14
 
@@ -3,6 +3,7 @@
3
3
  require 'active_support'
4
4
  require 'active_support/core_ext'
5
5
  require 'pathname'
6
+ require 'set'
6
7
 
7
8
  require_relative 'amber_component/configuration'
8
9
 
@@ -10,14 +10,22 @@ module ::AmberComponent
10
10
  desc 'Install the AmberComponent gem'
11
11
  source_root ::File.expand_path('templates', __dir__)
12
12
 
13
- # @return [Array<Symbol>]
14
- STIMULUS_INTEGRATIONS = %i[stimulus importmap jsbundling webpack esbuild rollup].freeze
15
-
16
13
  class_option :stimulus,
17
14
  desc: "Configure the app to use Stimulus.js wih components to make them interactive " \
18
- "[options: importmap (default), jsbundling, webpack, esbuild, rollup]"
15
+ "[options: importmap (default), webpacker (legacy), jsbundling, webpack, esbuild, rollup]"
16
+
17
+ class_option :styles,
18
+ desc: "Configure the app to generate components with a particular stylesheet format " \
19
+ "[options: css (default), scss, sass]"
20
+
21
+ class_option :views,
22
+ desc: "Configure the app to generate components with a particular view format " \
23
+ "[options: erb (default), haml, slim]"
19
24
 
20
25
  def setup
26
+ detect_stimulus
27
+ detect_styles
28
+ detect_views
21
29
  copy_file 'application_component.rb', 'app/components/application_component.rb'
22
30
  copy_file 'application_component_test_case.rb', 'test/application_component_test_case.rb'
23
31
  append_file 'test/test_helper.rb', "require_relative 'application_component_test_case'"
@@ -30,40 +38,107 @@ module ::AmberComponent
30
38
  require_components_css_in 'app/assets/stylesheets/application.scss.sass'
31
39
  require_components_css_in 'app/assets/stylesheets/application.sass.scss'
32
40
  configure_stimulus
41
+ create_initializer
33
42
  end
34
43
 
35
44
  private
36
45
 
37
- def configure_stimulus
38
- stimulus = options[:stimulus]&.to_sym
39
- return unless stimulus
46
+ def detect_styles
47
+ styles_option = options[:styles]&.to_sym
48
+ if !styles_option.nil? && !Configuration::ALLOWED_STYLES.include?(styles_option)
49
+ raise ::ArgumentError, "no such `stylesheet_format` as #{styles_option.inspect}"
50
+ end
40
51
 
41
- case stimulus
52
+ @styles =
53
+ if styles_option
54
+ styles_option
55
+ elsif defined?(::SassC)
56
+ :scss
57
+ else
58
+ :css
59
+ end
60
+ end
61
+
62
+ def detect_views
63
+ views_option = options[:views]&.to_sym
64
+ if !views_option.nil? && !Configuration::ALLOWED_VIEWS.include?(views_option)
65
+ raise ::ArgumentError, "no such `view_format` as #{views_option.inspect}"
66
+ end
67
+
68
+ @views =
69
+ if views_option
70
+ views_option
71
+ elsif defined?(::Haml)
72
+ :haml
73
+ elsif defined?(::Slim)
74
+ :slim
75
+ else
76
+ :erb
77
+ end
78
+ end
79
+
80
+ def detect_stimulus
81
+ stimulus_option = options[:stimulus]&.to_sym
82
+ return unless stimulus_option
83
+
84
+ case stimulus_option
42
85
  when :stimulus
43
86
  if defined?(::Jsbundling)
44
- stimulus_integration = :jsbundling
45
- configure_stimulus_jsbundling
87
+ stimulus_jsbundling!
88
+ elsif defined?(::Webpacker)
89
+ stimulus_webpacker!
46
90
  else
47
- stimulus_integration = :importmap
48
- configure_stimulus_importmap
91
+ stimulus_importmap!
49
92
  end
50
93
  when :importmap
51
- stimulus_integration = :importmap
52
- configure_stimulus_importmap
94
+ stimulus_importmap!
53
95
  when :jsbundling, :webpack, :esbuild, :rollup
54
- stimulus_integration = :jsbundling
55
- configure_stimulus_jsbundling
96
+ stimulus_jsbundling!
97
+ when :webpacker
98
+ stimulus_webpacker!
99
+ else
100
+ raise ::ArgumentError,
101
+ "no such stimulus integration as `#{options[:stimulus].inspect}`"
56
102
  end
103
+ end
104
+
105
+ def assert_styles
106
+ return if options[:styles].nil?
107
+ return if options[:styles].nil?
108
+ end
57
109
 
110
+ def configure_stimulus
111
+ case @stimulus
112
+ when :importmap then configure_stimulus_importmap
113
+ when :jsbundling then configure_stimulus_jsbundling
114
+ when :webpacker then configure_stimulus_webpacker
115
+ end
116
+ end
117
+
118
+ def create_initializer
58
119
  create_file 'config/initializers/amber_component.rb', <<~RUBY
59
120
  # frozen_string_literal: true
60
121
 
61
122
  ::AmberComponent.configure do |c|
62
- c.stimulus = :#{stimulus_integration}
123
+ c.stimulus = #{@stimulus.inspect} # #{Configuration::STIMULUS_INTEGRATIONS.to_a}
124
+ c.stylesheet_format = #{@styles.inspect} # #{Configuration::ALLOWED_STYLES.to_a}
125
+ c.view_format = #{@views.inspect} # #{Configuration::ALLOWED_VIEWS.to_a}
63
126
  end
64
127
  RUBY
65
128
  end
66
129
 
130
+ def stimulus_jsbundling!
131
+ @stimulus = :jsbundling
132
+ end
133
+
134
+ def stimulus_importmap!
135
+ @stimulus = :importmap
136
+ end
137
+
138
+ def stimulus_webpacker!
139
+ @stimulus = :webpacker
140
+ end
141
+
67
142
  def configure_stimulus_importmap
68
143
  install_importmap
69
144
  install_stimulus
@@ -88,6 +163,17 @@ module ::AmberComponent
88
163
  JS
89
164
  end
90
165
 
166
+ def configure_stimulus_webpacker
167
+ install_stimulus
168
+ append_file 'app/javascript/packs/application.js', %(import "controllers"\n)
169
+ append_file 'app/javascript/controllers/index.js', %(import "./components"\n)
170
+ create_file 'app/javascript/controllers/components.js', <<~JS
171
+ // This file has been created by `amber_component` and will
172
+ // register all stimulus controllers from your components
173
+ import { application } from "./application"
174
+ JS
175
+ end
176
+
91
177
  # @return [void]
92
178
  def install_importmap
93
179
  return if ::File.exist?('config/importmap.rb') && defined?(::Importmap)
@@ -7,33 +7,26 @@ class AmberComponentGenerator < ::Rails::Generators::NamedBase
7
7
  desc 'Generate a new component'
8
8
  source_root ::File.expand_path('templates', __dir__)
9
9
 
10
- # @return [Array<Symbol>]
11
- VIEW_FORMATS = %i[html erb haml slim].freeze
12
- # @return [Array<Symbol>]
13
- STYLE_FORMATS = %i[css scss sass].freeze
14
-
15
10
  class_option :view,
16
11
  aliases: ['-v'],
17
- desc: "Indicate what type of view should be generated eg. #{VIEW_FORMATS}"
12
+ desc: "Indicate what type of view should be generated " \
13
+ "eg. #{::AmberComponent::Configuration::ALLOWED_VIEWS}"
18
14
 
19
15
  class_option :css,
20
- aliases: ['--style', '-c'],
21
- desc: "Indicate what type of styles should be generated eg. #{STYLE_FORMATS}"
16
+ aliases: ['--styles', '-c'],
17
+ desc: "Indicate what type of styles should be generated " \
18
+ "eg. #{::AmberComponent::Configuration::ALLOWED_STYLES}"
22
19
 
23
20
  def generate_component
24
- @view_format = (options[:view] || :html).to_sym
25
- @view_format = :html if @view_format == :erb
26
-
27
- @style_format = options[:css]&.to_sym
21
+ set_view_format
22
+ set_stylesheet_format
28
23
 
29
- unless VIEW_FORMATS.include? @view_format
30
- puts "No such view format as `#{@view_format}`"
31
- return
24
+ unless ::AmberComponent::Configuration::ALLOWED_VIEWS.include? @view_format
25
+ raise ::ArgumentError, "No such view format as `#{@view_format}`"
32
26
  end
33
27
 
34
- if !@style_format.nil? && STYLE_FORMATS.include?(@style_format)
35
- puts "No such css/style format as `#{@style_format}`"
36
- return
28
+ unless ::AmberComponent::Configuration::ALLOWED_STYLES.include?(@stylesheet_format)
29
+ raise ::ArgumentError, "No such css/style format as `#{@stylesheet_format}`"
37
30
  end
38
31
 
39
32
  template 'component.rb.erb', "app/components/#{file_path}.rb"
@@ -53,6 +46,14 @@ class AmberComponentGenerator < ::Rails::Generators::NamedBase
53
46
 
54
47
  private
55
48
 
49
+ def set_view_format
50
+ @view_format = options[:view]&.to_sym || ::AmberComponent.configuration.view_format || :erb
51
+ end
52
+
53
+ def set_stylesheet_format
54
+ @stylesheet_format = options[:style]&.to_sym || ::AmberComponent.configuration.stylesheet_format || :css
55
+ end
56
+
56
57
  # @return [Boolean]
57
58
  def stimulus?
58
59
  ::AmberComponent.configuration.stimulus?
@@ -90,9 +91,10 @@ class AmberComponentGenerator < ::Rails::Generators::NamedBase
90
91
 
91
92
  # @return [void]
92
93
  def create_stylesheet
93
- if (@style_format.nil? && defined?(::SassC)) || @style_format == :scss
94
+ case @stylesheet_format
95
+ when :scss
94
96
  template 'style.scss.erb', "app/components/#{file_path}/style.scss"
95
- elsif @style_format == :sass
97
+ when :sass
96
98
  template 'style.sass.erb', "app/components/#{file_path}/style.sass"
97
99
  else
98
100
  template 'style.css.erb', "app/components/#{file_path}/style.css"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amber_component
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruby-Amber
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2022-11-13 00:00:00.000000000 Z
13
+ date: 2022-11-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: actionview