Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke app:template via Rake::Task[].invoke to avoid reloading rakefile #239

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/tasks/importmap_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace :importmap do
desc "Setup Importmap for the app"
task :install do
system RbConfig.ruby, "./bin/rails", "app:template", "LOCATION=#{File.expand_path("../install/install.rb", __dir__)}"
previous_location = ENV["LOCATION"]
ENV["LOCATION"] = File.expand_path("../install/install.rb", __dir__)
Rake::Task["app:template"].invoke
ENV["LOCATION"] = previous_location
end
end
14 changes: 13 additions & 1 deletion test/installer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ class InstallerTest < ActiveSupport::TestCase
end
end

test "doesn't load rakefile twice" do
with_new_rails_app do |app_dir|
rakefile = File.read("#{app_dir}/Rakefile")
rakefile = "puts \"I've been logged twice!\" \n" + rakefile
File.write("#{app_dir}/Rakefile", rakefile)

out, err = run_command("bin/rails", "importmap:install")

assert_equal 1, out.scan(/I've been logged twice!/).size
end
end

private
def with_new_rails_app
# Unset testing dummy app so app generator doesn't get confused in Rails 6.1 and 7.0.
Expand All @@ -54,7 +66,7 @@ def with_new_rails_app

run_command("bundle", "install")

yield
yield(app_dir)
end
end
end
Expand Down