Class: Opal::Vite::Rails::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/opal/vite/rails/generators/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_routeObject



65
66
67
# File 'lib/opal/vite/rails/generators/install_generator.rb', line 65

def add_route
  route "get '/opal_demo', to: 'opal_demo#index'"
end

#check_vite_railsObject



12
13
14
15
16
17
18
# File 'lib/opal/vite/rails/generators/install_generator.rb', line 12

def check_vite_rails
  unless defined?(ViteRuby)
    say "ViteRails is not installed. Installing it first...", :yellow
    run "bundle add vite_rails"
    run "bundle exec vite install"
  end
end

#create_application_rbObject



25
26
27
# File 'lib/opal/vite/rails/generators/install_generator.rb', line 25

def create_application_rb
  template "application.rb.tt", "app/opal/application.rb"
end

#create_controllerObject



69
70
71
72
73
74
75
76
# File 'lib/opal/vite/rails/generators/install_generator.rb', line 69

def create_controller
  create_file "app/controllers/opal_demo_controller.rb", <<~RUBY
    class OpalDemoController < ApplicationController
      def index
      end
    end
  RUBY
end

#create_example_viewObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/opal/vite/rails/generators/install_generator.rb', line 53

def create_example_view
  create_file "app/views/opal_demo/index.html.erb", <<~ERB
    <h1>Opal + Vite + Rails Demo</h1>

    <div id="opal-content">
      <p>Check your browser console to see Opal output!</p>
    </div>

    <%= opal_javascript_tag "application" %>
  ERB
end

#create_opal_directoryObject



20
21
22
23
# File 'lib/opal/vite/rails/generators/install_generator.rb', line 20

def create_opal_directory
  empty_directory "app/opal"
  create_file "app/opal/.keep"
end

#create_package_json_entryObject



46
47
48
49
50
51
# File 'lib/opal/vite/rails/generators/install_generator.rb', line 46

def create_package_json_entry
  if File.exist?("package.json")
    say "Adding vite-plugin-opal to package.json...", :green
    say "Run: npm install vite-plugin-opal", :yellow
  end
end

#create_vite_configObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/opal/vite/rails/generators/install_generator.rb', line 29

def create_vite_config
  if File.exist?("vite.config.ts")
    inject_into_file "vite.config.ts", after: "import { defineConfig } from 'vite'\n" do
      "import opal from 'vite-plugin-opal'\n"
    end

    inject_into_file "vite.config.ts", after: "plugins: [\n" do
      "    opal({\n" \
      "      loadPaths: ['./app/opal'],\n" \
      "      sourceMap: true\n" \
      "    }),\n"
    end
  else
    template "vite.config.ts.tt", "vite.config.ts"
  end
end

#show_post_install_messageObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/opal/vite/rails/generators/install_generator.rb', line 78

def show_post_install_message
  say "\n" + "="*60, :green
  say "Opal-Vite installed successfully!", :green
  say "="*60, :green
  say "\nNext steps:", :yellow
  say "  1. Install JavaScript dependencies:", :cyan
  say "     npm install vite-plugin-opal"
  say "\n  2. Start Vite dev server:", :cyan
  say "     bin/vite dev"
  say "\n  3. Start Rails server:", :cyan
  say "     rails server"
  say "\n  4. Visit:", :cyan
  say "     http://localhost:3000/opal_demo"
  say "\n" + "="*60, :green
end