makit 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +7 -0
  2. data/.makit.project.json +4 -0
  3. data/.makit.project.yml +2 -0
  4. data/.rubocop.yml +22 -0
  5. data/.ruby-version +1 -0
  6. data/CHANGELOG.md +8 -0
  7. data/CODE_OF_CONDUCT.md +84 -0
  8. data/LICENSE +21 -0
  9. data/README.md +119 -0
  10. data/Rakefile +190 -0
  11. data/docs/Commands.md +50 -0
  12. data/docs_/Commands.md +166 -0
  13. data/docs_/Minitest.Timeouts.md +332 -0
  14. data/examples/protoc/Rakefile +31 -0
  15. data/examples/rake_default/Rakefile +5 -0
  16. data/examples/rubygem-foo/.gitkeep +0 -0
  17. data/examples/rubygem-foo/Rakefile +3 -0
  18. data/examples/run_mp/Rakefile +8 -0
  19. data/exe/makit +5 -0
  20. data/lib/makit/apache.rb +32 -0
  21. data/lib/makit/cli/clean.rb +14 -0
  22. data/lib/makit/cli/clone.rb +59 -0
  23. data/lib/makit/cli/init.rb +38 -0
  24. data/lib/makit/cli/main.rb +33 -0
  25. data/lib/makit/cli/make.rb +54 -0
  26. data/lib/makit/cli/new.rb +37 -0
  27. data/lib/makit/cli/nuget_cache.rb +38 -0
  28. data/lib/makit/cli/pull.rb +31 -0
  29. data/lib/makit/cli/setup.rb +71 -0
  30. data/lib/makit/cli/work.rb +21 -0
  31. data/lib/makit/command_runner.rb +237 -0
  32. data/lib/makit/commands.rb +21 -0
  33. data/lib/makit/content/default_gitignore.rb +5 -0
  34. data/lib/makit/content/default_gitignore.txt +222 -0
  35. data/lib/makit/content/default_rakefile.rb +11 -0
  36. data/lib/makit/content/gem_rakefile.rb +14 -0
  37. data/lib/makit/content/ruby_gitlab-ci.yml +15 -0
  38. data/lib/makit/data.rb +50 -0
  39. data/lib/makit/directories.rb +140 -0
  40. data/lib/makit/directory.rb +120 -0
  41. data/lib/makit/dotnet.rb +16 -0
  42. data/lib/makit/environment.rb +123 -0
  43. data/lib/makit/files.rb +47 -0
  44. data/lib/makit/git.rb +66 -0
  45. data/lib/makit/gitlab_runner.rb +60 -0
  46. data/lib/makit/humanize.rb +89 -0
  47. data/lib/makit/logging.rb +96 -0
  48. data/lib/makit/markdown.rb +75 -0
  49. data/lib/makit/mp/basic_object_mp.rb +16 -0
  50. data/lib/makit/mp/project_mp.rb +160 -0
  51. data/lib/makit/mp/string_mp.rb +101 -0
  52. data/lib/makit/nuget.rb +57 -0
  53. data/lib/makit/protoc.rb +61 -0
  54. data/lib/makit/serializer.rb +70 -0
  55. data/lib/makit/storage.rb +131 -0
  56. data/lib/makit/symbols.rb +149 -0
  57. data/lib/makit/tasks.rb +63 -0
  58. data/lib/makit/tree.rb +37 -0
  59. data/lib/makit/v1/makit.v1.proto +103 -0
  60. data/lib/makit/v1/makit.v1_pb.rb +30 -0
  61. data/lib/makit/v1/makit.v1_services_pb.rb +26 -0
  62. data/lib/makit/version.rb +12 -0
  63. data/lib/makit/wix.rb +92 -0
  64. data/lib/makit/zip.rb +17 -0
  65. data/lib/makit.rb +243 -0
  66. data/makit.generated.sln +30 -0
  67. data/makit.sln +69 -0
  68. data/pages/.gitignore +5 -0
  69. data/pages/404.html +25 -0
  70. data/pages/Gemfile +33 -0
  71. data/pages/Gemfile.lock +88 -0
  72. data/pages/_config.yml +55 -0
  73. data/pages/_layouts/default.html +1 -0
  74. data/pages/_posts/2024-10-05-welcome-to-jekyll.markdown +29 -0
  75. data/pages/about.markdown +18 -0
  76. data/pages/index.markdown +6 -0
  77. data/sig/makit.rbs +4 -0
  78. data/src/ClassLib/Class1.cs +6 -0
  79. data/src/ClassLib/ClassLib.csproj +13 -0
  80. metadata +251 -0
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+ require "json"
3
+ require "yaml"
4
+
5
+ module Makit
6
+ module V1
7
+ class Project
8
+ def to_yaml
9
+ data = JSON.parse(self.to_pretty_json)
10
+ data.to_yaml.sub(/^---\n/, '')
11
+ end
12
+
13
+ def self.create
14
+ project = nil
15
+ if (File.exist?(".makit.project.yml"))
16
+ yaml = File.read(".makit.project.yml")
17
+ # convert yaml to json
18
+ data = YAML.load(yaml)
19
+ json = JSON.pretty_generate(data)
20
+ project = Makit::V1::Project.decode_json(json)
21
+
22
+ else
23
+ if (File.exist?(".makit.project.json"))
24
+ json = File.read("project.json")
25
+ project = Makit::V1::Project.decode_json(json)
26
+ else
27
+ project = Makit::V1::Project.new
28
+ end
29
+ end
30
+ project.set_default_values
31
+ project
32
+ end
33
+
34
+ def set_default_values
35
+ #self.language = "csharp"
36
+ #self.primary_artifact_type = "nuget"
37
+ if self.git_remote_url.nil? || self.git_remote_url.empty?
38
+ self.git_remote_url = `git remote get-url origin`.strip
39
+ end
40
+ if self.name.nil? || self.name.empty?
41
+ if(!self.git_remote_url.nil? && !self.git_remote_url.empty?)
42
+ self.name =get_name(File.basename(self.git_remote_url,".git"))# get_capitalized_name(File.basename(self.git_remote_url, ".git"))
43
+ else
44
+ self.name =get_name(File.basename(Dir.getwd))# get_capitalized_name(File.basename(Dir.getwd))
45
+ end
46
+ end
47
+ self
48
+ end
49
+
50
+ def get_name(name)
51
+ if !self.git_remote_url.nil? && !self.git_remote_url.empty?
52
+ is_dotnet = self.git_remote_url.include?("nuget")
53
+ if is_dotnet
54
+ get_capitalized_name(name)
55
+ end
56
+ end
57
+ name.downcase
58
+ end
59
+ def get_capitalized_name(name)
60
+ name.split(".").map(&:capitalize).join(".")
61
+ end
62
+
63
+ def save_as(filename)
64
+ extension = File.extname(filename)
65
+ case extension
66
+ when ".json"
67
+ File.write(filename, self.to_pretty_json)
68
+ when ".yml"
69
+ File.write(filename, self.to_yaml)
70
+ else
71
+ raise "unsupported file extension: #{extension}"
72
+ end
73
+ end
74
+
75
+ def with_dotnet_project(template,name,output)
76
+ if !self.dotnet_projects.any? { |project| project.output== output }
77
+ project = Makit::V1::DotNetProject.new
78
+ project.template = template
79
+ project.name = name
80
+ project.output = output
81
+ self.dotnet_projects << project
82
+ end
83
+ end
84
+
85
+ def setup_dotnet_projects
86
+ if self.dotnet_projects.any?
87
+ if(!File.exist?("#{self.name}.sln"))
88
+ puts " Creating solution file: " + "#{self.name}.sln".colorize(:green)
89
+ "dotnet new sln -n #{self.name}".run unless File.exist?("#{self.name}.sln")
90
+ else
91
+ puts " Solution file already exists: " + "#{self.name}.sln".colorize(:yellow)
92
+ end
93
+ self.dotnet_projects.each do |project|
94
+ if(!Dir.exist?("#{project.output}"))
95
+ puts " Creating project file: " + "#{project.name}/#{project.name}.csproj".colorize(:green)
96
+ "dotnet new #{project.template} -n #{project.name} -o #{project.output}".run unless Dir.exist?("#{project.output}")
97
+
98
+ # add the .csproj file to the .sln
99
+ puts " Adding project to solution: " + "#{project.output}/#{project.name}.csproj".colorize(:green)
100
+ "dotnet sln #{self.name}.sln add #{project.output}/#{project.name}.csproj".run
101
+ else
102
+ puts " Project file already exists: " + "#{project.name}/#{project.name}.csproj".colorize(:yellow)
103
+ end
104
+ end
105
+ else
106
+ puts " no dotnet projects found".colorize(:yellow)
107
+ end
108
+ end
109
+
110
+ def build_dotnet_projects
111
+ self.dotnet_projects.each do |project|
112
+ "dotnet build #{project.output} --configuration Release".run
113
+ end
114
+ end
115
+
116
+ def test_dotnet_projects
117
+ self.dotnet_projects.each do |project|
118
+ "dotnet test #{project.output} --configuration Release".run
119
+ end
120
+ end
121
+ end # class Project
122
+ end # module V1
123
+ end # module Makit
124
+
125
+
126
+ #task :setup do
127
+ # if(Makit::PROJECT.git_remote_url.include?("nuget") ||
128
+ # Makit::PROJECT.primary_artifact_type.include?("nuget"))
129
+
130
+ # if(!File.exist?("#{Makit::PROJECT.name}.sln"))
131
+ # puts " Creating solution file: " + "#{Makit::PROJECT.name}.sln".colorize(:green)
132
+ # "dotnet new sln -n #{Makit::PROJECT.name}".run unless File.exist?("#{Makit::PROJECT.name}.sln")
133
+ # end
134
+ # end
135
+
136
+ # Makit::PROJECT.dotnet_projects.each do |project|
137
+ # if(!Dir.exist?("#{project.output}"))
138
+ # puts " Creating project file: " + "#{project.name}/#{project.name}.csproj".colorize(:green)
139
+ # "dotnet new #{project.template} -n #{project.name} -o #{project.output}".run unless Dir.exist?("#{project.output}")
140
+ # end
141
+ # end
142
+
143
+ # add all .csproj file to the .sln
144
+ # Dir.glob("**/*.csproj").each do |file|
145
+ # "dotnet sln #{Makit::PROJECT.name}.sln add #{file}".run
146
+ # end
147
+ #template = "nunit"
148
+ #"dotnet new #{template} -n #{Makit::PROJECT.name}.Tests -o src/#{Makit::PROJECT.name}".run unless Dir.exist?("#{Makit::PROJECT.name}.Tests")
149
+
150
+ #if(Makit::PROJECt.primary_artifact_type.include?("nuget"))
151
+ # puts
152
+ #end
153
+ # end
154
+
155
+
156
+ #"dotnet build src/Facilities.Models/Facilities.Models.csproj --configuration Release".run
157
+ # "dotnet build test/Facilities.Models.Tests/Facilities.Models.Tests.csproj --configuration Release".run
158
+ # "dotnet build examples/Facilities.Models.Wasm.Demo/Facilities.Models.Wasm.Demo.csproj --configuration Release".run
159
+
160
+ #"dotnet test test/Facilities.Models.Tests/Facilities.Models.Tests.csproj --configuration Release".run
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+ require "json"
3
+ # monkey patch String class with a run method
4
+
5
+ class String
6
+ def run(args = nil)
7
+ if args.nil?
8
+ command = self
9
+ Makit::RUNNER.run(Makit::RUNNER::parse_command_request(command))
10
+ else
11
+ command = self
12
+ request = Makit::RUNNER.parse_args(command)
13
+ if args.is_a?(Hash)
14
+ args.each do |key, value|
15
+ request.send("#{key}=", value)
16
+ end
17
+ end
18
+ Makit::RUNNER.run(request)
19
+ end
20
+ end
21
+
22
+ def try(args = nil)
23
+ if args.nil?
24
+ command = self
25
+ Makit::RUNNER.try(command)
26
+ else
27
+ command = self
28
+ request = Makit::RUNNER.parse_args(command)
29
+ if args.is_a?(Hash)
30
+ args.each do |key, value|
31
+ request.send("#{key}=", value)
32
+ end
33
+ end
34
+ Makit::RUNNER.try(request)
35
+ end
36
+ end
37
+
38
+ # Read a value from a JSON file
39
+ # key is a string with the key to read, e.g. "AzureAd.Authority"
40
+ def get_json_value(key)
41
+ json = File.read(self)
42
+ data = JSON.parse(json)
43
+
44
+ # key delimiter is '.' so we can access nested keys
45
+ key.split(".").each do |k|
46
+ data = data[k]
47
+ end
48
+ data
49
+ end
50
+
51
+ alias_method :retrieve, :get_json_value
52
+
53
+ # Set a value in a JSON file
54
+ # key is a string with the key to set, e.g. "AzureAd.Authority"
55
+ def set_json_value(key, value)
56
+ file = File.read(self)
57
+ data = JSON.parse(file)
58
+ keys = key.split(".")
59
+ current = data
60
+
61
+ # Traverse and create any missing keys
62
+ keys[0..-2].each do |k|
63
+ current[k] ||= {} # Create a new hash if the key doesn't exist
64
+ current = current[k]
65
+ end
66
+
67
+ # Set the value for the final key
68
+ current[keys[-1]] = value
69
+
70
+ # Write the JSON back to the file
71
+ File.write(self, JSON.pretty_generate(data))
72
+ end
73
+
74
+ # Alias for set_json_value
75
+ alias_method :assign, :set_json_value
76
+
77
+ def to_lines(max_length=80,indent_length=5)
78
+ if(self.length <= max_length)
79
+ return self
80
+ else
81
+ indent = " " * indent_length
82
+ words = self.split(" ")
83
+ lines = []
84
+ line = ""
85
+ words.each do |word|
86
+ if((line + word).length > max_length)
87
+ lines << line
88
+ line = indent + word
89
+ else
90
+ if(line.length == 0)
91
+ line = word
92
+ else
93
+ line += " " + word
94
+ end
95
+ end
96
+ end
97
+ lines << line
98
+ return lines.join("\n")
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,57 @@
1
+ require "tmpdir"
2
+ # This module provides classes for the Makit gem.
3
+ module Makit
4
+ # This class provide methods for working with the Nuget package cache
5
+ #
6
+ # Example:
7
+ #
8
+ # Makit::Directory.cache("Google.Protobuf", "3.27.2")
9
+ #
10
+ # dotnet nuget locals all --list
11
+ # dotnet nuget locals all --clear
12
+ #
13
+ class NuGet
14
+ def self.get_cache_dir(package, version)
15
+ File.join(Makit::Directories::NUGET_PACKAGE_CACHE, package, version)
16
+ end
17
+
18
+ def self.cache(package, version)
19
+ # if the package is already cached, there is nothing to do
20
+ if (!Dir.exist?(get_cache_dir(package, version)))
21
+ Dir.mktmpdir do |dir|
22
+ # Use the temp directory here
23
+ Dir.chdir(dir) do
24
+ system("dotnet new classlib -n ClassLib")
25
+ Dir.chdir("ClassLib") do
26
+
27
+ # display a list of directories in the current directory
28
+ puts Dir.entries(Dir.pwd)
29
+ # display a list of files in the current directory
30
+ puts Dir.entries(Dir.pwd)
31
+ puts "dotnet add ClassLib.csproj package #{package} --version #{version}"
32
+ system("dotnet add ClassLib.csproj package #{package} --version #{version}")
33
+ end
34
+ end
35
+ # The directory and its contents will be removed automatically after the block
36
+ end
37
+ end
38
+ end
39
+
40
+ def self.clear_cache(package, version)
41
+ if (Dir.exist?(get_cache_dir(package, version)))
42
+ FileUtils.rm_rf(get_cache_dir(package, version))
43
+ end
44
+ end
45
+
46
+ # get the latest version of the package
47
+ def self.get_latest_version(package)
48
+ dir = File.join(Makit::Directories::NUGET_PACKAGE_CACHE, package)
49
+ if (Dir.exist?(dir))
50
+ versions = Dir.entries(dir).select { |entry| File.directory?(File.join(dir, entry)) && !(entry == "." || entry == "..") }
51
+ highest_version = Makit::Version::get_highest_version(versions)
52
+ return highest_version
53
+ end
54
+ nil
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,61 @@
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 Protoc
8
+ def self.grpc_tools_path
9
+ File.join(Makit::Directories::NUGET_PACKAGE_CACHE, "grpc.tools", Makit::Protoc::get_latest_grpc_tools_version)
10
+ end
11
+
12
+ def self.grpc_csharp_plugin_path
13
+ Makit::Protoc::find_plugin_path("grpc_csharp_plugin")
14
+ end
15
+ def self.setup
16
+ if (!Dir.exist?(Makit::Directories::NUGET_PACKAGE_CACHE))
17
+ puts " warning: Nuget package cache directory #{Makit::Directories::NUGET_PACKAGE_CACHE} not found".colorize(:red)
18
+ puts " warning: Unable to proceed with protoc setup".colorize(:red)
19
+ else
20
+ Makit::NuGet::cache("Grpc.Tools", "2.66.0")
21
+ raise "Nuget package path #{Makit::Directories::NUGET_PACKAGE_CACHE} not found" if !Dir.exist?(Makit::Directories::NUGET_PACKAGE_CACHE)
22
+
23
+ # test if protoc is installed by running >protoc --version
24
+ which_protoc = Makit::Environment.which("protoc")
25
+ if (Makit::Environment.which("protoc").nil?)
26
+ puts " protoc not found, installing...".colorize(:red)
27
+ "go version".run
28
+ "go install google.golang.org/protobuf/cmd/protoc-gen-go@latest".run
29
+ "go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest".run
30
+ "go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest".run
31
+ else
32
+ puts " protoc found at #{which_protoc}".colorize(:green)
33
+ end
34
+ raise "protoc not found" if !Makit::Environment.which("protoc")
35
+
36
+ #"git clone https://github.com/googleapis/googleapis.git third_party".run unless File.directory?("third_party/google")
37
+ puts " grpc_tools_path " + "#{Makit::Protoc::grpc_tools_path}".colorize(:green)
38
+ puts " grpc_csharp_plugin_path " + "#{Makit::Protoc::grpc_csharp_plugin_path}".colorize(:green)
39
+ end
40
+ end
41
+
42
+ # get the latest version of the grpc.tools package
43
+ def self.get_latest_grpc_tools_version
44
+ Makit::NuGet::get_latest_version("Grpc.Tools")
45
+ end
46
+
47
+ #GRPC_CSHARP_PLUGIN_PATH = Makit::Protoc::find_plugin_path("grpc_csharp_plugin")
48
+ def self.find_plugin_path(plugin_name)
49
+ # search for a file matching the pattern *grpc_csharp_plugin* in the grpc.tools directory
50
+ # should match on Windows and Linux
51
+ if (Makit::Environment.is_windows?)
52
+ plugin_name = plugin_name + ".exe"
53
+ end
54
+
55
+ plugin_path = Dir.glob("#{Makit::Directories::GRPC_TOOLS_PATH}/**/#{plugin_name}").first
56
+
57
+ raise "Plugin path #{plugin_path} not found" if !File.exist?(plugin_path)
58
+ plugin_path
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+ require "google/protobuf"
3
+ # This module provides classes for the Makit gem.
4
+ module Makit
5
+ module Proto3Formats
6
+ JSON = 0,
7
+ PRETTY_JSON = 1,
8
+ BINARY = 3
9
+ end
10
+
11
+ # This class provide methods for serializing and deserializing objects.
12
+ #
13
+ class Serializer
14
+ attr_accessor :format_map
15
+
16
+ def initialize(proto3_format = Proto3Formats::PRETTY_JSON)
17
+ @format_maps = {
18
+ ::Google::Protobuf::MessageExts => proto3_format,
19
+ }
20
+ end
21
+
22
+ # Serialize an object to a byte[] or a string
23
+ def serialize(object)
24
+ raise "Object is nil" if object.nil?
25
+ raise "object is not of type ::Google::Protobuf::MessageExts" if !object.is_a? ::Google::Protobuf::MessageExts
26
+
27
+ case @format_maps[::Google::Protobuf::MessageExts]
28
+ when Proto3Formats::JSON
29
+ puts "Serializing to protobuf json format"
30
+ object.to_json
31
+ when Proto3Formats::PRETTY_JSON
32
+ puts "Serializing to protobuf pretty json format"
33
+ json = object.to_json
34
+ pretty_json = JSON.pretty_generate(JSON.parse(json))
35
+ pretty_json
36
+ when Proto3Formats::BINARY
37
+ puts "Serializing to protobuf binary format"
38
+ serialized_object = object.to_proto
39
+ else
40
+ raise "Unknown serialization format"
41
+ end
42
+ end
43
+
44
+ # Deserialize a byte[] or a string to an object
45
+ def deserialize(type, data)
46
+ raise "data is nil" if data.nil?
47
+ case @format_maps[::Google::Protobuf::MessageExts]
48
+ when Proto3Formats::JSON
49
+ raise "data is not of type string" if !data.is_a? String
50
+ json = data
51
+ puts "Deserializing from protobuf json format into type #{type}"
52
+ # use the from_json method to deserialize the object
53
+ type.decode_json(json)
54
+ when Proto3Formats::PRETTY_JSON
55
+ raise "data is not of type string" if !data.is_a? String
56
+ json = data
57
+ puts "Deserializing from protobuf pretty json format into type #{type}"
58
+ # use the from_json method to deserialize the object
59
+ type.decode_json(json)
60
+ when Proto3Formats::BINARY
61
+ #raise "data is not of type byte[]" if !data.is_a? Array
62
+ bytes = data
63
+ puts "Deserializing from protobuf binary format into type #{type}"
64
+ type.decode(bytes)
65
+ else
66
+ raise "Unknown serialization format"
67
+ end
68
+ end
69
+ end # class Storage
70
+ end # module Makit
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This module provides classes for the Makit gem.
4
+ module Makit
5
+ # This class provide methods for serializing and deserializing objects.
6
+ #
7
+ class Storage
8
+ # declare a private class variable, connection_string
9
+ attr_accessor :directory #= nil
10
+ attr_accessor :protoc_json_serializer # = Makit::Serializer::new()
11
+ attr_accessor :protoc_binary_serializer # = Makit::Serializer::new()
12
+
13
+ def initialize(directory)
14
+ @directory = directory
15
+ @protoc_json_serializer = Makit::Serializer::new(Makit::Proto3Formats::JSON)
16
+ @protoc_binary_serializer = Makit::Serializer::new(Makit::Proto3Formats::BINARY)
17
+ end
18
+
19
+ def save(object, key)
20
+ puts "=" * 80
21
+ puts "Saving object to storage"
22
+ puts "=" * 80
23
+ raise "directory is nil" if @directory.nil?
24
+ raise "Object is nil" if object.nil?
25
+ raise "Key is nil" if key.nil?
26
+
27
+ # get the fully qualfied type name of the object
28
+ type_name = object.class.to_s
29
+ puts "Type name: #{type_name}"
30
+
31
+ type_rel_dir = type_name.downcase.gsub("::", "/")
32
+ puts "Type relative directory: #{type_rel_dir}"
33
+
34
+ # extract the file extension from the key
35
+ file_extension = File.extname(key)
36
+ puts "File extension: #{file_extension}"
37
+
38
+ serializer = nil
39
+ if file_extension == ".json"
40
+ serializer = @protoc_json_serializer
41
+ elsif file_extension == ".pb"
42
+ serializer = @protoc_binary_serializer
43
+ else
44
+ raise "Unknown file extension: #{file_extension}"
45
+ end
46
+ bytes = serializer.serialize(object)
47
+ puts "Serialized object size: #{bytes.size} bytes"
48
+
49
+ rel_filename = File.join(type_rel_dir, "#{key}")
50
+ puts "Relative filename: #{rel_filename}"
51
+
52
+ filename = File.join(@directory, rel_filename)
53
+ puts "Saving object to file: #{filename}"
54
+
55
+ # make sure the file directory has been created
56
+ FileUtils.mkdir_p(File.dirname(filename)) unless File.directory?(File.dirname(filename))
57
+ # write the bytes to the file
58
+ File.open(filename, "wb") do |file|
59
+ file.write(bytes)
60
+ end
61
+
62
+ puts "=" * 80
63
+ end
64
+
65
+ def load(type, key)
66
+ raise "directory is nil" if @directory.nil?
67
+ raise "Key is nil" if key.nil?
68
+
69
+ # get the fully qualfied type name of the object
70
+ type_name = type.to_s # object.class.to_s
71
+ puts "Type name: #{type_name}"
72
+
73
+ type_rel_dir = type_name.downcase.gsub("::", "/")
74
+ puts "Type relative directory: #{type_rel_dir}"
75
+
76
+ # extract the file extension from the key
77
+ file_extension = File.extname(key)
78
+ puts "File extension: #{file_extension}"
79
+
80
+ rel_filename = File.join(type_rel_dir, "#{key}")
81
+ puts "Relative filename: #{rel_filename}"
82
+
83
+ filename = File.join(@directory, rel_filename)
84
+ puts "Loading object from file: #{filename}"
85
+
86
+ # make sure the file exists
87
+ raise "File does not exist: #{filename}" unless File.exist?(filename)
88
+
89
+ # read the bytes from the file
90
+ bytes = File.read(filename, mode: "rb")
91
+ puts "Read object from file: #{filename}"
92
+
93
+ # extract the file extension from the key
94
+ file_extension = File.extname(key)
95
+ puts "File extension: #{file_extension}"
96
+
97
+ serializer = nil
98
+ if file_extension == ".json"
99
+ serializer = @protoc_json_serializer
100
+ elsif file_extension == ".pb"
101
+ serializer = @protoc_binary_serializer
102
+ else
103
+ raise "Unknown file extension: #{file_extension}"
104
+ end
105
+
106
+ # deserialize the object
107
+ object = serializer.deserialize(type, bytes)
108
+ puts "Deserialized object size: #{bytes.size} bytes"
109
+
110
+ # if the object type inherits from Google::Protobuf::AbstractMessage,
111
+ # then serialize to protobuf json format
112
+ #if object.is_a? Google::Protobuf::MessageExts
113
+ # puts "Serializing to protobuf json format"
114
+ # serialized_object = object.to_json
115
+ # pretty_json = JSON.pretty_generate(JSON.parse(serialized_object))
116
+ # puts pretty_json
117
+ #else
118
+ # puts "Serializing to Marshal format"
119
+ # serialized_object = Marshal.dump(object)
120
+ #end
121
+ # serialize the object
122
+ #serialized_object = Marshal.dump(object)
123
+
124
+ # save the object to the storage
125
+ # ...
126
+ puts "=" * 80
127
+
128
+ object
129
+ end
130
+ end # class Storage
131
+ end # module Makit