Class: Frozen::Generators::UiGenerator

Inherits:
FrozenRails::Generator show all
Defined in:
lib/generators/frozen/ui_generator.rb

Constant Summary

Constants included from Nodeable

Nodeable::REQUIRED_EXECUTABLES

Instance Method Summary collapse

Instance Method Details

#add_gemsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generators/frozen/ui_generator.rb', line 16

def add_gems
  add_frozen_gems <<~RUBY
    # frozen:ui
    gem "view_component"
    gem "precompiled_assets"
  RUBY

  comment_lines "Gemfile", /propshaft/
  comment_lines "Gemfile", /jsbundling-rails/
  add_frozen_gems <<~RUBY, env: "development"
    # frozen:ui
    gem "listen"
  RUBY

  add_frozen_gems <<~RUBY, env: "local"
    # frozen:ui
    gem "lookbook"
  RUBY
end

#add_js_dependenciesObject



55
56
57
58
59
60
# File 'lib/generators/frozen/ui_generator.rb', line 55

def add_js_dependencies
  in_root do
    run "yarn add unpoly", abort_on_failure: true
    run "yarn add --dev jasmine jasmine_dom_matchers", abort_on_failure: true
  end
end

#add_routesObject



164
165
166
167
168
169
170
171
172
# File 'lib/generators/frozen/ui_generator.rb', line 164

def add_routes
  append_to_routes <<~RUBY
    # frozen:ui
    if Rails.env.local?
      mount Lookbook::Engine, at: "/lookbook"
      resources :jasmine, only: [ :index ]
    end
  RUBY
end

#bundle_gemsObject



36
37
38
# File 'lib/generators/frozen/ui_generator.rb', line 36

def bundle_gems
  bundle!
end

#check_prerequisitesObject



10
11
12
13
14
# File 'lib/generators/frozen/ui_generator.rb', line 10

def check_prerequisites
  in_root do
    require_js_toolchain!
  end
end

#configure_esbuildObject



46
47
48
49
50
51
52
53
# File 'lib/generators/frozen/ui_generator.rb', line 46

def configure_esbuild
  in_root do
    run "yarn add --dev esbuild-plugin-import-glob esbuild-plugin-text-replace esbuild-manifest-plugin", abort_on_failure: true
    copy_file "esbuild.config.js"
    run 'npm pkg set type="module"', abort_on_failure: true
    run 'npm pkg set scripts.build="node esbuild.config.js"', abort_on_failure: true
  end
end

#copy_demo_componentObject



174
175
176
177
# File 'lib/generators/frozen/ui_generator.rb', line 174

def copy_demo_component
  copy_directory "demo_component", "app/components"
  copy_directory "demo_component_tests", "test/components"
end

#replace_assetsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/frozen/ui_generator.rb', line 62

def replace_assets
  # Remove everything from app/assets except app/assets/rouge
  in_root do
    assets_path = Pathname.new("app/assets")
    FileUtils.mkdir_p(assets_path) unless assets_path.exist?

    assets_path.children.each do |entry|
      next if entry.basename.to_s == "rouge"

      FileUtils.rm_rf(entry)
    end
  end
  remove_dir "app/javascript"
  copy_directory "assets", "app/assets"
  copy_file "application.html.erb", "app/views/layouts/application.html.erb", force: true
end

#setup_jasmineObject



159
160
161
162
# File 'lib/generators/frozen/ui_generator.rb', line 159

def setup_jasmine
  copy_directory "jasmine/views", "app/views"
  copy_file "jasmine/controllers/jasmine_controller.rb", "app/controllers/jasmine_controller.rb"
end

#setup_lookbookObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/generators/frozen/ui_generator.rb', line 141

def setup_lookbook
  copy_file "lookbook/lookbook_helper.rb", "app/helpers/lookbook_helper.rb"
  copy_file "lookbook/component_preview.html.erb", "app/views/layouts/component_preview.html.erb"

  config = <<~RUBY
    # frozen:ui
    config.lookbook.preview_paths = [ "test/components" ]
    config.lookbook.preview_collection_label = "Components"
    config.lookbook.page_collection_label = "Documentation"
    config.lookbook.page_route = "docs"
    config.lookbook.page_paths = [ "test/components/docs" ]
    config.lookbook.ui_theme = "zinc"
  RUBY

  append_to_application_config config, env: "development"
  append_to_application_config config, env: "test"
end

#setup_view_componentObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/generators/frozen/ui_generator.rb', line 125

def setup_view_component
  copy_file "application_component.rb", "app/components/application_component.rb"
  copy_directory "lib", "lib"

  append_to_application_config <<~RUBY
    # frozen:ui
    config.i18n.available_locales = [ :en, :de ]
    config.view_component.parent_class = "ApplicationComponent"
    config.view_component.generate.preview = true
    config.view_component.generate.preview_path = "test/components"
    config.view_component.generate.locale = true
    config.view_component.previews.default_layout = "component_preview"
    config.asset_path = "/assets"
  RUBY
end

#switch_to_precompiled_assetsObject



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
# File 'lib/generators/frozen/ui_generator.rb', line 79

def switch_to_precompiled_assets
  in_root do
    # CAUTION: Very brittle!
    FileUtils.move("content/pages/rails-static", "app/assets/images/pages/rails-static")
  end
  comment_lines "config/application.rb", /config\.assets\.paths/
  comment_lines "config/environments/development.rb", /config\.assets\.quiet/
  remove_file "config/initializers/assets.rb"

  insert_into_file ".github/workflows/ci.yml", <<YAML1, before: "\n      - name: Run tests", force: true

- uses: actions/setup-node@v6
  with:
    node-version-file: '.nvmrc'
    cache: 'yarn'
- run: npm install -g yarn
- run: yarn install --frozen-lockfile
YAML1

  insert_into_file ".github/workflows/ci.yml", <<YAML2, before: "\n      - name: Run System Tests", force: true

- uses: actions/setup-node@v6
  with:
    node-version-file: '.nvmrc'
    cache: 'yarn'
- run: npm install -g yarn
- run: yarn install --frozen-lockfile
YAML2

  insert_into_file ".github/workflows/parklife.yml", <<YAML3, after: "bundler-cache: true\n"

    - uses: actions/setup-node@v6
with:
  node-version-file: '.nvmrc'
  cache: 'yarn'
    - run: npm install -g yarn
    - run: yarn install --frozen-lockfile

YAML3

  insert_into_file ".gitlab-ci.yml", <<YAML4, after: "    - bundle install\n"
    - npm install -g yarn
    - yarn install --frozen-lockfile
YAML4
end


40
41
42
43
44
# File 'lib/generators/frozen/ui_generator.rb', line 40

def symlink_node_version
  in_root do
    create_link ".nvmrc", ".node-version" if File.exist?(".node-version")
  end
end