Module: AppStack

Extended by:
AppStack
Included in:
AppStack
Defined in:
lib/app_stack.rb,
lib/app_stack/version.rb

Overview

AppStack module

Constant Summary collapse

CONF_FILE =
'.app_stack.yml'
VERSION =
'1.0.0'

Instance Method Summary collapse

Instance Method Details

#carp(job, state = 'done'.green, v = 1) ⇒ Object

print debug / information message to console based on verbose level



136
137
138
139
140
141
# File 'lib/app_stack.rb', line 136

def carp(job, state = 'done'.green, v = 1)
  return if @verbose < v
  dots = 70 - job.size
  job += ' ' + '.' * dots if dots > 0
  puts job + ' ' + state
end

#copy_file!(f, target) ⇒ Object

copy file if newer



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/app_stack.rb', line 197

def copy_file!(f, target)
  # directory?
  if File.directory?(f)
    if File.directory?(target)
      done = 'exists'.green
    else
      FileUtils.mkdir_p target
      done = 'created'.bold.green
    end
  else
    if newer?(f, target)
      target_dir = File.dirname(target)
      FileUtils.mkdir_p target_dir unless File.directory?(target_dir)
      FileUtils.copy f, target
      done = 'copied'.bold.green
    else
      done = 'keep'.white
    end
  end
  done
end

#export_list(dir) ⇒ Object

find a list of file to copy based on export setting



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/app_stack.rb', line 168

def export_list(dir)
  dir_conf = YAML.load(File.read(dir + '/' + File.basename(@conf_file)))
  dir_conf['export'] ||= []

  # update attr list for assign to template files
  @attrs.deep_merge! dir_conf['attrs'] if dir_conf['attrs'] &&
                                         dir_conf['attrs'].is_a?(Hash)

  flist = []
  # export list defined in stack app's configuration
  dir_conf['export'].each do |e|
    Dir[dir + '/' + e].each { |f| flist << f.sub(/^#{dir}\/?/, '') }
  end

  # collect include/exclude list from configuration of app-root
  inc_list, exc_list = [], []
  @config['include'].each do |inc|
    Dir[dir + '/' + inc].each { |f| inc_list << f.sub(/^#{dir}\/?/, '') }
  end

  @config['exclude'].each do |exc|
    Dir[dir + '/' + exc].each { |f| exc_list << f.sub(/^#{dir}\/?/, '') }
  end

  # adjust by include/exclude and
  flist + inc_list - gitignore_list(dir) - exc_list
end

#gitignore_list(dir) ⇒ Object

fetch (and cache) git ignore file lists for a specific directory



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/app_stack.rb', line 144

def gitignore_list(dir)
  @gitignore_list ||= {}
  @gitignore_list[dir] ||= []

  ilist = []
  if File.exists?(dir + '/.gitignore')
    File.read(dir + '/.gitignore').split("\n").each do |line|
      Dir[dir + '/' + line].each do |f|
        f.sub!(/^#{dir}\//, '')
        ilist << f unless ilist.include?(f)
      end
    end
  end
  @gitignore_list[dir] = ilist
end

#load_configuration(conf_file) ⇒ Object

convert directory names, load configuration from yaml file rubocop:disable MethodLength



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

def load_configuration(conf_file)
  @conf_file = conf_file || CONF_FILE
  @config = YAML.load(File.read(@conf_file))

  @app_root = @config['app_root'] || File.dirname(@conf_file)
  @app_root = File.expand_path(@app_root)
  @stack_dir = @config['stack_dir'] || '../stack_apps'
  @stack_dir = File.expand_path(@app_root + '/' +
                    @stack_dir) if @stack_dir.match(/^\.\.?\//)

  @verbose = @config['verbose'] || 1
  @verbose = @verbose.to_i

  @config['tpl_ext'] ||= '.erb'

  # attrs to assigned into template
  @attrs = {}
  # file list under the app_root
  @self_files = []

  @files = @config['files'] || {}
end

#merge_stacks!(stack) ⇒ Object

copy from a stack of applications



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/app_stack.rb', line 92

def merge_stacks!(stack)
  stack.each do |app|
    app_dir = @stack_dir + '/' + app
    raise "no directory found for #{app}" unless File.directory?(app_dir)
    raise "no configuration found for #{app}" unless
             File.exists?(app_dir + '/' + File.basename(@conf_file))

    # loop over remote files
    elist = export_list(app_dir)
    elist.each do |file|
      # skip .erb file as template
      next if file.match(/#{@config['tpl_ext']}$/) &&
              elist.include?(file.sub(/#{@config['tpl_ext']}$/, ''))
      # find the absolute path for source and target file for copy
      src_f = File.expand_path(app_dir + '/' + file)
      tgt_f = File.expand_path(@app_root + '/' + file)

      if @files[file] == '__self' # don't handle it
        carp "From #{app.blue.bold} #{file.bold}",
             'skip, use '.white + @files[file], 2
      else # not registered as self, copy over
        unless @files[file] == app
          carp "By #{app.bold.blue}, overwrite #{@files[file].bold} #{file}",
               'ok'.green, 1 if @files[file]
          @files[file] = app
        end

        if File.exists?(src_f + @config['tpl_ext'])
          carp "From #{app.blue.bold} render #{file.bold}",
               render_file!(src_f + @config['tpl_ext'], tgt_f), 1

        else
          carp "From #{app.blue.bold} copy #{file.bold}",
               copy_file!(src_f, tgt_f), 1
        end

        # register the copied file to app-root file list
        @self_files << file unless @self_files.include?(file)
      end
    end
  end
end

#newer?(f1, f2) ⇒ Boolean

if f1 newer than f2, or f2 not exits but f1 does.

Returns:

  • (Boolean)


161
162
163
164
165
# File 'lib/app_stack.rb', line 161

def newer?(f1, f2)
  return false unless File.exists?(f1)
  return true unless File.exists?(f2)
  File.mtime(f1) > File.mtime(f2)
end

#register_self!(dir) ⇒ Object

for files already in the app root, register to no-copy list



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/app_stack.rb', line 59

def register_self!(dir)
  Find.find(dir).each do |f|
    next if f == dir
    basename = f.sub(/^#{dir}\//, '')
    next if basename.match(/^.git$/)
    next if basename.match(/^.git\W/)
    next if gitignore_list(dir).include?(basename)

    if @files[basename]
      carp "From #{'self'.blue.bold} #{basename.bold} ",
           'keep'.white, 2 if @files[basename] == '__self'
    else
      carp "From #{'self'.blue.bold} #{basename.bold}",
           'registed'.green.bold, 1
      @files[basename] = '__self'
    end

    @self_files << f unless @self_files.include?(f)
  end
end

#render_file!(f, target) ⇒ Object

render from erb if newer



220
221
222
223
224
225
226
227
228
229
# File 'lib/app_stack.rb', line 220

def render_file!(f, target)
  # done = 'keep'.white
  # if newer?(f, target)
  tilt = Tilt::ERBTemplate.new(f)
  oh = File.open(target, 'wb')
  oh.write tilt.render(OpenStruct.new(@attrs.deep_merge(@config['attrs'])))
  oh.close
  'rendered'.bold.green
  # end
end

#render_self!(files) ⇒ Object

render file in app-root



81
82
83
84
85
86
87
88
89
# File 'lib/app_stack.rb', line 81

def render_self!(files)
  files.each do |f|
    if File.exists?(f + @config['tpl_ext'])
      basename = f.sub(/^#{@app_root}\//, '')
      carp "From #{'self'.blue.bold} render #{basename.bold}",
           render_file!(f + @config['tpl_ext'], f), 1
    end
  end
end

#stackup!(conf_file) ⇒ Object

public entry ponit, receive a configuration filename, copy source files on to the directory contains the configuration file.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/app_stack.rb', line 21

def stackup!(conf_file)
  load_configuration(conf_file)

  register_self!(@app_root)
  merge_stacks!(@config['stack'])
  render_self!(@self_files)

  # rewrite configuration back to app-stack file
  @config['files'] = @files
  File.open(conf_file, 'wb') { |fh| fh.puts YAML.dump(@config) }
end