Skip to content

Commit

Permalink
Merge pull request #5 from RoleModel/add-app-json-generator
Browse files Browse the repository at this point in the history
Include chrome buildpack in app_json
  • Loading branch information
mikehale authored Aug 15, 2024
2 parents df2bbc7 + a11ea57 commit 41f0987
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
37 changes: 37 additions & 0 deletions lib/generators/chromium/pdf/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,43 @@ def create_pdf_job_templates
copy_file 'app/jobs/generate_pdf_job.rb'
copy_file 'config/initializers/good_job.rb'
end

def include_chrome_buildpack_in_app_json
say(<<~SAY, :yellow)
N.B. app.json changes are only applied to new heroku apps.
In the case where your app already exists make sure to manually add the buildpack by running the command:
`heroku buildpacks:add heroku-community/chrome-for-testing`
SAY
add_buildpack_to_app_json
end

private

def add_buildpack_to_app_json
in_root do
update_json_file('app.json') do |data|
data['buildpacks'] << { 'url' => 'heroku-community/chrome-for-testing' }
end
end
end

def update_json_file(file_name)
data = if File.exist?(file_name)
ActiveSupport::JSON.decode(File.read(file_name))
else
{}
end
data['buildpacks'] ||= []

yield data

File.write(file_name, pretty_json(data))
end

def pretty_json(data)
require 'json'
JSON.pretty_generate(data)
end
end
end
end
Expand Down
16 changes: 15 additions & 1 deletion test/generators/install_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
class InstallGeneratorTest < Rails::Generators::TestCase
tests ::Chromium::Pdf::Generators::InstallGenerator
destination File.expand_path('../../tmp', __dir__)
setup :prepare_destination
setup do
prepare_destination
end

test 'pdf job template is created' do
run_generator
Expand All @@ -18,4 +20,16 @@ class InstallGeneratorTest < Rails::Generators::TestCase
run_generator
assert_file 'config/initializers/good_job.rb'
end

test 'app.json is created' do
@app_json_path = "#{destination_root}/app.json"

File.write(@app_json_path, ActiveSupport::JSON.encode({}))
run_generator
assert_includes(File.read(@app_json_path), 'heroku-community/chrome-for-testing')

File.write(@app_json_path, ActiveSupport::JSON.encode({ buildpacks: [] }))
run_generator
assert_includes(File.read(@app_json_path), 'heroku-community/chrome-for-testing')
end
end

0 comments on commit 41f0987

Please sign in to comment.