codependency 0.1.0
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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +52 -0
- data/Rakefile +6 -0
- data/codependency.gemspec +19 -0
- data/lib/codependency.rb +7 -0
- data/lib/codependency/graph.rb +28 -0
- data/lib/codependency/node.rb +33 -0
- data/lib/codependency/parser.rb +16 -0
- data/lib/codependency/version.rb +3 -0
- data/spec/codependency/graph_spec.rb +16 -0
- data/spec/codependency/node_spec.rb +19 -0
- data/spec/codependency/parser_spec.rb +31 -0
- data/spec/fixtures/body.rb +2 -0
- data/spec/fixtures/earth.rb +4 -0
- data/spec/fixtures/mars.rb +4 -0
- data/spec/fixtures/phobos.rb +5 -0
- data/spec/fixtures/planet.rb +4 -0
- data/spec/spec_helper.rb +6 -0
- metadata +90 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jeremy Ruppel
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Codependency
|
2
|
+
|
3
|
+
Codependency is a simple comment-based dependency graph that you can use on arbitrary files. At the moment, it only supports ruby-style comments, files with the `.rb` extension, and files in the same flat directory.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'codependency'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install codependency
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Say you have two files, `bar.rb` and `foo.rb`. `bar` comes before `foo` in a natural naming scheme, but `bar` has a dependency on `foo`. We can express this using a simple comment syntax at the head of the file like this:
|
22
|
+
|
23
|
+
**bar.rb:**
|
24
|
+
|
25
|
+
``` rb
|
26
|
+
# require foo
|
27
|
+
|
28
|
+
class Bar
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
**foo.rb:**
|
33
|
+
|
34
|
+
``` rb
|
35
|
+
class Foo
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Then, we create a dependency graph to determine the order in which the files might need to be loaded, inserted, or compiled:
|
40
|
+
|
41
|
+
``` rb
|
42
|
+
graph = Codependency::Graph.new %w| bar.rb foo.rb |
|
43
|
+
graph.files # => ["foo.rb", "bar.rb"]
|
44
|
+
```
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/codependency/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jeremy Ruppel"]
|
6
|
+
gem.email = ["jeremy.ruppel@gmail.com"]
|
7
|
+
gem.description = %q{Simple comment-based dependency graph for arbitrary files}
|
8
|
+
gem.summary = %q{Simple comment-based dependency graph for arbitrary files}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "codependency"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Codependency::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rspec', '>= 2.10.0'
|
19
|
+
end
|
data/lib/codependency.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Codependency
|
2
|
+
class Graph
|
3
|
+
def initialize( filenames )
|
4
|
+
@nodes = Hash[ filenames.map { |f| [ f, Node.new( f ) ] } ]
|
5
|
+
end
|
6
|
+
attr_reader :nodes
|
7
|
+
|
8
|
+
def files
|
9
|
+
nodes.reduce( [ ] ) do |list, (filename, node)|
|
10
|
+
resolve node, list
|
11
|
+
list
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
def resolve( node, list )
|
18
|
+
unless list.include? node.filename
|
19
|
+
node.dependencies.each do |dep|
|
20
|
+
# TODO need to check here to make sure
|
21
|
+
# we actually have a node for this dependency
|
22
|
+
resolve nodes[ dep ], list
|
23
|
+
end
|
24
|
+
list << node.filename
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
module Codependency
|
4
|
+
class Node
|
5
|
+
def initialize( filename )
|
6
|
+
raise Errno::ENOENT unless File.exist?( filename )
|
7
|
+
@filename = filename
|
8
|
+
end
|
9
|
+
attr_reader :filename
|
10
|
+
|
11
|
+
def dependencies
|
12
|
+
parser.parse( filename ).map { |f| dirname.join( "#{f}#{extname}" ).to_s }
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
def dirname
|
18
|
+
path.dirname
|
19
|
+
end
|
20
|
+
|
21
|
+
def extname
|
22
|
+
path.extname
|
23
|
+
end
|
24
|
+
|
25
|
+
def path
|
26
|
+
@path ||= Pathname.new filename
|
27
|
+
end
|
28
|
+
|
29
|
+
def parser
|
30
|
+
@parser ||= Parser.new
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Codependency
|
2
|
+
class Parser
|
3
|
+
|
4
|
+
def parse( file )
|
5
|
+
IO.readlines( file ).take_while do |line|
|
6
|
+
line =~ pattern
|
7
|
+
end.map { |line| line[ pattern, 1 ] }
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
def pattern
|
13
|
+
@pattern ||= /^# require (.+)$/
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Codependency::Graph do
|
4
|
+
let( :path ){ File.join( File.dirname( __FILE__ ), '../fixtures' ) }
|
5
|
+
subject { Codependency::Graph.new Dir[ File.join( path, '*.rb' ) ] }
|
6
|
+
|
7
|
+
its( :files ){ should eq(
|
8
|
+
[
|
9
|
+
"#{path}/body.rb",
|
10
|
+
"#{path}/planet.rb",
|
11
|
+
"#{path}/earth.rb",
|
12
|
+
"#{path}/mars.rb",
|
13
|
+
"#{path}/phobos.rb"
|
14
|
+
]
|
15
|
+
) }
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Codependency::Node do
|
4
|
+
let( :path ){ File.join( File.dirname( __FILE__ ), '../fixtures' ) }
|
5
|
+
|
6
|
+
context 'when the file exists' do
|
7
|
+
subject { Codependency::Node.new "#{path}/planet.rb" }
|
8
|
+
|
9
|
+
its( :dependencies ){ should eq( [ "#{path}/body.rb" ] ) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'when the file does not exist' do
|
13
|
+
it 'should raise an error' do
|
14
|
+
expect {
|
15
|
+
Codependency::Node.new "#{path}/pluto.rb"
|
16
|
+
}.to raise_error( Errno::ENOENT )
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Codependency::Parser do
|
4
|
+
let( :path ){ File.join( File.dirname( __FILE__ ), '../fixtures' ) }
|
5
|
+
let( :parser ){ Codependency::Parser.new }
|
6
|
+
|
7
|
+
context 'body' do
|
8
|
+
subject { parser.parse( "#{path}/body.rb" ) }
|
9
|
+
it { should eq( [ ] ) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'earth' do
|
13
|
+
subject { parser.parse( "#{path}/earth.rb" ) }
|
14
|
+
it { should eq( [ 'planet' ] ) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'mars' do
|
18
|
+
subject { parser.parse( "#{path}/mars.rb" ) }
|
19
|
+
it { should eq( [ 'planet' ] ) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'phobos' do
|
23
|
+
subject { parser.parse( "#{path}/phobos.rb" ) }
|
24
|
+
it { should eq( [ 'body', 'mars' ] ) }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'planet' do
|
28
|
+
subject { parser.parse( "#{path}/planet.rb" ) }
|
29
|
+
it { should eq( [ 'body' ] ) }
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codependency
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeremy Ruppel
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-09 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.10.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.10.0
|
30
|
+
description: Simple comment-based dependency graph for arbitrary files
|
31
|
+
email:
|
32
|
+
- jeremy.ruppel@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- codependency.gemspec
|
43
|
+
- lib/codependency.rb
|
44
|
+
- lib/codependency/graph.rb
|
45
|
+
- lib/codependency/node.rb
|
46
|
+
- lib/codependency/parser.rb
|
47
|
+
- lib/codependency/version.rb
|
48
|
+
- spec/codependency/graph_spec.rb
|
49
|
+
- spec/codependency/node_spec.rb
|
50
|
+
- spec/codependency/parser_spec.rb
|
51
|
+
- spec/fixtures/body.rb
|
52
|
+
- spec/fixtures/earth.rb
|
53
|
+
- spec/fixtures/mars.rb
|
54
|
+
- spec/fixtures/phobos.rb
|
55
|
+
- spec/fixtures/planet.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
homepage: ''
|
58
|
+
licenses: []
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.8.19
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Simple comment-based dependency graph for arbitrary files
|
81
|
+
test_files:
|
82
|
+
- spec/codependency/graph_spec.rb
|
83
|
+
- spec/codependency/node_spec.rb
|
84
|
+
- spec/codependency/parser_spec.rb
|
85
|
+
- spec/fixtures/body.rb
|
86
|
+
- spec/fixtures/earth.rb
|
87
|
+
- spec/fixtures/mars.rb
|
88
|
+
- spec/fixtures/phobos.rb
|
89
|
+
- spec/fixtures/planet.rb
|
90
|
+
- spec/spec_helper.rb
|