makit 0.0.21 → 0.0.22

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a0c2a2e3b37f4f156c9a9f73c42659a6ff5a07cc9589ee2653f6bb3ce632599
4
- data.tar.gz: c5c3e7980e0a50544295b262ac60d7032e781e86395c27de65e0bd61eea217a6
3
+ metadata.gz: d20004d1c96605bb23375807ee5541a266909981ad0c81893c719a2c7574c5fb
4
+ data.tar.gz: 467f47e68f11b74a165d2a26c21c69edfc75fc37a6dc183ae6045005717afeec
5
5
  SHA512:
6
- metadata.gz: 51df9a2a2a61614f1aa0ecd84d9c038398f448f4676bd00a3895c58498863b4d03452d81160d78c46ed54a01762a78aa878055a1d840bae717c49b9559128e72
7
- data.tar.gz: 5095904bc74245d33e697630b87bf91d8f0117505ae25bf1c57624dbfe28a412eee4df708da9c821a7f6a21e6715b248341d20a3bfce69af5e387c721f64ce3b
6
+ metadata.gz: 91c8cbbf9bf948fb47f7acbb7edde9048ba630c29ac6c28c799cca4fbda8c5cf2dc0e18b0368b9085bc238ce76a9c545deb7fa83c3dbd846a4c0b4c7cecceea0
7
+ data.tar.gz: e4f4153782e42b0d0af6bcfff17694a05ec20c41978ee272860ea0519bcb989b5a8947beb2ea34aef432dce377212b258c80d2c1aa5b98e387b83304e190a300
@@ -3,6 +3,7 @@ require "open3"
3
3
  require "socket"
4
4
  require "etc"
5
5
  require "logger"
6
+ require_relative "mp/command_request.mp"
6
7
 
7
8
  # This module provides classes for the Makit gem.
8
9
  module Makit
@@ -10,21 +11,18 @@ module Makit
10
11
  #
11
12
  class CommandRunner
12
13
  attr_accessor :show_output_on_success, :log_to_artifacts, :commands
14
+ attr_accessor :debug
13
15
 
14
16
  def initialize
15
17
  @show_output_on_success = false
16
- @log_to_artifacts = true
18
+ @log_to_artifacts = false
17
19
  @commands = []
20
+ @debug = false
18
21
  end
19
22
 
20
23
  def get_cache_filename(command)
21
- # test if the command_request is a Makit::V1::CommandRequest
22
- #if command_request.is_a? Makit::V1::CommandRequest || command_request.is_a? Makit::V1::Command
23
- # also replacing any path delimiters with an underscore
24
- int_hash = Digest::SHA256.hexdigest("{command.name}.#{command.arguments.join(" ")}")
25
- # int_hash
26
- #int_hash = command_request.to_hash
27
- hash_string = "#{int_hash}"[0, 8]
24
+ int_hash = Digest::SHA256.hexdigest("#{command.name}.#{command.arguments.join(" ")}")
25
+ hash_string = "#{int_hash}"
28
26
  cache_filename = Makit::Directories::PROJECT_ARTIFACTS +
29
27
  "/commands/cache/#{hash_string}.pb"
30
28
  # create the directory if it does not already exist
@@ -37,31 +35,35 @@ module Makit
37
35
  # otherwise run the command and save the result to a cache file
38
36
  # then return the result
39
37
  def cache_run(command_request, timestamp)
40
- # combine the command name and arguments into a single string
41
- # and use it to create a cache filename, making sure it is a valid filename,
42
- # by replacing all characters that are not valid in a filename with an underscore
43
- # also replacing any path delimiters with an underscore
44
- int_hash = command_request.to_hash
45
- hash_string = "#{int_hash}"[0, 8]
38
+ raise "Invalid command_request" unless command_request.is_a? Makit::V1::CommandRequest
46
39
  cache_filename = get_cache_filename(command_request)
47
-
48
- #Makit::Directories::PROJECT_ARTIFACTS +
49
- # "/commands/#{hash_string}.pb"
50
- #puts "cache_filename: #{cache_filename}"
51
40
 
41
+ if debug
42
+ puts " timestamp: #{Makit::Humanize.get_humanized_timestamp(timestamp)}"
43
+ puts " command.name: #{command_request.name}"
44
+ puts " command.arguments: #{command_request.arguments.join(" ")}"
45
+ puts " cache_filename: #{cache_filename}"
46
+ end
52
47
  #cache_filename = Makit::Directories::PROJECT_ARTIFACTS + "/commands/#{command_request.name}.#{command_request.arguments.join("_")}.#{timestamp.seconds}.pb"
53
48
  if File.exist?(cache_filename)
49
+ cache_timestamp = File.mtime(cache_filename)
50
+ if debug
51
+ puts " cache timestamp: #{Makit::Humanize.get_humanized_timestamp(cache_timestamp)}"
52
+ end
54
53
  #puts "cache file date: #{File.mtime(cache_filename)}"
55
54
  if (File.mtime(cache_filename) > timestamp)
56
- puts " found cached command (newer than #{timestamp})".colorize(:grey)
55
+
56
+ #puts " found cached command (newer than #{timestamp})".colorize(:grey)
57
+ #puts " cache_filename: #{cache_filename}"
57
58
  command = Makit::Serializer.open(cache_filename, Makit::V1::Command)
58
- show_command(command)
59
+ show_cached_command(command)
59
60
  if command.exit_code != 0
60
61
  abort "cached command failed: #{command.name} #{command.arguments.join(" ")}"
61
62
  end
62
- return command #Makit::Serializer.open(cache_filename, Makit::V1::Command)
63
+ return command
63
64
  else
64
- #puts "cache_filename exists, but is older than #{timestamp}"
65
+ puts " cache_filename exists, but is older than #{timestamp}" if debug
66
+ File.delete(cache_filename)
65
67
  end
66
68
  end
67
69
 
@@ -88,6 +90,20 @@ module Makit
88
90
  end
89
91
  end
90
92
 
93
+ def show_cached_command(command)
94
+ if command.exit_code != 0
95
+ puts Makit::CommandRunner.get_command_summary(command) + " (exit code #{command.exit_code})".colorize(:default)
96
+ puts " directory: #{command.directory}\n"
97
+ puts " duration: #{command.duration.seconds} seconds\n"
98
+ puts Makit::Humanize::indent_string(command.output, 2) if command.output.length > 0
99
+ puts Makit::Humanize::indent_string(command.error, 2) if command.error.length > 0
100
+ #exit 1 if command.exit_on_error
101
+ else
102
+ puts Makit::CommandRunner.get_command_summary(command).strip_color_codes.colorize(:grey) + " (#{command.duration.seconds} seconds)".colorize(:grey)
103
+ puts Makit::Humanize::indent_string(command.output, 2).colorize(:default) if show_output_on_success
104
+ end
105
+ end
106
+
91
107
  # Run a command and return a Makit::V1::Command.
92
108
  def run(command_request)
93
109
  raise "Invalid command_request" unless command_request.is_a? Makit::V1::CommandRequest
@@ -97,33 +113,16 @@ module Makit
97
113
 
98
114
  log_to_artifacts(command) if @log_to_artifacts
99
115
  show_command(command)
100
- #exit 1 if exit_on_error & command.exit_code != 1
101
- #if command.exit_code != 0
102
- # puts Makit::CommandRunner.get_command_summary(command) + " (exit code #{command.exit_code})".colorize(:default)
103
- # puts " directory: #{command.directory}\n"
104
- # puts " duration: #{command.duration.seconds} seconds\n"
105
- # puts Makit::Humanize::indent_string(command.output, 2) if command.output.length > 0
106
- # puts Makit::Humanize::indent_string(command.error, 2) if command.error.length > 0
107
- # exit 1 if command_request.exit_on_error
108
- #else
109
- # puts Makit::CommandRunner.get_command_summary(command) + " (#{command.duration.seconds} seconds)".colorize(:cyan)
110
- # puts Makit::Humanize::indent_string(command.output, 2).colorize(:default) if show_output_on_success
111
- #end
112
-
116
+ if command.exit_code != 0
117
+ exit 1 if command_request.exit_on_error
118
+ end
113
119
  commands.push(command)
114
120
  command
115
121
  end
116
122
 
117
123
  def log_to_artifacts(command)
118
- #dir = File.join(Makit::Directories::PROJECT_ARTIFACTS, "commands")
119
- #FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
120
- #filename_friendly_timestamp = Time.now.strftime("%Y.%m.%d_%H%M%S")
121
- #log_filename = File.join(dir, "#{filename_friendly_timestamp}.json")
122
124
  log_filename = get_cache_filename(command)
123
- # serialize to protobuf json
124
- json = command.to_json
125
- pretty_json = JSON.pretty_generate(JSON.parse(json))
126
- File.write(log_filename, pretty_json)
125
+ Makit::Serializer.save_as(log_filename, command)
127
126
  end
128
127
 
129
128
  # Run a command and return a Makit::V1::Command.
@@ -91,10 +91,13 @@ module Makit
91
91
  end
92
92
 
93
93
  def self.get_git_tracked_files(directory_path)
94
- output, status = Open3.capture2("git ls-files", chdir: directory_path)
95
- raise "Failed to list git-tracked files" unless status.success?
94
+ #raise "do not use this method"
95
+ #output, status = Open3.capture2("git ls-files directory", chdir: directory_path)
96
+ #raise "Failed to list git-tracked files" unless status.success?
97
+ #command = "git ls-files #{directory_path}".run
96
98
 
97
- output.split("\n")
99
+ #command.output.split("\n")
100
+ `git ls-files #{directory_path}`.split("\n")
98
101
  end
99
102
 
100
103
  def self.get_newest_git_file(directory_path)
@@ -110,7 +113,6 @@ module Makit
110
113
  newest_file.nil? ? Time.now : File.mtime(newest_file)
111
114
  end
112
115
 
113
-
114
116
  # Normalize the path by removing any leading or trailing slashes
115
117
  # and replacing any backslashes with forward slashes
116
118
  def self.normalize(path)
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This module provides classes for the Makit gem.
4
+ module Makit
5
+ # This class provide methods for working with the system Environment.
6
+ #
7
+ class FileInfo
8
+ attr_accessor :name, :mtime, :size
9
+
10
+ def initialize(name:, mtime:, size:)
11
+ @name = name
12
+ @mtime = mtime
13
+ @size = size
14
+ end
15
+ end
16
+ end
data/lib/makit/git.rb CHANGED
@@ -39,6 +39,20 @@ module Makit
39
39
  "git archive --format zip --output #{zipfilename} HEAD".run
40
40
  end
41
41
 
42
+ def self.get_file_infos()
43
+ file_infos = []
44
+ command = `git ls-files`
45
+ command.split("\n").map do |path|
46
+ begin
47
+ file_infos << FileInfo.new(name: path, mtime: File.mtime(path), size: File.size(path))
48
+ rescue
49
+ next
50
+ end
51
+ end
52
+ file_infos.sort_by! { |info| info.mtime }.reverse!
53
+ file_infos
54
+ end
55
+
42
56
  def self.branch
43
57
  `git branch --show-current`.strip
44
58
  end
@@ -14,6 +14,11 @@ module Makit
14
14
  format("%.#{precision}f %s", size, units[exp])
15
15
  end
16
16
 
17
+ def self.get_humanized_timestamp(timestamp)
18
+ return timestamp.strftime("%Y-%m-%d %I:%M:%S %p") if timestamp.respond_to?(:strftime)
19
+ timestamp.strftime("%Y-%m-%d %H:%M:%S")
20
+ end
21
+
17
22
  def self.get_make_result_summary(make_result)
18
23
  summary = "Make Result\n"
19
24
  summary += " Repository: #{make_result.repository}\n"
@@ -88,19 +93,19 @@ module Makit
88
93
 
89
94
  def self.get_humanized_duration(seconds)
90
95
  minutes = (seconds / 60).to_i
91
- seconds = (seconds % 60).to_i
92
- hours = (minutes / 60).to_i
93
- minutes = minutes % 60
94
- days = (hours / 24).to_i
95
- hours = hours % 24
96
+ seconds = (seconds % 60).to_i
97
+ hours = (minutes / 60).to_i
98
+ minutes = minutes % 60
99
+ days = (hours / 24).to_i
100
+ hours = hours % 24
96
101
 
97
- parts = []
98
- parts << "#{days} days" if days > 0
99
- parts << "#{hours} hours" if hours > 0
100
- parts << "#{minutes} minutes" if minutes > 0
101
- parts << "#{seconds} seconds" if seconds > 0
102
+ parts = []
103
+ parts << "#{days} days" if days > 0
104
+ parts << "#{hours} hours" if hours > 0
105
+ parts << "#{minutes} minutes" if minutes > 0
106
+ parts << "#{seconds} seconds" if seconds > 0
102
107
 
103
- parts.join(", ")
108
+ parts.join(", ")
104
109
  end
105
110
  end
106
111
  end
@@ -1,13 +1,16 @@
1
- # frozen_string_literal: true
2
- require "digest"
3
-
4
- module Makit
5
- module V1
6
- class CommandRequest
7
- def to_hash
8
- int_hash = Digest::SHA256.hexdigest("{self.name}.#{self.arguments.join(" ")}")
9
- int_hash
10
- end
11
- end
12
- end
13
- end # module Makit
1
+ # frozen_string_literal: true
2
+ require "digest"
3
+
4
+ module Makit
5
+ module V1
6
+ #class CommandRequest
7
+ # def to_hash
8
+ # int_hash = Digest::SHA256.hexdigest("{self.name}.#{self.arguments.join(" ")}")
9
+ # int_hash
10
+ # end
11
+ # def to_hash_string
12
+ # hash_string = "#{int_hash}"
13
+ # end
14
+ # end
15
+ end
16
+ end # module Makit
@@ -150,7 +150,7 @@ module Makit
150
150
  #Makit::DotNet.new_solution(self.name)
151
151
  Makit::DotNet.sln_add_projects(self.name)
152
152
  else
153
- puts " no dotnet projects found".colorize(:yellow)
153
+ #puts " no dotnet projects found".colorize(:yellow)
154
154
  end
155
155
  end
156
156
 
@@ -179,7 +179,6 @@ module Makit
179
179
  #newest_file = Makit::Directory::get_newest_file_or_now(project.output)
180
180
  command_request = Makit::RUNNER::parse_command_request(command)
181
181
  RUNNER.cache_run(command_request, newest_file_timestamp)
182
-
183
182
  end
184
183
  project.build_args.each do |build_arg|
185
184
  project_path = File.join(project.output, "#{project.name}.csproj")
@@ -35,25 +35,14 @@ class String
35
35
  end
36
36
  end
37
37
 
38
- def cache_run(args = nil, timestamp = nil)
39
-
38
+ def cache_run(timestamp = nil)
40
39
  if timestamp.nil?
41
- timestamp = Makit::Directory.get_newest_git_file_timestamp(Makit::Directories::PROJECT_ROOT)
40
+ timestamp = Makit::GIT_FILE_INFOS.first.mtime #GIT_FILE_INFOS. Makit::Directory.get_newest_git_file_timestamp(Makit::Directories::PROJECT_ROOT)
42
41
  #timestamp = Makit::Timestamp.now
43
42
  end
44
- if args.nil?
45
- command = self
46
- Makit::RUNNER.cache_run(Makit::RUNNER::parse_command_request(command), timestamp)
47
- else
48
- command = self
49
- request = Makit::RUNNER.parse_args(command)
50
- if args.is_a?(Hash)
51
- args.each do |key, value|
52
- request.send("#{key}=", value)
53
- end
54
- end
55
- Makit::RUNNER.cache_run(request, timestamp)
56
- end
43
+
44
+ command = self
45
+ Makit::RUNNER.cache_run(Makit::RUNNER::parse_command_request(command), timestamp)
57
46
  end
58
47
 
59
48
  # Read a value from a JSON file
data/lib/makit/tasks.rb CHANGED
@@ -3,19 +3,7 @@ require "digest"
3
3
  require "rake/clean"
4
4
  #require "sqlite3"
5
5
 
6
- # Register the at_exit hook for cleanup
7
- at_exit do
8
- puts "Performing cleanup for Makit..."
9
- # Add your cleanup code here
10
- # For example, close files, remove temporary files, etc.
11
- # store current state of Makit::PROJECT to a file
12
- #json_filename = File.join(Makit::Directories::PROJECT_ROOT, ".makit.project.json")
13
- #puts "Saving project state to #{json_filename}"
14
- #Makit::PROJECT.save_as(json_filename)
15
- #yml_filename = File.join(Makit::Directories::PROJECT_ROOT, ".makit.project.yml")
16
- #puts "Saving project state to #{yml_filename}"
17
- # Makit::PROJECT.save_as(yml_filename)
18
- end
6
+
19
7
 
20
8
  # This module provides classes for the Makit gem.
21
9
  module Makit
@@ -63,5 +51,7 @@ end
63
51
 
64
52
  # Register the at_exit hook for cleanup
65
53
  at_exit do
66
- puts "at_exit in tasks.rb...."
54
+ #puts "at_exit in tasks.rb...."
55
+ duration = Time.now - Makit::STARTTIME
56
+ puts " completed in ".colorize(:grey) + Makit::Humanize.get_humanized_duration(duration).colorize(:green)
67
57
  end
data/lib/makit/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Makit
4
- VERSION = "0.0.21"
4
+ VERSION = "0.0.22"
5
5
 
6
6
  class Version
7
7
  # given an array of version strings, return the highest version
data/lib/makit.rb CHANGED
@@ -15,19 +15,19 @@ module Makit
15
15
  class Error < StandardError; end
16
16
 
17
17
  # Constants
18
- if File.exist? ".makit.project.json"
19
- PROJECT = Makit::Serializer.open(".makit.project.json", Makit::V1::Project)
20
- else
21
- if File.exist? ".makit.project.yml"
22
- PROJECT = Makit::Serializer.open(".makit.project.yml", Makit::V1::Project)
18
+ if File.exist? ".makit.project.json"
19
+ PROJECT = Makit::Serializer.open(".makit.project.json", Makit::V1::Project)
23
20
  else
24
- PROJECT = Makit::V1::Project.new
25
- PROJECT.set_default_values
21
+ if File.exist? ".makit.project.yml"
22
+ PROJECT = Makit::Serializer.open(".makit.project.yml", Makit::V1::Project)
23
+ else
24
+ PROJECT = Makit::V1::Project.new
25
+ PROJECT.set_default_values
26
+ end
26
27
  end
27
- end
28
-
29
28
 
30
29
  STARTTIME = Time.now
30
+
31
31
  IS_GIT_REPO = Dir.exist? ".git"
32
32
  DETACHED = `git status`.include?("detached")
33
33
  IS_READ_ONLY = !IS_GIT_REPO || DETACHED
@@ -40,6 +40,7 @@ end
40
40
  ENV["COMMIT_BRANCH"] = Makit::Git::branch
41
41
 
42
42
  RUNNER = CommandRunner.new
43
+ GIT_FILE_INFOS = Makit::Git::get_file_infos
43
44
  #RUNNER.log_to_artifacts = true
44
45
  # Variables
45
46
  log_level = Logger::INFO
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: makit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.21
4
+ version: 0.0.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lou Parslow
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-07 00:00:00.000000000 Z
11
+ date: 2024-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clamp
@@ -166,6 +166,7 @@ files:
166
166
  - lib/makit/directory.rb
167
167
  - lib/makit/dotnet.rb
168
168
  - lib/makit/environment.rb
169
+ - lib/makit/fileinfo.rb
169
170
  - lib/makit/files.rb
170
171
  - lib/makit/git.rb
171
172
  - lib/makit/gitlab_runner.rb