dependencies 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ task :test do
2
+ system "cd test && ruby dependencies_test.rb"
3
+ end
4
+
5
+ task :default => :test
data/bin/dep ADDED
@@ -0,0 +1,13 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require "dependencies/dep"
4
+
5
+ env = ARGV.first
6
+
7
+ deps = Dep.new(File.read("dependencies"))
8
+
9
+ deps.filter(env).each do |dep|
10
+ puts dep
11
+ end
12
+
13
+ deps.require(env)
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "dependencies"
3
+ s.version = "0.0.2"
4
+ s.summary = "Specify your project's dependencies in one file."
5
+ s.authors = ["Damian Janowski", "Michel Martens"]
6
+ s.email = ["djanowski@dimaion.com", "michel@soveran.com"]
7
+
8
+ s.rubyforge_project = "dependencies"
9
+
10
+ s.executables << "dep"
11
+
12
+ s.files = ["Rakefile", "bin/dep", "dependencies.gemspec", "lib/dependencies/dep.rb", "lib/dependencies.rb", "test/dependencies_test.rb", "test/vendor/barz-2.0", "test/vendor/baz-1.0"]
13
+ end
@@ -0,0 +1,90 @@
1
+ class Dep
2
+ attr :dependencies
3
+
4
+ class Dependency
5
+ attr :name
6
+ attr :version
7
+ attr :environment
8
+
9
+ def initialize(name, version = nil, environment = nil)
10
+ @name = name
11
+ @version = version if version && !version.empty?
12
+ @environment = environment ? environment.split(/\, ?/) : []
13
+ end
14
+
15
+ def for_environment?(env)
16
+ environment.empty? || environment.include?(env)
17
+ end
18
+
19
+ def version_number
20
+ version[/([\d\.]+)$/, 1]
21
+ end
22
+
23
+ def vendor_name
24
+ version ? "#{name}-#{version_number}" : name
25
+ end
26
+
27
+ def vendor_path
28
+ Dir[File.join('vendor', "#{vendor_name}*", 'lib')].first
29
+ end
30
+
31
+ def require_vendor
32
+ $:.unshift(File.expand_path(vendor_path)) if vendor_path
33
+ end
34
+
35
+ def require_gem
36
+ begin
37
+ gem(*[name, version].compact)
38
+ true
39
+ rescue Gem::LoadError => e
40
+ false
41
+ end
42
+ end
43
+
44
+ def require
45
+ require_vendor || require_gem
46
+ end
47
+
48
+ def to_s
49
+ [name, version, ("(#{environment.join(", ")})" unless environment.empty?)].compact.join(" ")
50
+ end
51
+ end
52
+
53
+ def initialize(dependencies)
54
+ @dependencies = []
55
+ @missing = []
56
+
57
+ dependencies.each_line do |line|
58
+ next unless line =~ /^([\w\-_]+) ?([<~=> \d\.]+)?( \(([\w, ]+)\))?$/
59
+ @dependencies << Dependency.new($1, $2, $4)
60
+ end
61
+ end
62
+
63
+ def filter(environment)
64
+ @dependencies.select do |dep|
65
+ dep.for_environment?(environment)
66
+ end
67
+ end
68
+
69
+ def require(environment)
70
+ filter(environment).each do |dep|
71
+ @missing << dep unless dep.require
72
+ end
73
+
74
+ if !@missing.empty?
75
+ $stderr.puts "\nMissing dependencies:\n\n"
76
+
77
+ @missing.each do |dep|
78
+ $stderr.puts " #{dep}"
79
+ end
80
+
81
+ $stderr.puts
82
+ end
83
+
84
+ $:.unshift File.expand_path("lib")
85
+ end
86
+
87
+ def each(&block)
88
+ @dependencies.each(&block)
89
+ end
90
+ end
@@ -0,0 +1,3 @@
1
+ require "dependencies/dep"
2
+
3
+ Dep.new(File.read("dependencies")).require(ENV["RACK_ENV"].to_s)
@@ -0,0 +1,115 @@
1
+ require "rubygems"
2
+ require "ruby-debug"
3
+
4
+ gem "contest"
5
+
6
+ require "contest"
7
+
8
+ require "pp"
9
+ require "stringio"
10
+ require "fileutils"
11
+
12
+ $:.unshift LIB = File.join(File.dirname(__FILE__), "..", "lib")
13
+
14
+ class DependenciesTest < Test::Unit::TestCase
15
+ def do_require
16
+ load "dependencies.rb"
17
+ end
18
+
19
+ def with_dependencies(deps)
20
+ File.open("dependencies", "w") do |f|
21
+ f.write(deps)
22
+ end
23
+
24
+ yield
25
+ ensure
26
+ FileUtils.rm("dependencies")
27
+ end
28
+
29
+ context "lib" do
30
+ setup do
31
+ @load_path = $LOAD_PATH.dup
32
+ end
33
+
34
+ test "loads dependencies from ./vendor" do
35
+ with_dependencies "bar" do
36
+ do_require
37
+
38
+ assert_equal File.expand_path("vendor/bar/lib"), $:[1]
39
+ end
40
+ end
41
+
42
+ test "add ./lib to the load path" do
43
+ with_dependencies "" do
44
+ do_require
45
+
46
+ assert_equal File.expand_path("lib"), $:.first
47
+ end
48
+ end
49
+
50
+ test "alert about missing dependencies" do
51
+ with_dependencies "foo 1.0" do
52
+ err = capture_stderr do
53
+ do_require
54
+ end
55
+
56
+ assert err.include?("Missing dependencies:\n\n foo 1.0")
57
+ end
58
+ end
59
+
60
+ test "load environment-specific dependencies" do
61
+ begin
62
+ ENV["RACK_ENV"] = "integration"
63
+
64
+ with_dependencies "bar\nbarz 2.0 (test)\nbaz 1.0 (integration)" do
65
+ do_require
66
+
67
+ assert $:.include?(File.expand_path("vendor/bar/lib"))
68
+ assert $:.include?(File.expand_path("vendor/baz-1.0/lib"))
69
+ assert !$:.include?(File.expand_path("vendor/barz-2.0/lib"))
70
+ end
71
+
72
+ ensure
73
+ ENV.delete "RACK_ENV"
74
+ end
75
+ end
76
+
77
+ teardown do
78
+ $LOAD_PATH.replace(@load_path)
79
+ end
80
+ end
81
+
82
+ context "binary" do
83
+ def dep(args = nil)
84
+ %x{ruby -I#{LIB} -rubygems #{File.expand_path(File.join("../bin/dep"))} #{args}}
85
+ end
86
+
87
+ test "prints all dependencies" do
88
+ with_dependencies "foo ~> 1.0\nbar" do
89
+ out = dep
90
+
91
+ assert_equal "foo ~> 1.0\nbar\n", out
92
+ end
93
+ end
94
+
95
+ test "prints dependencies based on given environment" do
96
+ with_dependencies "foo 1.0 (test)\nbar (development)\nbarz 2.0\nbaz 0.1 (test)" do
97
+ out = dep "test"
98
+
99
+ assert_equal "foo 1.0 (test)\nbarz 2.0\nbaz 0.1 (test)\n", out
100
+ end
101
+ end
102
+ end
103
+
104
+ protected
105
+
106
+ def capture_stderr
107
+ begin
108
+ err, $stderr = $stderr, StringIO.new
109
+ yield
110
+ ensure
111
+ $stderr, err = err, $stderr
112
+ end
113
+ err.string
114
+ end
115
+ end
metadata CHANGED
@@ -1,28 +1,36 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dependencies
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - TBD
7
+ - Damian Janowski
8
+ - Michel Martens
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2009-06-19 00:00:00 -03:00
13
+ date: 2009-06-24 00:00:00 -03:00
13
14
  default_executable:
14
15
  dependencies: []
15
16
 
16
- description: TBD
17
- email: TBD
18
- executables: []
19
-
17
+ description:
18
+ email:
19
+ - djanowski@dimaion.com
20
+ - michel@soveran.com
21
+ executables:
22
+ - dep
20
23
  extensions: []
21
24
 
22
25
  extra_rdoc_files: []
23
26
 
24
- files: []
25
-
27
+ files:
28
+ - Rakefile
29
+ - bin/dep
30
+ - dependencies.gemspec
31
+ - lib/dependencies/dep.rb
32
+ - lib/dependencies.rb
33
+ - test/dependencies_test.rb
26
34
  has_rdoc: true
27
35
  homepage:
28
36
  licenses: []
@@ -46,10 +54,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
54
  version:
47
55
  requirements: []
48
56
 
49
- rubyforge_project:
57
+ rubyforge_project: dependencies
50
58
  rubygems_version: 1.3.4
51
59
  signing_key:
52
60
  specification_version: 3
53
- summary: TBD
61
+ summary: Specify your project's dependencies in one file.
54
62
  test_files: []
55
63