raykit 0.0.470 → 0.0.471

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