filander 0.2.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/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/lib/filander.rb +77 -0
- data/lib/filander/actions/base.rb +117 -0
- data/lib/filander/actions/cmd.rb +12 -0
- data/lib/filander/actions/copy_directory.rb +17 -0
- data/lib/filander/actions/copy_file.rb +16 -0
- data/lib/filander/actions/create_file.rb +13 -0
- data/lib/filander/actions/empty_directory.rb +16 -0
- data/lib/filander/actions/inject_into_file.rb +39 -0
- data/lib/filander/actions/inside.rb +20 -0
- data/lib/filander/actions/template.rb +17 -0
- data/spec/filander/actions/base_spec.rb +60 -0
- data/spec/filander/actions/copy_file_spec.rb +30 -0
- data/spec/filander/actions/create_file_spec.rb +31 -0
- data/spec/filander/actions/empty_directory_spec.rb +25 -0
- data/spec/filander/actions/inject_into_file_spec.rb +47 -0
- data/spec/filander/actions/inside_spec.rb +52 -0
- data/spec/filander/actions/template_spec.rb +40 -0
- data/spec/filander_spec.rb +45 -0
- data/spec/source/mydir/myfile +1 -0
- data/spec/source/mydir/mytemplate +1 -0
- data/spec/source/myfile +1 -0
- data/spec/source/mytemplate +1 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +26 -0
- metadata +100 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Johannes J. Schmidt
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= filander
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Johannes J. Schmidt. See LICENSE for details.
|
data/lib/filander.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'filander/actions/base'
|
3
|
+
require 'filander/actions/cmd'
|
4
|
+
require 'filander/actions/copy_directory'
|
5
|
+
require 'filander/actions/copy_file'
|
6
|
+
require 'filander/actions/create_file'
|
7
|
+
require 'filander/actions/empty_directory'
|
8
|
+
require 'filander/actions/inject_into_file'
|
9
|
+
require 'filander/actions/inside'
|
10
|
+
require 'filander/actions/template'
|
11
|
+
|
12
|
+
module Filander
|
13
|
+
def self.included(base)
|
14
|
+
base.class_eval do
|
15
|
+
def self.source_root(dirname)
|
16
|
+
Filander.source_root = dirname
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.destination_root(dirname)
|
20
|
+
Filander.destination_root = dirname
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.quiet(value)
|
24
|
+
Filander.quiet = value
|
25
|
+
end
|
26
|
+
|
27
|
+
# can be :pretend, :skip, :force
|
28
|
+
def self.behavior(value)
|
29
|
+
Filander.behavior = value
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class << self
|
35
|
+
attr_reader :source_root_stack, :destination_root_stack
|
36
|
+
attr_accessor :quiet, :behavior
|
37
|
+
|
38
|
+
def source_root=(dirname)
|
39
|
+
add_source_root dirname
|
40
|
+
end
|
41
|
+
|
42
|
+
def source_root
|
43
|
+
@source_root_stack ||= []
|
44
|
+
@source_root_stack.last
|
45
|
+
end
|
46
|
+
|
47
|
+
def destination_root=(dirname)
|
48
|
+
add_destination_root dirname
|
49
|
+
end
|
50
|
+
|
51
|
+
def destination_root
|
52
|
+
@destination_root_stack ||= []
|
53
|
+
@destination_root_stack.last
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def add_source_root(dirname)
|
59
|
+
@source_root_stack ||= []
|
60
|
+
@source_root_stack.push dirname
|
61
|
+
end
|
62
|
+
|
63
|
+
def add_destination_root(dirname)
|
64
|
+
@destination_root_stack ||= []
|
65
|
+
@destination_root_stack.push dirname
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
include Cmd
|
70
|
+
include CopyDirectory
|
71
|
+
include CopyFile
|
72
|
+
include CreateFile
|
73
|
+
include EmptyDirectory
|
74
|
+
include InjectIntoFile
|
75
|
+
include Inside
|
76
|
+
include Template
|
77
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module Filander
|
2
|
+
module Base
|
3
|
+
def join_source(filename = nil)
|
4
|
+
join Filander.source_root, filename
|
5
|
+
rescue
|
6
|
+
raise 'Please specify source_root'
|
7
|
+
end
|
8
|
+
|
9
|
+
def join_destination(filename = nil)
|
10
|
+
join Filander.destination_root, filename
|
11
|
+
rescue
|
12
|
+
raise 'Please specify destination_root'
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_directory_for(destination = nil)
|
16
|
+
dest_dir = File.dirname(join_destination(destination))
|
17
|
+
|
18
|
+
FileUtils.mkdir_p dest_dir unless File.exists?(dest_dir)
|
19
|
+
end
|
20
|
+
|
21
|
+
def with_report(destination, content = nil)
|
22
|
+
filename = join_destination(destination)
|
23
|
+
|
24
|
+
skip = Filander.behavior == :skip
|
25
|
+
if File.exists?(filename)
|
26
|
+
if File.file?(filename) && content == File.read(filename)
|
27
|
+
report :identical, destination
|
28
|
+
skip = true
|
29
|
+
elsif File.directory?(filename) && content == entries(filename)
|
30
|
+
report :identical, destination
|
31
|
+
skip = true
|
32
|
+
else
|
33
|
+
case Filander.behavior
|
34
|
+
when :force
|
35
|
+
report :force, destination
|
36
|
+
when :skip
|
37
|
+
report :skip, destination
|
38
|
+
else
|
39
|
+
file = $terminal.color(filename, :bold) if defined? $terminal
|
40
|
+
skip = nil
|
41
|
+
puts 'Overwrite %s [Nyc]?' % file
|
42
|
+
until !skip.nil?
|
43
|
+
answer = STDIN.gets.chomp.downcase
|
44
|
+
case answer
|
45
|
+
when '', 'n', 'no'
|
46
|
+
skip = true
|
47
|
+
when 'y', 'yes'
|
48
|
+
skip = false
|
49
|
+
when 'c', 'cancel'
|
50
|
+
exit
|
51
|
+
else
|
52
|
+
puts 'Please anwer either [Y]es, [N]o or [C]ancel'
|
53
|
+
skip = nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
report skip ? :skip : :force, destination
|
57
|
+
end
|
58
|
+
end
|
59
|
+
else
|
60
|
+
report :create, destination
|
61
|
+
end
|
62
|
+
|
63
|
+
yield unless skip || Filander.behavior == :pretend
|
64
|
+
end
|
65
|
+
|
66
|
+
def report(verb, filename)
|
67
|
+
return if Filander.quiet
|
68
|
+
|
69
|
+
# TODO: make this nice
|
70
|
+
if Filander.destination_root
|
71
|
+
begin
|
72
|
+
name = Pathname.new(File.expand_path(join_destination(filename))).relative_path_from(Pathname.new(File.expand_path(Filander.destination_root_stack.first)))
|
73
|
+
rescue
|
74
|
+
name = filename
|
75
|
+
end
|
76
|
+
else
|
77
|
+
name = filename
|
78
|
+
end
|
79
|
+
|
80
|
+
# left: cyan
|
81
|
+
color = case verb
|
82
|
+
when :create
|
83
|
+
:green
|
84
|
+
when :force, :skip
|
85
|
+
:yellow
|
86
|
+
when :conflict
|
87
|
+
:red
|
88
|
+
when :execute
|
89
|
+
:magenta
|
90
|
+
else
|
91
|
+
:blue
|
92
|
+
end
|
93
|
+
|
94
|
+
verb = verb.to_s.rjust(12)
|
95
|
+
verb = $terminal.color(verb, :bold, color) if defined? $terminal
|
96
|
+
|
97
|
+
puts '%s %s' % [verb, name]
|
98
|
+
end
|
99
|
+
|
100
|
+
def entries(dirname)
|
101
|
+
basename = Pathname.new(dirname)
|
102
|
+
array = Dir[File.join(dirname, '**')]
|
103
|
+
array.map! { |f| Pathname.new f }
|
104
|
+
array.map! { |p| p.relative_path_from basename }
|
105
|
+
array
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
def join(root, filename)
|
111
|
+
return Filander.source_root if filename.nil?
|
112
|
+
return filename if filename =~ /^\//
|
113
|
+
return File.expand_path(filename) if filename =~ /^~/
|
114
|
+
File.join root, filename
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Filander
|
2
|
+
module CopyDirectory
|
3
|
+
include Base
|
4
|
+
|
5
|
+
def copy_directory(source, destination = source)
|
6
|
+
create_directory_for destination
|
7
|
+
filename = join_source(source)
|
8
|
+
raise "Source file `#{filename}' does not exist" unless File.exists?(filename)
|
9
|
+
|
10
|
+
dest = join_destination(destination)
|
11
|
+
with_report destination, entries(filename) do
|
12
|
+
FileUtils.rm_rf dest
|
13
|
+
FileUtils.cp_r filename, dest
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Filander
|
2
|
+
module CopyFile
|
3
|
+
include Base
|
4
|
+
|
5
|
+
def copy_file(source, destination = source)
|
6
|
+
create_directory_for destination
|
7
|
+
filename = join_source(source)
|
8
|
+
raise "Source file `#{filename}' does not exist" unless File.exists?(filename)
|
9
|
+
|
10
|
+
content = File.read(filename) rescue nil
|
11
|
+
with_report destination, content do
|
12
|
+
FileUtils.cp filename, join_destination(destination)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Filander
|
2
|
+
module CreateFile
|
3
|
+
include Base
|
4
|
+
|
5
|
+
def create_file(destination, content)
|
6
|
+
create_directory_for destination
|
7
|
+
|
8
|
+
with_report destination, content do
|
9
|
+
File.open(join_destination(destination), "w") { |file| file << content }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Filander
|
2
|
+
module EmptyDirectory
|
3
|
+
include Base
|
4
|
+
|
5
|
+
def empty_directory(destination)
|
6
|
+
dest_dir = join_destination(destination)
|
7
|
+
|
8
|
+
if File.exists?(dest_dir)
|
9
|
+
report :exist, destination
|
10
|
+
else
|
11
|
+
report :create, destination
|
12
|
+
FileUtils.mkdir_p dest_dir
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Filander
|
2
|
+
module InjectIntoFile
|
3
|
+
include Base
|
4
|
+
|
5
|
+
def inject_into_file(destination, *args, &block)
|
6
|
+
filename = join_destination(destination)
|
7
|
+
|
8
|
+
if block_given?
|
9
|
+
data, config = block.call, args.shift
|
10
|
+
else
|
11
|
+
data, config = args.shift, args.shift
|
12
|
+
end
|
13
|
+
|
14
|
+
position = config[:after] ? :after : :before
|
15
|
+
flag = config[position]
|
16
|
+
flag = Regexp.escape(flag) unless flag.is_a?(Regexp)
|
17
|
+
|
18
|
+
replacement = if position == :after
|
19
|
+
'\0' + data
|
20
|
+
else
|
21
|
+
data + '\0'
|
22
|
+
end
|
23
|
+
|
24
|
+
present = if position == :after
|
25
|
+
/#{flag}#{Regexp.escape data}/
|
26
|
+
else
|
27
|
+
/#{Regexp.escape data}#{flag}/
|
28
|
+
end
|
29
|
+
|
30
|
+
content = File.read(filename) if File.file?(filename)
|
31
|
+
content ||= ''
|
32
|
+
content.gsub!(/#{flag}/, replacement) unless content =~ present
|
33
|
+
|
34
|
+
with_report destination, content do
|
35
|
+
File.open(filename, "w") { |file| file << content }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Filander
|
2
|
+
module Inside
|
3
|
+
include Base
|
4
|
+
|
5
|
+
def inside(source, destination = source, &block)
|
6
|
+
dest_dir = join_destination(destination)
|
7
|
+
dirname = join_source(source)
|
8
|
+
|
9
|
+
FileUtils.mkdir_p dest_dir unless File.exists?(dest_dir)
|
10
|
+
|
11
|
+
FileUtils.cd(dirname) do
|
12
|
+
Filander.destination_root_stack.push(dest_dir)
|
13
|
+
Filander.source_root_stack.push(dirname)
|
14
|
+
yield
|
15
|
+
Filander.source_root_stack.pop
|
16
|
+
Filander.destination_root_stack.pop
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
module Filander
|
4
|
+
module Template
|
5
|
+
include Base
|
6
|
+
|
7
|
+
def template(source, destination = source)
|
8
|
+
create_directory_for destination
|
9
|
+
context = instance_eval('binding')
|
10
|
+
content = ERB.new(File.read(join_source(source)), nil, '-').result(context)
|
11
|
+
|
12
|
+
with_report destination, content do
|
13
|
+
File.open(join_destination(destination), "w") { |file| file << content }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe "Filander" do
|
4
|
+
describe "Base" do
|
5
|
+
include Filander::Base
|
6
|
+
|
7
|
+
describe "join_source" do
|
8
|
+
it "should return source_root" do
|
9
|
+
join_source.should == Filander.source_root
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should raise if source_root is nil" do
|
13
|
+
lambda { join_source 'abc' }.should raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should join source_root and filename" do
|
17
|
+
Filander.source_root = 'abc'
|
18
|
+
join_source('def').should == 'abc/def'
|
19
|
+
Filander.source_root = nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "join_destination" do
|
24
|
+
it "should return destination_root" do
|
25
|
+
join_destination.should == Filander.destination_root
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should raise if destination_root is nil" do
|
29
|
+
lambda { join_destination 'abc' }.should raise_error
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should join destination_root and filename" do
|
33
|
+
Filander.destination_root = 'abc'
|
34
|
+
join_destination('def').should == 'abc/def'
|
35
|
+
Filander.destination_root = nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "create_directory_for" do
|
40
|
+
it "should raise if destination_root is nil" do
|
41
|
+
lambda { create_directory_for 'abc' }.should raise_error
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "with source and destination" do
|
45
|
+
before do
|
46
|
+
setup_roots
|
47
|
+
end
|
48
|
+
|
49
|
+
after do
|
50
|
+
teardown_roots
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should create destination directory" do
|
54
|
+
create_directory_for('mydir/myfile')
|
55
|
+
File.directory?(join_destination('mydir')).should be_true
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe "Filander" do
|
4
|
+
describe "CopyFile" do
|
5
|
+
include Filander::CopyFile
|
6
|
+
|
7
|
+
before do
|
8
|
+
setup_roots
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
teardown_roots
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should copy myfile to otherfile" do
|
16
|
+
copy_file 'myfile', 'otherfile'
|
17
|
+
File.file?(join_destination('otherfile')).should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should copy file" do
|
21
|
+
copy_file 'myfile'
|
22
|
+
File.file?(join_destination('myfile')).should be_true
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should copy file and create directory" do
|
26
|
+
copy_file 'mydir/myfile'
|
27
|
+
File.file?(join_destination('mydir/myfile')).should be_true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe "Filander" do
|
4
|
+
describe "CreateFile" do
|
5
|
+
include Filander::CreateFile
|
6
|
+
|
7
|
+
before do
|
8
|
+
setup_roots
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
teardown_roots
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should create file" do
|
16
|
+
create_file 'myfile', 'data'
|
17
|
+
File.file?(join_destination('myfile')).should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should create file and subdir" do
|
21
|
+
create_file 'mydir/myfile', 'data'
|
22
|
+
File.directory?(join_destination('mydir')).should be_true
|
23
|
+
File.file?(join_destination('mydir/myfile')).should be_true
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should write content" do
|
27
|
+
create_file 'myfile', 'data'
|
28
|
+
File.read(join_destination('myfile')).should == 'data'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe "Filander" do
|
4
|
+
describe "EmptyDirectory" do
|
5
|
+
include Filander::EmptyDirectory
|
6
|
+
|
7
|
+
before do
|
8
|
+
setup_roots
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
teardown_roots
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should create empty directory" do
|
16
|
+
empty_directory 'mydir'
|
17
|
+
File.directory?(join_destination('mydir')).should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should create empty pending directories" do
|
21
|
+
empty_directory 'mydir/mydir'
|
22
|
+
File.directory?(join_destination('mydir/mydir')).should be_true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe "Filander" do
|
4
|
+
describe "InjectIntoFile" do
|
5
|
+
include Filander::InjectIntoFile
|
6
|
+
|
7
|
+
before do
|
8
|
+
setup_roots
|
9
|
+
FileUtils.mkdir_p Filander.destination_root
|
10
|
+
@filename = join_destination('myfile')
|
11
|
+
File.open(@filename, 'w') { |file| file << 'data' }
|
12
|
+
end
|
13
|
+
|
14
|
+
after do
|
15
|
+
teardown_roots
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should inject data before specified content into file" do
|
19
|
+
inject_into_file 'myfile', 'mydata', :before => 'data'
|
20
|
+
File.read(@filename).should == "mydatadata"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should inject data after specified content into file" do
|
24
|
+
inject_into_file 'myfile', 'mydata', :after => 'data'
|
25
|
+
File.read(@filename).should == "datamydata"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should inject data from block before specified content into file" do
|
29
|
+
inject_into_file 'myfile', :before => 'data' do
|
30
|
+
'mydata'
|
31
|
+
end
|
32
|
+
File.read(@filename).should == "mydatadata"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should inject data from block after specified content into file" do
|
36
|
+
inject_into_file 'myfile', :after => 'data' do
|
37
|
+
'mydata'
|
38
|
+
end
|
39
|
+
File.read(@filename).should == "datamydata"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should inject data before specified content via regexp into file" do
|
43
|
+
inject_into_file 'myfile', 'mydata', :before => /ta/
|
44
|
+
File.read(@filename).should == "damydatata"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe "Filander" do
|
4
|
+
describe "Inside" do
|
5
|
+
include Filander::Inside
|
6
|
+
|
7
|
+
before do
|
8
|
+
setup_roots
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
teardown_roots
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should change to source directory" do
|
16
|
+
inside 'mydir' do
|
17
|
+
Dir.pwd.should == File.join(SOURCE_ROOT, 'mydir')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should change to source directory otherdir" do
|
22
|
+
inside 'mydir', 'otherdir' do
|
23
|
+
Dir.pwd.should == File.join(SOURCE_ROOT, 'mydir')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should set destination directory" do
|
28
|
+
inside 'mydir' do
|
29
|
+
Filander.destination_root == File.join(DESTINATION_ROOT, 'mydir')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should set destination directory to otherdir" do
|
34
|
+
inside 'mydir', 'otherdir' do
|
35
|
+
Filander.destination_root == File.join(DESTINATION_ROOT, 'otherdir')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should reset pwd" do
|
40
|
+
pwd = FileUtils.pwd
|
41
|
+
inside 'mydir' do
|
42
|
+
end
|
43
|
+
FileUtils.pwd.should == pwd
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should create destination directory" do
|
47
|
+
inside 'mydir' do
|
48
|
+
end
|
49
|
+
File.directory?(join_destination('mydir')).should be_true
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe "Filander" do
|
4
|
+
describe "Template" do
|
5
|
+
include Filander::Template
|
6
|
+
|
7
|
+
def data
|
8
|
+
"mydata"
|
9
|
+
end
|
10
|
+
|
11
|
+
before do
|
12
|
+
setup_roots
|
13
|
+
end
|
14
|
+
|
15
|
+
after do
|
16
|
+
teardown_roots
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should create myfile" do
|
20
|
+
template 'mytemplate', 'myfile'
|
21
|
+
File.file?(join_destination('myfile')).should be_true
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should create file" do
|
25
|
+
template 'mytemplate'
|
26
|
+
File.file?(join_destination('mytemplate')).should be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should create file and subdir" do
|
30
|
+
template 'mydir/mytemplate'
|
31
|
+
File.directory?(join_destination('mydir')).should be_true
|
32
|
+
File.file?(join_destination('mydir/mytemplate')).should be_true
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should write erb evaluated content" do
|
36
|
+
template 'mytemplate'
|
37
|
+
File.read(join_destination('mytemplate')).should == "mydata\n"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Filander" do
|
4
|
+
describe "source_root" do
|
5
|
+
it "should be accessible" do
|
6
|
+
Filander.source_root = 'abc'
|
7
|
+
Filander.source_root.should == 'abc'
|
8
|
+
Filander.source_root = nil
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "destination_root" do
|
13
|
+
it "should be accessible" do
|
14
|
+
Filander.destination_root = 'abc'
|
15
|
+
Filander.destination_root.should == 'abc'
|
16
|
+
Filander.destination_root = nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "actions" do
|
21
|
+
it "should include CopyFile" do
|
22
|
+
Filander.included_modules.should include(Filander::CopyFile)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should include CreateFile" do
|
26
|
+
Filander.included_modules.should include(Filander::CreateFile)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should include EmptyDirectory" do
|
30
|
+
Filander.included_modules.should include(Filander::EmptyDirectory)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should include InjectIntoFile" do
|
34
|
+
Filander.included_modules.should include(Filander::InjectIntoFile)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should include Inside" do
|
38
|
+
Filander.included_modules.should include(Filander::Inside)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should include Template" do
|
42
|
+
Filander.included_modules.should include(Filander::Template)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
data
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= data %>
|
data/spec/source/myfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
data
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= data %>
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'filander'
|
4
|
+
require 'spec'
|
5
|
+
require 'spec/autorun'
|
6
|
+
|
7
|
+
Spec::Runner.configure do |config|
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
SOURCE_ROOT = File.expand_path(File.dirname(__FILE__) + '/source')
|
12
|
+
DESTINATION_ROOT = File.expand_path(File.dirname(__FILE__) + '/destination')
|
13
|
+
|
14
|
+
Filander.quiet = true
|
15
|
+
Filander.behavior = :force
|
16
|
+
|
17
|
+
def setup_roots
|
18
|
+
Filander.source_root = SOURCE_ROOT
|
19
|
+
Filander.destination_root = DESTINATION_ROOT
|
20
|
+
end
|
21
|
+
|
22
|
+
def teardown_roots
|
23
|
+
FileUtils.rm_rf(Filander.destination_root)
|
24
|
+
Filander.source_root = nil
|
25
|
+
Filander.destination_root = nil
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filander
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Johannes J. Schmidt
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-08 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Helps dealing with files and directories
|
35
|
+
email: schmidt@netzmerk.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- lib/filander.rb
|
45
|
+
- lib/filander/actions/base.rb
|
46
|
+
- lib/filander/actions/cmd.rb
|
47
|
+
- lib/filander/actions/copy_directory.rb
|
48
|
+
- lib/filander/actions/copy_file.rb
|
49
|
+
- lib/filander/actions/create_file.rb
|
50
|
+
- lib/filander/actions/empty_directory.rb
|
51
|
+
- lib/filander/actions/inject_into_file.rb
|
52
|
+
- lib/filander/actions/inside.rb
|
53
|
+
- lib/filander/actions/template.rb
|
54
|
+
- LICENSE
|
55
|
+
- README.rdoc
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/jo/filander
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --charset=UTF-8
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.3.6
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: File manipulation tools
|
86
|
+
test_files:
|
87
|
+
- spec/filander_spec.rb
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
- spec/filander/actions/inside_spec.rb
|
90
|
+
- spec/filander/actions/copy_file_spec.rb
|
91
|
+
- spec/filander/actions/inject_into_file_spec.rb
|
92
|
+
- spec/filander/actions/template_spec.rb
|
93
|
+
- spec/filander/actions/empty_directory_spec.rb
|
94
|
+
- spec/filander/actions/create_file_spec.rb
|
95
|
+
- spec/filander/actions/base_spec.rb
|
96
|
+
- spec/source/myfile
|
97
|
+
- spec/source/mydir/myfile
|
98
|
+
- spec/source/mydir/mytemplate
|
99
|
+
- spec/source/mytemplate
|
100
|
+
- spec/spec.opts
|