Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate component stylesheet file with preferred stylesheet engine #28

Merged
merged 4 commits into from
Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 0 additions & 106 deletions features/component_generator.feature

This file was deleted.

58 changes: 58 additions & 0 deletions features/component_generator/base.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Feature: Component generator - Base

Scenario: Component
Given I run `rails new my_app --skip-spring`
And I cd to "my_app"
And I append to "Gemfile" with:
"""
gem 'webpacker', '~> 3.0'
gem 'komponent', path: '../../..'
"""
When I run `bundle install`
And I run `rails webpacker:install`
And I run `rails generate komponent:install`
And I run `rails generate component AwesomeButton`
And I cd to "frontend/components"
Then the following files should exist:
| awesome_button/_awesome_button.html.erb |
| awesome_button/awesome_button.scss |
| awesome_button/awesome_button.js |
| awesome_button/awesome_button_component.rb |
And the file named "index.js" should contain:
"""
import "components/awesome_button/awesome_button";
"""

Scenario: Component with namespaces
Given I run `rails new my_app --skip-spring`
And I cd to "my_app"
And I append to "Gemfile" with:
"""
gem 'webpacker', '~> 3.0'
gem 'komponent', path: '../../..'
"""
When I run `bundle install`
And I run `rails webpacker:install`
And I run `rails generate komponent:install`
And I run `rails generate component admin/sub_admin/AwesomeButton`
And I cd to "frontend/components"
Then the following files should exist:
| admin/index.js |
| admin/sub_admin/index.js |
| admin/sub_admin/awesome_button/_awesome_button.html.erb |
| admin/sub_admin/awesome_button/awesome_button.scss |
| admin/sub_admin/awesome_button/awesome_button.js |
| admin/sub_admin/awesome_button/admin_sub_admin_awesome_button_component.rb |
And the file named "index.js" should contain:
"""
import "components/admin";
"""
And the file named "admin/index.js" should contain:
"""
import "components/admin/sub_admin";
"""
And the file named "admin/sub_admin/index.js" should contain:
"""
import "components/admin/sub_admin/awesome_button/awesome_button";
"""

38 changes: 38 additions & 0 deletions features/component_generator/locale.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Feature: Component generator - Locale option

Scenario: Component with `--locale` option
Given I run `rails new my_app --skip-spring`
And I cd to "my_app"
And I append to "Gemfile" with:
"""
gem 'webpacker', '~> 3.0'
gem 'komponent', path: '../../..'
"""
When I run `bundle install`
And I run `rails webpacker:install`
And I run `rails generate komponent:install`
And I run `rails generate component AwesomeButton --locale`
And I cd to "frontend/components"
Then a file named "awesome_button/awesome_button.en.yml" should exist

Scenario: Component with `--locale` option and additional `fr` locale
Given I run `rails new my_app --skip-spring`
And I cd to "my_app"
And I append to "Gemfile" with:
"""
gem 'webpacker', '~> 3.0'
gem 'komponent', path: '../../..'
"""
And a file named "config/initializers/custom_configuration.rb" with:
"""
Rails.application.config.i18n.available_locales = [:en, :fr]
"""
When I run `bundle install`
And I run `rails webpacker:install`
And I run `rails generate komponent:install`
And I run `rails generate component AwesomeButton --locale`
And I cd to "frontend/components/awesome_button"
Then the following files should exist:
| awesome_button.en.yml |
| awesome_button.fr.yml |

70 changes: 70 additions & 0 deletions features/component_generator/stylesheet_engine.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Feature: Component generator - Stylesheet engine

Scenario: Component with `scss` stylesheet engine
Given I run `rails new my_app --skip-spring`
And I cd to "my_app"
And I append to "Gemfile" with:
"""
gem 'webpacker', '~> 3.0'
gem 'komponent', path: '../../..'
"""
When I run `bundle install`
And I run `rails webpacker:install`
And I run `rails generate komponent:install`
And I run `rails generate component AwesomeButton`
And I cd to "frontend/components/awesome_button"
Then a file named "awesome_button.scss" should exist

Scenario: Component with `sass` stylesheet engine set in `generators.stylesheet_engine`
Given I run `rails new my_app --skip-spring`
And I cd to "my_app"
And I append to "Gemfile" with:
"""
gem 'webpacker', '~> 3.0'
gem 'komponent', path: '../../..'
"""
And a file named "config/initializers/custom_configuration.rb" with:
"""
Rails.application.config.generators.stylesheet_engine = :sass
"""
When I run `bundle install`
And I run `rails webpacker:install`
And I run `rails generate komponent:install`
And I run `rails generate component AwesomeButton`
And I cd to "frontend/components/awesome_button"
Then a file named "awesome_button.scss" should exist

Scenario: Component with `sass` stylesheet engine set in `sass.preferred syntax`
Given I run `rails new my_app --skip-spring`
And I cd to "my_app"
And I append to "Gemfile" with:
"""
gem 'webpacker', '~> 3.0'
gem 'komponent', path: '../../..'
"""
And a file named "config/initializers/custom_configuration.rb" with:
"""
Rails.application.config.sass.preferred_syntax = :sass
"""
When I run `bundle install`
And I run `rails webpacker:install`
And I run `rails generate komponent:install`
And I run `rails generate component AwesomeButton`
And I cd to "frontend/components/awesome_button"
Then a file named "awesome_button.sass" should exist

Scenario: Component with `css` stylesheet engine
Given I run `rails new my_app --skip-spring`
And I cd to "my_app"
And I append to "Gemfile" with:
"""
gem 'webpacker', '~> 3.0'
gem 'komponent', path: '../../..'
"""
And I remove "sass-rails" gem
When I run `bundle install`
And I run `rails webpacker:install`
And I run `rails generate komponent:install`
And I run `rails generate component AwesomeButton`
And I cd to "frontend/components/awesome_button"
Then a file named "awesome_button.css" should exist
48 changes: 48 additions & 0 deletions features/component_generator/template_engine.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Feature: Component generator - Template engine

Scenario: Component with `erb` template engine
Given I run `rails new my_app --skip-spring`
And I cd to "my_app"
And I append to "Gemfile" with:
"""
gem 'webpacker', '~> 3.0'
gem 'komponent', path: '../../..'
"""
When I run `bundle install`
And I run `rails webpacker:install`
And I run `rails generate komponent:install`
And I run `rails generate component AwesomeButton`
And I cd to "frontend/components/awesome_button"
Then a file named "_awesome_button.html.erb" should exist

Scenario: Component with `haml` template engine
Given I run `rails new my_app --skip-spring`
And I cd to "my_app"
And I append to "Gemfile" with:
"""
gem 'haml-rails'
gem 'webpacker', '~> 3.0'
gem 'komponent', path: '../../..'
"""
When I run `bundle install`
And I run `rails webpacker:install`
And I run `rails generate komponent:install`
And I run `rails generate component AwesomeButton`
And I cd to "frontend/components/awesome_button"
Then a file named "_awesome_button.html.haml" should exist

Scenario: Component with `slim` template engine
Given I run `rails new my_app --skip-spring`
And I cd to "my_app"
And I append to "Gemfile" with:
"""
gem 'slim-rails'
gem 'webpacker', '~> 3.0'
gem 'komponent', path: '../../..'
"""
When I run `bundle install`
And I run `rails webpacker:install`
And I run `rails generate komponent:install`
And I run `rails generate component AwesomeButton`
And I cd to "frontend/components/awesome_button"
Then a file named "_awesome_button.html.slim" should exist
14 changes: 14 additions & 0 deletions features/step_definitions/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Given("I remove {string} gem") do |gem_name|
file_name = "Gemfile"
file_name = expand_path(file_name)

tmp = Tempfile.new

File.foreach(file_name) do |line|
tmp.puts(line) unless /#{gem_name}/ =~ line
end

tmp.close
FileUtils.mv(tmp.path, file_name)
tmp.unlink
end
22 changes: 20 additions & 2 deletions lib/generators/component/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def create_view_file
end

def create_css_file
template "css.erb", component_path + "#{component_name}.css"
template "#{stylesheet_engine}.erb", component_path + "#{component_name}.#{stylesheet_engine}"
end

def create_js_file
Expand Down Expand Up @@ -87,10 +87,28 @@ def component_name
end

def template_engine
Rails.application.config.app_generators.rails[:template_engine] || :erb
app_generators.rails[:template_engine] || :erb
end

def stylesheet_engine
if sass = rails_configuration.try(:sass)
sass[:preferred_syntax]
else
:css
end
end

def locale?
options[:locale]
end

private

def rails_configuration
Rails.application.config
end

def app_generators
rails_configuration.app_generators
end
end
1 change: 1 addition & 0 deletions lib/generators/component/templates/sass.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.<%= component_class_name %>
1 change: 1 addition & 0 deletions lib/generators/component/templates/scss.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.<%= component_class_name %> {}