Skip to content

Commit

Permalink
Include bundler group in dependencies.txt.
Browse files Browse the repository at this point in the history
[Finishes #34236669]
  • Loading branch information
Adam Berlin and David Edwards committed Sep 11, 2012
1 parent 4303c91 commit f050725
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 14 deletions.
18 changes: 18 additions & 0 deletions features/rake_tasks/generate_dependencies.feature
Expand Up @@ -46,3 +46,21 @@ Feature: rake license:generate_dependencies

And I run "rake license:action_items"
Then I should not see "my_javascript_library" in its output

Scenario: I want to see the group that my dependencies belong to in the dependencies.txt
Given I have a rails application with license finder
And my rails app depends on a gem "mit_gem" licensed with "MIT" in the "production" bundler groups
When I run "rake license:generate_dependencies"
Then license finder should generate a file "dependencies.txt" containing:
"""
mit_gem 0.0.0, MIT, production
"""

Scenario: I have specified multiple groups for my gem
Given I have a rails application with license finder
And my rails app depends on a gem "mit_gem" licensed with "MIT" in the "production, demo, staging" bundler groups
When I run "rake license:generate_dependencies"
Then license finder should generate a file "dependencies.txt" containing:
"""
mit_gem 0.0.0, MIT, production, demo, staging
"""
20 changes: 18 additions & 2 deletions features/step_definitions/steps.rb
Expand Up @@ -35,6 +35,10 @@
@user.add_dependency_to_app gem_name, license
end

Given /^my rails app depends on a gem "(.*?)" licensed with "(.*?)" in the "(.*?)" bundler groups$/ do |gem_name, license, bundler_groups|
@user.add_dependency_to_app gem_name, license, bundler_groups
end

Given /^I whitelist the "(.*?)" license$/ do |license|
@user.configure_license_finder_whitelist [license]
end
Expand Down Expand Up @@ -67,6 +71,10 @@
File.read(File.join(@user.app_path, filename)).should == text.gsub(/^\s+/, "")
end

Then /^license finder should generate a file "([^"]*)" containing:$/ do |filename, text|
File.read(File.join(@user.app_path, filename)).should include(text.gsub(/^\s+/, ""))
end

Then /^I should see the following settings for "([^"]*)":$/ do |name, yaml|
expected_settings = YAML.load(yaml)
all_settings = YAML.load(File.read(@user.dependencies_file_path))
Expand Down Expand Up @@ -137,7 +145,9 @@ def add_to_rakefile(line)
`echo \"#{line}\" >> #{app_path}/Rakefile`
end

def add_dependency_to_app(gem_name, license)
def add_dependency_to_app(gem_name, license, bundler_groups = "")
bundler_groups = bundler_groups.split(',').map(&:strip)

`mkdir #{projects_path}/#{gem_name}`

File.open("#{projects_path}/#{gem_name}/#{gem_name}.gemspec", 'w') do |file|
Expand All @@ -152,7 +162,13 @@ def add_dependency_to_app(gem_name, license)
GEMSPEC
end

system "cd #{app_path} && echo \"gem '#{gem_name}', path: '#{File.join(projects_path, gem_name)}'\" >> Gemfile"
gemfile_bits = []
gemfile_bits << "gem '#{gem_name}'"
gemfile_bits << "path: '#{File.join(projects_path, gem_name)}'"
gemfile_bits << "groups: #{bundler_groups.to_s.tr('\"', '\'')}" if bundler_groups.size > 1
gemfile_bits << "group: '#{bundler_groups.first}'" if bundler_groups.size == 1

system "cd #{app_path} && echo \"#{gemfile_bits.join(", ")} \" >> Gemfile"

bundle_app
end
Expand Down
1 change: 1 addition & 0 deletions lib/license_finder.rb
Expand Up @@ -39,6 +39,7 @@ def self.load_rake_tasks
end

require 'license_finder/railtie' if defined?(Rails)
require 'license_finder/bundler_dependency_query'
require 'license_finder/finder'
require 'license_finder/gem_spec_details'
require 'license_finder/license'
Expand Down
51 changes: 51 additions & 0 deletions lib/license_finder/bundler_dependency_query.rb
@@ -0,0 +1,51 @@
module LicenseFinder
class BundlerDependencyQuery
def dependencies
bundler_definition.specs_for(requested_groups).map do |spec|
dependency = define_a_new_dependency_from_a_gemspec(spec)
add_additional_information_from_bundler_to_a_dependency(dependency)
end
end

private

def add_additional_information_from_bundler_to_a_dependency(dependency)
bundler_dependency = find_bundlers_representation_of_a_dependency_by_name(dependency.name)

if bundler_dependency
dependency.bundler_groups = bundler_dependency.groups
end

dependency
end

def define_a_new_dependency_from_a_gemspec(gemspec)
GemSpecDetails.new(gemspec).dependency
end

def find_bundlers_representation_of_a_dependency_by_name(name)
bundler_dependencies.detect { |dep| dep.name == name }
end

def requested_groups
bundler_definition.groups - LicenseFinder.config.ignore_groups
end

def gemfile_path
Pathname.new("Gemfile").expand_path
end

def lockfile_path
root = gemfile_path.dirname
root.join('Gemfile.lock')
end

def bundler_dependencies
@bundler_dependencies ||= bundler_definition.dependencies
end

def bundler_definition
@bundler_definition ||= Bundler::Definition.build(gemfile_path, lockfile_path, nil)
end
end
end
9 changes: 8 additions & 1 deletion lib/license_finder/dependency.rb
@@ -1,6 +1,7 @@
module LicenseFinder
class Dependency
attr_accessor :name, :version, :license, :approved, :license_url, :notes, :license_files, :readme_files, :source
attr_accessor :name, :version, :license, :approved, :license_url, :notes, :license_files,
:readme_files, :source, :bundler_groups

def self.from_hash(attrs)
attrs['license_files'] = attrs['license_files'].map { |lf| lf['path'] } if attrs['license_files']
Expand All @@ -19,6 +20,7 @@ def initialize(attributes = {})
@notes = attributes['notes'] || ''
@license_files = attributes['license_files'] || []
@readme_files = attributes['readme_files'] || []
@bundler_groups = attributes['bundler_groups'] || []
end

def merge(other)
Expand Down Expand Up @@ -81,6 +83,11 @@ def to_yaml
def to_s
url = ", #{license_url}" if license_url != ''
str = "#{name} #{version}, #{license}#{url}"

if bundler_groups.size > 0
str << ", #{bundler_groups.join(', ')}"
end

if license == 'other'
str << "\n license files:"
unless self.license_files.empty?
Expand Down
9 changes: 1 addition & 8 deletions lib/license_finder/dependency_list.rb
Expand Up @@ -4,14 +4,7 @@ class DependencyList
attr_reader :dependencies

def self.from_bundler
gemfile = Pathname.new("Gemfile").expand_path
root = gemfile.dirname
lockfile = root.join('Gemfile.lock')
definition = Bundler::Definition.build(gemfile, lockfile, nil)

groups = definition.groups - LicenseFinder.config.ignore_groups

new(definition.specs_for(groups).map { |spec| GemSpecDetails.new(spec).dependency })
new(BundlerDependencyQuery.new.dependencies)
end

def initialize(dependencies)
Expand Down
2 changes: 1 addition & 1 deletion lib/license_finder/finder.rb
Expand Up @@ -11,10 +11,10 @@ def write_files
File.open(LicenseFinder.config.dependencies_yaml, 'w+') do |f|
f.puts new_list.to_yaml
end

File.open(LicenseFinder.config.dependencies_text, 'w+') do |f|
f.puts new_list.to_s
end

end

def action_items
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/license_finder/dependency_list_spec.rb
Expand Up @@ -34,6 +34,7 @@ def license
describe 'from Bundler' do
subject do
mock_bundler = Object.new
stub(mock_bundler).dependencies { [] }
stub(Bundler::Definition).build {mock_bundler}
stub(mock_bundler).groups {[]}
stub(mock_bundler).specs_for { [@mock_gemspec.new('gem1', '1.2.3'), @mock_gemspec.new('gem2', '0.4.2')] }
Expand All @@ -55,7 +56,6 @@ def license
it { dep.name.should == 'gem2' }
it { dep.version.should == '0.4.2' }
end

end

describe '#from_yaml' do
Expand Down
5 changes: 4 additions & 1 deletion spec/lib/license_finder/dependency_spec.rb
Expand Up @@ -11,7 +11,8 @@
'notes' => 'some notes',
'license_files' => [{'path' => '/Users/pivotal/foo/lic1'}, {'path' => '/Users/pivotal/bar/lic2'}],
'readme_files' => [{'path' => '/Users/pivotal/foo/Readme1'}, {'path' => '/Users/pivotal/bar/Readme2'}],
'source' => "bundle"
'source' => "bundle",
'bundler_groups' => nil
}
end

Expand Down Expand Up @@ -49,6 +50,8 @@
its(:license_files) { should == ["/Users/pivotal/foo/lic1", "/Users/pivotal/bar/lic2"] }
its(:readme_files) { should == ["/Users/pivotal/foo/Readme1", "/Users/pivotal/bar/Readme2"] }
its(:source) { should == "bundle" }
its(:bundler_groups) { should == [] }

its(:as_yaml) do
should == {
'name' => 'spec_name',
Expand Down

0 comments on commit f050725

Please sign in to comment.