Skip to content

Commit

Permalink
Generators for installation and example view component
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Barone committed Aug 8, 2024
1 parent 422afea commit ff1b07b
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 15 deletions.
147 changes: 147 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,76 @@ PATH
remote: .
specs:
rails-storybook (0.1.0)
rails (~> 7.0)
slim (~> 5.2)

GEM
remote: https://rubygems.org/
specs:
actioncable (7.1.3.4)
actionpack (= 7.1.3.4)
activesupport (= 7.1.3.4)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
zeitwerk (~> 2.6)
actionmailbox (7.1.3.4)
actionpack (= 7.1.3.4)
activejob (= 7.1.3.4)
activerecord (= 7.1.3.4)
activestorage (= 7.1.3.4)
activesupport (= 7.1.3.4)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.1.3.4)
actionpack (= 7.1.3.4)
actionview (= 7.1.3.4)
activejob (= 7.1.3.4)
activesupport (= 7.1.3.4)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.2)
actionpack (7.1.3.4)
actionview (= 7.1.3.4)
activesupport (= 7.1.3.4)
nokogiri (>= 1.8.5)
racc
rack (>= 2.2.4)
rack-session (>= 1.0.1)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
actiontext (7.1.3.4)
actionpack (= 7.1.3.4)
activerecord (= 7.1.3.4)
activestorage (= 7.1.3.4)
activesupport (= 7.1.3.4)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.1.3.4)
activesupport (= 7.1.3.4)
builder (~> 3.1)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
activejob (7.1.3.4)
activesupport (= 7.1.3.4)
globalid (>= 0.3.6)
activemodel (7.1.3.4)
activesupport (= 7.1.3.4)
activerecord (7.1.3.4)
activemodel (= 7.1.3.4)
activesupport (= 7.1.3.4)
timeout (>= 0.4.0)
activestorage (7.1.3.4)
actionpack (= 7.1.3.4)
activejob (= 7.1.3.4)
activerecord (= 7.1.3.4)
activesupport (= 7.1.3.4)
marcel (~> 1.0)
activesupport (7.1.3.4)
base64
bigdecimal
Expand All @@ -19,13 +85,19 @@ GEM
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
builder (3.3.0)
concurrent-ruby (1.3.3)
connection_pool (2.4.1)
crass (1.0.6)
date (3.3.4)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
diff-lcs (1.5.1)
drb (2.2.1)
erubi (1.13.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
Expand All @@ -34,8 +106,34 @@ GEM
reline (>= 0.4.2)
json (2.7.2)
language_server-protocol (3.17.0.3)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
net-pop
net-smtp
marcel (1.0.4)
mini_mime (1.1.5)
mini_portile2 (2.8.7)
minitest (5.24.1)
mutex_m (0.2.0)
net-imap (0.4.14)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.2)
timeout
net-smtp (0.5.0)
net-protocol
nio4r (2.7.3)
nokogiri (1.16.7)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-darwin)
racc (~> 1.4)
parallel (1.25.1)
parser (3.3.4.0)
ast (~> 2.4.1)
Expand All @@ -44,6 +142,42 @@ GEM
stringio
racc (1.8.1)
rack (3.1.7)
rack-session (2.0.0)
rack (>= 3.0.0)
rack-test (2.1.0)
rack (>= 1.3)
rackup (2.1.0)
rack (>= 3)
webrick (~> 1.8)
rails (7.1.3.4)
actioncable (= 7.1.3.4)
actionmailbox (= 7.1.3.4)
actionmailer (= 7.1.3.4)
actionpack (= 7.1.3.4)
actiontext (= 7.1.3.4)
actionview (= 7.1.3.4)
activejob (= 7.1.3.4)
activemodel (= 7.1.3.4)
activerecord (= 7.1.3.4)
activestorage (= 7.1.3.4)
activesupport (= 7.1.3.4)
bundler (>= 1.15.0)
railties (= 7.1.3.4)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.1.3.4)
actionpack (= 7.1.3.4)
activesupport (= 7.1.3.4)
irb
rackup (>= 1.0.0)
rake (>= 12.2)
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.2.1)
rdoc (6.7.0)
Expand Down Expand Up @@ -96,11 +230,23 @@ GEM
rubocop-performance
rubocop-rails
ruby-progressbar (1.13.0)
slim (5.2.1)
temple (~> 0.10.0)
tilt (>= 2.1.0)
stringio (3.1.1)
strscan (3.1.0)
temple (0.10.3)
thor (1.3.1)
tilt (2.4.0)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
webrick (1.8.1)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.17)

PLATFORMS
ruby
Expand All @@ -109,6 +255,7 @@ PLATFORMS
DEPENDENCIES
bundler (~> 2.5)
debug (~> 1.6)
rails (~> 7.0)
rails-storybook!
rake (~> 13.2)
rspec (~> 3.13)
Expand Down
62 changes: 62 additions & 0 deletions lib/generators/storybook/example_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'rails/generators'

class Storybook::ExampleGenerator < Rails::Generators::Base
def install_view_components
insert_into_file "Gemfile", "gem 'view_component'"
system "bundle install"
end

def create_example_component
create_file "app/components/example_component.rb", <<~RUBY
# frozen_string_literal: true
class ExampleComponent < ViewComponent::Base
slim_template <<~SLIM
span
= @title
SLIM
def initialize(title:)
@title = title
end
end
RUBY
end

def create_example_component_preview
create_file "test/components/previews/example_component_preview.rb", <<~RUBY
# frozen_string_literal: true
class ExampleComponentPreview < ViewComponent::Preview
layout "storybook"
def default
render(ExampleComponent.new(title: "title"))
end
def with_content_block
render(ExampleComponent.new(title: "This component accepts a block of content")) do
tag.div do
content_tag(:span, "Hello")
end
end
end
end
RUBY
end

def create_example_story
create_file "stories/example_component.stories.json", <<~JSON
{
"title": "ExampleComponent",
"stories": [
{
"name": "default",
"parameters": {
"server": { "id": "example_component/default" }
}
}
]
}
JSON
end
end
57 changes: 57 additions & 0 deletions lib/generators/storybook/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'rails/generators'

class Storybook::InstallGenerator < Rails::Generators::Base
def install_storybook
system "npx storybook@latest init --yes --dev --no-dev --builder webpack5"
system "yarn add @storybook/server --dev"
system "yarn add @storybook/server-webpack5 --dev"
system "yarn install"
insert_into_file "Procfile.dev", "storybook: yarn storybook -p 6006"
end

def install_rack_cors
insert_into_file "Gemfile", " gem 'rack-cors'\n", after: "group :development do\n"
system "bundle install"
create_file "config/initializers/cors.rb", <<~RUBY
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :patch, :put]
end
end
RUBY
end

def create_storybook_layout_slim
create_file "app/views/layouts/storybook.html.slim", <<~SLIM
doctype html
html lang='en'
head
title Trainyard
meta charset="UTF-8"
meta[name="viewport" content="width=device-width,initial-scale=1"]
= csrf_meta_tags
= csp_meta_tag
= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload"
= stylesheet_link_tag "application"
= javascript_pack_tag "application"
body
div
main.ml-64.p-8
== yield
SLIM
end

def update_storybook
# Main.js
insert_into_file ".storybook/main.js", "\n \"@storybook/server\",", after: "addons: ["
gsub_file '.storybook/main.js', "*.stories.@(js|jsx|mjs|ts|tsx)", "*.stories.@(js|jsx|mjs|ts|tsx|json)"
gsub_file '.storybook/main.js', "@storybook/react-webpack5", "@storybook/server-webpack5"

# Preview.js
base_url = "http://localhost:3000/rails/view_components"
insert_into_file ".storybook/preview.js", "\n server: { url: \"#{base_url}\", },", after: "parameters: {"

# insert_into_file ".storybook/main.js", "@storybook/addon-controls", after: "addons: ["
end
end
8 changes: 0 additions & 8 deletions lib/rails/storybook.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/rails/storybook/version.rb

This file was deleted.

6 changes: 6 additions & 0 deletions lib/storybook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "storybook/version"

module Storybook
class Error < StandardError; end
# Your code goes here...
end
5 changes: 5 additions & 0 deletions lib/storybook/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

module Storybook
VERSION = "0.1.0"
end

9 changes: 7 additions & 2 deletions rails-storybook.gemspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "rails/storybook/version"
require "storybook/version"

Gem::Specification.new do |spec|
spec.name = "rails-storybook"
spec.version = Rails::Storybook::VERSION
spec.version = Storybook::VERSION
spec.authors = [ "Nicholas Barone" ]
spec.email = [ "nicktbarone@gmail.com" ]

Expand All @@ -23,6 +23,11 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = [ "lib" ]


spec.add_dependency "rails", "~> 7.0"
spec.add_dependency "slim", "~> 5.2"
spec.add_development_dependency "rails", "~> 7.0"

spec.add_development_dependency "bundler", "~> 2.5"
spec.add_development_dependency "rake", "~> 13.2"
spec.add_development_dependency "rspec", "~> 3.13"
Expand Down

0 comments on commit ff1b07b

Please sign in to comment.