Class: Observ::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/observ/install/install_generator.rb

Overview

Generator for installing Observ assets in a Rails application

Usage:

rails generate observ:install
rails generate observ:install --styles-dest=custom/path
rails generate observ:install --js-dest=custom/path
rails generate observ:install --skip-index
rails generate observ:install --skip-routes  # Don't auto-mount engine
rails generate observ:install --force        # Skip confirmation prompt
rails generate observ:install --skip-vite-entrypoint  # Don't generate Vite entry point

Defined Under Namespace

Classes: GeneratorLogger

Instance Method Summary collapse

Instance Method Details

#confirm_installationObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/generators/observ/install/install_generator.rb', line 56

def confirm_installation
  return if options[:force]

  styles_dest = options[:styles_dest] || Observ::AssetInstaller::DEFAULT_STYLES_DEST
  js_dest = options[:js_dest] || Observ::AssetInstaller::DEFAULT_JS_DEST

  say "\n"
  say "=" * 80, :cyan
  say "Observ Installation", :cyan
  say "=" * 80, :cyan
  say "\n"

  # Collect files that will actually be copied
  stylesheets_to_copy = collect_files_to_copy("stylesheets", "*.scss", styles_dest)
  js_files_to_copy = collect_files_to_copy("javascripts", "*.js", js_dest)
  index_will_be_generated = !options[:skip_index] && will_generate_index?(js_dest)
  route_will_be_added = !options[:skip_routes] && !route_already_exists?
  vite_entrypoint_will_be_created = !options[:skip_vite_entrypoint] && will_create_vite_entrypoint?

  # Check if there are any changes to make
  total_changes = stylesheets_to_copy.count + js_files_to_copy.count +
                 (index_will_be_generated ? 1 : 0) + (route_will_be_added ? 1 : 0) +
                 (vite_entrypoint_will_be_created ? 1 : 0)

  if total_changes == 0
    say "No changes needed - all files are up to date!", :green
    say "\n"
    return
  end

  say "The following changes will be made:", :yellow
  say "\n"

  # Show stylesheet files that will be copied
  if stylesheets_to_copy.any?
    say "Stylesheets (#{stylesheets_to_copy.count} files to #{styles_dest}):", :yellow
    stylesheets_to_copy.each do |file|
      say "#{File.basename(file)}", :white
    end
    say "\n"
  end

  # Show JavaScript files that will be copied
  if js_files_to_copy.any?
    say "JavaScript Controllers (#{js_files_to_copy.count} files to #{js_dest}):", :yellow
    js_files_to_copy.each do |file|
      say "#{File.basename(file)}", :white
    end
    say "\n"
  end

  # Show generated files
  generated_files = []
  generated_files << "#{js_dest}/index.js (controller index)" if index_will_be_generated
  generated_files << "#{options[:vite_entrypoint_dest]}/observ.js (Vite entry point)" if vite_entrypoint_will_be_created

  if generated_files.any?
    say "Generated Files:", :yellow
    generated_files.each { |file| say "#{file}", :white }
    say "\n"
  end

  # Show routes
  if route_will_be_added
    say "Routes (will be added to config/routes.rb):", :yellow
    say '  mount Observ::Engine, at: "/observ"', :white
    say "\n"
  elsif !options[:skip_routes]
    say "Routes:", :green
    say "  Engine already mounted in config/routes.rb", :green
    say "\n"
  end

  unless yes?("Do you want to proceed with the installation? (y/n)", :yellow)
    say "\nInstallation cancelled.", :red
    exit 0
  end
  say "\n"
end

#install_assetsObject



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/generators/observ/install/install_generator.rb', line 151

def install_assets
  installer = Observ::AssetInstaller.new(
    gem_root: Observ::Engine.root,
    app_root: Rails.root,
    logger: GeneratorLogger.new(self)
  )

  @result = installer.install(
    styles_dest: options[:styles_dest],
    js_dest: options[:js_dest],
    generate_index: !options[:skip_index]
  )
end

#install_vite_entrypointObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/generators/observ/install/install_generator.rb', line 165

def install_vite_entrypoint
  return if options[:skip_vite_entrypoint]

  dest_dir = Rails.root.join(options[:vite_entrypoint_dest])
  dest_file = dest_dir.join("observ.js")

  if dest_file.exist?
    say "  Vite entry point already exists: #{dest_file}", :yellow
    return
  end

  say "Creating Vite entry point...", :cyan
  say "-" * 80, :cyan

  FileUtils.mkdir_p(dest_dir)
  copy_file "observ.js", dest_file
  say "  Created #{dest_file}", :green
  say "\n"
end

#mount_engineObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/generators/observ/install/install_generator.rb', line 136

def mount_engine
  return if options[:skip_routes]

  say "Checking routes...", :cyan
  say "-" * 80, :cyan

  if route_already_exists?
    say "  Engine already mounted in config/routes.rb", :yellow
  else
    route 'mount Observ::Engine, at: "/observ"'
    say "  ✓ Added route: mount Observ::Engine, at: \"/observ\"", :green
  end
  say "\n"
end

#show_post_install_messageObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/generators/observ/install/install_generator.rb', line 185

def show_post_install_message
  say "\n"
  say "=" * 80, :green
  say "Observ installed successfully!", :green
  say "=" * 80, :green
  say "\n"

  if @result[:registration] && @result[:registration][:suggestions]
    say "⚠ Action required:", :yellow
    @result[:registration][:suggestions].each do |suggestion|
      say "  #{suggestion}", :yellow
    end
    say "\n"
  end

  say "Next steps:", :cyan

  if options[:skip_vite_entrypoint]
    say "  1. Import Observ in your application", :cyan
    say "     Add to app/javascript/application.js:", :cyan
    say "     import 'observ'", :cyan
    say "\n"
  else
    say "  1. Add 'observ' to your Vite entrypoints (if not auto-detected)", :cyan
    say "     The entry point was created at: #{options[:vite_entrypoint_dest]}/observ.js", :cyan
    say "\n"
  end

  say "  2. Restart your development server", :cyan
  say "     bin/dev or rails server", :cyan
  say "\n"
  say "  3. Visit /observ in your browser", :cyan
  unless options[:skip_routes]
    say "     (Engine mounted at /observ)", :cyan
  end
  say "\n"
end