makit 0.0.5 → 0.0.6
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/lib/makit/apache.rb +32 -32
- data/lib/makit/cli/clean.rb +14 -14
- data/lib/makit/cli/clone.rb +59 -59
- data/lib/makit/cli/init.rb +38 -38
- data/lib/makit/cli/main.rb +33 -33
- data/lib/makit/cli/make.rb +54 -54
- data/lib/makit/cli/new.rb +37 -37
- data/lib/makit/cli/nuget_cache.rb +38 -38
- data/lib/makit/cli/pull.rb +31 -31
- data/lib/makit/cli/setup.rb +71 -71
- data/lib/makit/cli/work.rb +21 -21
- data/lib/makit/command_runner.rb +321 -318
- data/lib/makit/commands.rb +21 -21
- data/lib/makit/content/default_gitignore.rb +5 -5
- data/lib/makit/content/default_rakefile.rb +11 -11
- data/lib/makit/content/gem_rakefile.rb +14 -14
- data/lib/makit/data.rb +50 -50
- data/lib/makit/directories.rb +140 -140
- data/lib/makit/directory.rb +153 -153
- data/lib/makit/dotnet.rb +83 -83
- data/lib/makit/environment.rb +123 -123
- data/lib/makit/files.rb +47 -47
- data/lib/makit/git.rb +66 -66
- data/lib/makit/gitlab_runner.rb +60 -60
- data/lib/makit/humanize.rb +89 -89
- data/lib/makit/logging.rb +96 -96
- data/lib/makit/markdown.rb +75 -75
- data/lib/makit/mp/basic_object_mp.rb +16 -16
- data/lib/makit/mp/project_mp.rb +160 -156
- data/lib/makit/mp/string_mp.rb +107 -107
- data/lib/makit/nuget.rb +57 -57
- data/lib/makit/protoc.rb +61 -61
- data/lib/makit/serializer.rb +115 -115
- data/lib/makit/storage.rb +131 -131
- data/lib/makit/symbols.rb +149 -149
- data/lib/makit/tasks.rb +67 -67
- data/lib/makit/tree.rb +37 -37
- data/lib/makit/v1/makit.v1_pb.rb +4 -3
- data/lib/makit/v1/makit.v1_services_pb.rb +25 -25
- data/lib/makit/version.rb +12 -12
- data/lib/makit/wix.rb +95 -95
- data/lib/makit/zip.rb +17 -17
- data/lib/makit.rb +243 -243
- metadata +3 -3
data/lib/makit/protoc.rb
CHANGED
@@ -1,61 +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
|
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
|
data/lib/makit/serializer.rb
CHANGED
@@ -1,115 +1,115 @@
|
|
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
|
-
YAML = 2,
|
9
|
-
BINARY = 3
|
10
|
-
end
|
11
|
-
|
12
|
-
# This class provide methods for serializing and deserializing objects.
|
13
|
-
#
|
14
|
-
class Serializer
|
15
|
-
attr_accessor :format_map
|
16
|
-
|
17
|
-
def self.save_as(filename, object)
|
18
|
-
extension = File.extname(filename)
|
19
|
-
case extension
|
20
|
-
when ".json"
|
21
|
-
File.write(filename, Makit::Serializer.new(Makit::Proto3Formats::PRETTY_JSON).serialize(object))
|
22
|
-
when ".yml"
|
23
|
-
File.write(filename, Makit::Serializer.new(Makit::Proto3Formats::YAML).serialize(object))
|
24
|
-
when ".pb"
|
25
|
-
File.open(filename, "wb") do |file|
|
26
|
-
file.write(Makit::Serializer.new(Makit::Proto3Formats::BINARY).serialize(object))
|
27
|
-
end
|
28
|
-
|
29
|
-
#File.write(filename, Makit::Serializer.new(Makit::Proto3Formats::BINARY).serialize(object))
|
30
|
-
else
|
31
|
-
raise "unsupported file extension: #{extension}"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.open(filename, type)
|
36
|
-
extension = File.extname(filename)
|
37
|
-
case extension
|
38
|
-
when ".json"
|
39
|
-
Makit::Serializer.new(Makit::Proto3Formats::PRETTY_JSON).deserialize(type, File.read(filename))
|
40
|
-
when ".yml"
|
41
|
-
Makit::Serializer.new(Makit::Proto3Formats::YAML).deserialize(type, File.read(filename))
|
42
|
-
when ".pb"
|
43
|
-
data = File.open(filename, "rb") { |file| file.read }
|
44
|
-
Makit::Serializer.new(Makit::Proto3Formats::BINARY).deserialize(type, data)
|
45
|
-
else
|
46
|
-
raise "unsupported file extension: #{extension}"
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def initialize(proto3_format = Proto3Formats::PRETTY_JSON)
|
51
|
-
@format_maps = {
|
52
|
-
::Google::Protobuf::MessageExts => proto3_format,
|
53
|
-
}
|
54
|
-
end
|
55
|
-
|
56
|
-
# Serialize an object to a byte[] or a string
|
57
|
-
def serialize(object)
|
58
|
-
raise "Object is nil" if object.nil?
|
59
|
-
raise "object is not of type ::Google::Protobuf::MessageExts" if !object.is_a? ::Google::Protobuf::MessageExts
|
60
|
-
|
61
|
-
case @format_maps[::Google::Protobuf::MessageExts]
|
62
|
-
when Proto3Formats::JSON
|
63
|
-
#puts "Serializing to protobuf json format"
|
64
|
-
object.to_json
|
65
|
-
when Proto3Formats::PRETTY_JSON
|
66
|
-
#puts "Serializing to protobuf pretty json format"
|
67
|
-
json = object.to_json
|
68
|
-
pretty_json = JSON.pretty_generate(JSON.parse(json))
|
69
|
-
pretty_json
|
70
|
-
when Proto3Formats::YAML
|
71
|
-
#puts "Serializing to protobuf yaml format"
|
72
|
-
data = JSON.parse(object.to_json)
|
73
|
-
data.to_yaml.sub(/^---\n/, "")
|
74
|
-
when Proto3Formats::BINARY
|
75
|
-
#puts "Serializing to protobuf binary format"
|
76
|
-
serialized_object = object.to_proto
|
77
|
-
else
|
78
|
-
raise "Unknown serialization format"
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
# Deserialize a byte[] or a string to an object
|
83
|
-
def deserialize(type, data)
|
84
|
-
raise "data is nil" if data.nil?
|
85
|
-
case @format_maps[::Google::Protobuf::MessageExts]
|
86
|
-
when Proto3Formats::JSON
|
87
|
-
raise "data is not of type string" if !data.is_a? String
|
88
|
-
json = data
|
89
|
-
#puts "Deserializing from protobuf json format into type #{type}"
|
90
|
-
# use the from_json method to deserialize the object
|
91
|
-
type.decode_json(json)
|
92
|
-
when Proto3Formats::PRETTY_JSON
|
93
|
-
raise "data is not of type string" if !data.is_a? String
|
94
|
-
json = data
|
95
|
-
#puts "Deserializing from protobuf pretty json format into type #{type}"
|
96
|
-
# use the from_json method to deserialize the object
|
97
|
-
type.decode_json(json)
|
98
|
-
when Proto3Formats::YAML
|
99
|
-
raise "data is not of type string" if !data.is_a? String
|
100
|
-
yaml = data
|
101
|
-
# convert yaml to json
|
102
|
-
json = JSON.pretty_generate(YAML.safe_load(yaml))
|
103
|
-
#puts "Deserializing from protobuf yaml format into type #{type}"
|
104
|
-
type.decode_json(json)
|
105
|
-
when Proto3Formats::BINARY
|
106
|
-
#raise "data is not of type byte[]" if !data.is_a? Array
|
107
|
-
bytes = data
|
108
|
-
#puts "Deserializing from protobuf binary format into type #{type}"
|
109
|
-
type.decode(bytes)
|
110
|
-
else
|
111
|
-
raise "Unknown serialization format"
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end # class Storage
|
115
|
-
end # module Makit
|
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
|
+
YAML = 2,
|
9
|
+
BINARY = 3
|
10
|
+
end
|
11
|
+
|
12
|
+
# This class provide methods for serializing and deserializing objects.
|
13
|
+
#
|
14
|
+
class Serializer
|
15
|
+
attr_accessor :format_map
|
16
|
+
|
17
|
+
def self.save_as(filename, object)
|
18
|
+
extension = File.extname(filename)
|
19
|
+
case extension
|
20
|
+
when ".json"
|
21
|
+
File.write(filename, Makit::Serializer.new(Makit::Proto3Formats::PRETTY_JSON).serialize(object))
|
22
|
+
when ".yml"
|
23
|
+
File.write(filename, Makit::Serializer.new(Makit::Proto3Formats::YAML).serialize(object))
|
24
|
+
when ".pb"
|
25
|
+
File.open(filename, "wb") do |file|
|
26
|
+
file.write(Makit::Serializer.new(Makit::Proto3Formats::BINARY).serialize(object))
|
27
|
+
end
|
28
|
+
|
29
|
+
#File.write(filename, Makit::Serializer.new(Makit::Proto3Formats::BINARY).serialize(object))
|
30
|
+
else
|
31
|
+
raise "unsupported file extension: #{extension}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.open(filename, type)
|
36
|
+
extension = File.extname(filename)
|
37
|
+
case extension
|
38
|
+
when ".json"
|
39
|
+
Makit::Serializer.new(Makit::Proto3Formats::PRETTY_JSON).deserialize(type, File.read(filename))
|
40
|
+
when ".yml"
|
41
|
+
Makit::Serializer.new(Makit::Proto3Formats::YAML).deserialize(type, File.read(filename))
|
42
|
+
when ".pb"
|
43
|
+
data = File.open(filename, "rb") { |file| file.read }
|
44
|
+
Makit::Serializer.new(Makit::Proto3Formats::BINARY).deserialize(type, data)
|
45
|
+
else
|
46
|
+
raise "unsupported file extension: #{extension}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def initialize(proto3_format = Proto3Formats::PRETTY_JSON)
|
51
|
+
@format_maps = {
|
52
|
+
::Google::Protobuf::MessageExts => proto3_format,
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
# Serialize an object to a byte[] or a string
|
57
|
+
def serialize(object)
|
58
|
+
raise "Object is nil" if object.nil?
|
59
|
+
raise "object is not of type ::Google::Protobuf::MessageExts" if !object.is_a? ::Google::Protobuf::MessageExts
|
60
|
+
|
61
|
+
case @format_maps[::Google::Protobuf::MessageExts]
|
62
|
+
when Proto3Formats::JSON
|
63
|
+
#puts "Serializing to protobuf json format"
|
64
|
+
object.to_json
|
65
|
+
when Proto3Formats::PRETTY_JSON
|
66
|
+
#puts "Serializing to protobuf pretty json format"
|
67
|
+
json = object.to_json
|
68
|
+
pretty_json = JSON.pretty_generate(JSON.parse(json))
|
69
|
+
pretty_json
|
70
|
+
when Proto3Formats::YAML
|
71
|
+
#puts "Serializing to protobuf yaml format"
|
72
|
+
data = JSON.parse(object.to_json)
|
73
|
+
data.to_yaml.sub(/^---\n/, "")
|
74
|
+
when Proto3Formats::BINARY
|
75
|
+
#puts "Serializing to protobuf binary format"
|
76
|
+
serialized_object = object.to_proto
|
77
|
+
else
|
78
|
+
raise "Unknown serialization format"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Deserialize a byte[] or a string to an object
|
83
|
+
def deserialize(type, data)
|
84
|
+
raise "data is nil" if data.nil?
|
85
|
+
case @format_maps[::Google::Protobuf::MessageExts]
|
86
|
+
when Proto3Formats::JSON
|
87
|
+
raise "data is not of type string" if !data.is_a? String
|
88
|
+
json = data
|
89
|
+
#puts "Deserializing from protobuf json format into type #{type}"
|
90
|
+
# use the from_json method to deserialize the object
|
91
|
+
type.decode_json(json)
|
92
|
+
when Proto3Formats::PRETTY_JSON
|
93
|
+
raise "data is not of type string" if !data.is_a? String
|
94
|
+
json = data
|
95
|
+
#puts "Deserializing from protobuf pretty json format into type #{type}"
|
96
|
+
# use the from_json method to deserialize the object
|
97
|
+
type.decode_json(json)
|
98
|
+
when Proto3Formats::YAML
|
99
|
+
raise "data is not of type string" if !data.is_a? String
|
100
|
+
yaml = data
|
101
|
+
# convert yaml to json
|
102
|
+
json = JSON.pretty_generate(YAML.safe_load(yaml))
|
103
|
+
#puts "Deserializing from protobuf yaml format into type #{type}"
|
104
|
+
type.decode_json(json)
|
105
|
+
when Proto3Formats::BINARY
|
106
|
+
#raise "data is not of type byte[]" if !data.is_a? Array
|
107
|
+
bytes = data
|
108
|
+
#puts "Deserializing from protobuf binary format into type #{type}"
|
109
|
+
type.decode(bytes)
|
110
|
+
else
|
111
|
+
raise "Unknown serialization format"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end # class Storage
|
115
|
+
end # module Makit
|
data/lib/makit/storage.rb
CHANGED
@@ -1,131 +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
|
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
|