Module: AppStack::LocalFilesParser

Included in:
StackApp
Defined in:
lib/app_stack/local_files_parser.rb

Overview

parse local files from a local app directory

Instance Method Summary collapse

Instance Method Details

#app_nameObject

use the direct name of the config file as app name



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

def app_name
  File.basename(directory)
end

#attrsObject



6
7
8
# File 'lib/app_stack/local_files_parser.rb', line 6

def attrs
  @config[:attrs]
end

#directoryObject



10
11
12
# File 'lib/app_stack/local_files_parser.rb', line 10

def directory
  File.expand_path('../', @file)
end

#export_files(group = 'default') ⇒ Object

get export files list (copy_from => copy_to hash) for a specific group



29
30
31
32
33
# File 'lib/app_stack/local_files_parser.rb', line 29

def export_files(group = 'default')
  @export_files ||= parse_export_files
  group = 'default' if group.to_s == 'defaults' # back-compatibility
  @export_files[group.to_s]
end

#find_files(plist) ⇒ Object

from array (or string) of file patterns, glob files and build a large array



87
88
89
90
91
92
93
# File 'lib/app_stack/local_files_parser.rb', line 87

def find_files(plist)
  fhsh = {}
  plist.each do |file_pattern|
    fhsh.merge! glob_files(file_pattern)
  end
  fhsh
end

#full_path(file) ⇒ Object



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

def full_path(file)
  File.expand_path(file, directory)
end

#glob_files(dir, allow_hash = false) ⇒ Object

from file pattern to copy_from => copy_to (remove directory header) hash



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/app_stack/local_files_parser.rb', line 63

def glob_files(dir, allow_hash = false)
  fhsh = {}
  if dir.is_a?(Hash)
    raise 'Can not use hash here for ' + dir.keys.join.to_s + ' in file ' +
      filename unless allow_hash
    dir.each do |f, t|
      t = '/' + t.gsub(/^\//, '')
      fhsh[switch_to_tpl(f)] = t
      echo "found: #{short_path(switch_to_tpl(f)).blue} to: ",
           short_path(t)
    end
  else
    Dir[File.expand_path(dir, directory)].each do |f|
      fhsh[switch_to_tpl(f)] = f.gsub(/^#{directory}/, '')
      echo "found: #{short_path(switch_to_tpl(f)).blue} to: ",
           short_path(short_path(f.gsub(/^#{directory}/, '')))
    end
  end

  fhsh
end

#parse_export_filesObject

get all exported files in groups



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/app_stack/local_files_parser.rb', line 36

def parse_export_files
  # 'file_group_name' => { copy_from => copy_to_base_name }
  @export_files ||= {}
  @config[:export].each do |p_list| # pattern list
    if p_list.is_a?(Hash) # group, file list
      p_list.each do |n, p|
        @export_files[n] ||= {}
        @export_files[n].merge! find_files(p)
      end
    elsif p_list.is_a?(String)
      @export_files['default'] ||= {}
      @export_files['default'].merge! find_files([p_list])
    else
      fail 'invalid export option ' + p_list.inspect
    end
  end
  @export_files
end

#short_path(path) ⇒ Object



23
24
25
26
# File 'lib/app_stack/local_files_parser.rb', line 23

def short_path(path)
  lpath = path.sub(/^((c|r)\:)?#{directory}\/?/, '')
  lpath.sub(/^\//, '')
end

#switch_to_tpl(file) ⇒ Object



55
56
57
58
59
60
# File 'lib/app_stack/local_files_parser.rb', line 55

def switch_to_tpl(file)
  f = full_path(file)
  tpl_file = nil
  @config[:tpl_ext].each { |ex| tpl_file = f + ex if File.exists?(f + ex) }
  tpl_file ? 'r:' + tpl_file : 'c:' + f
end