From 90e3480dc80141996415474a12b4b60c0a7e4e2d Mon Sep 17 00:00:00 2001 From: Tim Riley Date: Fri, 27 Nov 2026 23:33:18 +1100 Subject: [PATCH] Generate JRuby-compatible apps from `hanami new` on JRuby When run on JRuby, `hanami new` now uses the JDBC driver gem (jdbc-sqlite3, jdbc-postgres or jdbc-mysql), sets a `jdbc:` DATABASE_URL in `.env`, and leaves out Puma's cluster mode settings, since JRuby can't fork workers. Previously the generated app failed at `bundle install`. Also stop `bin/setup` from running `npm install` in apps generated with `--skip-assets`. --- .github/workflows/database-e2e.sh | 8 +- CHANGELOG.md | 2 + lib/hanami/cli/generators/context.rb | 25 +++++- lib/hanami/cli/generators/gem/app/gemfile.erb | 8 +- lib/hanami/cli/generators/gem/app/puma.erb | 6 ++ lib/hanami/cli/generators/gem/app/setup.erb | 2 + spec/unit/hanami/cli/commands/gem/new_spec.rb | 77 +++++++++++++++++++ 7 files changed, 114 insertions(+), 14 deletions(-) diff --git a/.github/workflows/database-e2e.sh b/.github/workflows/database-e2e.sh index 5bdb397e..f9537460 100755 --- a/.github/workflows/database-e2e.sh +++ b/.github/workflows/database-e2e.sh @@ -40,24 +40,22 @@ HANAMI_DB_GEM=$(grep '^gem "hanami-db"' "$GITHUB_WORKSPACE/Gemfile") sed -i "s|^\([[:space:]]*\)gem \"hanami-db\".*|\1$HANAMI_DB_GEM|" testapp/Gemfile grep -qF "$HANAMI_DB_GEM" testapp/Gemfile +# `hanami new` should choose the JDBC driver gem and URL when run on JRuby if [ "$JRUBY" = "true" ]; then case "$E2E_DATABASE_FLAG" in sqlite) - original_gem="sqlite3" jdbc_gem="jdbc-sqlite3" ;; postgres) - original_gem="pg" jdbc_gem="jdbc-postgres" ;; mysql) - original_gem="mysql2" jdbc_gem="jdbc-mysql" ;; esac - sed -i "s#^gem \"${original_gem}\"#gem \"${jdbc_gem}\"#" testapp/Gemfile - grep -q "gem \"${jdbc_gem}\"" testapp/Gemfile + grep -q "^gem \"${jdbc_gem}\"" testapp/Gemfile + grep -q "^DATABASE_URL=jdbc:" testapp/.env fi cd testapp diff --git a/CHANGELOG.md b/CHANGELOG.md index d8ca0c9c..43b9e0f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,8 @@ and this project adheres to [Break Versioning](https://www.taoensso.com/break-ve - Decode percent-encoded DB user and password for CLI tools (@aaronmallen #457) - `hanami generate slice` no longer generates view files (`view.rb`, `views/helpers.rb`, `views/context.rb` and the templates directory) when hanami-view is not bundled, such as in apps generated with `hanami new --skip-view`. (@aaronmallen in #443) - Fix a typo in a comment in the `config/assets.js` generated by `hanami new`. (@oleksify in #439) +- Generate apps that work on JRuby when `hanami new` runs on JRuby. The Gemfile uses the JDBC driver gem (`jdbc-sqlite3`, `jdbc-postgres` or `jdbc-mysql`) in place of the C extension gem, `.env` sets a `jdbc:` `DATABASE_URL`, and `config/puma.rb` leaves out the cluster mode settings, since JRuby can't fork workers. Previously the generated app failed at `bundle install`. (@timriley in #483) +- `bin/setup` generated by `hanami new --skip-assets` no longer runs `npm install`. (@timriley in #483) ### Security diff --git a/lib/hanami/cli/generators/context.rb b/lib/hanami/cli/generators/context.rb index cb4b0525..d5338a76 100644 --- a/lib/hanami/cli/generators/context.rb +++ b/lib/hanami/cli/generators/context.rb @@ -108,18 +108,37 @@ def generate_mysql? generate_db? && database_option == Commands::Gem::New::DATABASE_MYSQL end + # JRuby connects through JDBC, so it needs a "jdbc:" URL. JDBC drivers take the user as a + # query param rather than userinfo. def database_url if generate_sqlite? - "sqlite://db/#{app}.sqlite" + jruby? ? "jdbc:sqlite:db/#{app}.sqlite" : "sqlite://db/#{app}.sqlite" elsif generate_postgres? - "postgres://localhost/#{app}" + jruby? ? "jdbc:postgresql://localhost/#{app}" : "postgres://localhost/#{app}" elsif generate_mysql? - "mysql2://root@localhost/#{app}" + jruby? ? "jdbc:mysql://localhost/#{app}?user=root" : "mysql2://root@localhost/#{app}" else raise "Unknown database option: #{database_option}" end end + # JRuby can't install the C extension database gems, so it uses the JDBC driver gems. + def database_gem + if generate_sqlite? + jruby? ? "jdbc-sqlite3" : "sqlite3" + elsif generate_postgres? + jruby? ? "jdbc-postgres" : "pg" + elsif generate_mysql? + jruby? ? "jdbc-mysql" : "mysql2" + else + raise "Unknown database option: #{database_option}" + end + end + + def jruby? + RUBY_ENGINE == "jruby" + end + def bundled_views? Hanami.bundled?("hanami-view") end diff --git a/lib/hanami/cli/generators/gem/app/gemfile.erb b/lib/hanami/cli/generators/gem/app/gemfile.erb index 2c0299fe..ad5336b5 100644 --- a/lib/hanami/cli/generators/gem/app/gemfile.erb +++ b/lib/hanami/cli/generators/gem/app/gemfile.erb @@ -54,12 +54,8 @@ gem "dry-validation", "~> 1.11" gem "i18n", "~> 1.14" gem "puma", ">= 7.1" gem "rake" -<%- if generate_sqlite? -%> -gem "sqlite3" -<%- elsif generate_postgres? -%> -gem "pg" -<%- elsif generate_mysql? -%> -gem "mysql2" +<%- if generate_db? -%> +gem "<%= database_gem %>" <%- end -%> <%- if generate_view? && template_engine == "haml" -%> gem "haml" diff --git a/lib/hanami/cli/generators/gem/app/puma.erb b/lib/hanami/cli/generators/gem/app/puma.erb index f45cc201..8fd2fcbb 100644 --- a/lib/hanami/cli/generators/gem/app/puma.erb +++ b/lib/hanami/cli/generators/gem/app/puma.erb @@ -15,6 +15,11 @@ max_threads_count = ENV.fetch("HANAMI_MAX_THREADS", 5) min_threads_count = ENV.fetch("HANAMI_MIN_THREADS") { max_threads_count } threads min_threads_count, max_threads_count +<%- if jruby? -%> + +# Puma runs a single process on JRuby, since JRuby can't fork workers. JRuby threads run in +# parallel, so to handle more requests, raise HANAMI_MAX_THREADS instead. +<%- else -%> # # Workers (aka Puma/Ruby processes) @@ -42,3 +47,4 @@ if puma_cluster_mode Hanami.shutdown end end +<%- end -%> diff --git a/lib/hanami/cli/generators/gem/app/setup.erb b/lib/hanami/cli/generators/gem/app/setup.erb index 7051702c..c9498d8c 100644 --- a/lib/hanami/cli/generators/gem/app/setup.erb +++ b/lib/hanami/cli/generators/gem/app/setup.erb @@ -16,9 +16,11 @@ announce() { announce "Running bundle install..." bundle check || bundle install +<%- if generate_assets? -%> announce "\nRunning npm install..." npm install +<%- end -%> <%- if generate_db? -%> announce "\nPreparing the database..." hanami db prepare diff --git a/spec/unit/hanami/cli/commands/gem/new_spec.rb b/spec/unit/hanami/cli/commands/gem/new_spec.rb index 40fad180..bacdc199 100644 --- a/spec/unit/hanami/cli/commands/gem/new_spec.rb +++ b/spec/unit/hanami/cli/commands/gem/new_spec.rb @@ -29,6 +29,13 @@ before do allow(bundler).to receive(:bundle).with(a_string_starting_with("binstubs")) .and_return(successful_system_call_result) + + # The generated files differ on JRuby. Expect the CRuby files unless an example says otherwise, + # so these specs pass on either Ruby. + # + # Stub the constant within Context only, which its own lookup finds first. Stubbing the global + # constant would break other code, such as JRuby loading its native extensions. + stub_const("Hanami::CLI::Generators::Context::RUBY_ENGINE", "ruby") end it "normalizes app name" do @@ -1433,6 +1440,9 @@ module Types # Procfile.dev expect(fs.read("Procfile.dev")).to_not match(/hanami assets watch/) + # bin/setup + expect(fs.read("bin/setup")).to_not match(/npm/) + # config/assets.js expect(fs.exist?("config/assets.js")).to be(false) @@ -1751,6 +1761,73 @@ class App < Hanami::App end end + describe "on JRuby" do + before do + stub_const("Hanami::CLI::Generators::Context::RUBY_ENGINE", "jruby") + end + + it "generates app with JDBC gem and URL for --database=sqlite" do + subject.call(app: app, database: "sqlite") + + fs.chdir(app) do + expect(fs.read("Gemfile")).to include(%(gem "jdbc-sqlite3")) + expect(fs.read("Gemfile")).to_not include(%(gem "sqlite3")) + expect(fs.read(".env")).to include("DATABASE_URL=jdbc:sqlite:db/#{app}.sqlite\n") + expect(fs.read(".gitignore")).to include("db/*.sqlite*") + expect(fs.exist?("db/.keep")).to be(true) + end + end + + it "generates app with JDBC gem and URL for --database=postgres" do + subject.call(app: app, database: "postgres") + + fs.chdir(app) do + expect(fs.read("Gemfile")).to include(%(gem "jdbc-postgres")) + expect(fs.read("Gemfile")).to_not include(%(gem "pg")) + expect(fs.read(".env")).to include("DATABASE_URL=jdbc:postgresql://localhost/#{app}\n") + end + end + + it "generates app with JDBC gem and URL for --database=mysql" do + subject.call(app: app, database: "mysql") + + fs.chdir(app) do + expect(fs.read("Gemfile")).to include(%(gem "jdbc-mysql")) + expect(fs.read("Gemfile")).to_not include(%(gem "mysql2")) + expect(fs.read(".env")).to include("DATABASE_URL=jdbc:mysql://localhost/#{app}?user=root\n") + end + end + + it "generates puma config without workers" do + subject.call(app: app) + + puma = <<~EXPECTED + # frozen_string_literal: true + + # + # Environment and port + # + port ENV.fetch("HANAMI_PORT", 2300) + environment ENV.fetch("HANAMI_ENV", "development") + + # + # Threads within each Puma/Ruby process (aka worker) + # + + # Configure the minimum and maximum number of threads to use to answer requests. + max_threads_count = ENV.fetch("HANAMI_MAX_THREADS", 5) + min_threads_count = ENV.fetch("HANAMI_MIN_THREADS") { max_threads_count } + + threads min_threads_count, max_threads_count + + # Puma runs a single process on JRuby, since JRuby can't fork workers. JRuby threads run in + # parallel, so to handle more requests, raise HANAMI_MAX_THREADS instead. + EXPECTED + + expect(fs.read("#{app}/config/puma.rb")).to eq(puma) + end + end + it "doesn't generate for unsupported database" do expect { subject.call(app: app, database: "foodb")