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

Implement Bundler install_if #176

Merged
merged 2 commits into from
Aug 9, 2021
Merged

Implement Bundler install_if #176

merged 2 commits into from
Aug 9, 2021

Conversation

n-rodriguez
Copy link
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
Contributor Author

@n-rodriguez
Copy link
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

@@ -124,7 +131,7 @@ def dependencies_entry_for_dup
@dependencies.for_dup
end

[:gits, :paths, :platforms, :groups, :source_blocks].
[:gits, :paths, :platforms, :groups, :source_blocks, :install_if].
Copy link
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?

@@ -5,7 +5,7 @@ class BundlerDSL
attr_reader :dependencies

PARTS = %w(source ruby_version gits paths dependencies groups
platforms source_blocks gemspec)
platforms source_blocks install_if gemspec)
Copy link
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
Contributor Author

@nickcharlton wdyt?

@n-rodriguez
Copy link
Contributor Author

Hi there! any news?

@n-rodriguez
Copy link
Contributor Author

Ping @nickcharlton

@n-rodriguez
Copy link
Contributor Author

Hi there! Any news?

@n-rodriguez
Copy link
Contributor Author

It would be nice to have some news...

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

@n-rodriguez
Copy link
Contributor Author

Ping @nickcharlton

1 similar comment
@n-rodriguez
Copy link
Contributor Author

Ping @nickcharlton

@n-rodriguez
Copy link
Contributor Author

Ping @nickcharlton

Copy link
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!

@@ -5,7 +5,7 @@ class BundlerDSL
attr_reader :dependencies

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

Choose a reason for hiding this comment

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

Yes please!

@@ -124,7 +131,7 @@ def dependencies_entry_for_dup
@dependencies.for_dup
end

[:gits, :paths, :platforms, :groups, :source_blocks].
[:gits, :paths, :platforms, :groups, :source_blocks, :install_if].
Copy link
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
Contributor Author

@nickcharlton ping

@nickcharlton
Copy link
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
n-rodriguez added a commit to n-rodriguez/appraisal that referenced this pull request Oct 25, 2022
Failure/Error: Combustion.initialize! :active_record, :action_controller

SyntaxError:
  /home/nicolas/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/appraisal-40956d3ae826/lib/appraisal/gemfile.rb:33: syntax error, unexpected `end', expecting end-of-input

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

<Proc:0x00007f7aab911770 /home/nicolas/PROJECTS/CONCERTO/gems/ajax-datatables-rails/gemfiles/rails_6.1.4.gemfile:8 (lambda)>
<Proc:0x00007f7aab9177d8 /home/nicolas/PROJECTS/CONCERTO/gems/ajax-datatables-rails/gemfiles/rails_6.1.4.gemfile:12 (lambda)>
<Proc:0x00007f7aab917490 /home/nicolas/PROJECTS/CONCERTO/gems/ajax-datatables-rails/gemfiles/rails_6.1.4.gemfile:16 (lambda)>
"-> { ENV[\"DB_ADAPTER\"] == \"sqlite3\" }"
"-> { ENV[\"DB_ADAPTER\"] == \"mysql2\" }"
"-> { ENV[\"DB_ADAPTER\"] == \"oracle_enhanced\" }"
@adrianthedev
Copy link

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
Member

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

@n-rodriguez
Copy link
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
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

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
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.

n-rodriguez added a commit to n-rodriguez/appraisal that referenced this pull request Apr 19, 2023
Failure/Error: Combustion.initialize! :active_record, :action_controller

SyntaxError:
  /home/nicolas/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/appraisal-40956d3ae826/lib/appraisal/gemfile.rb:33: syntax error, unexpected `end', expecting end-of-input

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

<Proc:0x00007f7aab911770 /home/nicolas/PROJECTS/CONCERTO/gems/ajax-datatables-rails/gemfiles/rails_6.1.4.gemfile:8 (lambda)>
<Proc:0x00007f7aab9177d8 /home/nicolas/PROJECTS/CONCERTO/gems/ajax-datatables-rails/gemfiles/rails_6.1.4.gemfile:12 (lambda)>
<Proc:0x00007f7aab917490 /home/nicolas/PROJECTS/CONCERTO/gems/ajax-datatables-rails/gemfiles/rails_6.1.4.gemfile:16 (lambda)>
"-> { ENV[\"DB_ADAPTER\"] == \"sqlite3\" }"
"-> { ENV[\"DB_ADAPTER\"] == \"mysql2\" }"
"-> { ENV[\"DB_ADAPTER\"] == \"oracle_enhanced\" }"
n-rodriguez added a commit to n-rodriguez/appraisal that referenced this pull request Aug 4, 2023
Failure/Error: Combustion.initialize! :active_record, :action_controller

SyntaxError:
  /home/nicolas/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/appraisal-40956d3ae826/lib/appraisal/gemfile.rb:33: syntax error, unexpected `end', expecting end-of-input

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

<Proc:0x00007f7aab911770 /home/nicolas/PROJECTS/CONCERTO/gems/ajax-datatables-rails/gemfiles/rails_6.1.4.gemfile:8 (lambda)>
<Proc:0x00007f7aab9177d8 /home/nicolas/PROJECTS/CONCERTO/gems/ajax-datatables-rails/gemfiles/rails_6.1.4.gemfile:12 (lambda)>
<Proc:0x00007f7aab917490 /home/nicolas/PROJECTS/CONCERTO/gems/ajax-datatables-rails/gemfiles/rails_6.1.4.gemfile:16 (lambda)>
"-> { ENV[\"DB_ADAPTER\"] == \"sqlite3\" }"
"-> { ENV[\"DB_ADAPTER\"] == \"mysql2\" }"
"-> { ENV[\"DB_ADAPTER\"] == \"oracle_enhanced\" }"
n-rodriguez added a commit to n-rodriguez/appraisal that referenced this pull request Aug 3, 2024
Failure/Error: Combustion.initialize! :active_record, :action_controller

SyntaxError:
  /home/nicolas/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/appraisal-40956d3ae826/lib/appraisal/gemfile.rb:33: syntax error, unexpected `end', expecting end-of-input

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

<Proc:0x00007f7aab911770 /home/nicolas/PROJECTS/CONCERTO/gems/ajax-datatables-rails/gemfiles/rails_6.1.4.gemfile:8 (lambda)>
<Proc:0x00007f7aab9177d8 /home/nicolas/PROJECTS/CONCERTO/gems/ajax-datatables-rails/gemfiles/rails_6.1.4.gemfile:12 (lambda)>
<Proc:0x00007f7aab917490 /home/nicolas/PROJECTS/CONCERTO/gems/ajax-datatables-rails/gemfiles/rails_6.1.4.gemfile:16 (lambda)>
"-> { ENV[\"DB_ADAPTER\"] == \"sqlite3\" }"
"-> { ENV[\"DB_ADAPTER\"] == \"mysql2\" }"
"-> { ENV[\"DB_ADAPTER\"] == \"oracle_enhanced\" }"
n-rodriguez added a commit to n-rodriguez/appraisal that referenced this pull request Aug 3, 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 (thoughtbot#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: "../"
```
n-rodriguez added a commit to n-rodriguez/appraisal 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 (thoughtbot#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: "../"
```
n-rodriguez added a commit to n-rodriguez/appraisal 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 (thoughtbot#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: "../"
```
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