Class: Raykit::Project

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "", description = "", version = "") ⇒ Project

TODO: refactor to remove the defaults



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/raykit/project.rb', line 24

def initialize(name = "", description = "", version = "")
  @description = description if description.length.positive?
  @version = version if version.length.positive?
  @timeout = 1000 * 60 * 15
  @verbose = false
  @timer = Raykit::Timer.new
  @remote = ""
  @commit_message_filename = "commit_message.tmp"
  if Dir.exist?(RAKE_DIRECTORY)
    @directory = RAKE_DIRECTORY
    if Dir.exist?("#{RAKE_DIRECTORY}/.git") && Dir.exist?(@directory)
      @git_directory = Raykit::Git::Directory.new(@directory)
      @remote = @git_directory.remote
    end
  else
    @directory = ""
  end

  # @log=Log.new("#{RAKE_DIRECTORY}/tmp/raykit.log")
  if (name.length.positive?)
    @name = name
  else
    if defined?(NAME)
      @name = NAME
    else
      slns = Dir.glob("*.sln")
      if slns.length == 1
        @name = slns[0].gsub(".sln", "")
      else
        gemspecs = Dir.glob("*.gemspec")
        if gemspecs.length == 1
          @name = gemspecs[0].gsub(".gemspec", "")
        else
          remote_parts = @remote.split("/")
          @name = remote_parts[-1].gsub(".git", "") if remote_parts.length.positive?
        end
      end
    end
  end

  if version.length.zero? && defined?(VERSION) && VERSION.is_a?(String) && VERSION.length > 0
    @version = VERSION
  else
    @version = Raykit::Version.detect(@name, true) if version.length.zero?
  end

  @repository = Raykit::Git::Repository.new(@remote)
  @values = Hash::new()
  @commands = Array::new()
end

Instance Attribute Details

#description ⇒ Object

Returns the value of attribute description.



9
10
11
# File 'lib/raykit/project.rb', line 9

def description
  @description
end

#directory ⇒ Object (readonly)

Returns the value of attribute directory.



79
80
81
# File 'lib/raykit/project.rb', line 79

def directory
  @directory
end

#log ⇒ Object (readonly)

Returns the value of attribute log.



79
80
81
# File 'lib/raykit/project.rb', line 79

def log
  @log
end

#name ⇒ Object

Returns the value of attribute name.



9
10
11
# File 'lib/raykit/project.rb', line 9

def name
  @name
end

#remote ⇒ Object (readonly)

Returns the value of attribute remote.



79
80
81
# File 'lib/raykit/project.rb', line 79

def remote
  @remote
end

#target ⇒ Object

Returns the value of attribute target.



9
10
11
# File 'lib/raykit/project.rb', line 9

def target
  @target
end

#timer ⇒ Object

Returns the value of attribute timer.



9
10
11
# File 'lib/raykit/project.rb', line 9

def timer
  @timer
end

#verbose ⇒ Object

Returns the value of attribute verbose.



9
10
11
# File 'lib/raykit/project.rb', line 9

def verbose
  @verbose
end

#version ⇒ Object

Returns the value of attribute version.



9
10
11
# File 'lib/raykit/project.rb', line 9

def version
  @version
end

Instance Method Details

#branch ⇒ Object



90
91
92
93
# File 'lib/raykit/project.rb', line 90

def branch
  return "main" if @git_directory.nil?
  @git_directory.branch
end

#commit(commit_message) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/raykit/project.rb', line 262

def commit(commit_message)
  warn "[DEPRECATION] 'commit_message_filename' is deprecated. Use a run command for better transparency."
  Dir.chdir(@directory) do
    if File.exist?(".gitignore")
      status = `git status`
      if status.include?("Changes not staged for commit:")
        run("git add --all")
        if GIT_DIRECTORY.outstanding_commit?
          if File.exist?(@commit_message_filename)
            run("git commit --file #{@commit_message_filename}")
            File.delete(@commit_message_filename)
          else
            run("git commit -m'#{commit_message}'")
          end
        end
      end
    else
      puts "warning: .gitignore not found."
    end
  end
  self
end

#commit_message_filename ⇒ Object



257
258
259
260
# File 'lib/raykit/project.rb', line 257

def commit_message_filename
  warn "[DEPRECATION] 'commit_message_filename' is deprecated."
  @commit_message_filename
end

#detached? ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
# File 'lib/raykit/project.rb', line 128

def detached?
  return true if @git_directory.nil?
  @git_directory.detached?
end

#elapsed ⇒ Object



199
200
201
202
203
204
205
206
# File 'lib/raykit/project.rb', line 199

def elapsed
  elapsed = @timer.elapsed
  if elapsed < 1.0
    "#{format("%.1f", @timer.elapsed)}s"
  else
    "#{format("%.0f", @timer.elapsed)}s"
  end
end

#get_dev_dir(subdir) ⇒ Object



75
76
77
# File 'lib/raykit/project.rb', line 75

def get_dev_dir(subdir)
  Raykit::Environment.get_dev_dir(subdir)
end

#get_markdown_nvp(key, value, pad_char) ⇒ Object



248
249
250
# File 'lib/raykit/project.rb', line 248

def get_markdown_nvp(key, value, pad_char)
  "\n| " + key.to_s.ljust(36, pad_char) + "| " + value.to_s.ljust(36, pad_char)
end

#has_diff?(filename) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
# File 'lib/raykit/project.rb', line 133

def has_diff?(filename)
  Dir.chdir(@directory) do
    text = `git diff #{filename}`.strip
    return true if text.length.positive?
  end
end

#info ⇒ Object



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

def info
  ljust = 35
  puts ""
  puts "PROJECT.name".ljust(ljust) + Rainbow(PROJECT.name).yellow.bright
  puts "PROJECT.version".ljust(ljust) + Rainbow(PROJECT.version).yellow.bright
  puts "PROJECT.remote".ljust(ljust) + Rainbow(PROJECT.remote).yellow.bright
  puts "PROJECT.branch".ljust(ljust) + Rainbow(PROJECT.branch).yellow.bright
  puts "PROJECT.detached?".ljust(ljust) + Rainbow(PROJECT.detached?).yellow.bright
  # puts "PROJECT.target".ljust(ljust) + Rainbow(PROJECT.target).yellow.bright
  # puts "PROJECT.target_md5".ljust(ljust) + Rainbow(PROJECT.target_md5).yellow.bright
  puts "PROJECT.latest_tag".ljust(ljust) + Rainbow(PROJECT.latest_tag).yellow.bright
  puts "PROJECT.latest_tag_commit".ljust(ljust) + Rainbow(PROJECT.latest_tag_commit).yellow.bright
  puts "PROJECT.latest_tag_md5".ljust(ljust) + Rainbow(PROJECT.latest_tag_md5).yellow.bright
  puts "PROJECT.latest_commit".ljust(ljust) + Rainbow(PROJECT.latest_commit).yellow.bright
  puts "PROJECT.last_modified_filename".ljust(ljust) + Rainbow(PROJECT.last_modified_filename).yellow.bright
  # puts "PROJECT.elapsed".ljust(ljust) + Rainbow(PROJECT.elapsed).yellow.bright
  puts "PROJECT.size".ljust(ljust) + Rainbow(PROJECT.size).yellow.bright
  puts "PROJECT.size_pack".ljust(ljust) + Rainbow(PROJECT.size_pack).yellow.bright
  puts "PROJECT.outstanding_commit?".ljust(ljust) + Rainbow(PROJECT.outstanding_commit?).yellow.bright
  puts "PROJECT.outstanding_tag?".ljust(ljust) + Rainbow(PROJECT.outstanding_tag?).yellow.bright
  puts ""
  self
end

#last_modified_filename ⇒ Object



171
172
173
174
175
# File 'lib/raykit/project.rb', line 171

def last_modified_filename
  Dir.chdir(@directory) do
    Dir.glob("**/*.*").select { |f| File.file?(f) }.max_by { |f| File.mtime(f) }
  end
end

#latest_commit ⇒ Object

latest local commit



100
101
102
103
104
105
106
107
# File 'lib/raykit/project.rb', line 100

def latest_commit
  return "" if detached?
  Dir.chdir(@directory) do
    text = `git log -n 1`
    scan = text.scan(/commit (\w+)\s/)
    return scan[0][0].to_s
  end
end

#latest_tag ⇒ Object

def update_version @version=Raykit::Version.detect(@name,@verbose) end



155
156
157
# File 'lib/raykit/project.rb', line 155

def latest_tag
  `git describe  --abbrev=0`.strip
end

#latest_tag_commit ⇒ Object



159
160
161
# File 'lib/raykit/project.rb', line 159

def latest_tag_commit
  `git show-ref -s #{latest_tag}`.strip
end

#latest_tag_md5 ⇒ Object



163
164
165
166
167
168
169
# File 'lib/raykit/project.rb', line 163

def latest_tag_md5
  text = `git tag #{latest_tag} -n3`
  scan = text.scan(/md5=(\w{32})/)
  return scan[0][0].to_s.strip if scan.length.positive? && scan[0].length.positive?

  ""
end

#outstanding_commit? ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
# File 'lib/raykit/project.rb', line 109

def outstanding_commit?
  Dir.chdir(@directory) do
    return true unless `git status`.include?("nothing to commit")
  end
  false
end

#outstanding_tag? ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
# File 'lib/raykit/project.rb', line 116

def outstanding_tag?
  # puts `git log -n 1`
  # !latest_tag_commit.eql?(latest_commit)
  # commit 2e4cb6d6c0721e16cd06afee85b7cdc27354921b (HEAD -> master, tag: 0.0.180, origin/master, origin/HEAD)
  outstanding_commit? || !`git log -n 1`.include?("tag:")
end

#pull ⇒ Object



295
296
297
298
299
300
# File 'lib/raykit/project.rb', line 295

def pull
  Dir.chdir(@directory) do
    run("git pull")
  end
  self
end

#push ⇒ Object



285
286
287
288
289
290
291
292
293
# File 'lib/raykit/project.rb', line 285

def push
  Dir.chdir(@directory) do
    status = `git status`
    if status.include?('use "git push"')
      run("git push")
    end
  end
  self
end

#read_only? ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
# File 'lib/raykit/project.rb', line 123

def read_only?
  return true if !File.exist?(".git") || detached?
  return false
end

#run(command, quit_on_failure = true) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/raykit/project.rb', line 324

def run(command, quit_on_failure = true)
  if command.is_a?(Array)
    command.each { |subcommand| run(subcommand, quit_on_failure) }
  else
    cmd = Command.new(command).set_timeout(@timeout).run
    cmd.summary
    elapsed_str = Timer.get_elapsed_str(cmd.elapsed, 0)
    if !cmd.exitstatus.nil? && cmd.exitstatus.zero?
    else
      # display error details
      cmd.details
      if quit_on_failure
        abort
      end
    end
    cmd.save
    @commands << cmd
    cmd
  end
end

#set_version(version) ⇒ Object

def version if @version.nil? @version = Raykit::Version.detect(@name, @verbose) end @version end



147
148
149
# File 'lib/raykit/project.rb', line 147

def set_version(version)
  @version = version
end

#size ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/raykit/project.rb', line 177

def size
  Dir.chdir(@directory) do
    text = `git count-objects -v -H`
    if matches = text.match(/size: ([. \w]+)$/)
      matches[1]
    else
      text
    end
  end
end

#size_pack ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/raykit/project.rb', line 188

def size_pack
  Dir.chdir(@directory) do
    text = `git count-objects -v -H`
    if matches = text.match(/size-pack: ([. \w]+)$/)
      matches[1]
    else
      text
    end
  end
end

#summary ⇒ Object



252
253
254
255
# File 'lib/raykit/project.rb', line 252

def summary
  info if @verbose
  puts "[#{elapsed}] #{Rainbow(@name).yellow.bright} #{Rainbow(version).yellow.bright}"
end

#tag ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/raykit/project.rb', line 302

def tag
  warn "[DEPRECATION] 'tag' is deprecated. Use run command(s) for better transparency."
  Dir.chdir(@directory) do
    specific_tag = `git tag -l #{@version}`.strip
    puts Rainbow("git tag - #{@version}").green if @verbose
    puts `git tag -l #{@version}` if @verbose
    if specific_tag.length.zero?
      puts "tag #{@version} not detected." if @verbose
      tag = `git describe --abbrev=0 --tags`.strip
      if @version != tag
        puts "latest tag #{@tag}" if @verbose
        run("git tag #{@version} -m'#{@version}'")
        run("git push origin #{@version}")
        # push
      end
    elsif @verbose
      puts "already has tag #{specific_tag}"
    end
  end
  self
end

#target_md5 ⇒ Object

def target @target end



84
85
86
87
88
# File 'lib/raykit/project.rb', line 84

def target_md5
  return Digest::MD5.file(target).to_s.strip if !target.nil? && File.exist?(target)

  ""
end

#to_markdown ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/raykit/project.rb', line 232

def to_markdown()
  md = "# #{@name}"
  md += get_markdown_nvp("Name", "Value", " ")
  md += get_markdown_nvp("-", "-", "-")
  md += get_markdown_nvp("Version", "#{@version}", " ")
  md += get_markdown_nvp("Machine", "#{Raykit::Environment::machine}", " ")
  md += get_markdown_nvp("User", "#{Raykit::Environment::user}", " ")
  @values.each do |key, value|
    md += get_markdown_nvp(key, value, " ")
  end
  @commands.each do |cmd|
    md = md + "\n" + cmd.to_markdown
  end
  md
end

#values ⇒ Object



95
96
97
# File 'lib/raykit/project.rb', line 95

def values
  @values
end