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

Load Bowerfile from all gem dependencies before load #162

Merged
merged 4 commits into from
Aug 13, 2015

Conversation

gabealmer
Copy link
Contributor

I faced the same limitation as discussed in #133, so implemented an easy solution by checking all gem dependencies for a Bowerfile.
If one found then it is evaluated before the main Bowerfile of the project what you are working on.
It works like all definitions would be in the same Bowerfile. Maybe it's not the ideal solution, but at least it works.

@SergeyKishenin
Copy link
Collaborator

Awesome, thanks!

SergeyKishenin added a commit that referenced this pull request Aug 13, 2015
Load Bowerfile from all gem dependencies before load
@SergeyKishenin SergeyKishenin merged commit acd11e1 into rharriso:master Aug 13, 2015
SergeyKishenin added a commit that referenced this pull request Aug 13, 2015
@louman
Copy link

louman commented Oct 11, 2017

Sorry to revive this old PR but I'm having issues with it and the code doesn't seem right. @gabealmer maybe you can help...

I'm using 0.11 with rails 5, I've created the initializer with:

BowerRails.configure do |bower_rails|
  bower_rails.use_gem_deps_for_bowerfile = true
end

When I run bundle exec rake bower:install, the gems dependencies don't get installed. Debugging I found out that the initializer is not being called at all. It kinda makes sense since the rake tasks don't seem to load the rails environment.

For testing purposes, I updated the rake task to load the environment:

desc "Install only dependencies, excluding devDependencies from bower"
    task production: :environment do |_, args|
      args.with_defaults(:options => '')
      BowerRails::Performer.perform do |bower|
        sh "#{bower} install -p #{args[:options]}"
      end
    end

Now the initializer is called but after the DSL config is evaluated, causing any configurations to be ignored.

Decided to investigate this evaluation and found that it's intentionally done before rails initialization as below:

config.before_initialize do |app|
  @dsl = BowerRails::Dsl.evalute(BowerRails.root_path, @@bowerfile)
  @dsl.final_assets_path.map do |assets_root, assets_path|
    app.config.assets.paths <<
      Rails.root.join(assets_root, assets_path, BowerRails.bower_components_directory)
  end
end

or in the performer class:

def dsl
   @dsl ||= BowerRails::Dsl.evalute(root_path, "Bowerfile")
end

That makes me think that you're not supposed to load the rails environment from the rake task but then I ask, how can you run rake tasks with custom configurations if the initializer is never loaded?

@gabealmer
Copy link
Contributor Author

@louman I use it only for a rake task, so I had to configure BowerRails in the Rakefile (or in a separate .rake file) And even I only load it to the development environment in the Gemfile.

So personally I don't use the rails initializer, nor the BowerRails::Railtie class. This is because sprockets already includes JS dependencies from bower.json files, so after you run in your project:

rake bower:install

I think you are all set.
What I had to tweak is that in an engine I have to call:

rake app:bower:install:development

while in my main Rails app I have to call rake bower:install (It can be eliminated with a simple check to define an alias for that)

Do you really want to load it to the application, or just the documentation was misleading regarding to this?

@louman
Copy link

louman commented Oct 12, 2017

I don't really need the environment, just the configuration to also read Bowerfiles from gems.

Can you post an example of your rakefile?

Thanks in advance.

@gabealmer
Copy link
Contributor Author

For example I have this rake task file lib/engine_name/rake_tasks/bower.rake in one of my engines:

if defined?(BowerRails)
  BowerRails.configure do |config|
    config.use_gem_deps_for_bowerfile = true
  end

  unless Rake::Task.task_defined?('bower:install')
    namespace :bower do
      task :install do
        Rake::Task['app:bower:install:development'].invoke
      end
    end
  end
end

And then in every gem and in the main app's Rakefile I have this:

load 'engine_name/rake_tasks/bower.rake'

With this you can call rake bower:install in every gem and in the main app as well.

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.

3 participants