makit 0.0.99 → 0.0.112
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +41 -0
- data/exe/makit +5 -0
- data/lib/makit/apache.rb +28 -32
- data/lib/makit/cli/build_commands.rb +500 -0
- data/lib/makit/cli/generators/base_generator.rb +74 -0
- data/lib/makit/cli/generators/dotnet_generator.rb +50 -0
- data/lib/makit/cli/generators/generator_factory.rb +49 -0
- data/lib/makit/cli/generators/node_generator.rb +50 -0
- data/lib/makit/cli/generators/ruby_generator.rb +77 -0
- data/lib/makit/cli/generators/rust_generator.rb +50 -0
- data/lib/makit/cli/generators/templates/dotnet_templates.rb +167 -0
- data/lib/makit/cli/generators/templates/node_templates.rb +161 -0
- data/lib/makit/cli/generators/templates/ruby/gemfile.rb +26 -0
- data/lib/makit/cli/generators/templates/ruby/gemspec.rb +40 -0
- data/lib/makit/cli/generators/templates/ruby/main_lib.rb +33 -0
- data/lib/makit/cli/generators/templates/ruby/rakefile.rb +35 -0
- data/lib/makit/cli/generators/templates/ruby/readme.rb +63 -0
- data/lib/makit/cli/generators/templates/ruby/test.rb +39 -0
- data/lib/makit/cli/generators/templates/ruby/test_helper.rb +29 -0
- data/lib/makit/cli/generators/templates/ruby/version.rb +29 -0
- data/lib/makit/cli/generators/templates/rust_templates.rb +128 -0
- data/lib/makit/cli/main.rb +62 -33
- data/lib/makit/cli/project_commands.rb +868 -0
- data/lib/makit/cli/repository_commands.rb +661 -0
- data/lib/makit/cli/utility_commands.rb +521 -0
- data/lib/makit/commands/factory.rb +359 -0
- data/lib/makit/commands/middleware/base.rb +73 -0
- data/lib/makit/commands/middleware/cache.rb +248 -0
- data/lib/makit/commands/middleware/command_logger.rb +320 -0
- data/lib/makit/commands/middleware/unified_logger.rb +243 -0
- data/lib/makit/commands/middleware/validator.rb +269 -0
- data/lib/makit/commands/request.rb +254 -0
- data/lib/makit/commands/result.rb +323 -0
- data/lib/makit/commands/runner.rb +337 -0
- data/lib/makit/commands/strategies/base.rb +160 -0
- data/lib/makit/commands/strategies/synchronous.rb +134 -0
- data/lib/makit/commands.rb +51 -21
- data/lib/makit/configuration/gitlab_helper.rb +60 -0
- data/lib/makit/configuration/project.rb +127 -0
- data/lib/makit/configuration/rakefile_helper.rb +43 -0
- data/lib/makit/configuration/step.rb +34 -0
- data/lib/makit/configuration.rb +14 -0
- data/lib/makit/content/default_gitignore.rb +7 -5
- data/lib/makit/content/default_rakefile.rb +13 -11
- data/lib/makit/content/gem_rakefile.rb +16 -14
- data/lib/makit/context.rb +1 -0
- data/lib/makit/data.rb +49 -50
- data/lib/makit/directories.rb +141 -145
- data/lib/makit/directory.rb +262 -276
- data/lib/makit/docs/files.rb +89 -94
- data/lib/makit/docs/rake.rb +102 -106
- data/lib/makit/dotnet/cli.rb +65 -0
- data/lib/makit/dotnet/project.rb +153 -0
- data/lib/makit/dotnet/solution.rb +38 -0
- data/lib/makit/dotnet/solution_classlib.rb +239 -0
- data/lib/makit/dotnet/solution_console.rb +264 -0
- data/lib/makit/dotnet/solution_maui.rb +354 -0
- data/lib/makit/dotnet/solution_wasm.rb +275 -0
- data/lib/makit/dotnet/solution_wpf.rb +304 -0
- data/lib/makit/dotnet.rb +102 -219
- data/lib/makit/email.rb +90 -61
- data/lib/makit/environment.rb +142 -139
- data/lib/makit/examples/runner.rb +370 -0
- data/lib/makit/exceptions.rb +45 -0
- data/lib/makit/fileinfo.rb +24 -26
- data/lib/makit/files.rb +43 -47
- data/lib/makit/gems.rb +29 -28
- data/lib/makit/git/cli.rb +54 -0
- data/lib/makit/git/repository.rb +90 -0
- data/lib/makit/git.rb +98 -145
- data/lib/makit/gitlab_runner.rb +59 -60
- data/lib/makit/humanize.rb +137 -129
- data/lib/makit/indexer.rb +47 -56
- data/lib/makit/logging/configuration.rb +305 -0
- data/lib/makit/logging/format_registry.rb +84 -0
- data/lib/makit/logging/formatters/base.rb +39 -0
- data/lib/makit/logging/formatters/console_formatter.rb +140 -0
- data/lib/makit/logging/formatters/json_formatter.rb +65 -0
- data/lib/makit/logging/formatters/plain_text_formatter.rb +71 -0
- data/lib/makit/logging/formatters/text_formatter.rb +64 -0
- data/lib/makit/logging/log_request.rb +115 -0
- data/lib/makit/logging/logger.rb +163 -0
- data/lib/makit/logging/sinks/base.rb +91 -0
- data/lib/makit/logging/sinks/console.rb +72 -0
- data/lib/makit/logging/sinks/file_sink.rb +92 -0
- data/lib/makit/logging/sinks/structured.rb +129 -0
- data/lib/makit/logging/sinks/unified_file_sink.rb +303 -0
- data/lib/makit/logging.rb +530 -106
- data/lib/makit/markdown.rb +75 -75
- data/lib/makit/mp/basic_object_mp.rb +17 -16
- data/lib/makit/mp/command_mp.rb +13 -13
- data/lib/makit/mp/command_request.mp.rb +17 -16
- data/lib/makit/mp/project_mp.rb +199 -210
- data/lib/makit/mp/string_mp.rb +193 -176
- data/lib/makit/nuget.rb +74 -72
- data/lib/makit/port.rb +32 -34
- data/lib/makit/process.rb +163 -65
- data/lib/makit/protoc.rb +107 -104
- data/lib/makit/rake/cli.rb +196 -0
- data/lib/makit/rake.rb +25 -25
- data/lib/makit/ruby/cli.rb +185 -0
- data/lib/makit/ruby.rb +25 -0
- data/lib/makit/secrets.rb +51 -51
- data/lib/makit/serializer.rb +130 -115
- data/lib/makit/services/builder.rb +186 -0
- data/lib/makit/services/error_handler.rb +226 -0
- data/lib/makit/services/repository_manager.rb +229 -0
- data/lib/makit/services/validator.rb +112 -0
- data/lib/makit/setup/classlib.rb +53 -0
- data/lib/makit/setup/gem.rb +263 -0
- data/lib/makit/setup/runner.rb +45 -0
- data/lib/makit/setup.rb +5 -0
- data/lib/makit/show.rb +110 -110
- data/lib/makit/storage.rb +126 -131
- data/lib/makit/symbols.rb +170 -149
- data/lib/makit/task_info.rb +128 -86
- data/lib/makit/tasks/at_exit.rb +13 -0
- data/lib/makit/tasks/build.rb +19 -0
- data/lib/makit/tasks/clean.rb +11 -0
- data/lib/makit/tasks/hook_manager.rb +393 -0
- data/lib/makit/tasks/init.rb +47 -0
- data/lib/makit/tasks/integrate.rb +17 -0
- data/lib/makit/tasks/pull_incoming.rb +11 -0
- data/lib/makit/tasks/setup.rb +6 -0
- data/lib/makit/tasks/sync.rb +12 -0
- data/lib/makit/tasks/tag.rb +15 -0
- data/lib/makit/tasks/task_monkey_patch.rb +79 -0
- data/lib/makit/tasks.rb +15 -150
- data/lib/makit/test_cache.rb +239 -0
- data/lib/makit/tree.rb +37 -37
- data/lib/makit/v1/makit.v1_pb.rb +3 -4
- data/lib/makit/v1/makit.v1_services_pb.rb +27 -25
- data/lib/makit/version.rb +5 -61
- data/lib/makit/version_util.rb +21 -0
- data/lib/makit/wix.rb +95 -95
- data/lib/makit/yaml.rb +29 -17
- data/lib/makit/zip.rb +17 -17
- data/lib/makit copy.rb +44 -0
- data/lib/makit.rb +40 -267
- metadata +117 -110
- data/lib/makit/cli/clean.rb +0 -14
- data/lib/makit/cli/clone.rb +0 -59
- data/lib/makit/cli/init.rb +0 -38
- data/lib/makit/cli/make.rb +0 -54
- data/lib/makit/cli/new.rb +0 -37
- data/lib/makit/cli/nuget_cache.rb +0 -38
- data/lib/makit/cli/pull.rb +0 -31
- data/lib/makit/cli/setup.rb +0 -71
- data/lib/makit/cli/work.rb +0 -21
- data/lib/makit/command_runner.rb +0 -404
- data/lib/makit/content/default_gitignore.txt +0 -222
data/lib/makit/humanize.rb
CHANGED
@@ -1,129 +1,137 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# This module provides classes for the Makit gem.
|
4
|
-
module Makit
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
summary
|
34
|
-
make_result.
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
summary
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
if command.
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
if
|
124
|
-
parts <<
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This module provides classes for the Makit gem.
|
4
|
+
module Makit
|
5
|
+
# Provides utilities for converting data into human-readable formats.
|
6
|
+
#
|
7
|
+
# This class handles formatting of file sizes, timestamps, durations,
|
8
|
+
# and build result summaries for display to users.
|
9
|
+
#
|
10
|
+
# @example Basic usage
|
11
|
+
# Makit::Humanize.get_humanized_size(1024) # => "1.00 KB"
|
12
|
+
# Makit::Humanize.get_humanized_duration(3661) # => "1 hour, 1 minute, 1 second"
|
13
|
+
#
|
14
|
+
class Humanize
|
15
|
+
def self.get_humanized_size(bytes, precision = 2)
|
16
|
+
units = %w[B KB MB GB TB PB]
|
17
|
+
return "0 B" if bytes.zero?
|
18
|
+
|
19
|
+
exp = (Math.log(bytes) / Math.log(1024)).to_i
|
20
|
+
exp = units.size - 1 if exp >= units.size
|
21
|
+
|
22
|
+
size = bytes.to_f / (1024 ** exp)
|
23
|
+
format("%.#{precision}f %s", size, units[exp])
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.get_humanized_timestamp(timestamp)
|
27
|
+
return timestamp.strftime("%Y-%m-%d %I:%M:%S %p") if timestamp.respond_to?(:strftime)
|
28
|
+
|
29
|
+
timestamp.strftime("%Y-%m-%d %H:%M:%S")
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.get_make_result_summary(make_result)
|
33
|
+
summary = "Make Result\n"
|
34
|
+
summary += " Repository: #{make_result.repository}\n"
|
35
|
+
summary += " Commit: #{make_result.commit}\n"
|
36
|
+
summary += " Branch: #{make_result.branch}\n"
|
37
|
+
summary += " Tag: #{make_result.tag}\n"
|
38
|
+
summary += " Device: #{make_result.device}\n"
|
39
|
+
summary += " Runtime Identifier: #{make_result.runtime_identifier}\n"
|
40
|
+
summary += " Initial Size: #{get_humanized_size(make_result.initial_size)}\n"
|
41
|
+
summary += " Final Size: #{get_humanized_size(make_result.final_size)}\n"
|
42
|
+
summary += " Delta Size: #{get_humanized_size(make_result.final_size - make_result.initial_size)}\n"
|
43
|
+
summary += " Commands: (#{make_result.commands.length})\n"
|
44
|
+
make_result.commands.each do |command|
|
45
|
+
details = get_command_details(command)
|
46
|
+
summary += "\n"
|
47
|
+
summary += indent_string(details, 4)
|
48
|
+
summary += "\n"
|
49
|
+
end
|
50
|
+
|
51
|
+
summary
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.get_commands(commands)
|
55
|
+
message = ""
|
56
|
+
commands.each do |command|
|
57
|
+
message += Makit::Humanize.get_command_details(command)
|
58
|
+
end
|
59
|
+
message
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.get_command_summary(command)
|
63
|
+
symbol = Makit::Symbols.warning
|
64
|
+
symbol = Makit::Symbols.checkmark if !command.exit_code.nil? && command.exit_code.zero?
|
65
|
+
symbol = Makit::Symbols.error if command.exit_code != 0
|
66
|
+
"#{symbol} #{command.name} #{command.arguments.join(" ")}"
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.get_command_details(command)
|
70
|
+
summary = "#{get_command_summary(command)}\n"
|
71
|
+
summary += " Name: #{command.name}\n"
|
72
|
+
summary += " Arguments: #{command.arguments.join(" ")}\n"
|
73
|
+
summary += " Directory: #{command.directory}\n"
|
74
|
+
summary += " Exit Code: #{command.exit_code}\n"
|
75
|
+
if command.output.length.positive?
|
76
|
+
summary += " Output:\n"
|
77
|
+
summary += indent_string(command.output, 4)
|
78
|
+
summary += "\n"
|
79
|
+
end
|
80
|
+
if command.error.length.positive?
|
81
|
+
summary += " Error:\n"
|
82
|
+
summary += indent_string(command.error, 4)
|
83
|
+
summary += "\n"
|
84
|
+
end
|
85
|
+
summary
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.indent_string(string, spaces)
|
89
|
+
string.split("\n").map { |line| (" " * spaces) + line }.join("\n")
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.get_protobuf_timestamp(timestamp)
|
93
|
+
Time.at(timestamp.seconds, timestamp.nanos / 1000.0).strftime("%Y-%m-%d %H:%M:%S")
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.get_protobuf_duration(duration)
|
97
|
+
total_seconds = duration.seconds + (duration.nanos / 1_000_000_000.0)
|
98
|
+
hours = (total_seconds / 3600).to_i
|
99
|
+
minutes = ((total_seconds % 3600) / 60).to_i
|
100
|
+
seconds = (total_seconds % 60).round(2)
|
101
|
+
"#{hours}h #{minutes}m #{seconds}s"
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.get_humanized_duration(seconds_value)
|
105
|
+
minutes = (seconds_value / 60).to_i
|
106
|
+
seconds = (seconds_value % 60).to_i
|
107
|
+
hours = (minutes / 60).to_i
|
108
|
+
minutes %= 60
|
109
|
+
days = (hours / 24).to_i
|
110
|
+
hours %= 24
|
111
|
+
milliseconds = (seconds_value % 1 * 1000).to_i
|
112
|
+
|
113
|
+
parts = []
|
114
|
+
parts << "#{days} days" if days.positive?
|
115
|
+
parts << "#{hours} hours" if hours.positive?
|
116
|
+
if minutes.positive?
|
117
|
+
parts << if minutes == 1
|
118
|
+
"1 minute"
|
119
|
+
else
|
120
|
+
"#{minutes} minutes"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
if seconds.positive?
|
124
|
+
parts << if seconds == 1
|
125
|
+
"1 second"
|
126
|
+
else
|
127
|
+
"#{seconds} seconds"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
# parts << "#{seconds} seconds" if seconds > 0
|
131
|
+
parts << "#{milliseconds} milliseconds" if milliseconds.positive? && seconds < 1
|
132
|
+
|
133
|
+
parts << "0 seconds" if parts.empty?
|
134
|
+
parts.join(", ")
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
data/lib/makit/indexer.rb
CHANGED
@@ -1,56 +1,47 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# This module provides classes for the Makit gem.
|
4
|
-
module Makit
|
5
|
-
# This class provide methods for indexing objects.
|
6
|
-
#
|
7
|
-
class Indexer
|
8
|
-
attr_accessor :keywords_index # Hash of string key to string[] of keyword
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
@
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
hash
|
20
|
-
|
21
|
-
value
|
22
|
-
|
23
|
-
|
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
|
-
terms.each { |term|
|
49
|
-
if (keywords.include?(term))
|
50
|
-
match_count += 1
|
51
|
-
end
|
52
|
-
}
|
53
|
-
return match_count
|
54
|
-
end
|
55
|
-
end # class Indexer
|
56
|
-
end # module Makit
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This module provides classes for the Makit gem.
|
4
|
+
module Makit
|
5
|
+
# This class provide methods for indexing objects.
|
6
|
+
#
|
7
|
+
class Indexer
|
8
|
+
attr_accessor :keywords_index, :protoc_json_serializer # Hash of string key to string[] of keyword
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@keywords_index = {}
|
12
|
+
@protoc_json_serializer = Makit::Serializer.new(Makit::Proto3Formats::JSON)
|
13
|
+
end
|
14
|
+
|
15
|
+
def index(key, item)
|
16
|
+
# item must be serializable to json
|
17
|
+
keywords = []
|
18
|
+
hash = JSON.parse(item.to_json)
|
19
|
+
hash.each_value do |value|
|
20
|
+
value = value.to_s.downcase
|
21
|
+
keywords << value if value.length >= 3 && !keywords.include?(value)
|
22
|
+
end
|
23
|
+
keywords.each do |keyword|
|
24
|
+
@keywords_index[keyword] = [] unless @keywords_index.key?(keyword)
|
25
|
+
@keywords_index[keyword] << key unless @keywords_index[keyword].include?(key)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def search(query)
|
30
|
+
keys = []
|
31
|
+
# todo, remove terms less that length of 3
|
32
|
+
terms = query.downcase.split.reject { |term| term.length < 3 }
|
33
|
+
keywords_index.each do |key, value| # {|kvp|
|
34
|
+
keys << key if get_match_count(terms, value) == terms.length
|
35
|
+
end
|
36
|
+
keys
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_match_count(terms, keywords)
|
40
|
+
match_count = 0
|
41
|
+
terms.each do |term|
|
42
|
+
match_count += 1 if keywords.include?(term)
|
43
|
+
end
|
44
|
+
match_count
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,305 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Makit
|
4
|
+
module Logging
|
5
|
+
# Configuration management for unified logging
|
6
|
+
#
|
7
|
+
# Provides schema validation, default configurations, and helper methods
|
8
|
+
# for setting up the unified file sink with various output configurations.
|
9
|
+
#
|
10
|
+
# @example Basic configuration
|
11
|
+
# config = Configuration.new(
|
12
|
+
# configurations: [
|
13
|
+
# { file: $stdout, format: :console },
|
14
|
+
# { file: "logs/app.log", format: :json }
|
15
|
+
# ]
|
16
|
+
# )
|
17
|
+
#
|
18
|
+
# @example YAML configuration
|
19
|
+
# config = Configuration.from_yaml("config/logging.yml")
|
20
|
+
#
|
21
|
+
# @example Environment-based configuration
|
22
|
+
# config = Configuration.for_environment(:production)
|
23
|
+
class Configuration
|
24
|
+
# @return [Array<Hash>] list of output configurations
|
25
|
+
attr_reader :configurations
|
26
|
+
|
27
|
+
# Initialize configuration
|
28
|
+
#
|
29
|
+
# @param configurations [Array<Hash>] list of output configurations
|
30
|
+
def initialize(configurations: [])
|
31
|
+
@configurations = configurations
|
32
|
+
end
|
33
|
+
|
34
|
+
# Create configuration from YAML file
|
35
|
+
#
|
36
|
+
# @param yaml_file [String] path to YAML configuration file
|
37
|
+
# @return [Configuration] new configuration instance
|
38
|
+
def self.from_yaml(yaml_file)
|
39
|
+
require "yaml"
|
40
|
+
data = YAML.load_file(yaml_file)
|
41
|
+
configurations = data["logging"]["sinks"] || []
|
42
|
+
|
43
|
+
# Convert string keys to symbols
|
44
|
+
configurations = configurations.map do |config|
|
45
|
+
config.transform_keys(&:to_sym)
|
46
|
+
end
|
47
|
+
|
48
|
+
new(configurations: configurations)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Create configuration from JSON file
|
52
|
+
#
|
53
|
+
# @param json_file [String] path to JSON configuration file
|
54
|
+
# @return [Configuration] new configuration instance
|
55
|
+
def self.from_json(json_file)
|
56
|
+
require "json"
|
57
|
+
data = JSON.parse(File.read(json_file))
|
58
|
+
configurations = data["logging"]["sinks"] || []
|
59
|
+
|
60
|
+
# Convert string keys to symbols
|
61
|
+
configurations = configurations.map do |config|
|
62
|
+
config.transform_keys(&:to_sym)
|
63
|
+
end
|
64
|
+
|
65
|
+
new(configurations: configurations)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Create environment-specific configuration
|
69
|
+
#
|
70
|
+
# @param environment [Symbol] environment name (:development, :production, :test)
|
71
|
+
# @return [Configuration] new configuration instance
|
72
|
+
def self.for_environment(environment)
|
73
|
+
case environment
|
74
|
+
when :development
|
75
|
+
development_config
|
76
|
+
when :production
|
77
|
+
production_config
|
78
|
+
when :test
|
79
|
+
test_config
|
80
|
+
when :ci
|
81
|
+
ci_config
|
82
|
+
else
|
83
|
+
default_config
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# Get default configuration
|
88
|
+
#
|
89
|
+
# @return [Configuration] default configuration
|
90
|
+
def self.default_config
|
91
|
+
new(
|
92
|
+
configurations: [
|
93
|
+
{ file: $stdout, format: :console },
|
94
|
+
{ file: "artifacts/makit.log", format: :json, append: true },
|
95
|
+
],
|
96
|
+
)
|
97
|
+
end
|
98
|
+
|
99
|
+
# Get development configuration
|
100
|
+
#
|
101
|
+
# @return [Configuration] development configuration
|
102
|
+
def self.development_config
|
103
|
+
new(
|
104
|
+
configurations: [
|
105
|
+
{
|
106
|
+
file: $stdout,
|
107
|
+
format: :console,
|
108
|
+
show_timestamp: true,
|
109
|
+
show_level: true,
|
110
|
+
},
|
111
|
+
{
|
112
|
+
file: "logs/development.log",
|
113
|
+
format: :text,
|
114
|
+
append: true,
|
115
|
+
include_context: true,
|
116
|
+
},
|
117
|
+
{
|
118
|
+
file: "logs/debug.log",
|
119
|
+
format: :json,
|
120
|
+
append: true,
|
121
|
+
min_level: :debug,
|
122
|
+
},
|
123
|
+
],
|
124
|
+
)
|
125
|
+
end
|
126
|
+
|
127
|
+
# Get production configuration
|
128
|
+
#
|
129
|
+
# @return [Configuration] production configuration
|
130
|
+
def self.production_config
|
131
|
+
new(
|
132
|
+
configurations: [
|
133
|
+
{
|
134
|
+
file: "logs/application.log",
|
135
|
+
format: :json,
|
136
|
+
append: true,
|
137
|
+
include_metadata: true,
|
138
|
+
rotation: { max_size: "100MB", max_files: 10 },
|
139
|
+
},
|
140
|
+
{
|
141
|
+
file: "logs/errors.log",
|
142
|
+
format: :json,
|
143
|
+
append: true,
|
144
|
+
min_level: :error,
|
145
|
+
include_metadata: true,
|
146
|
+
},
|
147
|
+
],
|
148
|
+
)
|
149
|
+
end
|
150
|
+
|
151
|
+
# Get test configuration
|
152
|
+
#
|
153
|
+
# @return [Configuration] test configuration
|
154
|
+
def self.test_config
|
155
|
+
new(
|
156
|
+
configurations: [
|
157
|
+
{
|
158
|
+
file: "logs/test.log",
|
159
|
+
format: :text,
|
160
|
+
append: false,
|
161
|
+
include_context: true,
|
162
|
+
},
|
163
|
+
],
|
164
|
+
)
|
165
|
+
end
|
166
|
+
|
167
|
+
# Get CI/CD configuration
|
168
|
+
#
|
169
|
+
# @return [Configuration] CI configuration
|
170
|
+
def self.ci_config
|
171
|
+
new(
|
172
|
+
configurations: [
|
173
|
+
{
|
174
|
+
file: $stdout,
|
175
|
+
format: :console,
|
176
|
+
show_level: true,
|
177
|
+
},
|
178
|
+
{
|
179
|
+
file: "artifacts/build.log",
|
180
|
+
format: :json,
|
181
|
+
append: true,
|
182
|
+
include_metadata: true,
|
183
|
+
},
|
184
|
+
],
|
185
|
+
)
|
186
|
+
end
|
187
|
+
|
188
|
+
# Validate configuration
|
189
|
+
#
|
190
|
+
# @return [Boolean] true if valid
|
191
|
+
# @raise [ArgumentError] if configuration is invalid
|
192
|
+
def validate!
|
193
|
+
raise ArgumentError, "configurations must be an array" unless @configurations.is_a?(Array)
|
194
|
+
raise ArgumentError, "at least one configuration is required" if @configurations.empty?
|
195
|
+
|
196
|
+
@configurations.each_with_index do |config, index|
|
197
|
+
validate_single_configuration(config, index)
|
198
|
+
end
|
199
|
+
|
200
|
+
true
|
201
|
+
end
|
202
|
+
|
203
|
+
# Check if configuration is valid
|
204
|
+
#
|
205
|
+
# @return [Boolean] true if valid
|
206
|
+
def valid?
|
207
|
+
validate!
|
208
|
+
true
|
209
|
+
rescue ArgumentError
|
210
|
+
false
|
211
|
+
end
|
212
|
+
|
213
|
+
# Get configuration as hash
|
214
|
+
#
|
215
|
+
# @return [Hash] configuration as hash
|
216
|
+
def to_hash
|
217
|
+
{
|
218
|
+
logging: {
|
219
|
+
sinks: @configurations,
|
220
|
+
},
|
221
|
+
}
|
222
|
+
end
|
223
|
+
|
224
|
+
# Save configuration to YAML file
|
225
|
+
#
|
226
|
+
# @param yaml_file [String] path to YAML file
|
227
|
+
# @return [void]
|
228
|
+
def save_yaml(yaml_file)
|
229
|
+
require "yaml"
|
230
|
+
FileUtils.mkdir_p(File.dirname(yaml_file))
|
231
|
+
File.write(yaml_file, to_hash.to_yaml)
|
232
|
+
end
|
233
|
+
|
234
|
+
# Save configuration to JSON file
|
235
|
+
#
|
236
|
+
# @param json_file [String] path to JSON file
|
237
|
+
# @return [void]
|
238
|
+
def save_json(json_file)
|
239
|
+
require "json"
|
240
|
+
FileUtils.mkdir_p(File.dirname(json_file))
|
241
|
+
File.write(json_file, JSON.pretty_generate(to_hash))
|
242
|
+
end
|
243
|
+
|
244
|
+
private
|
245
|
+
|
246
|
+
# Validate a single configuration
|
247
|
+
#
|
248
|
+
# @param config [Hash] configuration to validate
|
249
|
+
# @param index [Integer] configuration index for error messages
|
250
|
+
# @raise [ArgumentError] if configuration is invalid
|
251
|
+
def validate_single_configuration(config, index)
|
252
|
+
raise ArgumentError, "configuration #{index} must be a hash" unless config.is_a?(Hash)
|
253
|
+
|
254
|
+
required_keys = [:file, :format]
|
255
|
+
missing_keys = required_keys - config.keys
|
256
|
+
raise ArgumentError, "configuration #{index} missing required keys: #{missing_keys.join(", ")}" unless missing_keys.empty?
|
257
|
+
|
258
|
+
# Validate file
|
259
|
+
file = config[:file]
|
260
|
+
unless file.is_a?(String) || file.is_a?(IO) || file.respond_to?(:write)
|
261
|
+
raise ArgumentError, "configuration #{index} file must be a String path, IO object, or writable object"
|
262
|
+
end
|
263
|
+
|
264
|
+
# Validate format
|
265
|
+
format = config[:format]
|
266
|
+
unless format.is_a?(Symbol) || format.is_a?(String)
|
267
|
+
raise ArgumentError, "configuration #{index} format must be a Symbol or String"
|
268
|
+
end
|
269
|
+
|
270
|
+
# Validate log levels if specified
|
271
|
+
if config[:min_level] && !valid_log_level?(config[:min_level])
|
272
|
+
raise ArgumentError, "configuration #{index} min_level must be a valid log level"
|
273
|
+
end
|
274
|
+
|
275
|
+
if config[:max_level] && !valid_log_level?(config[:max_level])
|
276
|
+
raise ArgumentError, "configuration #{index} max_level must be a valid log level"
|
277
|
+
end
|
278
|
+
|
279
|
+
# Validate rotation if specified
|
280
|
+
if config[:rotation] && !valid_rotation_config?(config[:rotation])
|
281
|
+
raise ArgumentError, "configuration #{index} rotation must be a valid rotation configuration"
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
# Check if log level is valid
|
286
|
+
#
|
287
|
+
# @param level [Symbol] log level to check
|
288
|
+
# @return [Boolean] true if valid
|
289
|
+
def valid_log_level?(level)
|
290
|
+
%i[debug info warn error fatal success].include?(level)
|
291
|
+
end
|
292
|
+
|
293
|
+
# Check if rotation configuration is valid
|
294
|
+
#
|
295
|
+
# @param rotation [Hash] rotation configuration
|
296
|
+
# @return [Boolean] true if valid
|
297
|
+
def valid_rotation_config?(rotation)
|
298
|
+
return false unless rotation.is_a?(Hash)
|
299
|
+
|
300
|
+
valid_keys = [:max_size, :max_files, :compress, :pattern]
|
301
|
+
rotation.keys.all? { |key| valid_keys.include?(key) }
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|