Module: MTBuild::Utils

Defined in:
lib/mtbuild/utils.rb

Class Method Summary collapse

Class Method Details

.ensure_array(input) ⇒ Object



34
35
36
37
# File 'lib/mtbuild/utils.rb', line 34

def self.ensure_array(input)
  input = [input] unless input.respond_to?(:to_ary)
  return input
end

.expand_file_list(included_files, excluded_files, base_folder = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mtbuild/utils.rb', line 17

def self.expand_file_list(included_files, excluded_files, base_folder=nil)
  file_list = FileList.new()

  included_files = Utils.ensure_array(included_files).flatten.collect{|s| (base_folder and (Pathname.new s).relative?) ? File.join(base_folder, s) : s}
  file_list.include(included_files)

  excluded_files = Utils.ensure_array(excluded_files).flatten.collect{|e| (base_folder and (Pathname.new e).relative?) ? File.join(base_folder, e) : e}
  file_list.exclude(*excluded_files)

  return file_list.to_ary.collect{|f| File.expand_path(f)}
end

.expand_folder_list(included_folders, base_folder = nil) ⇒ Object



29
30
31
32
# File 'lib/mtbuild/utils.rb', line 29

def self.expand_folder_list(included_folders, base_folder=nil)
  included_folders = Utils.ensure_array(included_folders).flatten.collect{|f| (base_folder and (Pathname.new f).relative?) ? File.join(base_folder, f) : f}
  return Dir.glob(included_folders).to_ary.reject{|f| !File.directory?f}.collect{|f| File.expand_path(f)}
end

.merge_configurations(default, override) ⇒ Object



39
40
41
42
43
44
# File 'lib/mtbuild/utils.rb', line 39

def self.merge_configurations(default, override)
  return default.merge(override) { |key, old_value, new_value|
    if old_value.is_a? Hash and new_value.is_a? Hash then merge_configurations(old_value, new_value)
    elsif old_value.is_a? Array and new_value.is_a? Array then old_value | new_value
    else new_value end }
end

.path_difference(root, subdir) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/mtbuild/utils.rb', line 7

def self.path_difference(root, subdir)
  root_path = Pathname.new(root)
  subdir_path = Pathname.new(subdir)
  difference_path = subdir_path.relative_path_from(root_path)
  difference = difference_path.to_path
  return nil if difference.include? '..'
  return '' if difference.eql? '.'
  return difference
end