Class: AppStack::StackApp

Inherits:
Object
  • Object
show all
Includes:
CopyListBuilder, LocalFilesParser
Defined in:
lib/app_stack/stack_app.rb

Overview

represent a single stacked application

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CopyListBuilder

#ask_diffs, #check_diff, #check_mtime, #copy_file!, #do_copy!, #do_render!, #import_files, #merge!, #newer?, #render_file!

Methods included from LocalFilesParser

#app_name, #attrs, #directory, #export_files, #find_files, #full_path, #glob_files, #parse_export_files, #short_path, #switch_to_tpl

Constructor Details

#initialize(filename, options) ⇒ StackApp

Returns a new instance of StackApp.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/app_stack/stack_app.rb', line 17

def initialize(filename, options)
  @filename, @options = filename, options
  @file = File.expand_path(filename)
  @file_mtime = File.mtime(@file)

  # default configuration, also restrict known keys and data type
  @config = {
    stack: [],
    export: [],
    exclude: [],
    attrs: {},
    stack_dir: '..',
    tpl_ext: %w[.erb .haml .liquid]
  }

  echo "load app #{app_name.bold} from file:", @file
  # use yaml file to set configuration
  YAML.load(File.open(@file, 'r:utf-8').read).each do |k, v|
    fail ParseError,
         "unkown option `#{k}` in #{@filename}" unless @config[k.to_sym]
    fail ParseError,
         "'#{k}' must be a #{@config[k.to_sym].class.to_s}" unless
           v.is_a?(@config[k.to_sym].class)
    echo "set #{k.blue} to: ", v
    @config[k.to_sym] = v
  end
end

Instance Attribute Details

#file_mtimeObject (readonly)

Returns the value of attribute file_mtime.



12
13
14
# File 'lib/app_stack/stack_app.rb', line 12

def file_mtime
  @file_mtime
end

Instance Method Details

#echo(msg, var = nil) ⇒ Object

echo message only if verbose specified to be true



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/app_stack/stack_app.rb', line 104

def echo(msg, var = nil)
  return unless @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_stackObject

load definitions from all apps in the stack config



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/app_stack/stack_app.rb', line 46

def load_stack
  unless @stack
    @stack = { } # stack_app => import list
    @config[:stack].each do |app_conf|
      if app_conf.is_a?(String)
        app_name, groups = app_conf, ['default']
      elsif app_conf.is_a?(Hash)
        fail 'invalid app: ' + app_conf.inspect if app_conf.keys.size > 1
        app_conf.each { |an, ac| app_name, groups = an, ac }
      else
        fail 'invalid stack app: ' + app_conf.inspect
      end

      conf_file = @config[:stack_dir].sub(/\/?$/, '/') + app_name
      conf_file += '/' + File.basename(@file)
      app = StackApp.new(conf_file, @options)
      @stack[app] = groups
    end
  end
  @stack
end

#stackup!Object

entry point: copy or simulate copy



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/app_stack/stack_app.rb', line 69

def stackup!
  @copy_list = {} # from_file_full_path => copy item
  new_attrs, @attr_mtime = {}, file_mtime
  load_stack.each do |app, import_conf|
    echo 'load files from ' + app.app_name.bold.blue

    # get file list from app
    app.import_files(import_conf).each do |f, t|
      @copy_list[f] = full_path(short_path(t))
    end

    # update global attrs
    new_attrs.deep_merge! app.attrs
    @attr_mtime = app.file_mtime if app.file_mtime > @attr_mtime
  end

  @config[:attrs] = new_attrs.deep_merge attrs
  echo 'the merged attributes', attrs

  # add local template files the copy list
  Dir[directory + "/**/*{#{@config[:tpl_ext].join(',')}}"].each do |f|
    of = f.gsub(/#{@config[:tpl_ext].join('|')}$/, '')
    if File.exists?(of)
      @copy_list['r:' + f] = of
      echo 'found local template file: ',
           short_path(f) + ' -> ' + short_path(of)
    else
      echo 'skip template file: ', f
    end
  end

  merge!
end