Permalink
Browse files

generate dependency from bundler gemspec, save/load from yml

  • Loading branch information...
1 parent eb9adc3 commit 344522fcf40d740f25034f8edcfd0cc8b884ea26 David Goudreau & Kurtis Seebaldt committed Mar 25, 2011
View
@@ -1,17 +1,29 @@
require 'pathname'
module LicenseFinder
-
-
ROOT_PATH = Pathname.new(__FILE__).dirname
def self.from_bundler
require 'bundler'
Bundler.load.specs.map { |spec| Finder.new(spec) }.sort_by &:sort_order
end
+
+ def self.to_yml
+ yml_string = self.from_bundler.map { |lf| lf.to_s(ARGV.first == "--with-licenses") }.join
+
+ if (File.exists?('./config'))
+ File.open('./config/dependencies.yml', 'w+') do |f|
+ f.puts yml_string
+ end
+ end
+ end
end
+require 'forwardable'
require 'license_finder/railtie' if defined?(Rails)
require 'license_finder/finder'
require 'license_finder/file_parser'
require 'license_finder/license_file'
require 'license_finder/readme_file'
+
+require 'license_finder/dependency'
+require 'license_finder/dependency_list'
@@ -0,0 +1,32 @@
+module LicenseFinder
+ class Dependency
+
+ attr_reader :name, :version, :license, :approved
+
+ def self.from_gemspec(gem_spec)
+ new(gem_spec.name, gem_spec.version, 'MIT', false)
+ end
+
+ def self.from_yaml(yml)
+ attrs = YAML.load(yml)
+ new(attrs['name'], attrs['version'], attrs['license'], attrs['approved'])
+ end
+
+ def self.from_hash(attrs)
+ new(attrs['name'], attrs['version'], attrs['license'], attrs['approved'])
+ end
+
+ def initialize(name, version, license, approved)
+ @name = name
+ @version = version
+ @license = license
+ @approved = approved
+ end
+
+ def to_yaml_entry
+ "- name: \"#{name}\"\n version: \"#{version}\"\n license: \"#{license}\"\n approved: #{approved}\n"
+ end
+
+ end
+end
+
@@ -0,0 +1,24 @@
+module LicenseFinder
+ class DependencyList
+
+ attr_reader :dependencies
+ def self.from_bundler
+ new(Bundler.load.specs.map{|s| Dependency.from_gemspec(s)})
+ end
+
+ def initialize(dependencies)
+ @dependencies = dependencies
+ end
+
+ def self.from_yaml(yml)
+ deps = YAML.load(yml)
+ new(deps.map{|dhash| Dependency.from_hash(dhash)})
+ end
+
+ def to_yaml
+ result = "--- \n"
+ dependencies.inject(result) {|r, d| r << d.to_yaml_entry; r}
+ end
+ end
+end
+
@@ -2,8 +2,6 @@
require 'rails'
module LicenseFinder
class Railtie < Rails::Railtie
- railtie_name :license_finder
-
rake_tasks do
load "tasks/license_finder.rake"
end
@@ -1,6 +1,6 @@
namespace :license do
desc 'generate a list of dependecy licenses'
task :check_dependencies do
- LicenseFinder.from_bundler.each { |lf| puts lf.to_s(ARGV.first == "--with-licenses") }
+ LicenseFinder.to_yml
end
end
@@ -0,0 +1,97 @@
+require 'spec_helper'
+
+class LicenseFinder::MockGemSpec2
+ def initialize(name = nil, version = nil, path = nil)
+ @name = name
+ @version = version
+ @path = path
+ end
+
+ def name
+ @name || 'spec_name'
+ end
+
+ def version
+ @version || '2.1.3'
+ end
+
+ def full_gem_path
+ @path || 'install/path'
+ end
+end
+
+describe LicenseFinder::DependencyList do
+ describe 'from Bundler' do
+ subject do
+ stub(Bundler).load.stub!.specs {[LicenseFinder::MockGemSpec2.new('gem1', '1.2.3'), LicenseFinder::MockGemSpec2.new('gem2', '0.4.2')]}
+ LicenseFinder::DependencyList.from_bundler
+ end
+
+ it "should have 2 dependencies" do
+ subject.dependencies.size.should == 2
+ end
+
+ describe "first" do
+ let(:dep) {subject.dependencies.first}
+ it { dep.name.should == 'gem1' }
+ it { dep.version.should == '1.2.3' }
+ it {dep.to_yaml_entry.should == "- name: \"gem1\"\n version: \"1.2.3\"\n license: \"MIT\"\n approved: false\n" }
+ end
+
+ describe "second" do
+ let(:dep) {subject.dependencies[1]}
+ it { dep.name.should == 'gem2' }
+ it { dep.version.should == '0.4.2' }
+ end
+
+ end
+
+ describe 'from yaml' do
+ subject { LicenseFinder::DependencyList.from_yaml("--- \n- name: \"gem1\"\n version: \"1.2.3\"\n license: \"MIT\"\n approved: false\n- name: \"gem2\"\n version: \"0.4.2\"\n license: \"MIT\"\n approved: false\n") }
+
+ it "should have 2 dependencies" do
+ subject.dependencies.size.should == 2
+ end
+
+ describe "first" do
+ let(:dep) {subject.dependencies.first}
+ it { dep.name.should == 'gem1' }
+ it { dep.version.should == '1.2.3' }
+ it {dep.to_yaml_entry.should == "- name: \"gem1\"\n version: \"1.2.3\"\n license: \"MIT\"\n approved: false\n" }
+ end
+
+ describe "second" do
+ let(:dep) {subject.dependencies[1]}
+ it { dep.name.should == 'gem2' }
+ it { dep.version.should == '0.4.2' }
+ end
+
+ end
+
+ describe 'to_yaml' do
+ it "should generate yaml" do
+ list = LicenseFinder::DependencyList.new([
+ LicenseFinder::Dependency.new('gem1', '1.2.3', 'MIT', false),
+ LicenseFinder::Dependency.new('gem2', '0.4.2', 'MIT', false)
+ ])
+
+ list.to_yaml.should == "--- \n- name: \"gem1\"\n version: \"1.2.3\"\n license: \"MIT\"\n approved: false\n- name: \"gem2\"\n version: \"0.4.2\"\n license: \"MIT\"\n approved: false\n"
+ end
+ end
+
+ describe 'round trip' do
+ it 'should recreate from to_yaml' do
+ list = LicenseFinder::DependencyList.new([
+ LicenseFinder::Dependency.new('gem1', '1.2.3', 'MIT', false),
+ LicenseFinder::Dependency.new('gem2', '0.4.2', 'MIT', false)
+ ])
+
+ new_list = LicenseFinder::DependencyList.from_yaml(list.to_yaml)
+ new_list.dependencies.size.should == 2
+ new_list.dependencies.first.name.should == 'gem1'
+ new_list.dependencies[1].name.should == 'gem2'
+ end
+ end
+end
+
+
View
@@ -0,0 +1,55 @@
+require 'spec_helper'
+
+class LicenseFinder::MockGemSpec3
+ def initialize(path = nil)
+ @path = path
+ end
+
+ def name
+ 'spec_name'
+ end
+
+ def version
+ '2.1.3'
+ end
+
+ def full_gem_path
+ @path || 'install/path'
+ end
+end
+
+#--
+# name: activerecord
+# version: 3.0.5
+# license: MIT
+# approved: true
+# license_url: http://foo.com/README
+
+describe LicenseFinder::Dependency do
+ describe 'from gemspec' do
+ subject { LicenseFinder::Dependency.from_gemspec(LicenseFinder::MockGemSpec3.new) }
+
+ its(:name) { should == 'spec_name' }
+ its(:version) { should == '2.1.3' }
+ its(:license) { should == 'MIT' }
+ its(:approved) { should == false }
+
+ its(:to_yaml_entry) {should == "- name: \"spec_name\"\n version: \"2.1.3\"\n license: \"MIT\"\n approved: false\n" }
+
+ end
+
+ describe 'from hash' do
+ subject { LicenseFinder::Dependency.from_hash({'name' => "spec_name", 'version' => "2.1.3", 'license' => "MIT", 'approved' => false}) }
+
+ its(:name) { should == 'spec_name' }
+ its(:version) { should == '2.1.3' }
+ its(:license) { should == 'MIT' }
+ its(:approved) { should == false }
+
+ its(:to_yaml_entry) {should == "- name: \"spec_name\"\n version: \"2.1.3\"\n license: \"MIT\"\n approved: false\n" }
+
+ end
+
+end
+
+
@@ -18,6 +18,30 @@ def full_gem_path
end
end
+describe LicenseFinder do
+ before(:each) do
+
+ end
+
+ it "should generate a yml file" do
+ output = StringIO.new
+ stub(File).open.yields(output)
+ stub(File).exists? {true}
+ LicenseFinder.to_yml
+ output.string.should_not == ''
+ end
+
+ it 'should update an existing yml file' do
+# generate_yml_file
+# update_yml_file_with approved=true
+# regenerate_yml_file
+# assert approved=true
+# assert approved=false for newly added gem
+
+
+ end
+end
+
describe LicenseFinder::Finder do
subject { LicenseFinder::Finder.new(LicenseFinder::MockGemSpec.new) }
View
@@ -3,6 +3,7 @@
require 'license_finder'
+require 'rspec'
RSpec.configure do |config|
config.mock_with :rr
end

0 comments on commit 344522f

Please sign in to comment.