borrower 0.0.3 → 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/lib/borrower/manifest.rb +46 -0
- data/lib/borrower/merge.rb +35 -0
- data/lib/borrower/transport.rb +5 -2
- data/lib/borrower/version.rb +1 -1
- data/lib/borrower.rb +4 -5
- data/spec/borrower/manifest_spec.rb +33 -0
- data/spec/borrower/merge_spec.rb +45 -0
- data/spec/borrower/transport_spec.rb +48 -0
- data/spec/borrower_spec.rb +31 -20
- data/spec/spec_helper.rb +31 -1
- metadata +11 -6
- data/spec/path_spec.rb +0 -43
- data/spec/transport_spec.rb +0 -46
@@ -0,0 +1,46 @@
|
|
1
|
+
module Borrower
|
2
|
+
|
3
|
+
class << self
|
4
|
+
def manifest &block
|
5
|
+
@_manifest ||= Manifest.new
|
6
|
+
yield @_manifest if block_given?
|
7
|
+
end
|
8
|
+
|
9
|
+
def find file
|
10
|
+
path = @_manifest.files.fetch(file) { false }
|
11
|
+
return path if path
|
12
|
+
|
13
|
+
matches = []
|
14
|
+
@_manifest.directories.each do |dir|
|
15
|
+
Dir[File.join( dir, '*' )].each do |possibility|
|
16
|
+
matches << possibility if possibility.match( file )
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
path = matches.sort { |a,b| a.length <=> b.length }.first
|
21
|
+
return path if path
|
22
|
+
|
23
|
+
raise "Could not file #{file}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Manifest
|
28
|
+
|
29
|
+
attr_reader :files
|
30
|
+
attr_reader :directories
|
31
|
+
|
32
|
+
def initialize
|
33
|
+
@files = {}
|
34
|
+
@directories = []
|
35
|
+
end
|
36
|
+
|
37
|
+
def file name, path
|
38
|
+
@files[name] = path
|
39
|
+
end
|
40
|
+
|
41
|
+
def dir dir
|
42
|
+
@directories.push dir
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Borrower
|
2
|
+
class << self
|
3
|
+
def merge content, options={}
|
4
|
+
Merge.new( content, options ).output
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class Merge
|
9
|
+
def initialize content, options={}
|
10
|
+
@content = content
|
11
|
+
@comment_symbol = options.fetch(:comment) { default_comment_symbol }
|
12
|
+
end
|
13
|
+
|
14
|
+
def output
|
15
|
+
merge_borrow_statements
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def default_comment_symbol
|
21
|
+
"#"
|
22
|
+
end
|
23
|
+
|
24
|
+
def merge_borrow_statements
|
25
|
+
@content.gsub /(?:#{@comment_symbol})= borrow '(.*?)'/ do |match|
|
26
|
+
contents_from_file($1)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def contents_from_file path
|
31
|
+
::Borrower::Path.contents(::Borrower.find(path))
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
data/lib/borrower/transport.rb
CHANGED
@@ -3,8 +3,11 @@ module Borrower
|
|
3
3
|
|
4
4
|
# routes a file from -> to
|
5
5
|
# @param from [String]
|
6
|
-
|
7
|
-
|
6
|
+
# @param args [Hash]
|
7
|
+
def move from, to, args={}
|
8
|
+
content = take(from)
|
9
|
+
content = Borrower.merge(content) if args[:merge]
|
10
|
+
put( content, to )
|
8
11
|
end
|
9
12
|
|
10
13
|
# handles taking a file
|
data/lib/borrower/version.rb
CHANGED
data/lib/borrower.rb
CHANGED
@@ -6,6 +6,8 @@ require "uri"
|
|
6
6
|
|
7
7
|
# borrower
|
8
8
|
require 'borrower/version'
|
9
|
+
require 'borrower/manifest'
|
10
|
+
require 'borrower/merge'
|
9
11
|
require 'borrower/path'
|
10
12
|
require 'borrower/transport'
|
11
13
|
|
@@ -20,11 +22,8 @@ module Borrower::DSL
|
|
20
22
|
# @param args [Hash] only to: is looked at
|
21
23
|
#
|
22
24
|
def borrow path, args
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
Borrower::Transport.move path, args[:to]
|
25
|
+
destination = args.delete(:to) { raise ArgumentError, "missing 'to:' argument" }
|
26
|
+
Borrower::Transport.move path, destination, args
|
28
27
|
end
|
29
28
|
|
30
29
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Borrower::Manifest do
|
4
|
+
|
5
|
+
before :each do
|
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.dir File.join( TMP )
|
10
|
+
m.dir File.join( TMP, 'files' )
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
after :each do
|
15
|
+
cleanup_tmp
|
16
|
+
end
|
17
|
+
|
18
|
+
it "finds a file path based on key name if set" do
|
19
|
+
Borrower.find("me").should == File.join( TMP, "test.txt" )
|
20
|
+
end
|
21
|
+
|
22
|
+
it "creates list of files from directories" do
|
23
|
+
Borrower.find("baz.txt").should == File.join( TMP, "baz.txt" )
|
24
|
+
Borrower.find("files/baz.txt").should == File.join( TMP, "files/baz.txt" )
|
25
|
+
Borrower.find("woo.txt").should == File.join( TMP, "files/woo.txt" )
|
26
|
+
Borrower.find("foo.txt").should == File.join( TMP, "files/foo.txt" )
|
27
|
+
end
|
28
|
+
|
29
|
+
it "raises error if no file is found" do
|
30
|
+
expect { Borrower.find("doesnt_exist.txt") }.to raise_error
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "moving files with a merge" do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
given_file "foo.txt", "hello I'm merged text"
|
7
|
+
Borrower.manifest do |m|
|
8
|
+
m.file "foo.txt", File.join( TMP, "foo.txt" )
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
after :each do
|
13
|
+
cleanup_tmp
|
14
|
+
end
|
15
|
+
|
16
|
+
it "merges contents of files" do
|
17
|
+
Borrower.merge("#= borrow 'foo.txt'").should == "hello I'm merged text"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "lets you pass other comment symbols" do
|
21
|
+
Borrower.merge("//= borrow 'foo.txt'", comment: "//" ).should == "hello I'm merged text"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "handles multiple files to merge with" do
|
25
|
+
given_file "woo.txt", "woo"
|
26
|
+
given_file "multiple.txt", <<-content
|
27
|
+
#= borrow 'foo.txt'
|
28
|
+
#= borrow 'woo.txt'
|
29
|
+
|
30
|
+
Some other things
|
31
|
+
content
|
32
|
+
Borrower.manifest do |m|
|
33
|
+
m.file 'multiple.txt', File.join( TMP, 'multiple.txt' )
|
34
|
+
m.file 'woo.txt', File.join( TMP, 'woo.txt' )
|
35
|
+
end
|
36
|
+
|
37
|
+
Borrower.merge(Borrower::Transport.take(Borrower.find("multiple.txt"))).should == <<-output
|
38
|
+
hello I'm merged text
|
39
|
+
woo
|
40
|
+
|
41
|
+
Some other things
|
42
|
+
output
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'borrower/transport'
|
3
|
+
|
4
|
+
describe Borrower::Transport do
|
5
|
+
include Borrower::Transport
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
given_file "file.txt", "Hi I'm a file"
|
9
|
+
end
|
10
|
+
|
11
|
+
after :each do
|
12
|
+
cleanup_tmp
|
13
|
+
end
|
14
|
+
|
15
|
+
let (:local_file) { File.join( TMP, 'file.txt' ) }
|
16
|
+
let (:destination_path) { File.join( TMP, 'destination/file.txt' ) }
|
17
|
+
|
18
|
+
|
19
|
+
describe "#take" do
|
20
|
+
it "returns the correct contents" do
|
21
|
+
take( local_file ).should == "Hi I'm a file"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#put" do
|
26
|
+
|
27
|
+
it "puts a string to a file" do
|
28
|
+
put "hello", destination_path
|
29
|
+
take( destination_path ).should == "hello"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#move" do
|
34
|
+
|
35
|
+
it "moves the file" do
|
36
|
+
move( local_file, destination_path )
|
37
|
+
take( destination_path ).should == "Hi I'm a file"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "overwrites an existing file" do
|
41
|
+
put "hello", destination_path
|
42
|
+
take( destination_path ).should == "hello" # just to make sure it was written to begin with
|
43
|
+
move( local_file, destination_path )
|
44
|
+
take( destination_path ).should == "Hi I'm a file"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
data/spec/borrower_spec.rb
CHANGED
@@ -2,26 +2,37 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Borrower do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
5
|
+
before :each do
|
6
|
+
given_file "file.txt", "Hi I'm a file"
|
7
|
+
given_file "merge.txt", "#= borrow '#{File.join( TMP, 'file.txt')}'"
|
8
|
+
end
|
9
|
+
|
10
|
+
after :each do
|
11
|
+
cleanup_tmp
|
12
|
+
end
|
13
|
+
|
14
|
+
let (:local_file) { File.join( TMP, 'file.txt' ) }
|
15
|
+
let (:merge_file) { File.join( TMP, 'merge.txt' ) }
|
16
|
+
let (:remote_file) { "https://gist.github.com/stevenosloan/5578606/raw/97ab1305184bdeac33472f9f1fcc1c9e278a1bb3/dummy.txt" }
|
17
|
+
let (:file_destination) { File.join( TMP, 'destination/file.txt' ) }
|
18
|
+
|
19
|
+
it "borrows local files" do
|
20
|
+
borrow local_file, to: file_destination
|
21
|
+
Borrower::Transport.take( file_destination ).should == "Hi I'm a file"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "borrows remote files" do
|
25
|
+
borrow remote_file, to: file_destination
|
26
|
+
Borrower::Transport.take( file_destination ).should == "Hello I'm a file"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "merges files if merge is set to true" do
|
30
|
+
borrow merge_file, to: file_destination, merge: true
|
31
|
+
Borrower::Transport.take( file_destination ).should == "Hi I'm a file"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "raises an error if missing arg with key 'to'" do
|
35
|
+
expect{ borrow( "foo", away: "bar" )}.to raise_error(ArgumentError)
|
25
36
|
end
|
26
37
|
|
27
38
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,2 +1,32 @@
|
|
1
1
|
require 'rspec'
|
2
|
-
require 'borrower'
|
2
|
+
require 'borrower'
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
TMP = File.join( Dir.pwd, "tmp" )
|
6
|
+
|
7
|
+
def given_files list
|
8
|
+
FileUtils.mkdir_p TMP
|
9
|
+
Array(list).each do |f|
|
10
|
+
file_path = File.join( TMP, f )
|
11
|
+
FileUtils.mkdir_p( File.dirname(file_path) )
|
12
|
+
FileUtils.touch( file_path )
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def given_file name, content
|
17
|
+
file_path = File.join( TMP, name )
|
18
|
+
FileUtils.mkdir_p( File.dirname(file_path) )
|
19
|
+
File.open( file_path, 'w' ) do |file|
|
20
|
+
file.write content
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def given_config contents
|
25
|
+
File.open( File.join( TMP, '.borrower' ), 'w' ) do |file|
|
26
|
+
file.write content
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def cleanup_tmp
|
31
|
+
`rm -rf #{TMP}`
|
32
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: borrower
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: net-ssh
|
@@ -35,14 +35,17 @@ executables: []
|
|
35
35
|
extensions: []
|
36
36
|
extra_rdoc_files: []
|
37
37
|
files:
|
38
|
+
- lib/borrower/manifest.rb
|
39
|
+
- lib/borrower/merge.rb
|
38
40
|
- lib/borrower/path.rb
|
39
41
|
- lib/borrower/transport.rb
|
40
42
|
- lib/borrower/version.rb
|
41
43
|
- lib/borrower.rb
|
44
|
+
- spec/borrower/manifest_spec.rb
|
45
|
+
- spec/borrower/merge_spec.rb
|
46
|
+
- spec/borrower/transport_spec.rb
|
42
47
|
- spec/borrower_spec.rb
|
43
|
-
- spec/path_spec.rb
|
44
48
|
- spec/spec_helper.rb
|
45
|
-
- spec/transport_spec.rb
|
46
49
|
homepage: http://github.com/stevenosloan/borrower
|
47
50
|
licenses: []
|
48
51
|
post_install_message:
|
@@ -69,7 +72,9 @@ specification_version: 3
|
|
69
72
|
summary: For borrowing little snippets of the web, or files, or really any snippet
|
70
73
|
of string.
|
71
74
|
test_files:
|
75
|
+
- spec/borrower/manifest_spec.rb
|
76
|
+
- spec/borrower/merge_spec.rb
|
77
|
+
- spec/borrower/transport_spec.rb
|
72
78
|
- spec/borrower_spec.rb
|
73
|
-
- spec/path_spec.rb
|
74
79
|
- spec/spec_helper.rb
|
75
|
-
|
80
|
+
has_rdoc:
|
data/spec/path_spec.rb
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Borrower::Path do
|
4
|
-
|
5
|
-
describe "#remote?" do
|
6
|
-
it "realizes a remote vs. local paths" do
|
7
|
-
Borrower::Path.remote?("http://google.com").should be_true
|
8
|
-
Borrower::Path.remote?("/path/to/file.rb").should be_false
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe "#exists?" do
|
13
|
-
|
14
|
-
it "recognizes missing files" do
|
15
|
-
Borrower::Path.exists?("this/file/is/missing.rb").should be_false
|
16
|
-
Borrower::Path.exists?("http://www.google.com/this/is/missing").should be_false
|
17
|
-
end
|
18
|
-
|
19
|
-
it "recognizes existing files" do
|
20
|
-
Borrower::Path.exists?( "http://www.google.com" ).should be_true
|
21
|
-
Borrower::Path.exists?( File.join( Dir.pwd, 'spec/fixture', "file.txt" ) ).should be_true
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "#contents" do
|
27
|
-
|
28
|
-
it "returns the content of the files" do
|
29
|
-
Borrower::Path.contents( File.join( Dir.pwd, 'spec/fixture', "file.txt" ) ).should == "Hi I'm a file"
|
30
|
-
end
|
31
|
-
|
32
|
-
it "returns the content of a remote file" do
|
33
|
-
Borrower::Path.contents( "https://gist.github.com/stevenosloan/5578606/raw/97ab1305184bdeac33472f9f1fcc1c9e278a1bb3/dummy.txt" ).should == "Hello I'm a file"
|
34
|
-
end
|
35
|
-
|
36
|
-
it "raises an error for a missing file" do
|
37
|
-
expect{ Borrower::Path.contents( "this/file/is/missing.rb" ) }.to raise_error(RuntimeError)
|
38
|
-
expect{ Borrower::Path.contents("http://www.google.com/this/is/missing") }.to raise_error(RuntimeError)
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
data/spec/transport_spec.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'borrower/transport'
|
3
|
-
|
4
|
-
describe Borrower::Transport do
|
5
|
-
include Borrower::Transport
|
6
|
-
|
7
|
-
describe "#take" do
|
8
|
-
it "returns the correct contents" do
|
9
|
-
take( File.join( Dir.pwd, 'spec/fixture', "file.txt" ) ).should == "Hi I'm a file"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
describe "#put" do
|
14
|
-
path = File.join( Dir.pwd, 'tmp', 'file.txt' )
|
15
|
-
|
16
|
-
after(:each) do
|
17
|
-
`rm -rf #{File.dirname(path)}`
|
18
|
-
end
|
19
|
-
|
20
|
-
it "puts a string to a file" do
|
21
|
-
put "hello", path
|
22
|
-
take( path ).should == "hello"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "#move" do
|
27
|
-
path = File.join( Dir.pwd, 'tmp', 'file.txt' )
|
28
|
-
|
29
|
-
after(:each) do
|
30
|
-
`rm -rf #{File.dirname(path)}`
|
31
|
-
end
|
32
|
-
|
33
|
-
it "moves the file" do
|
34
|
-
move( File.join( Dir.pwd, 'spec/fixture', 'file.txt'), path )
|
35
|
-
take( path ).should == "Hi I'm a file"
|
36
|
-
end
|
37
|
-
|
38
|
-
it "overwrites an existing file" do
|
39
|
-
put "hello", path
|
40
|
-
take( path ).should == "hello" # just to make sure it was written to begin with
|
41
|
-
move( File.join( Dir.pwd, 'spec/fixture', 'file.txt'), path )
|
42
|
-
take( path ).should == "Hi I'm a file"
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|