Class: Project

Inherits:
Hash
  • Object
show all
Defined in:
lib/base/project.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#execute, #to_html

Constructor Details

#initialize(value = "", fullname = "") ⇒ Project

Returns a new instance of Project.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/base/project.rb', line 15

def initialize(value = "", fullname = "")
  @filename = ""
  @env = Environment.new
  self[:url] = Project.get_url
  self[:fullname] = Project.get_fullname_from_url self[:url] if self[:url].length.positive?
  self[:timeout] = 60 * 5
  if value.is_a?(String)
    self[:url] = value if value.is_a?(String) && value.length.positive?
    self[:fullname] = Project.get_fullname_from_url self[:url]
  elsif value.is_a?(Hash)
    value.each { |k, v| self[k.to_sym] = v }
  elsif self[:url].length.positive?
    self[:fullname] = Project.get_fullname_from_url self[:url]
  end
  self[:fullname] = fullname if fullname.length.positive?
end

Instance Attribute Details

#envObject

Returns the value of attribute env.


13
14
15
# File 'lib/base/project.rb', line 13

def env
  @env
end

#filenameObject

Returns the value of attribute filename.


13
14
15
# File 'lib/base/project.rb', line 13

def filename
  @filename
end

Class Method Details

.get_fullname(directory) ⇒ Object


47
48
49
# File 'lib/base/project.rb', line 47

def self.get_fullname(directory)
  directory.gsub(@env.wrk_dir, "")
end

.get_fullname_from_url(url) ⇒ Object


51
52
53
# File 'lib/base/project.rb', line 51

def self.get_fullname_from_url(url)
  url.gsub("http://", "").gsub("https://", "").gsub(".com/", "/").gsub(".git", "")
end

.get_url(directory = Rake.application.original_dir) ⇒ Object


38
39
40
41
42
43
44
45
# File 'lib/base/project.rb', line 38

def self.get_url(directory = Rake.application.original_dir)
  url = ""
  Dir.chdir(directory) do
    url = `git config --get remote.origin.url`.strip if File.exist?(".git")
    url = Svn.url.strip if File.exist?(".svn")
  end
  url
end

Instance Method Details

#checkoutObject


95
96
97
98
99
100
101
102
# File 'lib/base/project.rb', line 95

def checkout
  if !File.exist?(wrk_dir) && self[:url].include?("svn")
    cmd = Command.new({ input: "svn checkout #{url} #{wrk_dir}", quiet: true,
                        ignore_failure: true })
    cmd.execute
    @env.out cmd.summary
  end
end

#clobberObject


274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/base/project.rb', line 274

def clobber
  clobberCmd = Command.new("clobber")
  clobberCmd[:exit_code] = 0
  if File.exist?(wrk_dir)
    Dir.remove wrk_dir, true
    @env.out "removed #{wrk_dir}"
  end
  if File.exist?(make_dir)
    Dir.remove make_dir, true
    @env.out "removed #{make_dir}"
  end
  clobberCmd
end

#cloneObject


86
87
88
89
90
91
92
93
# File 'lib/base/project.rb', line 86

def clone
  if !File.exist?(wrk_dir) && self[:url].include?(".git")
    cmd = Command.new({ input: "git clone #{self[:url]} #{wrk_dir}", quiet: true,
                        ignore_failure: true })
    cmd.execute
    @env.out cmd.summary
  end
end

#command_history(tags = nil) ⇒ Object


160
161
162
163
164
165
166
167
168
# File 'lib/base/project.rb', line 160

def command_history(tags = nil)
  commands = []
  log_filenames(tags).each do |logfile|
    cmd = Command.new(JSON.parse(IO.read(logfile)))
    commands << cmd
  rescue StandardError
  end
  commands
end

#fullnameObject


59
60
61
# File 'lib/base/project.rb', line 59

def fullname
  self[:fullname]
end

#get_logfile(tags) ⇒ Object


202
203
204
205
206
207
208
# File 'lib/base/project.rb', line 202

def get_logfile(tags)
  tagstring = ""
  tagstring = tags if tags.is_a?(String)
  tagstring = tags.join(".") if tags.is_a?(Array)
  name = "#{fullname}.#{tagstring}.json".gsub("/", ".")
  "#{@env.log_dir}/#{name}"
end

#infoObject


257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/base/project.rb', line 257

def info
  infoCmd = Command.new({ input: "info", exit_code: 0 })
  # out_cyan '========================================================='
  # out_cyan fullname
  out_property "fullname".fix(15), fullname
  out_property "url".fix(15), url
  wrk_history = command_history ["work"]
  out_property "work status".fix(15), "?" if wrk_history.length.zero?
  out_property "work status".fix(15), wrk_history[0].summary if wrk_history.length.positive?
  @env.out wrk_history[0].info if wrk_history.length.positive?
  make_history = command_history ["make", latest_tag]
  out_property "make status".fix(15), "?" if make_history.length.zero?
  out_property "make status".fix(15), make_history[0].summary if make_history.length.positive?
  @env.out make_history[0].info if make_history.length.positive?
  infoCmd
end

#last_work_mtimeObject


368
369
370
371
372
373
# File 'lib/base/project.rb', line 368

def last_work_mtime
  logfile = get_logfile ["work"]
  return File.mtime(logfile) if File.exist? logfile

  nil
end

#latest_tag(update = false) ⇒ Object


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/base/project.rb', line 118

def latest_tag(update = false)
  makedir = "#{@env.make_dir}/#{fullname}"
  FileUtils.mkdir_p(File.dirname(makedir)) unless File.exist?(File.dirname(makedir))
  if File.exist?(makedir)
    Dir.chdir(makedir) do
      Command.exit_code("git pull") if update
    end
  elsif update
    clone = Command.new("git clone #{url} #{makedir}")
    clone[:quiet] = true
    clone[:ignore_failure] = true
    clone.execute
  end
  if File.exist?(makedir)
    Dir.chdir(makedir) do
      return Git.latest_tag
    rescue StandardError
    end
  end
  ""
end

#listObject


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/base/project.rb', line 210

def list
  history = command_history
  if history.length.zero?
    @env.out "?      #{fullname}"
  else
    status = 0
    history.each do |c|
      status = c.exit_code if c.exit_code != 0
    end
    if status.zero?
      @env.out "       #{fullname}"
    elsif @env.colorize?
      require "ansi/code"
      @env.out ANSI.red + ANSI.bright + "X      #{fullname}" + ANSI.reset
    else
      @env.out "X      #{fullname}"
    end
  end
end

#log_filenames(tags = nil) ⇒ Object


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/base/project.rb', line 140

def log_filenames(tags = nil)
  tags = [] if tags.nil?
  filenames = []
  Dir.chdir(@env.log_dir) do
    dotname = fullname.gsub("/", ".")
    Dir.glob("#{dotname}*.json").each do |f|
      if tags.length.zero?
        filenames << "#{@env.log_dir}/#{f}"
      else
        has_tags = true
        tags.each do |tag|
          has_tags = false unless f.include? tag
        end
        filenames << "#{@env.log_dir}/#{f}" if has_tags
      end
    end
  end
  filenames
end

#make(tag = "") ⇒ Object


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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/base/project.rb', line 320

def make(tag = "")
  tag = latest_tag true if tag.length.zero?
  # return if tag.length==0
  raise "no tag specified" if tag.length.zero?

  rake_default = Command.new({ input: "rake default", quiet: true, ignore_failure: true })
  logfile = get_logfile ["make", tag]
  if File.exist?(logfile)
    rake_default.open logfile
    @env.out rake_default.summary true if (rake_default[:exit_code] != 0) || @env.show_success?
  else
    makedir = make_dir tag
    FileUtils.mkdir_p(File.dirname(makedir)) unless File.exist? File.dirname(makedir)
    if self[:url].include?(".git") && !File.exist?(makedir)
      clone = Command.new({ input: "git clone #{self[:url]} #{makedir}", quiet: true })
      clone.execute
    end
    if File.exist?(makedir)
      Dir.chdir(makedir) do
        checkout = Command.new({ input: "git checkout #{tag}", quiet: true })
        checkout.execute
        FileUtils.rm_r ".git"
        if !File.exist? "rakefile.rb"
          rake_default[:exit_code] = 1
          rake_default[:error] = "rakefile.rb not found."
          rake_default[:start_time] = Time.now
          rake_default[:end_time] = Time.now
        else
          # rake_default[:timeout] = self[:timeout]
          rake_default.execute
        end
        rake_default.save logfile
        update_status
        @env.out rake_default.summary true
        rake_default
      end
    elsif @env.debug?
      puts "Project make make_dir #{makedir} does not exist."
    end

    begin
      FileUtils.rm_r makedir
    rescue StandardError
    end
  end
  rake_default
end

#make_dir(tag = "") ⇒ Object


72
73
74
75
# File 'lib/base/project.rb', line 72

def make_dir(tag = "")
  "#{@env.make_dir}/#{fullname}" if tag.length.zero?
  "#{@env.make_dir}/#{fullname}-#{tag}"
end

#mark_work_up_to_dateObject


192
193
194
195
196
197
198
199
200
# File 'lib/base/project.rb', line 192

def mark_work_up_to_date
  if wrk_dir == Rake.application.original_dir
    logfile = get_logfile ["up2date"]
    puts "   writing #{logfile}" if Environment.default.debug?
    File.open(logfile, "w") { |f| f.write(" ") }
  elsif @env.debug?
    puts "wrk_dir does not match Rake.application.original_dir"
  end
end

#nameObject


63
64
65
66
# File 'lib/base/project.rb', line 63

def name
  parts = fullname.split("/")
  parts[parts.length - 1]
end

#out_brackets(message) ⇒ Object


230
231
232
233
234
235
236
237
# File 'lib/base/project.rb', line 230

def out_brackets(message)
  if @env.colorize?
    require "ansi/code"
    @env.out "[#{ANSI.blue}#{ANSI.bright}#{message}#{ANSI.reset}]"
  else
    @env.out "[#{message}]"
  end
end

#out_cyan(message) ⇒ Object


239
240
241
242
243
244
245
246
# File 'lib/base/project.rb', line 239

def out_cyan(message)
  if @env.colorize?
    require "ansi/code"
    @env.out ANSI.cyan + ANSI.bright + message + ANSI.reset
  else
    @env.out message.to_s
  end
end

#out_property(name, value) ⇒ Object


248
249
250
251
252
253
254
255
# File 'lib/base/project.rb', line 248

def out_property(name, value)
  if @env.colorize?
    require "ansi/code"
    @env.out "#{name}: " + ANSI.white + ANSI.bold + value.to_s.strip + ANSI.reset
  else
    @env.out "#{name}: #{value}"
  end
end

#pullObject


77
78
79
80
81
82
83
84
# File 'lib/base/project.rb', line 77

def pull
  if File.exist?(wrk_dir) && File.exist?("#{wrk_dir}/.git")
    Dir.chdir(wrk_dir) do
      puts "git pull (#{wrk_dir})"
      puts `git pull`
    end
  end
end

#rakeObject


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

def rake
  unless File.exist?(wrk_dir)
    clone
    checkout
  end
  if File.exist?(wrk_dir)
    Dir.chdir(wrk_dir) do
      rake = Command.new({ input: "rake", timeout: 300, ignore_failure: true })
      rake.execute
      @env.out rake.summary
    end
  end
end

#reportObject


408
# File 'lib/base/project.rb', line 408

def report; end

#set_timeout(value) ⇒ Object


32
33
34
35
36
# File 'lib/base/project.rb', line 32

def set_timeout(value)
  self[:timeout] = value if value.is_a? Numeric
  self[:timeout] = value.gsub("m", "").strip.to_f * 60 if value.include?("m")
  self[:timeout] = value.gsub("s", "").strip.to_f * 60 if value.include?("s")
end

#statusObject


398
399
400
401
402
403
404
405
406
# File 'lib/base/project.rb', line 398

def status
  status_logfile = "#{@env.root_dir}/log/#{fullname}/#{@env.user}@#{@env.machine}.status.json"
  update_status unless File.exist? status_logfile
  if File.exist?(status_logfile)
    statusHash = JSON.parse(IO.read(status_logfile))
    return statusHash["status"] if statusHash.key?("status")
  end
  "?"
end

#tagsObject


431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/base/project.rb', line 431

def tags
  tags = []
  unless File.exist? wrk_dir
    clone = Command.new({ input: "git clone #{self[:url]} #{wrk_dir}", quiet: true })
    clone.execute
  end
  Dir.chdir(wrk_dir) do
    Command.output("git tag").split('\n').each do |line|
      tag = line.strip
      tags << tag if tag.length.negative?
    end
  end
  tags
end

#updateObject


410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/base/project.rb', line 410

def update
  clone
  checkout
  if File.exist?(wrk_dir)
    Dir.chdir(wrk_dir) do
      if File.exist?(".git")
        pull = Command.execute(Command.new({ input: "git pull", quiet: true, ignore_failure: true }))
        @env.out pull.summary true
        return pull
      end
      if File.exist?(".svn")
        updateCmd = Command.execute(Command.new({ input: "svn update", quiet: true,
                                                  ignore_failure: true }))
        @env.out updateCmd.summary true
        return updateCmd
      end
    end
  end
  Command.new({ exit_code: 1 })
end

#update_statusObject


375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/base/project.rb', line 375

def update_status
  status_logfile = "#{@env.root_dir}/log/#{fullname}/#{@env.user}@#{@env.machine}.status.json"
  status = Hash.new({ "status" => "?" })
  wrk_logfile = "#{@env.root_dir}/log/#{fullname}/#{@env.user}@#{@env.machine}.json"
  if File.exist?(wrk_logfile)
    rake_default = Command.new(JSON.parse(IO.read(wrk_logfile)))
    status[:work_logfile] = wrk_logfile
    status["status"] = "0"
    status["status"] = "X" if rake_default[:exit_code] != 0
  end
  make_logfile = "#{@env.root_dir}/log/#{fullname}/#{latest_tag}/#{@env.user}@#{@env.machine}.json"
  if File.exist?(make_logfile)
    rake_default = Command.new(JSON.parse(IO.read(make_logfile)))
    status[:make_logfile] = make_logfile
    status["status"] = "0"
    status["status"] = "X" if rake_default[:exit_code] != 0
  else
    status["status"] = "?"
  end
  FileUtils.mkdir_p(File.dirname(status_logfile)) unless File.exist?(File.dirname(status_logfile))
  File.open(status_logfile, "w") { |f| f.write(status.to_json) }
end

#urlObject


55
56
57
# File 'lib/base/project.rb', line 55

def url
  self[:url]
end

#workObject


288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/base/project.rb', line 288

def work
  clone
  checkout
  logfile = get_logfile ["work"]
  if File.exist?(wrk_dir)
    rake_default = Command.new({ input: "rake default", quiet: true, ignore_failure: true })
    if last_work_mtime.nil? || last_work_mtime < Dir.get_latest_mtime(wrk_dir)
      Dir.chdir(wrk_dir) do
        @env.out fullname

        if !File.exist? "rakefile.rb"
          rake_default[:exit_code] = 1
          rake_default[:error] = "rakefile.rb not found."
          rake_default[:start_time] = Time.now
          rake_default[:end_time] = Time.now
        else
          # rake_default[:timeout] = self[:timeout]
          rake_default.execute
        end
        rake_default.save logfile
        update_status
        @env.out rake_default.summary true
        return rake_default
      end
    elsif File.exist?(logfile)
      rake_default.open logfile
      @env.out rake_default.summary true if rake_default[:exit_code] != 0 || @env.show_success?
    end
    rake_default
  end
end

#work_up_to_date?Boolean

Returns:

  • (Boolean)

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/base/project.rb', line 170

def work_up_to_date?
  if wrk_dir == Rake.application.original_dir
    logfile = get_logfile ["up2date"]
    if File.exist? logfile
      last_work_time = File.mtime(logfile)
      last_file_changed = Dir.get_latest_mtime Rake.application.original_dir
      if last_work_time > last_file_changed
        CLEAN.include logfile
        return true
      else
        puts "   deleting #{logfile}" if @env.debug?
        File.delete(logfile)
      end
    elsif @env.debug?
      puts "logfile #{logfile} does NOT exist."
    end
  elsif @env.debug?
    puts "wrk_dir does not match Rake.application.original_dir"
  end
  false
end

#wrk_dirObject


68
69
70
# File 'lib/base/project.rb', line 68

def wrk_dir
  "#{@env.wrk_dir}/#{fullname}"
end