borrower 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/borrower.rb +5 -3
- data/lib/borrower/content.rb +8 -8
- data/lib/borrower/manifest.rb +8 -0
- data/lib/borrower/manifest/config_file.rb +42 -0
- data/lib/borrower/version.rb +1 -1
- data/spec/borrower/manifest_spec.rb +81 -22
- data/spec/borrower_spec.rb +27 -0
- data/spec/spec_helper.rb +9 -2
- metadata +26 -27
data/lib/borrower.rb
CHANGED
@@ -36,10 +36,12 @@ module Borrower::DSL
|
|
36
36
|
def borrow path, options={}, &block
|
37
37
|
destination = options.delete(:to) { raise ArgumentError, "missing 'to:' argument" }
|
38
38
|
content = Borrower.take(path)
|
39
|
-
|
40
|
-
if
|
41
|
-
content =
|
39
|
+
|
40
|
+
if content.ascii_only?
|
41
|
+
content = Borrower.merge(content, options) if options.fetch(:merge) { false }
|
42
|
+
content = yield content if block_given?
|
42
43
|
end
|
44
|
+
|
43
45
|
Borrower.put content, destination
|
44
46
|
end
|
45
47
|
|
data/lib/borrower/content.rb
CHANGED
@@ -9,18 +9,14 @@ module Borrower
|
|
9
9
|
path = Borrower.find(path)
|
10
10
|
obj = Item.new( path )
|
11
11
|
|
12
|
-
|
13
|
-
return obj.body
|
14
|
-
else
|
15
|
-
return IO.read(path)
|
16
|
-
end
|
12
|
+
return obj.content if obj.exists?
|
17
13
|
|
18
14
|
raise "nothing exists at the provided path '#{path}'"
|
19
15
|
end
|
20
16
|
|
21
17
|
def put content, destination
|
22
18
|
FileUtils.mkdir_p( File.dirname(destination) )
|
23
|
-
File.open( destination, '
|
19
|
+
File.open( destination, 'wb' ) do |file|
|
24
20
|
file.write content
|
25
21
|
end
|
26
22
|
end
|
@@ -44,8 +40,12 @@ module Borrower
|
|
44
40
|
end
|
45
41
|
end
|
46
42
|
|
47
|
-
def
|
48
|
-
|
43
|
+
def content
|
44
|
+
if remote? && exists?
|
45
|
+
@_response.body
|
46
|
+
else
|
47
|
+
IO.read( @path )
|
48
|
+
end
|
49
49
|
end
|
50
50
|
|
51
51
|
private
|
data/lib/borrower/manifest.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'borrower/manifest/config_file'
|
2
|
+
|
1
3
|
module Borrower
|
2
4
|
class Manifest
|
3
5
|
|
@@ -7,6 +9,12 @@ module Borrower
|
|
7
9
|
def initialize
|
8
10
|
@files = {}
|
9
11
|
@directories = []
|
12
|
+
|
13
|
+
if Manifest::ConfigFile.present?
|
14
|
+
@manifest_file = Manifest::ConfigFile.new
|
15
|
+
@files = @files.merge( @manifest_file.files )
|
16
|
+
@directories = @directories.push( *@manifest_file.directories )
|
17
|
+
end
|
10
18
|
end
|
11
19
|
|
12
20
|
def file name, path
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Borrower
|
2
|
+
class Manifest
|
3
|
+
class ConfigFile
|
4
|
+
|
5
|
+
attr_reader :files
|
6
|
+
attr_reader :directories
|
7
|
+
|
8
|
+
SOURCE_FILE = ::Borrower::Content::Item.new( 'manifest.borrower' )
|
9
|
+
|
10
|
+
class << self
|
11
|
+
|
12
|
+
def present?
|
13
|
+
SOURCE_FILE.exists?
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
require 'yaml'
|
20
|
+
|
21
|
+
@files = {}.merge( manifest_file[:files] )
|
22
|
+
@directories = [].push( *manifest_file[:directories] )
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def manifest_file
|
28
|
+
return @_manifest_file if @manifest_file
|
29
|
+
|
30
|
+
@_manifest_file = {
|
31
|
+
files: raw_manifest_file['files'],
|
32
|
+
directories: raw_manifest_file['directories'].split(' ')
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
def raw_manifest_file
|
37
|
+
YAML.load( SOURCE_FILE.content )
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/borrower/version.rb
CHANGED
@@ -4,39 +4,98 @@ describe Borrower::Manifest do
|
|
4
4
|
|
5
5
|
before :each do
|
6
6
|
given_files [ "baz.txt", "files/baz.txt", "files/woo.txt", "files/foo.txt" ]
|
7
|
-
Borrower.manifest do |m|
|
8
|
-
m.file "me", File.join( TMP, "test.txt" )
|
9
|
-
m.file "remote", "https://gist.github.com/stevenosloan/5578606/raw/97ab1305184bdeac33472f9f1fcc1c9e278a1bb3/dummy.txt"
|
10
|
-
m.dir File.join( TMP )
|
11
|
-
m.dir File.join( TMP, 'files' )
|
12
|
-
end
|
13
7
|
end
|
14
8
|
|
15
9
|
after :each do
|
16
10
|
cleanup_tmp
|
17
11
|
end
|
18
12
|
|
19
|
-
|
20
|
-
|
21
|
-
|
13
|
+
let(:me_key) { "me" }
|
14
|
+
let(:me_file) { File.join( TMP, "test.txt" ) }
|
15
|
+
let(:remote_key) { "remote" }
|
16
|
+
let(:remote_file) { "https://gist.github.com/stevenosloan/5578606/raw/97ab1305184bdeac33472f9f1fcc1c9e278a1bb3/dummy.txt" }
|
17
|
+
let(:base_dir) { File.join( TMP ) }
|
18
|
+
let(:files_dir) { File.join( TMP, 'files' ) }
|
22
19
|
|
23
|
-
|
24
|
-
Borrower.find("http://google.com").should == "http://google.com"
|
25
|
-
end
|
20
|
+
context "with no manifest file" do
|
26
21
|
|
27
|
-
|
28
|
-
|
29
|
-
|
22
|
+
before :each do
|
23
|
+
Borrower.manifest do |m|
|
24
|
+
m.file me_key, me_file
|
25
|
+
m.file remote_key, remote_file
|
26
|
+
m.dir base_dir
|
27
|
+
m.dir files_dir
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "finds a file path based on key name if set" do
|
32
|
+
Borrower.find(me_key).should == me_file
|
33
|
+
end
|
34
|
+
|
35
|
+
it "returns remote paths if given fully resolved" do
|
36
|
+
Borrower.find("http://google.com").should == "http://google.com"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "finds named remote files" do
|
40
|
+
Borrower.find(remote_key).should == remote_file
|
41
|
+
end
|
42
|
+
|
43
|
+
it "creates list of files from directories" do
|
44
|
+
Borrower.find("baz.txt").should == File.join( TMP, "baz.txt" )
|
45
|
+
Borrower.find("files/baz.txt").should == File.join( TMP, "files/baz.txt" )
|
46
|
+
Borrower.find("woo.txt").should == File.join( TMP, "files/woo.txt" )
|
47
|
+
Borrower.find("foo.txt").should == File.join( TMP, "files/foo.txt" )
|
48
|
+
end
|
49
|
+
|
50
|
+
it "raises error if no file is found" do
|
51
|
+
expect { Borrower.find("doesnt_exist.txt") }.to raise_error
|
52
|
+
end
|
30
53
|
|
31
|
-
it "creates list of files from directories" do
|
32
|
-
Borrower.find("baz.txt").should == File.join( TMP, "baz.txt" )
|
33
|
-
Borrower.find("files/baz.txt").should == File.join( TMP, "files/baz.txt" )
|
34
|
-
Borrower.find("woo.txt").should == File.join( TMP, "files/woo.txt" )
|
35
|
-
Borrower.find("foo.txt").should == File.join( TMP, "files/foo.txt" )
|
36
54
|
end
|
37
55
|
|
38
|
-
|
39
|
-
|
56
|
+
context "with a borrower manifest file" do
|
57
|
+
|
58
|
+
before :each do
|
59
|
+
# ensure the manifest is reset
|
60
|
+
if Borrower.send(:instance_variable_defined?, :@_manifest)
|
61
|
+
Borrower.send(:remove_instance_variable, :@_manifest)
|
62
|
+
end
|
63
|
+
|
64
|
+
# move working dir to TMP
|
65
|
+
Dir.chdir TMP
|
66
|
+
end
|
67
|
+
|
68
|
+
after :each do
|
69
|
+
# move the working dir to root
|
70
|
+
Dir.chdir ROOT
|
71
|
+
end
|
72
|
+
|
73
|
+
before :each do
|
74
|
+
given_config %Q{
|
75
|
+
files:
|
76
|
+
#{me_key}: #{me_file}
|
77
|
+
#{remote_key}: #{remote_file}
|
78
|
+
|
79
|
+
directories:
|
80
|
+
#{base_dir}
|
81
|
+
#{files_dir}
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
it "detects that it is present" do
|
86
|
+
Borrower::Manifest::ConfigFile.present?.should be_true
|
87
|
+
end
|
88
|
+
|
89
|
+
it "adds the given files to the manifest" do
|
90
|
+
Borrower.manifest.files[me_key].should == me_file
|
91
|
+
Borrower.manifest.files[remote_key].should == remote_file
|
92
|
+
end
|
93
|
+
|
94
|
+
it "add the given directories to the manifest" do
|
95
|
+
Borrower.manifest.directories.include?( base_dir ).should be_true
|
96
|
+
Borrower.manifest.directories.include?( files_dir ).should be_true
|
97
|
+
end
|
98
|
+
|
40
99
|
end
|
41
100
|
|
42
101
|
end
|
data/spec/borrower_spec.rb
CHANGED
@@ -67,4 +67,31 @@ describe Borrower do
|
|
67
67
|
Borrower.take( file_destination ).should == "Hi I'm a file foo"
|
68
68
|
end
|
69
69
|
|
70
|
+
context "binary files" do
|
71
|
+
|
72
|
+
let(:binary_file) { File.join( Dir.pwd, 'spec/fixture', 'image.png' ) }
|
73
|
+
let(:binary_dest) { File.join( TMP, 'image.png' ) }
|
74
|
+
|
75
|
+
it "borrows them as it would any other file" do
|
76
|
+
borrow binary_file, to: binary_dest
|
77
|
+
|
78
|
+
expect( File.exists?(binary_dest) ).to be_true
|
79
|
+
end
|
80
|
+
|
81
|
+
it "skips merge attempts" do
|
82
|
+
borrow binary_file, to: binary_dest, merge: true
|
83
|
+
|
84
|
+
expect( File.exists?(binary_dest) ).to be_true
|
85
|
+
end
|
86
|
+
|
87
|
+
it "skips block manipulation" do
|
88
|
+
borrow binary_file, to: binary_dest do |f|
|
89
|
+
f.gsub( "foo", "bar" )
|
90
|
+
end
|
91
|
+
|
92
|
+
expect( File.exists?(binary_dest) ).to be_true
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
70
97
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
require 'rspec'
|
2
2
|
require 'fileutils'
|
3
3
|
|
4
|
+
begin
|
5
|
+
require 'pry-debugger'
|
6
|
+
rescue LoadError
|
7
|
+
puts "no biggie, but pry debugger isn't available"
|
8
|
+
end
|
9
|
+
|
4
10
|
TMP = File.join( Dir.pwd, "tmp" )
|
11
|
+
ROOT = Dir.pwd
|
5
12
|
|
6
13
|
def given_files list
|
7
14
|
FileUtils.mkdir_p TMP
|
@@ -20,8 +27,8 @@ def given_file name, content
|
|
20
27
|
end
|
21
28
|
end
|
22
29
|
|
23
|
-
def given_config
|
24
|
-
File.open( File.join( TMP, '.borrower' ), 'w' ) do |file|
|
30
|
+
def given_config content
|
31
|
+
File.open( File.join( TMP, 'manifest.borrower' ), 'w' ) do |file|
|
25
32
|
file.write content
|
26
33
|
end
|
27
34
|
end
|
metadata
CHANGED
@@ -1,79 +1,78 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: borrower
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
4
|
+
prerelease:
|
5
|
+
version: 0.7.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Steven Sloan
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-08-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: net-ssh
|
16
|
-
|
17
|
-
none: false
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
18
17
|
requirements:
|
19
18
|
- - ~>
|
20
19
|
- !ruby/object:Gem::Version
|
21
20
|
version: 2.6.5
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
21
|
none: false
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 2.6.5
|
30
|
-
|
31
|
-
|
27
|
+
none: false
|
28
|
+
prerelease: false
|
29
|
+
type: :runtime
|
30
|
+
description: ' For borrowing little snippets of the web, or files, or really any snippet
|
31
|
+
of string. '
|
32
32
|
email:
|
33
33
|
- stevenosloan@gmail.com
|
34
34
|
executables: []
|
35
35
|
extensions: []
|
36
36
|
extra_rdoc_files: []
|
37
37
|
files:
|
38
|
+
- lib/borrower.rb
|
38
39
|
- lib/borrower/content.rb
|
39
40
|
- lib/borrower/manifest.rb
|
40
41
|
- lib/borrower/merge.rb
|
41
42
|
- lib/borrower/public_api.rb
|
42
43
|
- lib/borrower/version.rb
|
43
|
-
- lib/borrower.rb
|
44
|
-
- spec/borrower/manifest_spec.rb
|
45
|
-
- spec/borrower/merge_spec.rb
|
44
|
+
- lib/borrower/manifest/config_file.rb
|
46
45
|
- spec/borrower_spec.rb
|
47
46
|
- spec/spec_helper.rb
|
47
|
+
- spec/borrower/manifest_spec.rb
|
48
|
+
- spec/borrower/merge_spec.rb
|
48
49
|
homepage: http://github.com/stevenosloan/borrower
|
49
50
|
licenses:
|
50
51
|
- MIT
|
51
|
-
post_install_message:
|
52
|
+
post_install_message:
|
52
53
|
rdoc_options: []
|
53
54
|
require_paths:
|
54
55
|
- lib
|
55
56
|
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
-
none: false
|
57
57
|
requirements:
|
58
|
-
- -
|
58
|
+
- - '>='
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '0'
|
61
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
61
|
none: false
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
63
|
requirements:
|
64
|
-
- -
|
64
|
+
- - '>='
|
65
65
|
- !ruby/object:Gem::Version
|
66
66
|
version: '0'
|
67
|
+
none: false
|
67
68
|
requirements: []
|
68
|
-
rubyforge_project:
|
69
|
-
rubygems_version: 1.8.
|
70
|
-
signing_key:
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.8.24
|
71
|
+
signing_key:
|
71
72
|
specification_version: 3
|
72
|
-
summary: For borrowing little snippets of the web, or files, or really any snippet
|
73
|
-
of string.
|
73
|
+
summary: For borrowing little snippets of the web, or files, or really any snippet of string.
|
74
74
|
test_files:
|
75
|
-
- spec/borrower/manifest_spec.rb
|
76
|
-
- spec/borrower/merge_spec.rb
|
77
75
|
- spec/borrower_spec.rb
|
78
76
|
- spec/spec_helper.rb
|
79
|
-
|
77
|
+
- spec/borrower/manifest_spec.rb
|
78
|
+
- spec/borrower/merge_spec.rb
|