Skip to content

Commit

Permalink
Improve Inertia initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Nov 22, 2024
1 parent fcf18ca commit 6cff2e8
Show file tree
Hide file tree
Showing 20 changed files with 206 additions and 80 deletions.
21 changes: 15 additions & 6 deletions .github/workflows/generators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@ jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
framework: [react, vue, svelte4, svelte]
framework: [react, vue, svelte, svelte4]
typescript: [true, false]
tailwind: [true, false]
ruby: ['3.3']
node: ['22']
inertia_version: ['1.2.0', 'next', 'beta']
exclude:
# 1.2.0 does not support typescript
- typescript: true
inertia_version: '1.2.0'
# 1.2.0 doesn't support Svelte 5
- framework: svelte
inertia_version: '1.2.0'

name: ${{ matrix.framework }} (TS:${{ matrix.typescript }}, TW:${{ matrix.tailwind }})
name: ${{ matrix.framework }} (TS:${{ matrix.typescript }}, TW:${{ matrix.tailwind }}, Inertia:${{ matrix.inertia_version }})

steps:
- uses: actions/checkout@v4
Expand All @@ -48,7 +57,7 @@ jobs:
tmp/bundle_cache
tmp/npm_cache
~/.npm
key: ${{ runner.os }}-deps-${{ matrix.framework }}-${{ hashFiles('**/Gemfile.lock') }}-${{ github.sha }}
key: ${{ runner.os }}-deps-${{ matrix.framework }}-${{ matrix.inertia_version }}-${{ hashFiles('**/Gemfile.lock') }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-deps-${{ matrix.framework }}-
${{ runner.os }}-deps-
Expand All @@ -60,13 +69,13 @@ jobs:
run: |
ts_flag=${{ matrix.typescript && '--typescript' || '--no-typescript' }}
tw_flag=${{ matrix.tailwind && '--tailwind' || '--no-tailwind' }}
bin/generate_scaffold_example --framework=${{ matrix.framework }} $ts_flag $tw_flag
bin/generate_scaffold_example --framework=${{ matrix.framework }} --inertia-version=${{ matrix.inertia_version }} $ts_flag $tw_flag
- name: Upload test artifacts
if: failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: test-output-${{ matrix.framework }}-ts${{ matrix.typescript }}-tw${{ matrix.tailwind }}
name: test-output-${{ matrix.framework }}-ts${{ matrix.typescript }}-tw${{ matrix.tailwind }}-v${{ matrix.inertia_version }}
path: |
tmp/scaffold_example/log
tmp/scaffold_example/tmp/screenshots
Expand Down
44 changes: 34 additions & 10 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config --exclude-limit 10000`
# on 2024-11-11 08:39:28 UTC using RuboCop version 1.68.0.
# on 2024-11-22 18:44:50 UTC using RuboCop version 1.68.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -29,6 +29,14 @@ Layout/BlockEndNewline:
Exclude:
- 'spec/inertia/rendering_spec.rb'

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, IndentOneStep, IndentationWidth.
# SupportedStyles: case, end
Layout/CaseIndentation:
Exclude:
- 'lib/inertia_rails/renderer.rb'

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowForAlignment.
Expand Down Expand Up @@ -57,6 +65,14 @@ Layout/EmptyLinesAroundBlockBody:
Exclude:
- 'spec/inertia/rails_mimic_spec.rb'

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyleAlignWith, Severity.
# SupportedStylesAlignWith: keyword, variable, start_of_line
Layout/EndAlignment:
Exclude:
- 'lib/inertia_rails/renderer.rb'

# Offense count: 3
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment.
Expand Down Expand Up @@ -123,20 +139,19 @@ Layout/SpaceInsideArrayLiteralBrackets:
- 'spec/dummy/app/controllers/inertia_test_controller.rb'
- 'spec/dummy/config/environments/production.rb'

# Offense count: 22
# Offense count: 20
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
# SupportedStyles: space, no_space
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceInsideBlockBraces:
Exclude:
- 'lib/inertia_rails/renderer.rb'
- 'spec/inertia/configuration_spec.rb'
- 'spec/inertia/error_sharing_spec.rb'
- 'spec/inertia/rendering_spec.rb'
- 'spec/inertia/request_spec.rb'

# Offense count: 130
# Offense count: 128
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
# SupportedStyles: space, no_space, compact
Expand All @@ -145,7 +160,6 @@ Layout/SpaceInsideHashLiteralBraces:
Exclude:
- 'lib/inertia_rails/controller.rb'
- 'lib/inertia_rails/middleware.rb'
- 'lib/patches/mapper.rb'
- 'spec/dummy/app/controllers/inertia_conditional_sharing_controller.rb'
- 'spec/dummy/app/controllers/inertia_share_test_controller.rb'
- 'spec/inertia/conditional_sharing_spec.rb'
Expand Down Expand Up @@ -282,6 +296,12 @@ Style/ConditionalAssignment:
Exclude:
- 'lib/inertia_rails/renderer.rb'

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
Style/EachWithObject:
Exclude:
- 'lib/inertia_rails/renderer.rb'

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AutoCorrect, EnforcedStyle, AllowComments.
Expand Down Expand Up @@ -420,12 +440,15 @@ Style/HashSyntax:
Exclude:
- 'lib/tasks/inertia_rails.rake'

# Offense count: 3
# Offense count: 6
# This cop supports safe autocorrection (--autocorrect).
Style/IfUnlessModifier:
Exclude:
- 'lib/inertia_rails/configuration.rb'
- 'lib/inertia_rails/controller.rb'
- 'lib/patches/better_errors.rb'
- 'lib/patches/debug_exceptions/patch-5-0.rb'
- 'lib/patches/debug_exceptions/patch-5-1.rb'
- 'spec/dummy/config/environments/test.rb'

# Offense count: 2
Expand All @@ -440,13 +463,14 @@ Style/KeywordArgumentsMerging:
Exclude:
- 'lib/inertia_rails/configuration.rb'

# Offense count: 2
# Offense count: 3
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: line_count_dependent, lambda, literal
Style/Lambda:
Exclude:
- 'spec/dummy/app/controllers/inertia_lambda_shared_props_controller.rb'
- 'spec/dummy/app/controllers/inertia_render_test_controller.rb'
- 'spec/dummy/app/controllers/transformed_inertia_rails_mimic_controller.rb'

# Offense count: 1
Expand Down Expand Up @@ -586,7 +610,7 @@ Style/SymbolArray:
EnforcedStyle: percent
MinSize: 3

# Offense count: 11
# Offense count: 16
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyleForMultiline.
# SupportedStylesForMultiline: comma, consistent_comma, no_comma
Expand All @@ -599,7 +623,7 @@ Style/TrailingCommaInArguments:
- 'spec/inertia/configuration_spec.rb'
- 'spec/inertia/rendering_spec.rb'

# Offense count: 17
# Offense count: 21
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyleForMultiline.
# SupportedStylesForMultiline: comma, consistent_comma, no_comma
Expand All @@ -623,7 +647,7 @@ Style/WordArray:
EnforcedStyle: percent
MinSize: 4

# Offense count: 16
# Offense count: 21
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
# URISchemes: http, https
Expand Down
33 changes: 20 additions & 13 deletions bin/generate_scaffold_example
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ OptionParser.new do |opts|
opts.on('--[no-]tailwind', 'Enable/disable Tailwind') do |t|
options[:tailwind] = t
end

opts.on('--inertia-version VERSION', 'Specify Inertia version') do |v|
options[:inertia_version] = v
end
end.parse!

# Build generator args string
generator_args = "--framework=#{options[:framework]}"
generator_args += ' --typescript' if options[:typescript]
generator_args += ' --install-vite'
generator_args += ' --install-tailwind' if options[:tailwind]
generator_args += ' --tailwind' if options[:tailwind]
generator_args += " --inertia-version=#{options[:inertia_version]}" if options[:inertia_version]

# Setup paths relative to project root
project_root = File.expand_path('..', __dir__)
Expand All @@ -57,25 +61,28 @@ system("rails new #{app_dir} -J")
# Install and configure with caching
Dir.chdir(app_dir) do
# Configure bundler to use cache in project root
system("bundle config set --local path '#{gem_cache}'")
system("bundle config set --local path '#{gem_cache}'", exception: true)

# Configure npm to use cache in project root
system("npm config set cache '#{npm_cache}'")
system("npm config set cache '#{npm_cache}'", exception: true)

# Install dependencies
system('bundle add inertia_rails --path ../../')
system('bundle add bcrypt')
system('bin/rails active_storage:install')
system('bundle add inertia_rails --path ../../', exception: true)
system('bundle add bcrypt', exception: true)
system('bin/rails active_storage:install', exception: true)

# Run install generator with configured options
system("bin/rails g inertia:install --no-interactive --force #{generator_args}")
system("bin/rails g inertia:install --no-interactive --force --vite #{generator_args} --verbose", exception: true)

# Generate a scaffold
system('bin/rails g inertia:scaffold user name email admin:boolean password:digest avatar:attachment')
system('bin/rails g inertia:scaffold post content:text published_at:date gallery:attachments')
system('bin/rails db:migrate')
system('bin/rails g inertia:scaffold user name email admin:boolean password:digest avatar:attachment', exception: true)
system('bin/rails g inertia:scaffold post content:text published_at:date gallery:attachments', exception: true)
system('bin/rails db:migrate', exception: true)

# Run tests
system('bin/rails test')
system('bin/rails test:system')
system('bin/rails test', exception: true)
system('bin/rails test:system', exception: true)

# Lint code
system('npm run check', exception: true) if options[:typescript]
end
4 changes: 2 additions & 2 deletions docs/cookbook/integrating-shadcn-ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ If you're starting fresh, create a new Rails application with Inertia (or skip t
rails new -JA shadcn-inertia-rails
cd shadcn-inertia-rails

rails generate inertia:install `--framework=react --typescript --install-vite --install-tailwind --no-interactive`
rails generate inertia:install `--framework=react --typescript --vite --tailwind --no-interactive`
Installing Inertia's Rails adapter
...
```
Expand All @@ -25,7 +25,7 @@ Installing Inertia's Rails adapter
rails new -JA shadcn-inertia-rails
cd shadcn-inertia-rails
rails generate inertia:install --framework=react --install-vite --install-tailwind --no-interactive
rails generate inertia:install --framework=react --vite --tailwind --no-interactive
Installing Inertia's Rails adapter
...
```
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/inertia/install/frameworks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ vue:
- "vue"
- "@vitejs/plugin-vue"
packages_ts:
- "typescript"
- "typescript@~5.6.2"
- "vue-tsc"
vite_plugin_import: "import vue from '@vitejs/plugin-vue'"
vite_plugin_call: "vue()"
Expand Down
17 changes: 11 additions & 6 deletions lib/generators/inertia/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class InstallGenerator < Rails::Generators::Base
class_option :interactive, type: :boolean, default: true,
desc: 'Whether to prompt for optional installations'

class_option :install_tailwind, type: :boolean, default: false,
desc: 'Whether to install Tailwind CSS'
class_option :install_vite, type: :boolean, default: false,
desc: 'Whether to install Vite Ruby'
class_option :tailwind, type: :boolean, default: false,
desc: 'Whether to install Tailwind CSS'
class_option :vite, type: :boolean, default: false,
desc: 'Whether to install Vite Ruby'
class_option :example_page, type: :boolean, default: true,
desc: 'Whether to add an example Inertia page'

Expand Down Expand Up @@ -127,6 +127,11 @@ def install_typescript
end

add_dependencies(*FRAMEWORKS[framework]['packages_ts'])

say 'Copying adding scripts to package.json'
run 'npm pkg set scripts.check="svelte-check --tsconfig ./tsconfig.json && tsc -p tsconfig.node.json"' if svelte?
run 'npm pkg set scripts.check="vue-tsc -p tsconfig.app.json && tsc -p tsconfig.node.json"' if framework == 'vue'
run 'npm pkg set scripts.check="tsc -p tsconfig.app.json && tsc -p tsconfig.node.json"' if framework == 'react'
end

def install_example_page
Expand Down Expand Up @@ -236,13 +241,13 @@ def vite_config_path
def install_vite?
return @install_vite if defined?(@install_vite)

@install_vite = options[:install_vite] || yes?('Would you like to install Vite Ruby? (y/n)', :green)
@install_vite = options[:vite] || yes?('Would you like to install Vite Ruby? (y/n)', :green)
end

def install_tailwind?
return @install_tailwind if defined?(@install_tailwind)

@install_tailwind = options[:install_tailwind] || yes?('Would you like to install Tailwind CSS? (y/n)', :green)
@install_tailwind = options[:tailwind] || yes?('Would you like to install Tailwind CSS? (y/n)', :green)
end

def typescript?
Expand Down
21 changes: 15 additions & 6 deletions lib/generators/inertia/install/templates/react/inertia.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,29 @@ createInertiaApp({

resolve: (name) => {
const pages = import.meta.glob('../pages/**/*.jsx', { eager: true })
return pages[`../pages/${name}.jsx`]
const page = pages[`../pages/${name}.jsx`]
if (!page) {
console.error(`Missing Inertia page component: '${name}.jsx'`)
}

// To use a default layout, import the Layout component
// and use the following lines.
// see https://inertia-rails.netlify.app/guide/pages#default-layouts
//
// const page = pages[`../pages/${name}.jsx`]
// page.default.layout ||= (page) => createElement(Layout, null, page)
// return page

return page
},

setup({ el, App, props }) {
const root = createRoot(el)

root.render(createElement(App, props))
if (el) {
createRoot(el).render(createElement(App, props))
} else {
console.error(
'Missing root element.\n\n' +
'If you see this error, it probably means you load Inertia.js on non-Inertia pages.\n' +
'Consider moving <%%= vite_javascript_tag "inertia" %> to the Inertia-specific layout instead.'
)
}
},
})
27 changes: 18 additions & 9 deletions lib/generators/inertia/install/templates/react/inertia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,30 @@ createInertiaApp({
// progress: false,

resolve: (name) => {
const pages = import.meta.glob<ResolvedComponent>('../pages/**/*.tsx', { eager: true })
return pages[`../pages/${name}.tsx`]
const pages = import.meta.glob<ResolvedComponent>('../pages/**/*.tsx', {eager: true})
const page = pages[`../pages/${name}.tsx`]
if (!page) {
console.error(`Missing Inertia page component: '${name}.tsx'`)
}

// To use a default layout, import the Layout component
// and use the following lines.
// and use the following line.
// see https://inertia-rails.netlify.app/guide/pages#default-layouts
//
// const page = pages[`../pages/${name}.tsx`]
// page.default.layout ||= (page) => createElement(Layout, null, page)
// return page
},

setup({ el, App, props }) {
const root = createRoot(el)
return page
},

root.render(createElement(App, props))
setup({el, App, props}) {
if (el) {
createRoot(el).render(createElement(App, props))
} else {
console.error(
'Missing root element.\n\n' +
'If you see this error, it probably means you load Inertia.js on non-Inertia pages.\n' +
'Consider moving <%%= vite_typescript_tag "inertia" %> to the Inertia-specific layout instead.'
)
}
},
})
Loading

0 comments on commit 6cff2e8

Please sign in to comment.