tesler 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -20
- data/README.md +27 -27
- data/Rakefile +53 -53
- data/VERSION +1 -1
- data/example/main.rb +46 -43
- data/example/src/subdir2/file7 +1 -1
- data/lib/tesler.rb +9 -9
- data/lib/tesler/as.rb +23 -23
- data/lib/tesler/config.rb +42 -42
- data/lib/tesler/copier.rb +81 -81
- data/lib/tesler/dsl.rb +20 -20
- data/lib/tesler/ext.rb +29 -29
- data/lib/tesler/operators/base.rb +19 -19
- data/lib/tesler/operators/logger.rb +39 -39
- data/lib/tesler/operators/run.rb +26 -26
- data/test/helper.rb +22 -22
- data/test/src/subdir2/file7 +1 -1
- data/test/test_tesler.rb +347 -325
- metadata +3 -3
data/lib/tesler/copier.rb
CHANGED
@@ -1,81 +1,81 @@
|
|
1
|
-
module Tesler
|
2
|
-
# This the class which receives the DSL commands.
|
3
|
-
class Copier
|
4
|
-
include Tesler::Operators::Base
|
5
|
-
include Tesler::Operators::Logger
|
6
|
-
include Tesler::Operators::Run
|
7
|
-
|
8
|
-
def initialize(directory_name)
|
9
|
-
@directory_name = directory_name
|
10
|
-
self.as(Tesler::Config.operator).set_directory(@directory_name)
|
11
|
-
end
|
12
|
-
|
13
|
-
# Method called by the DSL method 'copy'
|
14
|
-
def copy(file_name, options={})
|
15
|
-
filename = file_name.tesler
|
16
|
-
if not Tesler::Config.source_directory.blank?
|
17
|
-
filename = "#{Tesler::Config.source_directory}/#{filename}"
|
18
|
-
end
|
19
|
-
|
20
|
-
# if the file's name contains a star, then it is considered as a regular expression
|
21
|
-
if filename.include? "*"
|
22
|
-
regexp_copy(filename, options)
|
23
|
-
|
24
|
-
# the file's name is not a regular expression, so we copy it directly
|
25
|
-
else
|
26
|
-
direct_copy(filename, options)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
# This method create a sub-directory
|
31
|
-
def directory(directory_name, &block)
|
32
|
-
copier = Copier.new("#{@directory_name}/#{directory_name}")
|
33
|
-
copier.instance_eval(&block)
|
34
|
-
end
|
35
|
-
|
36
|
-
# Method called by the DSL method 'directory'
|
37
|
-
def self.directory(directory_name, &block)
|
38
|
-
copier = Copier.new(directory_name)
|
39
|
-
copier.instance_eval(&block)
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
# Analyses a regular expression and generate the list of file to copy
|
45
|
-
def regexp_copy(filename, options)
|
46
|
-
dirname = File.dirname(filename)
|
47
|
-
basename = File.basename(filename)
|
48
|
-
|
49
|
-
# generation of the ruby regexp corresponding to the file regexp
|
50
|
-
r = Regexp.new('^' + basename.gsub(/\./, '\.').gsub(/\*/, '(.*)') + '$')
|
51
|
-
|
52
|
-
# check if the directory exists
|
53
|
-
if not File.exists?(dirname)
|
54
|
-
Tesler::Config.output.puts "\tnot found\t#{dirname}"
|
55
|
-
return
|
56
|
-
end
|
57
|
-
|
58
|
-
Dir.entries(dirname).each do |entry|
|
59
|
-
next if %(. ..).include?(entry)
|
60
|
-
|
61
|
-
# when were processing regular expressions, the :rename option is ignored
|
62
|
-
options.delete(:rename)
|
63
|
-
direct_copy("#{dirname}/#{entry}", options) if entry =~ r
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
# This method checks if we're working whith a file or a directory and perform the corresponding copy operation
|
68
|
-
def direct_copy(filename, options)
|
69
|
-
if File.directory? filename
|
70
|
-
self.as(Tesler::Config.operator).copy_dir(filename, options)
|
71
|
-
|
72
|
-
elsif File.exists? filename
|
73
|
-
self.as(Tesler::Config.operator).copy_file(filename, options)
|
74
|
-
|
75
|
-
else
|
76
|
-
Tesler::Config.output.puts "\tnot found\t#{filename}"
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
1
|
+
module Tesler
|
2
|
+
# This the class which receives the DSL commands.
|
3
|
+
class Copier
|
4
|
+
include Tesler::Operators::Base
|
5
|
+
include Tesler::Operators::Logger
|
6
|
+
include Tesler::Operators::Run
|
7
|
+
|
8
|
+
def initialize(directory_name)
|
9
|
+
@directory_name = directory_name
|
10
|
+
self.as(Tesler::Config.operator).set_directory(@directory_name)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Method called by the DSL method 'copy'
|
14
|
+
def copy(file_name, options={})
|
15
|
+
filename = file_name.tesler
|
16
|
+
if not Tesler::Config.source_directory.blank?
|
17
|
+
filename = "#{Tesler::Config.source_directory}/#{filename}"
|
18
|
+
end
|
19
|
+
|
20
|
+
# if the file's name contains a star, then it is considered as a regular expression
|
21
|
+
if filename.include? "*"
|
22
|
+
regexp_copy(filename, options)
|
23
|
+
|
24
|
+
# the file's name is not a regular expression, so we copy it directly
|
25
|
+
else
|
26
|
+
direct_copy(filename, options)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# This method create a sub-directory
|
31
|
+
def directory(directory_name, &block)
|
32
|
+
copier = Copier.new("#{@directory_name}/#{directory_name}")
|
33
|
+
copier.instance_eval(&block)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Method called by the DSL method 'directory'
|
37
|
+
def self.directory(directory_name, &block)
|
38
|
+
copier = Copier.new(directory_name)
|
39
|
+
copier.instance_eval(&block)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# Analyses a regular expression and generate the list of file to copy
|
45
|
+
def regexp_copy(filename, options)
|
46
|
+
dirname = File.dirname(filename)
|
47
|
+
basename = File.basename(filename)
|
48
|
+
|
49
|
+
# generation of the ruby regexp corresponding to the file regexp
|
50
|
+
r = Regexp.new('^' + basename.gsub(/\./, '\.').gsub(/\*/, '(.*)') + '$')
|
51
|
+
|
52
|
+
# check if the directory exists
|
53
|
+
if not File.exists?(dirname)
|
54
|
+
Tesler::Config.output.puts "\tnot found\t#{dirname}"
|
55
|
+
return
|
56
|
+
end
|
57
|
+
|
58
|
+
Dir.entries(dirname).each do |entry|
|
59
|
+
next if %(. ..).include?(entry)
|
60
|
+
|
61
|
+
# when were processing regular expressions, the :rename option is ignored
|
62
|
+
options.delete(:rename)
|
63
|
+
direct_copy("#{dirname}/#{entry}", options) if entry =~ r
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# This method checks if we're working whith a file or a directory and perform the corresponding copy operation
|
68
|
+
def direct_copy(filename, options)
|
69
|
+
if File.directory? filename
|
70
|
+
self.as(Tesler::Config.operator).copy_dir(filename, options)
|
71
|
+
|
72
|
+
elsif File.exists? filename
|
73
|
+
self.as(Tesler::Config.operator).copy_file(filename, options)
|
74
|
+
|
75
|
+
else
|
76
|
+
Tesler::Config.output.puts "\tnot found\t#{filename}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
data/lib/tesler/dsl.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
# sets a directory where to copy files. If this directory doesn't exist, it is created
|
2
|
-
def directory(directory_name, &block)
|
3
|
-
Tesler::Copier.directory(directory_name.tesler, &block)
|
4
|
-
end
|
5
|
-
|
6
|
-
# sets the directory where the file that we are copying, are taken from
|
7
|
-
def source_directory(directory_name)
|
8
|
-
Tesler::Config.source_directory = directory_name
|
9
|
-
end
|
10
|
-
|
11
|
-
# reinitializes the configuration
|
12
|
-
def reset_config
|
13
|
-
Tesler::Config.reset
|
14
|
-
end
|
15
|
-
|
16
|
-
# in test mode, tesler only logs its actions without effectively copying the files
|
17
|
-
def run_in_test_mode
|
18
|
-
Tesler::Config.operator = Tesler::Operators::Logger
|
19
|
-
end
|
20
|
-
|
1
|
+
# sets a directory where to copy files. If this directory doesn't exist, it is created
|
2
|
+
def directory(directory_name, &block)
|
3
|
+
Tesler::Copier.directory(directory_name.tesler, &block)
|
4
|
+
end
|
5
|
+
|
6
|
+
# sets the directory where the file that we are copying, are taken from
|
7
|
+
def source_directory(directory_name)
|
8
|
+
Tesler::Config.source_directory = directory_name
|
9
|
+
end
|
10
|
+
|
11
|
+
# reinitializes the configuration
|
12
|
+
def reset_config
|
13
|
+
Tesler::Config.reset
|
14
|
+
end
|
15
|
+
|
16
|
+
# in test mode, tesler only logs its actions without effectively copying the files
|
17
|
+
def run_in_test_mode
|
18
|
+
Tesler::Config.operator = Tesler::Operators::Logger
|
19
|
+
end
|
20
|
+
|
data/lib/tesler/ext.rb
CHANGED
@@ -1,29 +1,29 @@
|
|
1
|
-
# define an extension for the Object class
|
2
|
-
class Object
|
3
|
-
# stolen from activesupport.
|
4
|
-
def blank?
|
5
|
-
respond_to?(:empty?) ? empty? : !self
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
# define an extension for the String class
|
10
|
-
class String
|
11
|
-
# actions :
|
12
|
-
# * replace the Windows directory separtor (\) by the unix separator (/)
|
13
|
-
# * replace tokens matching the format %TOKEN% by an environment variable
|
14
|
-
def tesler
|
15
|
-
# replace separators
|
16
|
-
s = File.join self.split("\\")
|
17
|
-
|
18
|
-
# environment variables
|
19
|
-
env_vars = self.scan(/%([a-zA-Z_]+)%/).map { |array| array.first }
|
20
|
-
env_vars.each do |var|
|
21
|
-
s.gsub!(Regexp.new("%#{var}%"), ENV[var])
|
22
|
-
end
|
23
|
-
|
24
|
-
File.join s.split("\\")
|
25
|
-
end
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
|
1
|
+
# define an extension for the Object class
|
2
|
+
class Object
|
3
|
+
# stolen from activesupport.
|
4
|
+
def blank?
|
5
|
+
respond_to?(:empty?) ? empty? : !self
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
# define an extension for the String class
|
10
|
+
class String
|
11
|
+
# actions :
|
12
|
+
# * replace the Windows directory separtor (\) by the unix separator (/)
|
13
|
+
# * replace tokens matching the format %TOKEN% by an environment variable
|
14
|
+
def tesler
|
15
|
+
# replace separators
|
16
|
+
s = File.join self.split("\\")
|
17
|
+
|
18
|
+
# environment variables
|
19
|
+
env_vars = self.scan(/%([a-zA-Z_]+)%/).map { |array| array.first }
|
20
|
+
env_vars.each do |var|
|
21
|
+
s.gsub!(Regexp.new("%#{var}%"), ENV[var])
|
22
|
+
end
|
23
|
+
|
24
|
+
File.join s.split("\\")
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
end
|
29
|
+
|
@@ -1,19 +1,19 @@
|
|
1
|
-
module Tesler
|
2
|
-
module Operators
|
3
|
-
module Base
|
4
|
-
def destination_name(file_name, options)
|
5
|
-
base_name = options[:rename]
|
6
|
-
base_name = File.basename(file_name) if base_name.nil?
|
7
|
-
destination = "#{@directory_name}/#{base_name}"
|
8
|
-
|
9
|
-
# if the directory option is set, create the sub-directory if necessary
|
10
|
-
if options[:directory]
|
11
|
-
FileUtils.mkdir_p "#{@directory_name}/#{options[:directory]}"
|
12
|
-
destination = "#{@directory_name}/#{options[:directory]}/#{base_name}"
|
13
|
-
end
|
14
|
-
|
15
|
-
destination
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
1
|
+
module Tesler
|
2
|
+
module Operators
|
3
|
+
module Base
|
4
|
+
def destination_name(file_name, options)
|
5
|
+
base_name = options[:rename]
|
6
|
+
base_name = File.basename(file_name) if base_name.nil?
|
7
|
+
destination = "#{@directory_name}/#{base_name}"
|
8
|
+
|
9
|
+
# if the directory option is set, create the sub-directory if necessary
|
10
|
+
if options[:directory]
|
11
|
+
FileUtils.mkdir_p "#{@directory_name}/#{options[:directory]}"
|
12
|
+
destination = "#{@directory_name}/#{options[:directory]}/#{base_name}"
|
13
|
+
end
|
14
|
+
|
15
|
+
destination
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,39 +1,39 @@
|
|
1
|
-
module Tesler
|
2
|
-
module Operators
|
3
|
-
# This module only logs the actions
|
4
|
-
module Logger
|
5
|
-
extend self
|
6
|
-
|
7
|
-
def set_directory(directory_name)
|
8
|
-
if File.exists? directory_name
|
9
|
-
Tesler::Config.output.puts "\texists\t#{directory_name}"
|
10
|
-
else
|
11
|
-
Tesler::Config.output.puts "\tcreate\t#{directory_name}"
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def copy_file(file_name, options)
|
16
|
-
self.as(Tesler::Config.operator).copy_dir(file_name, options)
|
17
|
-
end
|
18
|
-
|
19
|
-
def copy_dir(file_name, options)
|
20
|
-
Tesler::Config.output.puts "\tcopy\t#{file_name}\t#{destination_name(file_name, options)}"
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.destination_name(file_name, options)
|
24
|
-
base_name = options[:rename]
|
25
|
-
base_name = File.basename(file_name) if base_name.nil?
|
26
|
-
destination = "#{options[:destination]}/#{base_name}"
|
27
|
-
|
28
|
-
# if the directory option is set, create the sub-directory if necessary
|
29
|
-
if options[:directory]
|
30
|
-
FileUtils.mkdir_p "#{options[:destination]}/#{options[:directory]}"
|
31
|
-
destination = "#{options[:destination]}/#{options[:directory]}/#{base_name}"
|
32
|
-
end
|
33
|
-
|
34
|
-
destination
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
1
|
+
module Tesler
|
2
|
+
module Operators
|
3
|
+
# This module only logs the actions
|
4
|
+
module Logger
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def set_directory(directory_name)
|
8
|
+
if File.exists? directory_name
|
9
|
+
Tesler::Config.output.puts "\texists\t#{directory_name}"
|
10
|
+
else
|
11
|
+
Tesler::Config.output.puts "\tcreate\t#{directory_name}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def copy_file(file_name, options)
|
16
|
+
self.as(Tesler::Config.operator).copy_dir(file_name, options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def copy_dir(file_name, options)
|
20
|
+
Tesler::Config.output.puts "\tcopy\t#{file_name}\t#{destination_name(file_name, options)}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.destination_name(file_name, options)
|
24
|
+
base_name = options[:rename]
|
25
|
+
base_name = File.basename(file_name) if base_name.nil?
|
26
|
+
destination = "#{options[:destination]}/#{base_name}"
|
27
|
+
|
28
|
+
# if the directory option is set, create the sub-directory if necessary
|
29
|
+
if options[:directory]
|
30
|
+
FileUtils.mkdir_p "#{options[:destination]}/#{options[:directory]}"
|
31
|
+
destination = "#{options[:destination]}/#{options[:directory]}/#{base_name}"
|
32
|
+
end
|
33
|
+
|
34
|
+
destination
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
data/lib/tesler/operators/run.rb
CHANGED
@@ -1,26 +1,26 @@
|
|
1
|
-
require 'fileutils'
|
2
|
-
|
3
|
-
module Tesler
|
4
|
-
module Operators
|
5
|
-
# This class defines the effective copy operations
|
6
|
-
module Run
|
7
|
-
def set_directory(directory_name)
|
8
|
-
Tesler::Operators::Logger.set_directory(directory_name)
|
9
|
-
FileUtils.mkdir_p @directory_name
|
10
|
-
end
|
11
|
-
|
12
|
-
def copy_file(file_name, options)
|
13
|
-
self.as(Tesler::Config.operator).copy_dir(file_name, options)
|
14
|
-
end
|
15
|
-
|
16
|
-
def copy_dir(file_name, options)
|
17
|
-
# we add the destination in the options for the logger because it can't find it itself
|
18
|
-
Tesler::Operators::Logger.copy_dir(file_name, options.merge({:destination => @directory_name}))
|
19
|
-
|
20
|
-
# recursive copy
|
21
|
-
FileUtils.cp_r file_name, "#{destination_name(file_name, options)}"
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module Tesler
|
4
|
+
module Operators
|
5
|
+
# This class defines the effective copy operations
|
6
|
+
module Run
|
7
|
+
def set_directory(directory_name)
|
8
|
+
Tesler::Operators::Logger.set_directory(directory_name)
|
9
|
+
FileUtils.mkdir_p @directory_name
|
10
|
+
end
|
11
|
+
|
12
|
+
def copy_file(file_name, options)
|
13
|
+
self.as(Tesler::Config.operator).copy_dir(file_name, options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def copy_dir(file_name, options)
|
17
|
+
# we add the destination in the options for the logger because it can't find it itself
|
18
|
+
Tesler::Operators::Logger.copy_dir(file_name, options.merge({:destination => @directory_name}))
|
19
|
+
|
20
|
+
# recursive copy
|
21
|
+
FileUtils.cp_r file_name, "#{destination_name(file_name, options)}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
data/test/helper.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'test/unit'
|
3
|
-
require 'shoulda'
|
4
|
-
|
5
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
-
require 'tesler'
|
8
|
-
|
9
|
-
class Test::Unit::TestCase
|
10
|
-
end
|
11
|
-
|
12
|
-
# This class simulate the standard output
|
13
|
-
class Output
|
14
|
-
def messages
|
15
|
-
@messages ||= []
|
16
|
-
end
|
17
|
-
|
18
|
-
def puts(message)
|
19
|
-
messages << message
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
require 'tesler'
|
8
|
+
|
9
|
+
class Test::Unit::TestCase
|
10
|
+
end
|
11
|
+
|
12
|
+
# This class simulate the standard output
|
13
|
+
class Output
|
14
|
+
def messages
|
15
|
+
@messages ||= []
|
16
|
+
end
|
17
|
+
|
18
|
+
def puts(message)
|
19
|
+
messages << message
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|