-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add install, scaffold & controller generators
- Loading branch information
Showing
71 changed files
with
2,924 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require "rails/generators/rails/controller/controller_generator" | ||
require "inertia_rails_contrib/generators_helper" | ||
|
||
module Inertia | ||
module Generators | ||
class ControllerGenerator < Rails::Generators::ControllerGenerator | ||
include InertiaRailsContrib::GeneratorsHelper | ||
|
||
source_root File.expand_path("./templates", __dir__) | ||
|
||
remove_hook_for :template_engine | ||
|
||
hook_for :inertia_templates, required: true, default: InertiaRailsContrib::GeneratorsHelper.guess_inertia_template | ||
end | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
lib/generators/inertia/controller/templates/controller.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<% module_namespacing do -%> | ||
class <%= class_name %>Controller < <%= parent_class_name.classify %> | ||
<% actions.each do |action| -%> | ||
def <%= action %> | ||
render inertia: '<%= "#{inertia_base_path}/#{action.camelize}" %>' | ||
end | ||
<%= "\n" unless action == actions.last -%> | ||
<% end -%> | ||
end | ||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
module Inertia | ||
module Generators | ||
class InstallGenerator < Rails::Generators::Base | ||
source_root File.expand_path("./templates", __dir__) | ||
|
||
APPLICATION_LAYOUT = Rails.root.join("app/views/layouts/application.html.erb") | ||
|
||
FRAMEWORKS = { | ||
"react" => { | ||
packages: %w[@inertiajs/react react react-dom], | ||
dev_packages: %w[@vitejs/plugin-react], | ||
vite_plugin_import: "import react from '@vitejs/plugin-react'", | ||
vite_plugin_call: "react()", | ||
copy_files: { | ||
"InertiaExample.jsx" => "app/frontend/pages/InertiaExample.jsx", | ||
"InertiaExample.module.css" => "app/frontend/pages/InertiaExample.module.css", | ||
"../assets/react.svg" => "app/frontend/assets/react.svg", | ||
"../assets/inertia.svg" => "app/frontend/assets/inertia.svg", | ||
"../assets/vite_ruby.svg" => "app/frontend/assets/vite_ruby.svg" | ||
} | ||
}, | ||
"vue" => { | ||
packages: %w[@inertiajs/vue3 vue], | ||
dev_packages: %w[@vitejs/plugin-vue], | ||
vite_plugin_import: "import vue from '@vitejs/plugin-vue'", | ||
vite_plugin_call: "vue()", | ||
copy_files: { | ||
"InertiaExample.vue" => "app/frontend/pages/InertiaExample.vue", | ||
"../assets/vue.svg" => "app/frontend/assets/vue.svg", | ||
"../assets/inertia.svg" => "app/frontend/assets/inertia.svg", | ||
"../assets/vite_ruby.svg" => "app/frontend/assets/vite_ruby.svg" | ||
} | ||
}, | ||
"svelte" => { | ||
packages: %w[@inertiajs/svelte svelte @sveltejs/vite-plugin-svelte], | ||
dev_packages: %w[@vitejs/plugin-vue], | ||
vite_plugin_import: "import { svelte } from '@sveltejs/vite-plugin-svelte'", | ||
vite_plugin_call: "svelte()", | ||
copy_files: { | ||
"svelte.config.js" => "svelte.config.js", | ||
"InertiaExample.svelte" => "app/frontend/pages/InertiaExample.svelte", | ||
"../assets/svelte.svg" => "app/frontend/assets/svelte.svg", | ||
"../assets/inertia.svg" => "app/frontend/assets/inertia.svg", | ||
"../assets/vite_ruby.svg" => "app/frontend/assets/vite_ruby.svg" | ||
} | ||
} | ||
} | ||
|
||
def install | ||
say "Installing Inertia's Rails adapter" | ||
|
||
if package_manager.nil? | ||
say "Could not find a package.json file to install Inertia to.", :red | ||
exit! | ||
end | ||
|
||
unless ruby_vite? | ||
say "Could not find a Vite configuration file `config/vite.json`. This generator only supports Ruby on Rails with Vite.", :red | ||
exit! | ||
end | ||
|
||
install_inertia | ||
|
||
say "Inertia's Rails adapter successfully installed", :green | ||
end | ||
|
||
private | ||
|
||
def install_inertia | ||
say "Adding Inertia's Rails adapter initializer" | ||
template "initializer.rb", Rails.root.join("config/initializers/inertia_rails.rb").to_s | ||
|
||
say "Installing Inertia npm packages" | ||
add_packages(*FRAMEWORKS[framework][:packages]) | ||
add_packages("--save-dev", *FRAMEWORKS[framework][:dev_packages]) | ||
|
||
unless File.read(vite_config_path).include?(FRAMEWORKS[framework][:vite_plugin_import]) | ||
say "Adding Vite plugin for #{framework}" | ||
insert_into_file vite_config_path, "\n #{FRAMEWORKS[framework][:vite_plugin_call]},", after: "plugins: [" | ||
prepend_file vite_config_path, "#{FRAMEWORKS[framework][:vite_plugin_import]}\n" | ||
end | ||
|
||
unless Rails.root.join("package.json").read.include?('"type": "module"') | ||
say 'Add "type": "module", to the package.json file' | ||
gsub_file Rails.root.join("package.json").to_s, /\A\s*\{/, "{\n \"type\": \"module\"," | ||
end | ||
|
||
say "Copying inertia.js into Vite entrypoints", :blue | ||
template "#{framework}/inertia.js", Rails.root.join("app/frontend/entrypoints/inertia.js").to_s | ||
|
||
say "Adding inertia.js script tag to the application layout" | ||
headers = <<-ERB | ||
<%= vite_javascript_tag 'inertia' %> | ||
<%= inertia_headers %> | ||
ERB | ||
insert_into_file APPLICATION_LAYOUT.to_s, headers, after: "<%= vite_client_tag %>\n" | ||
|
||
if framework == "react" && !APPLICATION_LAYOUT.read.include?("vite_react_refresh_tag") | ||
say "Adding Vite React Refresh tag to the application layout" | ||
insert_into_file APPLICATION_LAYOUT.to_s, "<%= vite_react_refresh_tag %>\n ", before: "<%= vite_client_tag %>" | ||
end | ||
|
||
say "Copying example Inertia controller" | ||
template "controller.rb", Rails.root.join("app/controllers/inertia_example_controller.rb").to_s | ||
|
||
say "Adding a route for the example Inertia controller" | ||
route "get 'inertia-example', to: 'inertia_example#index'" | ||
|
||
say "Copying framework related files" | ||
FRAMEWORKS[framework][:copy_files].each do |source, destination| | ||
template "#{framework}/#{source}", Rails.root.join(destination).to_s | ||
end | ||
end | ||
|
||
def ruby_vite? | ||
Rails.root.join("config/vite.json").exist? && vite_config_path | ||
end | ||
|
||
def package_manager | ||
return @package_manager if defined?(@package_manager) | ||
|
||
@package_manager = detect_package_manager | ||
end | ||
|
||
def add_packages(*packages) | ||
run "#{package_manager} add #{packages.join(" ")}" | ||
end | ||
|
||
def detect_package_manager | ||
return nil unless Rails.root.join("package.json").exist? | ||
|
||
if Rails.root.join("package-lock.json").exist? | ||
"npm" | ||
elsif Rails.root.join("bun.config.js").exist? | ||
"bun" | ||
else | ||
"yarn" | ||
end | ||
end | ||
|
||
def vite_config_path | ||
@vite_config_path ||= Dir.glob(Rails.root.join("vite.config.{ts,js,mjs,cjs}")).first | ||
end | ||
|
||
def framework | ||
@framework ||= ask("What framework do you want to use with Turbo Mount?", limited_to: FRAMEWORKS.keys, default: "react") | ||
end | ||
end | ||
end | ||
end |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class InertiaExampleController < ApplicationController | ||
def index | ||
render inertia: "InertiaExample", props: { | ||
name: params.fetch(:name, "World") | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
InertiaRails.configure do |config| | ||
config.ssr_enabled = ViteRuby.config.ssr_build_enabled | ||
config.version = ViteRuby.digest | ||
end |
60 changes: 60 additions & 0 deletions
60
lib/generators/inertia/install/templates/react/InertiaExample.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Head } from '@inertiajs/react' | ||
import { useState } from 'react' | ||
|
||
import reactSvg from '/assets/react.svg' | ||
import inertiaSvg from '/assets/inertia.svg' | ||
import viteRubySvg from '/assets/vite_ruby.svg' | ||
|
||
import cs from './InertiaExample.module.css' | ||
|
||
export default function InertiaExample({ name }) { | ||
const [count, setCount] = useState(0) | ||
|
||
return ( | ||
<> | ||
<Head title="Inertia + Vite Ruby + React Example" /> | ||
|
||
<div className={cs.root}> | ||
<h1 className={cs.h1}>Hello {name}!</h1> | ||
|
||
<div> | ||
<a href="https://inertia-rails.netlify.app" target="_blank"> | ||
<img className={cs.logo} src={inertiaSvg} alt="Inertia logo" /> | ||
</a> | ||
<a href="https://vite-ruby.netlify.app" target="_blank"> | ||
<img | ||
className={`${cs.logo} ${cs.vite}`} | ||
src={viteRubySvg} | ||
alt="Vite Ruby logo" | ||
/> | ||
</a> | ||
<a href="https://react.dev" target="_blank"> | ||
<img | ||
className={`${cs.logo} ${cs.react}`} | ||
src={reactSvg} | ||
alt="React logo" | ||
/> | ||
</a> | ||
</div> | ||
|
||
<h2 className={cs.h2}>Inertia + Vite Ruby + React</h2> | ||
|
||
<div className="card"> | ||
<button | ||
className={cs.button} | ||
onClick={() => setCount((count) => count + 1)} | ||
> | ||
count is {count} | ||
</button> | ||
<p> | ||
Edit <code>app/frontend/pages/InertiaExample.jsx</code> and save to | ||
test HMR | ||
</p> | ||
</div> | ||
<p className={cs.readTheDocs}> | ||
Click on the Inertia, Vite Ruby, and React logos to learn more | ||
</p> | ||
</div> | ||
</> | ||
) | ||
} |
Oops, something went wrong.