Class: AppStack::App

Inherits:
Object
  • Object
show all
Includes:
ConfigParser
Defined in:
lib/app_stack/app.rb,
lib/app_stack/operator.rb,
lib/app_stack/compare_list.rb

Overview

reopen app class, extend with load_compare list function

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigParser

#load_config, #parse_export_groups!, #validate_config!

Constructor Details

#initialize(filename = nil, app_dir = nil) ⇒ App

Returns a new instance of App.



13
14
15
16
17
18
19
20
21
# File 'lib/app_stack/app.rb', line 13

def initialize(filename = nil, app_dir = nil)
  @conf_file, @app_dir = filename, app_dir
  @config = load_config
  info 'loaded configuration: ', @config

  @app_stacks = {}
  @compare_list = []
  @attrs = {}
end

Instance Attribute Details

#app_stacksObject (readonly)

Returns the value of attribute app_stacks.



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

def app_stacks
  @app_stacks
end

#attrsObject (readonly)

Returns the value of attribute attrs.



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

def attrs
  @attrs
end

#compare_listObject (readonly)

Returns the value of attribute compare_list.



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

def compare_list
  @compare_list
end

#conf_fileObject (readonly)

Returns the value of attribute conf_file.



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

def conf_file
  @conf_file
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#copy_listObject (readonly)

Returns the value of attribute copy_list.



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

def copy_list
  @copy_list
end

#diff_listObject (readonly)

Returns the value of attribute diff_list.



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

def diff_list
  @diff_list
end

#export_groupsObject (readonly)

Returns the value of attribute export_groups.



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

def export_groups
  @export_groups
end

#import_listObject (readonly)

Returns the value of attribute import_list.



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

def import_list
  @import_list
end

#rel_pathObject

Returns the value of attribute rel_path.



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

def rel_path
  @rel_path
end

#render_listObject (readonly)

Returns the value of attribute render_list.



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

def render_list
  @render_list
end

#sync_listObject (readonly)

Returns the value of attribute sync_list.



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

def sync_list
  @sync_list
end

Instance Method Details

#app_nameObject

use the direct name of the config file as app name



66
67
68
# File 'lib/app_stack/app.rb', line 66

def app_name
  directory && File.basename(directory)
end

#build_copy_list!Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/app_stack/operator.rb', line 7

def build_copy_list!
  @copy_list, @diff_list = [], []
  compare_list.each do |f|
    info f.label
    if File.exists?(f.to_file)
      if f.import?
        info f.skip('target exists')
        next
      end

      if f.diff?
        info f.label, 'add to diff list'
        @diff_list << f
      else
        info f.skip('not changed')
        next
      end
    end
    info f.label, 'add to copy list'
    @copy_list << f
  end
end

#directoryObject



60
61
62
63
# File 'lib/app_stack/app.rb', line 60

def directory
  @app_dir ||= (conf_file && File.dirname(conf_file))
  File.expand_path(@app_dir)
end

#get_file_path(fp) ⇒ Object

rubocop:enable LineLength, MethodLength



190
191
192
# File 'lib/app_stack/compare_list.rb', line 190

def get_file_path(fp)
  File.expand_path(fp, directory)
end

#get_group_files(group, app) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/app_stack/compare_list.rb', line 171

def get_group_files(group, app)
  file_list = []
  export_groups[group.to_s].each do |fp|
    if fp.match(/[\?\*\+\{\}]+/)
      Dir[get_file_path(fp)].each { |f| file_list << f }
    else
      file = get_file_path(fp)

      if File.exists?(file)
        file_list << file
      else
        warn "[WARN] #{fp.blue} not found for #{app.app_name.bold}, group #{group.bold}, skip."
      end
    end
  end
  file_list
end

#info(msg, var = nil) ⇒ Object

echo message only if verbose specified to be true rubocop:disable CyclomaticComplexity



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/app_stack/app.rb', line 72

def info(msg, var = nil)
  return unless options && options.verbose

  msg = "[#{app_name || self.class.name}] ".green + msg
  msg += "\n" if var && var.inspect.size > 30

  print msg
  if var
    print Term::ANSIColor.bold
    var.inspect.size > 30 ? PP.pp(var) : PP.singleline_pp(var)
    print Term::ANSIColor.reset
  end
  puts unless var && var.inspect.size > 30
end

#load_compare_list!Object



98
99
100
101
102
103
104
# File 'lib/app_stack/compare_list.rb', line 98

def load_compare_list!
  # @import_list.merge(@sync_list).merge(@render_list
  load_copy_list!(@import_list, import: true)
  load_copy_list!(@sync_list)
  load_render_list!(@render_list, render: true)
  load_local_render_list!(render: true)
end

#load_copy_list!(list, opts = {}) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/app_stack/compare_list.rb', line 106

def load_copy_list!(list, opts = {})
  list.each do |app, exp|
    exp.each do |p| # string as group name or hash as file mapping
      opts.merge! from_app: @app_stacks[app.to_s], to_app: self
      p.is_a?(String) ? load_group!(p, opts) : load_file_map!(p, opts)
    end
  end
end

#load_file_map!(hsh, opts) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/app_stack/compare_list.rb', line 157

def load_file_map!(hsh, opts)
  hsh.each do |f, t|
    t = f unless t && t.size > 0
    from_file = opts[:from_app].get_file_path(f)
    if File.exists?(from_file)
      @compare_list << CompareFile.new(
        opts.merge(from_file: opts[:from_app].get_file_path(f),
                   to_file: opts[:to_app].get_file_path(t)))
    else
      warn "[WARN] #{f.blue} not found in #{opts[:from_app].app_name.bold}, skip."
    end
  end
end

#load_group!(group, opts) ⇒ Object

rubocop:disable LineLength



149
150
151
152
153
154
155
# File 'lib/app_stack/compare_list.rb', line 149

def load_group!(group, opts)
  app = opts[:from_app]
  fail ParseError, "Error on #{@conf_file}, #{app.app_name.bold} did not export group #{group.bold}" unless app.export_groups[group.to_s]
  app.get_group_files(group, opts[:from_app]).each do |full_path|
    @compare_list << CompareFile.new(opts.merge(from_file: full_path))
  end
end

#load_local_render_list!(opts = {}) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/app_stack/compare_list.rb', line 134

def load_local_render_list!(opts = {})
  config.render_local.each do |fr, to|
    from_file = get_file_path(fr)
    to = fr.sub(/\.(erb|liquid|slim)$/, '') unless to.size > 0
    if File.exists?(from_file)
      @compare_list << CompareFile.new(
        opts.merge(from_app: self, to_app: self,
                   from_file: from_file, to_file: get_file_path(to)))
    else
      warn "[WARN] #{fr.blue} not found in #{app_name.bold} (local), skip."
    end
  end
end

#load_render_list!(list, opts = {}) ⇒ Object

rubocop:disable MethodLength



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/app_stack/compare_list.rb', line 116

def load_render_list!(list, opts = {})
  list.each do |appname, hsh|
    app = @app_stacks[appname.to_s]
    hsh.each do |fr, to|
      to = fr.sub(/\.(erb|liquid|slim)$/, '') unless to.size > 0
      from_file = app.get_file_path(fr)
      if File.exists?(from_file)
        @compare_list << CompareFile.new(
          opts.merge(from_app: app, to_app: self,
                     from_file: from_file,
                     to_file: get_file_path(to)))
      else
        warn "[WARN] #{fr.blue} not found in #{app.app_name.bold}, skip."
      end
    end
  end
end

#load_stack!Object

rubocop:disable MethodLength



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/app_stack/app.rb', line 24

def load_stack!
  # load_stack.each ...
  validate_config!

  # parse all stacks
  @import_list.merge(@sync_list).merge(@render_list).keys.each do |app|
    app_dir = File.expand_path(app, File.join(directory, config.stack_dir))
    # rubocop:disable LineLength
    fail ParseError, "Error on #{@conf_file}, stack app #{app} not found in #{app_dir}" unless File.directory?(app_dir)
    # rubocop:enable LineLength
    @app_stacks[app] = App.new(AppStack.find_conf_file(app_dir), app_dir)
    @app_stacks[app].rel_path = File.join(directory, config.stack_dir, app)
    @app_stacks[app].parse_export_groups!
    @attrs.deep_merge! @app_stacks[app].config.attrs

    # info "load app #{app.bold} as", @app_stacks[app]
    info "load app #{app.bold}"
  end

  @attrs.deep_merge! config.attrs
  info 'merged attrs', attrs
  @app_stacks
end

#merge!Object



30
31
32
33
34
35
36
# File 'lib/app_stack/operator.rb', line 30

def merge!
  case
  when AppStack.options.force then do_copy!
  when AppStack.options.simulate then do_simulate!
  else diff_list.size > 0 ? do_warn : do_copy!
  end
end

#optionsObject



56
57
58
# File 'lib/app_stack/app.rb', line 56

def options
  AppStack.options
end

#stackup!Object

rubocop:enable MethodLength



49
50
51
52
53
54
# File 'lib/app_stack/app.rb', line 49

def stackup!
  load_stack!
  load_compare_list!
  build_copy_list!
  merge!
end