Skip to content

Implement Bundler install_if#176

Merged
nickcharlton merged 2 commits intothoughtbot:masterfrom
n-rodriguez:wip/install_if
Aug 9, 2021
Merged

Implement Bundler install_if#176
nickcharlton merged 2 commits intothoughtbot:masterfrom
n-rodriguez:wip/install_if

Conversation

@n-rodriguez
Copy link
Copy Markdown
Contributor

@n-rodriguez n-rodriguez commented Jan 1, 2021

This an attempt to fix: #131

The trick here is to store the conditional as plain string. As explained here #132 (comment) :

We dont want the lambda evaluated on generate, we want it evaluated on bundle install. No idea how to do that and im too pneumonia sick to think :D

IMHO the easiest way is to write/store it as a string and render it directly in the generated file (verbatim).

So in Appraisal file :

install_if -> { ENV["DB_ADAPTER"] == "mysql2" } do
  gem 'mysql'
end

becomes

install_if '-> { ENV["DB_ADAPTER"] == "mysql2" }' do
  gem 'mysql'
end

After generation it becomes (in gemfiles/*.gemfile) :

install_if -> { ENV["DB_ADAPTER"] == "mysql2" } do
  gem 'mysql'
end

It should help to solve most use cases since you can put any valid Ruby code here :).

At first I tried https://github.com/banister/method_source but it immediately failed.

Because :

Cannot return source for dynamically defined methods.

So I ended up with this easy solution that does the job.

@n-rodriguez
Copy link
Copy Markdown
Contributor Author

@n-rodriguez
Copy link
Copy Markdown
Contributor Author

n-rodriguez commented Jan 1, 2021

The nice thing : it even merges gems with duplicated conditions :

install_if -> { ENV["DB_ADAPTER"] == "oracle_enhanced" } do
  gem "activerecord-oracle_enhanced-adapter", "~> 5.2.0"
  gem "ruby-oci8"
end

Here the yaml structure that generate gemfiles : https://github.com/jbox-web/ajax-datatables-rails/compare/wip/appraisal#diff-a7e1dc0d2145599ae30f5d8cdfa163e79e7708040d2f7cf36b36fab4559974a8R30

end

[:gits, :paths, :platforms, :groups, :source_blocks].
[:gits, :paths, :platforms, :groups, :source_blocks, :install_if].
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I fix it?


PARTS = %w(source ruby_version gits paths dependencies groups
platforms source_blocks gemspec)
platforms source_blocks install_if gemspec)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I fix it?

@n-rodriguez n-rodriguez mentioned this pull request Jan 6, 2021
@n-rodriguez
Copy link
Copy Markdown
Contributor Author

@nickcharlton wdyt?

@n-rodriguez
Copy link
Copy Markdown
Contributor Author

Hi there! any news?

@n-rodriguez
Copy link
Copy Markdown
Contributor Author

Ping @nickcharlton

@n-rodriguez
Copy link
Copy Markdown
Contributor Author

Hi there! Any news?

@n-rodriguez
Copy link
Copy Markdown
Contributor Author

It would be nice to have some news...

I hope I've not worked for nothing....

@n-rodriguez
Copy link
Copy Markdown
Contributor Author

Ping @nickcharlton

1 similar comment
@n-rodriguez
Copy link
Copy Markdown
Contributor Author

Ping @nickcharlton

@n-rodriguez
Copy link
Copy Markdown
Contributor Author

Ping @nickcharlton

Copy link
Copy Markdown
Member

@nickcharlton nickcharlton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for keeping this alive, @n-rodriguez and sorry for the long wait.

Two points on some Hound issues, if you could take a look at those, I'll come back and get this merged!


PARTS = %w(source ruby_version gits paths dependencies groups
platforms source_blocks gemspec)
platforms source_blocks install_if gemspec)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please!

end

[:gits, :paths, :platforms, :groups, :source_blocks].
[:gits, :paths, :platforms, :groups, :source_blocks, :install_if].
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please!

@n-rodriguez
Copy link
Copy Markdown
Contributor Author

@nickcharlton ping

@nickcharlton
Copy link
Copy Markdown
Member

Thanks for the ping! I'm going to merge this now.

It'll go out in our next release, which will be once we fix #173.

@nickcharlton nickcharlton merged commit 2f5be65 into thoughtbot:master Aug 9, 2021
@adrianthedev
Copy link
Copy Markdown

For some reason, the install_if method doesn't work for me.

install_if -> { ENV["CI"] } do
  gem "pluggy", path: "./gems/pluggy"
end

CleanShot 2023-02-01 at 15 30 06

CleanShot 2023-02-01 at 15 29 35

@nickcharlton
Copy link
Copy Markdown
Member

Can you open a new issue? Ideally with something we can try and replicate with!

@n-rodriguez
Copy link
Copy Markdown
Contributor Author

From your screenshot I see appraisal-2.4.1. This feature doesn't exist in this version, you need to use the main/master branch of appraisal.

By the way be sure to use quotes in your Appraisal file.

See: #176 (comment)

@n-rodriguez
Copy link
Copy Markdown
Contributor Author

n-rodriguez commented Feb 1, 2023

@nickcharlton maybe it's time to cut a new release? v2.4.1...master :)

@n-rodriguez n-rodriguez mentioned this pull request Feb 1, 2023
@adrianthedev
Copy link
Copy Markdown

Thanks @n-rodriguez. I only checked to see if I'm on the latest version. The main/master instruction missed me.

Will try now.

@nickcharlton
Copy link
Copy Markdown
Member

There will be a new issue soon. I'm tracking a few issues/catching up with lots of bundler changes so I think it's important to test these well.

nickcharlton pushed a commit that referenced this pull request Sep 23, 2024
```sh
An error occurred while loading ./spec/draper/draper_spec.rb.
Failure/Error: Bundler.require(*Rails.groups)

SyntaxError:
  /Users/nicolas/.asdf/installs/ruby/3.3.3/lib/ruby/gems/3.3.0/gems/appraisal-2.5.0/lib/appraisal/gemfile.rb:52: syntax error, unexpected `end'
```

Instead of using a raw String as explained here (#176) it uses the interpreted version:

```sh
--- |-
  source "https://rubygems.org"

  gem "appraisal"
  gem "capybara"
  gem "cuprite"
  gem "database_cleaner"
  gem "factory_bot"
  gem "faker"
  gem "guard-rspec"
  gem "puma"
  gem "rake"
  gem "rspec-rails"
  gem "rspec-retry"
  gem "rubocop"
  gem "rubocop-capybara"
  gem "rubocop-factory_bot"
  gem "rubocop-rake"
  gem "rubocop-rspec"
  gem "simplecov"
  gem "sqlite3", "~> 1.5.0"
  gem "rails", "6.1.7"

  install_if #<Proc:0x000000011f590cc0 /Users/nicolas/PROJECTS/CONCERTO/gems/draper/gemfiles/rails_6.1.7.gemfile:25 (lambda)> do
    gem "net-imap"
    gem "net-pop"
    gem "net-smtp"
  end

  install_if #<Proc:0x000000011f5909f0 /Users/nicolas/PROJECTS/CONCERTO/gems/draper/gemfiles/rails_6.1.7.gemfile:31 (lambda)> do
    gem "base64"
    gem "bigdecimal"
    gem "mutex_m"
    gem "drb"
    gem "logger"
  end

  gemspec path: "../"
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

install_if is missing

3 participants