Class: Six::Arma::Builder::Mod

Inherits:
Object
  • Object
show all
Defined in:
lib/six/arma/builder/mod.rb

Constant Summary collapse

LOG_LIMIT =
9999

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Mod

Returns a new instance of Mod.



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
44
45
46
47
48
49
# File 'lib/six/arma/builder/mod.rb', line 18

def initialize(config)
  @config = config
  @source = Git.open(@config[:source])
  @build = 0

  # FIXME: Somehow @source.status.untracked  seems to behave weird
  if @source.status.changed.class == Array
    data = @source.status.changed + @source.status.added + @source.status.deleted
  else
    data = @source.status.changed.merge @source.status.added.merge @source.status.deleted
  end
  if data.size > 0
    log.warn "WARNING: #{@config[:source]} Working folder dirty!"
  end

  @config[:revision][:previous] = @config[:revision][:current].clone
  @config[:revision][:current] = @source.log.first.to_s

  # TODO: Destination can be non git too!
  @destination = Rsync.open(@config[:destination], :log => log)
  @destination_changed = false
  @stats = []
  @config[:paths].each do |path|
    stat = Hash.new
    stat[:deleted], stat[:changed], stat[:changelog] = [], [], []
    stat[:cl] = Hash.new
    stat[:cl][:changed], stat[:cl][:fixed], stat[:cl][:added], stat[:cl][:updated], stat[:cl][:removed] = [], [], [], [], []
    @stats << stat
  end
  rep_cfg = File.open(File.join(@config[:destination], '.rsync', '.repository.yml')) {|file| YAML::load(file)}
  @build = rep_cfg[:version] + 1
end

Instance Method Details

#build_changed_addons(path, stat) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/six/arma/builder/mod.rb', line 268

def build_changed_addons(path, stat)
  stat[:deleted].each do |change|
    log.info "Deleting #{change}"
    change[/(.*)\/(.*)/]
    folder, file = $1, $2
    # TODO: Add PREFIX_  instead of * ?
    Dir[File.join(@config[:destination], path[:folder], "*#{file}*")].each do |file|
      FileUtils.rm_f file
    end
  end
  stat[:changed].each do |change|
    log.info "Building #{change}"
    file = File.join(@config[:source], change)
    fol_path = File.join(@config[:destination], path[:folder])
    obj = Six::Arma::Tools::Pbo.new(file, fol_path)
    overriden = false
=begin
    # TODO: Should work with change objects that contain the right bits?
    path[:overrides].each do |exclude|
      if change[/#{exclude[:folder]}\Z/]
        overriden = true
        log.info "Building #{change} overriden: #{exclude[:action]}"
        unless exclude[:action] == :ignore
          @destination_changed = true
          eval "obj.#{exclude[:action]}"
        end
      end
    end
    unless overriden
=end
    if File.exists?(File.join(file, "$IGNORE$"))
    else
      pbo = if @config[:pack_only] || File.exists?(File.join(file, "$NOBIN$"))
        obj.pack
      else
        obj.binarize
      end
      stat[:packed] << pbo
      @destination_changed = true
    end
  end
end

#clean_msg(msg, reg = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/six/arma/builder/mod.rb', line 184

def clean_msg(msg, reg = nil)
  msg = if reg
    msg.gsub(reg, '')
  else
    msg.clone
  end

  msg.gsub!(/\A[\t| ]*~[\t| ]*/, '')
  msg.strip!
  msg[/\A(.)/]
  msg.gsub!(/\A(.)/, $1.upcase)
  "* #{msg}"
end

#commit_destinationObject



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/six/arma/builder/mod.rb', line 455

def commit_destination
  # Combine the changelogs and everything
  # Commit and optional Push?
  #@destination_changed = true
  if @destination_changed
    puts
    log.info "Committing, Repacking and Pushing..."

    #path = File.join(BASE_PATH, 'test')
    #File.open(path, 'w') { |file| file.puts msgs.join("\n") }
    # TODO: Add specifically the changes!
    @destination.add
    @destination.commit#_all({:path => path})
    #FileUtils.rm_f path

    # TODO: First repack then push or reverse?
    #@destination.gc_legacy
    #@destination.repack
    #@destination.push
  else
    log.info "Nothing to commit, not pushing..."
  end
end

#logObject



51
52
53
# File 'lib/six/arma/builder/mod.rb', line 51

def log
  Six::Arma::Builder.log
end

#prep_folder(path, stat) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/six/arma/builder/mod.rb', line 235

def prep_folder(path, stat)
  #p stat[:changed]
  return unless (stat[:changed] + stat[:deleted]).size > 0
  if File.exist?(File.join(@config[:source], path[:folder], "main"))
    #puts "found version addon"
    file = File.join(@config[:source], path[:folder], "main", "script_mod.hpp")
    if File.exist?(file)
      #puts "Found script_mod"
      content = nil
      File.open(file) do |f|
        content = f.read
        content.gsub!(/#define BUILD .*/, "#define BUILD #{@build}")
      end
      #puts content
      #gets
      File.open(file, 'w') {|f| f.puts content}
      main = "#{path[:folder]}/main"
      unless stat[:changed].include?(main)
        stat[:changed] += [main]
      end

      if File.exist?(File.join(@config[:source], path[:folder], "version"))
        version = "#{path[:folder]}/version"
        unless stat[:changed].include?(version)
          stat[:changed] += [version]
        end
      end
    end
  end
  #puts "passed"
  #gets
end

#process_change(change, path, stat) ⇒ Object

, base)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/six/arma/builder/mod.rb', line 55

def process_change(change, path, stat)#, base)
  if change.path[/\A#{path[:folder]}\//]
    case path[:type]
      when :addons
        new = Hash.new
        new[:type] = change.type
        change.path[/\A(#{path[:folder]})\/([\w_\.]*)\/?(.*)/]
        new[:base], new[:folder], new[:file] = $1, $2, $3
        path = "#{new[:base]}/#{new[:folder]}"
        unless stat[:changed].include?(path)
          #log.info "#{change.typ} #{change.path}"
          case new[:type]
            when 'deleted'
              if FileTest.exist?(File.join(@config[:source], path))
                new[:type] = 'modified'
              end
            #when 'modified'
            when 'new'
              # if already existed, it was not added but modified, dont matter actually
              new[:type] = 'modified'
          end

          case new[:type]
            when 'deleted'
              stat[:deleted] << path unless stat[:deleted].include?(path)
            when 'modified'
              stat[:changed] << path
          end
        end
      when :store
        new = Hash.new
        new[:type] = change.type
        change.path[/\A(#{path[:folder]})\/([\w_\.]*)\/?(.*)/]
        new[:base], new[:folder], new[:file] = $1, $2, $3
        path = "#{new[:base]}/#{new[:folder]}"
        unless stat[:changed].include?(path)
          #log.info "#{change.typ} #{change.path}"
          case new[:type]
            when 'deleted'
              if FileTest.exist?(File.join(@config[:source], path))
                new[:type] = 'modified'
              end
            #when 'modified'
            when 'new'
              # if already existed, it was not added but modified, dont matter actually
              new[:type] = 'modified'
          end

          case new[:type]
            when 'deleted'
              stat[:deleted] << path unless stat[:deleted].include?(path)
            when 'modified'
              stat[:changed] << path
          end
        end
      else
        # TODO: Normal folder; Process each added / modified / deleted file on a per file basis, with no extra processing
    end
  end
end

#process_changed_files(path, stat) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/six/arma/builder/mod.rb', line 311

def process_changed_files(path, stat)
  des_path = File.join(@config[:destination], path[:folder])
  src_path = File.join(@config[:source], path[:folder])

  FileUtils.mkdir_p des_path unless File.exists?(des_path)

  stat[:deleted].each do |change|
    log.info "Deleting #{change}"
    change[/(.*)\/(.*)/]
    folder, file = $1, $2
    ppath = File.join(src_path, folder)
    unless File.exists? ppath
    # TODO: Add PREFIX_  instead of * ?
      FileUtils.rm_f File.join(@config[:destination], path[:folder], "#{folder}.tar")
    end
  end

  stat[:changed].each do |change|
    log.info "Building #{change}"
    file = File.join(@config[:source], change)
    # TODO: Also for adodn folders!
    change[/(.*)\/(.*)/]
    f = $2

    if !File.directory?(file)
      FileUtils.cp(file, des_path) # TODO: apply proper file date
      @destination_changed = true
    else
      if File.exists?(File.join(file, "$IGNORE$"))
      else
        Dir.chdir src_path do
          o = %x[tar -c #{f} > #{f}.tar]
          FileUtils.mv(File.join(src_path, "#{f}.tar"), des_path)
        end
        @destination_changed = true
      end
    end
  end
end

#process_diffsObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/six/arma/builder/mod.rb', line 116

def process_diffs
  puts
  log.info "Processing Repository: #{@config[:source]}, Branch: #{@source.current_branch}, Current Rev: #{@config[:revision][:current]} Previous Rev: #{@config[:revision][:previous]}"

  # Prepare stats
  @stats.each do |stat|
    stat[:deleted], stat[:changed] = [], []
  end

  # Prepare file diffs
  @source.diff(@config[:revision][:previous], @config[:revision][:current]).each do |diff|
    @config[:paths].each_with_index do |path, index|
      process_change(diff, path, @stats[index])#, 'Addons')
    end
  end

  # Process file and comment history
  history = @source.log(LOG_LIMIT)
  @config[:paths].each_with_index do |path, index|
    # Process Stats
    stat = @stats[index]
    stat[:changed].sort!
    stat[:deleted].sort!
    stat[:changelog] = []
    stat[:cl][:changed], stat[:cl][:fixed], stat[:cl][:added], stat[:cl][:updated], stat[:cl][:removed] = [], [], [], [], []

    log.info "Changed (#{stat[:changed].size}): #{stat[:changed].join(', ')}"
    log.info "Deleted (#{stat[:deleted].size}): #{stat[:deleted].join(', ')}"

    # Process Comment Changelog
    history.path(path[:folder])
    history.between(@config[:revision][:previous], @config[:revision][:current]).each do |c|
      c.message.split("\n").each do |msg|
        msg = "#{msg} [#{c.author.name}]"

        if msg[/\A~/] && !msg[/\A.?.?local/i]
          stat[:changelog] << msg
          case msg
            when ADDED
              stat[:cl][:added] << clean_msg(msg, ADDED)
            when FIXED
              stat[:cl][:fixed] << clean_msg(msg, FIXED)
            when REMOVED
              stat[:cl][:removed] << clean_msg(msg, REMOVED)
            when CHANGED
              stat[:cl][:changed] << clean_msg(msg, CHANGED)
            when UPDATED
              stat[:cl][:updated] << clean_msg(msg, UPDATED)
            else
              stat[:cl][:changed] << clean_msg(msg)
          end
        end
      end
    end

    stat[:changelog].sort!
    stat[:cl][:changed].sort!
    stat[:cl][:fixed].sort!
    stat[:cl][:added].sort!
    stat[:cl][:updated].sort!
    stat[:cl][:removed].sort!

    # TODO: Filter and use changelog in git commit on destination or changelog.txt?
    log.info "Changelog:"
    stat[:changelog].each { |msg| log.info msg }
  end
end

#process_filesObject



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

def process_files
  puts
  log.info "Processing Files ..."
  @config[:paths].each_with_index do |path, index|
    puts
    log.info "Path: #{path[:folder]}"
    stat = @stats[index]
    case path[:type]
      when :addons
        stat[:packed] = []
        prep_folder(path, stat)
        build_changed_addons(path, stat)
        #sign_changed_addons(path, stat)
        # TODO: Make choice
        #sign_all_addons(path, stat, true)
      when :store
        process_changed_files(path, stat)
    end
  end
end

#sign_all_addons(path, stat, force = false) ⇒ Object



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/six/arma/builder/mod.rb', line 383

def sign_all_addons(path, stat, force = false)
  if @destination_changed || force
    if stat[:changed].size > 0
      if @config[:key]
      else
        key = "#{@config[:name]}_b#{@build}"
        d = "C:/projects/keys"
        unless File.exists?(File.join(d, "#{key}.biprivatekey"))
          Dir.chdir d
          system "C:/tools/dsutils/dsCreateKey #{key}"
        end
        Dir[File.join(@config[:destination], "store", "keys", '*.bikey')].each do |f|
          FileUtils.rm_f(f)
        end
        FileUtils.cp(File.join(d, "#{key}.bikey"), File.join(@config[:destination], "store", "keys", "#{key}.bikey"))
      end
      puts
      log.info "Signing All..."
      destination = File.join(@config[:destination], path[:folder])
      Dir.chdir destination
      Dir[File.join(destination, "*.bisign")].each do |f|
        FileUtils::rm_f f
      end
      Dir['*.pbo'].each do |pbo|
        log.info "Signing #{pbo}"
        file = File.join(@config[:destination], path[:folder], pbo)
        obj = Six::Arma::Tools::Pbo.new(file, File.join(@config[:destination], path[:folder]))
        obj.sign({:key => File.join(d, "#{key}.biprivatekey")})
      end
      @destination_changed = true
    end
  end
end

#sign_changed_addons(path, stat) ⇒ Object

FIXME: !!



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/six/arma/builder/mod.rb', line 352

def sign_changed_addons(path, stat)
  if @destination_changed
    if @config[:key]
      puts
      log.info "Signing Changed..."
      Dir.chdir File.join(@config[:destination], path[:folder])
      stat[:changed].each do |change|
        source = File.join(@config[:source], change)
        prefix = File.join(source, "$PBOPREFIX$")
        change[/.*\/(.*)/]
        file = "#{$1}.pbo"

        if FileTest.exist?(prefix)
          File.open(prefix) do |f|
            content = f.read
            content[/\Ax\\(\w*)\\/]
            file = "#{$1}_#{file}" if $1
          end
        end

        log.info "Signing #{file}"
        file = File.join(@config[:destination], path[:folder], file)
        del = "#{file}.*.bisign"
        FileUtils::rm_f del if FileTest.exist?(del)
        obj = Six::Arma::Tools::Pbo.new(file, File.join(@config[:destination], path[:folder]))
        obj.sign({:key => @config[:key]})
      end
    end
  end
end

#sign_filesObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/six/arma/builder/mod.rb', line 219

def sign_files
  puts
  #log.info "Signing ..."
  @config[:paths].each_with_index do |path, index|
    puts
    log.info "Path: #{path[:folder]}"
    stat = @stats[index]
    case path[:type]
      when :addons
        #sign_changed_addons(path, stat)
        # TODO: Make choice
        sign_all_addons(path, stat, true)
    end
  end
end

#write_changelogObject



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/six/arma/builder/mod.rb', line 417

def write_changelog
  if @destination_changed
    puts
    log.info "Writing Changelog..."
    msgs = ["h2. #{Time.now}\n\nBuild #{@build}\nUpdated to source:@#{@config[:revision][:current]}"]
    @config[:paths].each_with_index do |path, index|
      stat = @stats[index]
      #msgs << "Path: #{path[:folder]}"
      #msgs += stat[:changelog]
      msgs << "\nh3. Changed\n\n" if stat[:cl][:changed].size > 0
      msgs += stat[:cl][:changed]
      msgs << "\nh3. Fixed\n\n" if stat[:cl][:fixed].size > 0
      msgs += stat[:cl][:fixed]
      msgs << "\nh3. Added\n\n" if stat[:cl][:added].size > 0
      msgs += stat[:cl][:added]
      msgs << "\nh3. Updated\n\n" if stat[:cl][:updated].size > 0
      msgs += stat[:cl][:updated]
      msgs << "\nh3. Removed\n\n" if stat[:cl][:removed].size > 0
      msgs += stat[:cl][:removed]
      msgs << "\n\n"
    end

    changelog = nil
    cl = File.join(@config[:destination], 'changelog.txt')
    File.open(cl) { |file| changelog = file.read } if FileTest.exists?(cl)

    File.open(File.join(@config[:destination], 'changelog.txt'), 'w') do |file|
      file.puts msgs
      file.puts changelog if changelog
    end

    File.open(File.join(BASE_PATH, "changelog.txt"), 'a') do |file|
      file.puts "h2. #{@config[:name].upcase}\n\n"
      file.puts msgs
    end
  end
end