raykit 0.0.466 → 0.0.467

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,339 +1,345 @@
1
- # frozen_string_literal: true
2
-
3
- require "rake/clean"
4
- require "digest"
5
- RAKE_DIRECTORY = Rake.application.original_dir
6
-
7
- module Raykit
8
- class Project
9
- attr_accessor :name, :timer, :verbose, :target
10
-
11
- @directory
12
- @version
13
- @remote
14
- @repository
15
- @git_directory
16
- @timeout
17
- @values
18
- @commands
19
-
20
- # @log
21
- # @commit_message_filename
22
-
23
- def initialize
24
- @timeout = 1000 * 60 * 15
25
- @verbose = false
26
- @timer = Raykit::Timer.new
27
- @remote = ""
28
- @commit_message_filename = "commit_message.tmp"
29
- if Dir.exist?(RAKE_DIRECTORY)
30
- @directory = RAKE_DIRECTORY
31
- if Dir.exist?("#{RAKE_DIRECTORY}/.git") && Dir.exist?(@directory)
32
- @git_directory = Raykit::Git::Directory.new(@directory)
33
- @remote = @git_directory.remote
34
- end
35
- else
36
- @directory = ""
37
- end
38
-
39
- # @log=Log.new("#{RAKE_DIRECTORY}/tmp/raykit.log")
40
-
41
- if defined?(NAME)
42
- @name = NAME
43
- else
44
- slns = Dir.glob("*.sln")
45
- if slns.length == 1
46
- @name = slns[0].gsub(".sln", "")
47
- else
48
- gemspecs = Dir.glob("*.gemspec")
49
- if gemspecs.length == 1
50
- @name = gemspecs[0].gsub(".gemspec", "")
51
- else
52
- remote_parts = @remote.split("/")
53
- @name = remote_parts[-1].gsub(".git", "") if remote_parts.length.positive?
54
- end
55
- end
56
- end
57
- @repository = Raykit::Git::Repository.new(@remote)
58
- @values = Hash::new()
59
- @commands = Array::new()
60
- end
61
-
62
- def get_dev_dir(subdir)
63
- Raykit::Environment.get_dev_dir(subdir)
64
- end
65
-
66
- attr_reader :log, :directory, :remote
67
-
68
- # def target
69
- # @target
70
- # end
71
- def target_md5
72
- return Digest::MD5.file(target).to_s.strip if !target.nil? && File.exist?(target)
73
-
74
- ""
75
- end
76
-
77
- def branch
78
- return "main" if @git_directory.nil?
79
- @git_directory.branch
80
- end
81
-
82
- def values
83
- @values
84
- end
85
-
86
- # latest local commit
87
- def latest_commit
88
- return "" if detached?
89
- Dir.chdir(@directory) do
90
- text = `git log -n 1`
91
- scan = text.scan(/commit (\w+)\s/)
92
- return scan[0][0].to_s
93
- end
94
- end
95
-
96
- def outstanding_commit?
97
- Dir.chdir(@directory) do
98
- return true unless `git status`.include?("nothing to commit")
99
- end
100
- false
101
- end
102
-
103
- def outstanding_tag?
104
- # puts `git log -n 1`
105
- # !latest_tag_commit.eql?(latest_commit)
106
- # commit 2e4cb6d6c0721e16cd06afee85b7cdc27354921b (HEAD -> master, tag: 0.0.180, origin/master, origin/HEAD)
107
- outstanding_commit? || !`git log -n 1`.include?("tag:")
108
- end
109
-
110
- def read_only?
111
- return true if !File.exist?(".git") || detached?
112
- return false
113
- end
114
-
115
- def detached?
116
- return true if @git_directory.nil?
117
- @git_directory.detached?
118
- end
119
-
120
- def has_diff?(filename)
121
- Dir.chdir(@directory) do
122
- text = `git diff #{filename}`.strip
123
- return true if text.length.positive?
124
- end
125
- end
126
-
127
- def version
128
- if @version.nil?
129
- # Dir.chdir(@directory) do
130
- # if(Dir.glob("*.gemspec").length > 0)
131
- # @version=`bump current`.gsub('Current version:','').strip
132
- # else
133
- # @version=Raykit::VersionHelper.detect(@name,@verbose)
134
- # end
135
- @version = Raykit::Version.detect(@name, @verbose)
136
- # end
137
- end
138
- @version
139
- end
140
-
141
- def set_version(version)
142
- @version = version
143
- end
144
-
145
- # def update_version
146
- # @version=Raykit::Version.detect(@name,@verbose)
147
- # end
148
-
149
- def latest_tag
150
- `git describe --abbrev=0`.strip
151
- end
152
-
153
- def latest_tag_commit
154
- `git show-ref -s #{latest_tag}`.strip
155
- end
156
-
157
- def latest_tag_md5
158
- text = `git tag #{latest_tag} -n3`
159
- scan = text.scan(/md5=(\w{32})/)
160
- return scan[0][0].to_s.strip if scan.length.positive? && scan[0].length.positive?
161
-
162
- ""
163
- end
164
-
165
- def last_modified_filename
166
- Dir.chdir(@directory) do
167
- Dir.glob("**/*.*").select { |f| File.file?(f) }.max_by { |f| File.mtime(f) }
168
- end
169
- end
170
-
171
- def size
172
- Dir.chdir(@directory) do
173
- text = `git count-objects -v -H`
174
- if matches = text.match(/size: ([. \w]+)$/)
175
- matches[1]
176
- else
177
- text
178
- end
179
- end
180
- end
181
-
182
- def size_pack
183
- Dir.chdir(@directory) do
184
- text = `git count-objects -v -H`
185
- if matches = text.match(/size-pack: ([. \w]+)$/)
186
- matches[1]
187
- else
188
- text
189
- end
190
- end
191
- end
192
-
193
- def elapsed
194
- elapsed = @timer.elapsed
195
- if elapsed < 1.0
196
- "#{format("%.1f", @timer.elapsed)}s"
197
- else
198
- "#{format("%.0f", @timer.elapsed)}s"
199
- end
200
- end
201
-
202
- def info
203
- ljust = 35
204
- puts ""
205
- puts "PROJECT.name".ljust(ljust) + Rainbow(PROJECT.name).yellow.bright
206
- puts "PROJECT.version".ljust(ljust) + Rainbow(PROJECT.version).yellow.bright
207
- puts "PROJECT.remote".ljust(ljust) + Rainbow(PROJECT.remote).yellow.bright
208
- puts "PROJECT.branch".ljust(ljust) + Rainbow(PROJECT.branch).yellow.bright
209
- puts "PROJECT.detached?".ljust(ljust) + Rainbow(PROJECT.detached?).yellow.bright
210
- # puts "PROJECT.target".ljust(ljust) + Rainbow(PROJECT.target).yellow.bright
211
- # puts "PROJECT.target_md5".ljust(ljust) + Rainbow(PROJECT.target_md5).yellow.bright
212
- puts "PROJECT.latest_tag".ljust(ljust) + Rainbow(PROJECT.latest_tag).yellow.bright
213
- puts "PROJECT.latest_tag_commit".ljust(ljust) + Rainbow(PROJECT.latest_tag_commit).yellow.bright
214
- puts "PROJECT.latest_tag_md5".ljust(ljust) + Rainbow(PROJECT.latest_tag_md5).yellow.bright
215
- puts "PROJECT.latest_commit".ljust(ljust) + Rainbow(PROJECT.latest_commit).yellow.bright
216
- puts "PROJECT.last_modified_filename".ljust(ljust) + Rainbow(PROJECT.last_modified_filename).yellow.bright
217
- # puts "PROJECT.elapsed".ljust(ljust) + Rainbow(PROJECT.elapsed).yellow.bright
218
- puts "PROJECT.size".ljust(ljust) + Rainbow(PROJECT.size).yellow.bright
219
- puts "PROJECT.size_pack".ljust(ljust) + Rainbow(PROJECT.size_pack).yellow.bright
220
- puts "PROJECT.outstanding_commit?".ljust(ljust) + Rainbow(PROJECT.outstanding_commit?).yellow.bright
221
- puts "PROJECT.outstanding_tag?".ljust(ljust) + Rainbow(PROJECT.outstanding_tag?).yellow.bright
222
- puts ""
223
- self
224
- end
225
-
226
- def to_markdown()
227
- md = "# #{@name}"
228
- md += get_markdown_nvp("Name", "Value", " ")
229
- md += get_markdown_nvp("-", "-", "-")
230
- md += get_markdown_nvp("Version", "#{@version}", " ")
231
- md += get_markdown_nvp("Machine", "#{Raykit::Environment::machine}", " ")
232
- md += get_markdown_nvp("User", "#{Raykit::Environment::user}", " ")
233
- @values.each do |key, value|
234
- md += get_markdown_nvp(key, value, " ")
235
- end
236
- @commands.each do |cmd|
237
- md = md + "\n" + cmd.to_markdown
238
- end
239
- md
240
- end
241
-
242
- def get_markdown_nvp(key, value, pad_char)
243
- "\n| " + key.to_s.ljust(36, pad_char) + "| " + value.to_s.ljust(36, pad_char)
244
- end
245
-
246
- def summary
247
- info if @verbose
248
- puts "[#{elapsed}] #{Rainbow(@name).yellow.bright} #{Rainbow(version).yellow.bright}"
249
- end
250
-
251
- def commit_message_filename
252
- warn "[DEPRECATION] 'commit_message_filename' is deprecated."
253
- @commit_message_filename
254
- end
255
-
256
- def commit(commit_message)
257
- warn "[DEPRECATION] 'commit_message_filename' is deprecated. Use a run command for better transparency."
258
- Dir.chdir(@directory) do
259
- if File.exist?(".gitignore")
260
- status = `git status`
261
- if status.include?("Changes not staged for commit:")
262
- run("git add --all")
263
- if GIT_DIRECTORY.outstanding_commit?
264
- if File.exist?(@commit_message_filename)
265
- run("git commit --file #{@commit_message_filename}")
266
- File.delete(@commit_message_filename)
267
- else
268
- run("git commit -m'#{commit_message}'")
269
- end
270
- end
271
- end
272
- else
273
- puts "warning: .gitignore not found."
274
- end
275
- end
276
- self
277
- end
278
-
279
- def push
280
- Dir.chdir(@directory) do
281
- status = `git status`
282
- if status.include?('use "git push"')
283
- run("git push")
284
- end
285
- end
286
- self
287
- end
288
-
289
- def pull
290
- Dir.chdir(@directory) do
291
- run("git pull")
292
- end
293
- self
294
- end
295
-
296
- def tag
297
- warn "[DEPRECATION] 'tag' is deprecated. Use run command(s) for better transparency."
298
- Dir.chdir(@directory) do
299
- specific_tag = `git tag -l #{@version}`.strip
300
- puts Rainbow("git tag - #{@version}").green if @verbose
301
- puts `git tag -l #{@version}` if @verbose
302
- if specific_tag.length.zero?
303
- puts "tag #{@version} not detected." if @verbose
304
- tag = `git describe --abbrev=0 --tags`.strip
305
- if @version != tag
306
- puts "latest tag #{@tag}" if @verbose
307
- run("git tag #{@version} -m'#{@version}'")
308
- run("git push origin #{@version}")
309
- # push
310
- end
311
- elsif @verbose
312
- puts "already has tag #{specific_tag}"
313
- end
314
- end
315
- self
316
- end
317
-
318
- def run(command, quit_on_failure = true)
319
- if command.is_a?(Array)
320
- command.each { |subcommand| run(subcommand, quit_on_failure) }
321
- else
322
- cmd = Command.new(command).set_timeout(@timeout).run
323
- cmd.summary
324
- elapsed_str = Timer.get_elapsed_str(cmd.elapsed, 0)
325
- if !cmd.exitstatus.nil? && cmd.exitstatus.zero?
326
- else
327
- # display error details
328
- cmd.details
329
- if quit_on_failure
330
- abort
331
- end
332
- end
333
- cmd.save
334
- @commands << cmd
335
- cmd
336
- end
337
- end
338
- end
339
- end
1
+ # frozen_string_literal: true
2
+
3
+ require "rake/clean"
4
+ require "digest"
5
+ RAKE_DIRECTORY = Rake.application.original_dir
6
+
7
+ module Raykit
8
+ class Project
9
+ attr_accessor :name, :description, :timer, :verbose, :target
10
+
11
+ @directory
12
+ @version
13
+ @remote
14
+ @repository
15
+ @git_directory
16
+ @timeout
17
+ @values
18
+ @commands
19
+
20
+ # @log
21
+ # @commit_message_filename
22
+
23
+ # TODO: refactor to remove the defaults
24
+ def initialize(name = "", description = "", version = "")
25
+ @description = description if description.length.positive?
26
+ @version = version if version.length.positive?
27
+ @timeout = 1000 * 60 * 15
28
+ @verbose = false
29
+ @timer = Raykit::Timer.new
30
+ @remote = ""
31
+ @commit_message_filename = "commit_message.tmp"
32
+ if Dir.exist?(RAKE_DIRECTORY)
33
+ @directory = RAKE_DIRECTORY
34
+ if Dir.exist?("#{RAKE_DIRECTORY}/.git") && Dir.exist?(@directory)
35
+ @git_directory = Raykit::Git::Directory.new(@directory)
36
+ @remote = @git_directory.remote
37
+ end
38
+ else
39
+ @directory = ""
40
+ end
41
+
42
+ # @log=Log.new("#{RAKE_DIRECTORY}/tmp/raykit.log")
43
+ if (name.length.positive?)
44
+ @name = name
45
+ else
46
+ if defined?(NAME)
47
+ @name = NAME
48
+ else
49
+ slns = Dir.glob("*.sln")
50
+ if slns.length == 1
51
+ @name = slns[0].gsub(".sln", "")
52
+ else
53
+ gemspecs = Dir.glob("*.gemspec")
54
+ if gemspecs.length == 1
55
+ @name = gemspecs[0].gsub(".gemspec", "")
56
+ else
57
+ remote_parts = @remote.split("/")
58
+ @name = remote_parts[-1].gsub(".git", "") if remote_parts.length.positive?
59
+ end
60
+ end
61
+ end
62
+ end
63
+ @repository = Raykit::Git::Repository.new(@remote)
64
+ @values = Hash::new()
65
+ @commands = Array::new()
66
+ end
67
+
68
+ def get_dev_dir(subdir)
69
+ Raykit::Environment.get_dev_dir(subdir)
70
+ end
71
+
72
+ attr_reader :log, :directory, :remote
73
+
74
+ # def target
75
+ # @target
76
+ # end
77
+ def target_md5
78
+ return Digest::MD5.file(target).to_s.strip if !target.nil? && File.exist?(target)
79
+
80
+ ""
81
+ end
82
+
83
+ def branch
84
+ return "main" if @git_directory.nil?
85
+ @git_directory.branch
86
+ end
87
+
88
+ def values
89
+ @values
90
+ end
91
+
92
+ # latest local commit
93
+ def latest_commit
94
+ return "" if detached?
95
+ Dir.chdir(@directory) do
96
+ text = `git log -n 1`
97
+ scan = text.scan(/commit (\w+)\s/)
98
+ return scan[0][0].to_s
99
+ end
100
+ end
101
+
102
+ def outstanding_commit?
103
+ Dir.chdir(@directory) do
104
+ return true unless `git status`.include?("nothing to commit")
105
+ end
106
+ false
107
+ end
108
+
109
+ def outstanding_tag?
110
+ # puts `git log -n 1`
111
+ # !latest_tag_commit.eql?(latest_commit)
112
+ # commit 2e4cb6d6c0721e16cd06afee85b7cdc27354921b (HEAD -> master, tag: 0.0.180, origin/master, origin/HEAD)
113
+ outstanding_commit? || !`git log -n 1`.include?("tag:")
114
+ end
115
+
116
+ def read_only?
117
+ return true if !File.exist?(".git") || detached?
118
+ return false
119
+ end
120
+
121
+ def detached?
122
+ return true if @git_directory.nil?
123
+ @git_directory.detached?
124
+ end
125
+
126
+ def has_diff?(filename)
127
+ Dir.chdir(@directory) do
128
+ text = `git diff #{filename}`.strip
129
+ return true if text.length.positive?
130
+ end
131
+ end
132
+
133
+ def version
134
+ if @version.nil?
135
+ # Dir.chdir(@directory) do
136
+ # if(Dir.glob("*.gemspec").length > 0)
137
+ # @version=`bump current`.gsub('Current version:','').strip
138
+ # else
139
+ # @version=Raykit::VersionHelper.detect(@name,@verbose)
140
+ # end
141
+ @version = Raykit::Version.detect(@name, @verbose)
142
+ # end
143
+ end
144
+ @version
145
+ end
146
+
147
+ def set_version(version)
148
+ @version = version
149
+ end
150
+
151
+ # def update_version
152
+ # @version=Raykit::Version.detect(@name,@verbose)
153
+ # end
154
+
155
+ def latest_tag
156
+ `git describe --abbrev=0`.strip
157
+ end
158
+
159
+ def latest_tag_commit
160
+ `git show-ref -s #{latest_tag}`.strip
161
+ end
162
+
163
+ def latest_tag_md5
164
+ text = `git tag #{latest_tag} -n3`
165
+ scan = text.scan(/md5=(\w{32})/)
166
+ return scan[0][0].to_s.strip if scan.length.positive? && scan[0].length.positive?
167
+
168
+ ""
169
+ end
170
+
171
+ def last_modified_filename
172
+ Dir.chdir(@directory) do
173
+ Dir.glob("**/*.*").select { |f| File.file?(f) }.max_by { |f| File.mtime(f) }
174
+ end
175
+ end
176
+
177
+ def size
178
+ Dir.chdir(@directory) do
179
+ text = `git count-objects -v -H`
180
+ if matches = text.match(/size: ([. \w]+)$/)
181
+ matches[1]
182
+ else
183
+ text
184
+ end
185
+ end
186
+ end
187
+
188
+ def size_pack
189
+ Dir.chdir(@directory) do
190
+ text = `git count-objects -v -H`
191
+ if matches = text.match(/size-pack: ([. \w]+)$/)
192
+ matches[1]
193
+ else
194
+ text
195
+ end
196
+ end
197
+ end
198
+
199
+ def elapsed
200
+ elapsed = @timer.elapsed
201
+ if elapsed < 1.0
202
+ "#{format("%.1f", @timer.elapsed)}s"
203
+ else
204
+ "#{format("%.0f", @timer.elapsed)}s"
205
+ end
206
+ end
207
+
208
+ def info
209
+ ljust = 35
210
+ puts ""
211
+ puts "PROJECT.name".ljust(ljust) + Rainbow(PROJECT.name).yellow.bright
212
+ puts "PROJECT.version".ljust(ljust) + Rainbow(PROJECT.version).yellow.bright
213
+ puts "PROJECT.remote".ljust(ljust) + Rainbow(PROJECT.remote).yellow.bright
214
+ puts "PROJECT.branch".ljust(ljust) + Rainbow(PROJECT.branch).yellow.bright
215
+ puts "PROJECT.detached?".ljust(ljust) + Rainbow(PROJECT.detached?).yellow.bright
216
+ # puts "PROJECT.target".ljust(ljust) + Rainbow(PROJECT.target).yellow.bright
217
+ # puts "PROJECT.target_md5".ljust(ljust) + Rainbow(PROJECT.target_md5).yellow.bright
218
+ puts "PROJECT.latest_tag".ljust(ljust) + Rainbow(PROJECT.latest_tag).yellow.bright
219
+ puts "PROJECT.latest_tag_commit".ljust(ljust) + Rainbow(PROJECT.latest_tag_commit).yellow.bright
220
+ puts "PROJECT.latest_tag_md5".ljust(ljust) + Rainbow(PROJECT.latest_tag_md5).yellow.bright
221
+ puts "PROJECT.latest_commit".ljust(ljust) + Rainbow(PROJECT.latest_commit).yellow.bright
222
+ puts "PROJECT.last_modified_filename".ljust(ljust) + Rainbow(PROJECT.last_modified_filename).yellow.bright
223
+ # puts "PROJECT.elapsed".ljust(ljust) + Rainbow(PROJECT.elapsed).yellow.bright
224
+ puts "PROJECT.size".ljust(ljust) + Rainbow(PROJECT.size).yellow.bright
225
+ puts "PROJECT.size_pack".ljust(ljust) + Rainbow(PROJECT.size_pack).yellow.bright
226
+ puts "PROJECT.outstanding_commit?".ljust(ljust) + Rainbow(PROJECT.outstanding_commit?).yellow.bright
227
+ puts "PROJECT.outstanding_tag?".ljust(ljust) + Rainbow(PROJECT.outstanding_tag?).yellow.bright
228
+ puts ""
229
+ self
230
+ end
231
+
232
+ def to_markdown()
233
+ md = "# #{@name}"
234
+ md += get_markdown_nvp("Name", "Value", " ")
235
+ md += get_markdown_nvp("-", "-", "-")
236
+ md += get_markdown_nvp("Version", "#{@version}", " ")
237
+ md += get_markdown_nvp("Machine", "#{Raykit::Environment::machine}", " ")
238
+ md += get_markdown_nvp("User", "#{Raykit::Environment::user}", " ")
239
+ @values.each do |key, value|
240
+ md += get_markdown_nvp(key, value, " ")
241
+ end
242
+ @commands.each do |cmd|
243
+ md = md + "\n" + cmd.to_markdown
244
+ end
245
+ md
246
+ end
247
+
248
+ def get_markdown_nvp(key, value, pad_char)
249
+ "\n| " + key.to_s.ljust(36, pad_char) + "| " + value.to_s.ljust(36, pad_char)
250
+ end
251
+
252
+ def summary
253
+ info if @verbose
254
+ puts "[#{elapsed}] #{Rainbow(@name).yellow.bright} #{Rainbow(version).yellow.bright}"
255
+ end
256
+
257
+ def commit_message_filename
258
+ warn "[DEPRECATION] 'commit_message_filename' is deprecated."
259
+ @commit_message_filename
260
+ end
261
+
262
+ def commit(commit_message)
263
+ warn "[DEPRECATION] 'commit_message_filename' is deprecated. Use a run command for better transparency."
264
+ Dir.chdir(@directory) do
265
+ if File.exist?(".gitignore")
266
+ status = `git status`
267
+ if status.include?("Changes not staged for commit:")
268
+ run("git add --all")
269
+ if GIT_DIRECTORY.outstanding_commit?
270
+ if File.exist?(@commit_message_filename)
271
+ run("git commit --file #{@commit_message_filename}")
272
+ File.delete(@commit_message_filename)
273
+ else
274
+ run("git commit -m'#{commit_message}'")
275
+ end
276
+ end
277
+ end
278
+ else
279
+ puts "warning: .gitignore not found."
280
+ end
281
+ end
282
+ self
283
+ end
284
+
285
+ def push
286
+ Dir.chdir(@directory) do
287
+ status = `git status`
288
+ if status.include?('use "git push"')
289
+ run("git push")
290
+ end
291
+ end
292
+ self
293
+ end
294
+
295
+ def pull
296
+ Dir.chdir(@directory) do
297
+ run("git pull")
298
+ end
299
+ self
300
+ end
301
+
302
+ def tag
303
+ warn "[DEPRECATION] 'tag' is deprecated. Use run command(s) for better transparency."
304
+ Dir.chdir(@directory) do
305
+ specific_tag = `git tag -l #{@version}`.strip
306
+ puts Rainbow("git tag - #{@version}").green if @verbose
307
+ puts `git tag -l #{@version}` if @verbose
308
+ if specific_tag.length.zero?
309
+ puts "tag #{@version} not detected." if @verbose
310
+ tag = `git describe --abbrev=0 --tags`.strip
311
+ if @version != tag
312
+ puts "latest tag #{@tag}" if @verbose
313
+ run("git tag #{@version} -m'#{@version}'")
314
+ run("git push origin #{@version}")
315
+ # push
316
+ end
317
+ elsif @verbose
318
+ puts "already has tag #{specific_tag}"
319
+ end
320
+ end
321
+ self
322
+ end
323
+
324
+ def run(command, quit_on_failure = true)
325
+ if command.is_a?(Array)
326
+ command.each { |subcommand| run(subcommand, quit_on_failure) }
327
+ else
328
+ cmd = Command.new(command).set_timeout(@timeout).run
329
+ cmd.summary
330
+ elapsed_str = Timer.get_elapsed_str(cmd.elapsed, 0)
331
+ if !cmd.exitstatus.nil? && cmd.exitstatus.zero?
332
+ else
333
+ # display error details
334
+ cmd.details
335
+ if quit_on_failure
336
+ abort
337
+ end
338
+ end
339
+ cmd.save
340
+ @commands << cmd
341
+ cmd
342
+ end
343
+ end
344
+ end
345
+ end