Class: DirDSL

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/dir_dsl/dir_dsl.rb,
lib/dir_dsl/version.rb

Constant Summary collapse

VERSION =
"1.0.7"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_root, to_root) ⇒ DirDSL

Returns a new instance of DirDSL.



9
10
11
12
13
# File 'lib/dir_dsl/dir_dsl.rb', line 9

def initialize from_root, to_root
  @from_root = File.expand_path(from_root)
  @to_root = File.expand_path(to_root)
  @global_ignores = []
end

Instance Attribute Details

#from_rootObject (readonly)

Returns the value of attribute from_root.



7
8
9
# File 'lib/dir_dsl/dir_dsl.rb', line 7

def from_root
  @from_root
end

#to_rootObject (readonly)

Returns the value of attribute to_root.



7
8
9
# File 'lib/dir_dsl/dir_dsl.rb', line 7

def to_root
  @to_root
end

Instance Method Details

#build(&block) ⇒ Object



15
16
17
# File 'lib/dir_dsl/dir_dsl.rb', line 15

def build(&block)
  MetaMethods::DslBuilder.instance.evaluate_dsl(self, nil, block)
end

#content(params) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dir_dsl/dir_dsl.rb', line 54

def content params
  source = params[:source]

  stream = source.kind_of?(String) ? StringIO.new(source) : source
  content = stream.read

  to_dir = to_dir(params[:to_dir], params[:name])

  create_directory to_dir unless File.exist? to_dir

  write_content_to_file content, "#{to_dir}/#{File.basename(params[:name])}"
end

#directory(params) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dir_dsl/dir_dsl.rb', line 67

def directory params
  if params[:from_dir]
    if params[:to_dir] == "." || params[:to_dir].nil?
      to_dir = params[:from_dir]
    else
      to_dir = params[:to_dir]
    end

    filter = params[:filter].nil? ? "**/*" : params[:filter]
    excludes = parse_excludes(params[:excludes])

    copy_files_with_excludes "#{from_root}/#{params[:from_dir]}", "#{to_root}/#{to_dir}", filter, excludes
  else
    to_dir = "#{to_root}/#{params[:to_dir]}"

    create_directory to_dir unless File.exist? to_dir
  end
end

#entries_sizeObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/dir_dsl/dir_dsl.rb', line 27

def entries_size
  list = Dir.glob("#{to_root}/**/*")

  cnt = 0
  list.each do |name|
    cnt += 1 if File.file?(name)
  end

  cnt
end

#entry_exist?(entry_name) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/dir_dsl/dir_dsl.rb', line 23

def entry_exist? entry_name
  File.exist? "#{to_root}/#{entry_name}"
end

#file(params) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/dir_dsl/dir_dsl.rb', line 46

def file params
  to_dir = to_dir(params[:to_dir], params[:name])

  create_directory to_dir unless File.exist? to_dir

  write_to_file "#{from_root}/#{params[:name]}", "#{to_dir}/#{File.basename(params[:name])}"
end

#global_ignore(ignore) ⇒ Object



19
20
21
# File 'lib/dir_dsl/dir_dsl.rb', line 19

def global_ignore ignore
  @global_ignores << ignore
end

#list(dir = ".") ⇒ Object



38
39
40
41
42
43
44
# File 'lib/dir_dsl/dir_dsl.rb', line 38

def list dir="."
  list = pattern_to_files "#{from_root}/#{dir}", "**/*"

  list.each_with_index do |name, index|
    list[index] = name["#{from_root}/#{dir}".length+1..-1]
  end
end