makit 0.0.83 → 0.0.84
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/generated/makit/v1/makit.v1_pb.rb +35 -0
- data/lib/generated/makit/v1/web/link_pb.rb +20 -0
- 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 +404 -404
- data/lib/makit/commands.rb +21 -21
- data/lib/makit/content/default_gitignore.rb +5 -5
- data/lib/makit/content/default_gitignore.txt +222 -222
- 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 +144 -144
- data/lib/makit/directory.rb +264 -264
- data/lib/makit/docs/files.rb +94 -94
- data/lib/makit/docs/rake.rb +106 -106
- data/lib/makit/dotnet.rb +219 -219
- data/lib/makit/email.rb +61 -61
- data/lib/makit/environment.rb +131 -131
- data/lib/makit/fileinfo.rb +26 -26
- data/lib/makit/files.rb +47 -47
- data/lib/makit/git.rb +145 -145
- data/lib/makit/gitlab_runner.rb +60 -60
- data/lib/makit/humanize.rb +129 -129
- data/lib/makit/indexer.rb +56 -56
- data/lib/makit/logging.rb +106 -106
- data/lib/makit/markdown.rb +75 -75
- data/lib/makit/mp/basic_object_mp.rb +16 -16
- data/lib/makit/mp/command_mp.rb +13 -13
- data/lib/makit/mp/command_request.mp.rb +16 -16
- data/lib/makit/mp/project_mp.rb +210 -210
- data/lib/makit/mp/string_mp.rb +137 -137
- data/lib/makit/nuget.rb +72 -72
- data/lib/makit/process.rb +26 -26
- data/lib/makit/protoc.rb +104 -104
- data/lib/makit/rake.rb +25 -25
- data/lib/makit/secrets.rb +51 -51
- data/lib/makit/serializer.rb +115 -115
- data/lib/makit/show.rb +110 -110
- data/lib/makit/storage.rb +131 -131
- data/lib/makit/symbols.rb +149 -149
- data/lib/makit/task_info.rb +86 -86
- data/lib/makit/tasks.rb +144 -144
- data/lib/makit/tree.rb +37 -37
- data/lib/makit/v1/makit.v1_services_pb.rb +25 -25
- data/lib/makit/version.rb +64 -65
- data/lib/makit/wix.rb +95 -95
- data/lib/makit/yaml.rb +17 -17
- data/lib/makit/zip.rb +17 -17
- data/lib/makit.rb +267 -267
- metadata +5 -3
data/lib/makit/protoc.rb
CHANGED
@@ -1,104 +1,104 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require "pathname"
|
3
|
-
|
4
|
-
# This module provides classes for the Makit gem.
|
5
|
-
module Makit
|
6
|
-
# This class provide methods for working with the system Environment.
|
7
|
-
#
|
8
|
-
class Protoc
|
9
|
-
def self.grpc_tools_path
|
10
|
-
File.join(Makit::Directories::NUGET_PACKAGE_CACHE, "grpc.tools", Makit::Protoc::get_latest_grpc_tools_version)
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.grpc_csharp_plugin_path
|
14
|
-
Makit::Protoc::find_plugin_path("grpc_csharp_plugin")
|
15
|
-
end
|
16
|
-
def self.setup
|
17
|
-
if (!Dir.exist?(Makit::Directories::NUGET_PACKAGE_CACHE))
|
18
|
-
puts " warning: Nuget package cache directory #{Makit::Directories::NUGET_PACKAGE_CACHE} not found".colorize(:red)
|
19
|
-
puts " warning: Unable to proceed with protoc setup".colorize(:red)
|
20
|
-
else
|
21
|
-
Makit::NuGet::cache("Grpc.Tools", "2.66.0")
|
22
|
-
raise "Nuget package path #{Makit::Directories::NUGET_PACKAGE_CACHE} not found" if !Dir.exist?(Makit::Directories::NUGET_PACKAGE_CACHE)
|
23
|
-
|
24
|
-
# test if protoc is installed by running >protoc --version
|
25
|
-
which_protoc = Makit::Environment.which("protoc")
|
26
|
-
if (Makit::Environment.which("protoc").nil?)
|
27
|
-
puts " protoc not found, installing...".colorize(:red)
|
28
|
-
"go version".run
|
29
|
-
"go install google.golang.org/protobuf/cmd/protoc-gen-go@latest".run
|
30
|
-
"go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest".run
|
31
|
-
"go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest".run
|
32
|
-
else
|
33
|
-
puts " protoc found at #{which_protoc}".colorize(:green)
|
34
|
-
end
|
35
|
-
raise "protoc not found" if !Makit::Environment.which("protoc")
|
36
|
-
|
37
|
-
#"git clone https://github.com/googleapis/googleapis.git third_party".run unless File.directory?("third_party/google")
|
38
|
-
puts " grpc_tools_path " + "#{Makit::Protoc::grpc_tools_path}".colorize(:green)
|
39
|
-
puts " grpc_csharp_plugin_path " + "#{Makit::Protoc::grpc_csharp_plugin_path}".colorize(:green)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# get the latest version of the grpc.tools package
|
44
|
-
def self.get_latest_grpc_tools_version
|
45
|
-
Makit::NuGet::get_latest_version("Grpc.Tools")
|
46
|
-
end
|
47
|
-
|
48
|
-
#GRPC_CSHARP_PLUGIN_PATH = Makit::Protoc::find_plugin_path("grpc_csharp_plugin")
|
49
|
-
def self.find_plugin_path(plugin_name)
|
50
|
-
# search for a file matching the pattern *grpc_csharp_plugin* in the grpc.tools directory
|
51
|
-
# should match on Windows and Linux
|
52
|
-
if (Makit::Environment.is_windows?)
|
53
|
-
plugin_name = plugin_name + ".exe"
|
54
|
-
end
|
55
|
-
|
56
|
-
plugin_path = Dir.glob("#{Makit::Directories::GRPC_TOOLS_PATH}/**/#{plugin_name}").first
|
57
|
-
|
58
|
-
raise "Plugin path #{plugin_path} not found" if !File.exist?(plugin_path)
|
59
|
-
plugin_path
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.get_proto_files_from_path(proto_source_dir)
|
63
|
-
proto_files = Array.new()
|
64
|
-
Dir.chdir(proto_source_dir) do
|
65
|
-
Dir.glob("**/*.proto").each do |proto|
|
66
|
-
proto_files << proto
|
67
|
-
end
|
68
|
-
end
|
69
|
-
proto_files
|
70
|
-
end
|
71
|
-
|
72
|
-
def self.generate_source_file(proto_filename, language, csharp_output_dir)
|
73
|
-
FileUtils.mkdir_p(csharp_output_dir) unless Dir.exist?(csharp_output_dir)
|
74
|
-
"protoc --#{language}_out=#{csharp_output_dir} #{proto_filename}".run
|
75
|
-
end
|
76
|
-
|
77
|
-
# language: charp, ruby, go, etc.
|
78
|
-
def self.generate_source_files(proto_source_dir, language, output_dir)
|
79
|
-
debug = false
|
80
|
-
puts "Generating source files for #{language} in #{proto_source_dir} to #{output_dir}" if debug
|
81
|
-
FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir)
|
82
|
-
|
83
|
-
path_a = Pathname.new(proto_source_dir)
|
84
|
-
path_b = Pathname.new(output_dir)
|
85
|
-
output_relative_path = path_b.relative_path_from(path_a)
|
86
|
-
puts "output_relative_path: #{output_relative_path}" if debug
|
87
|
-
|
88
|
-
Dir.chdir(proto_source_dir) do
|
89
|
-
Dir.glob("**/*.proto").each do |proto|
|
90
|
-
puts "Generating #{proto}" if debug
|
91
|
-
if (language == "csharp")
|
92
|
-
namespace_dir = File.dirname(proto)
|
93
|
-
output_dir = "#{output_relative_path}/#{namespace_dir}"
|
94
|
-
puts "Generating #{proto} in #{output_dir}" if debug
|
95
|
-
FileUtils.mkdir_p("#{output_dir}") unless Dir.exist?("#{output_dir}")
|
96
|
-
"protoc --csharp_out=#{output_dir} #{proto}".run
|
97
|
-
else
|
98
|
-
"protoc --#{language}_out=#{output_relative_path} #{proto}".run
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "pathname"
|
3
|
+
|
4
|
+
# This module provides classes for the Makit gem.
|
5
|
+
module Makit
|
6
|
+
# This class provide methods for working with the system Environment.
|
7
|
+
#
|
8
|
+
class Protoc
|
9
|
+
def self.grpc_tools_path
|
10
|
+
File.join(Makit::Directories::NUGET_PACKAGE_CACHE, "grpc.tools", Makit::Protoc::get_latest_grpc_tools_version)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.grpc_csharp_plugin_path
|
14
|
+
Makit::Protoc::find_plugin_path("grpc_csharp_plugin")
|
15
|
+
end
|
16
|
+
def self.setup
|
17
|
+
if (!Dir.exist?(Makit::Directories::NUGET_PACKAGE_CACHE))
|
18
|
+
puts " warning: Nuget package cache directory #{Makit::Directories::NUGET_PACKAGE_CACHE} not found".colorize(:red)
|
19
|
+
puts " warning: Unable to proceed with protoc setup".colorize(:red)
|
20
|
+
else
|
21
|
+
Makit::NuGet::cache("Grpc.Tools", "2.66.0")
|
22
|
+
raise "Nuget package path #{Makit::Directories::NUGET_PACKAGE_CACHE} not found" if !Dir.exist?(Makit::Directories::NUGET_PACKAGE_CACHE)
|
23
|
+
|
24
|
+
# test if protoc is installed by running >protoc --version
|
25
|
+
which_protoc = Makit::Environment.which("protoc")
|
26
|
+
if (Makit::Environment.which("protoc").nil?)
|
27
|
+
puts " protoc not found, installing...".colorize(:red)
|
28
|
+
"go version".run
|
29
|
+
"go install google.golang.org/protobuf/cmd/protoc-gen-go@latest".run
|
30
|
+
"go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest".run
|
31
|
+
"go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest".run
|
32
|
+
else
|
33
|
+
puts " protoc found at #{which_protoc}".colorize(:green)
|
34
|
+
end
|
35
|
+
raise "protoc not found" if !Makit::Environment.which("protoc")
|
36
|
+
|
37
|
+
#"git clone https://github.com/googleapis/googleapis.git third_party".run unless File.directory?("third_party/google")
|
38
|
+
puts " grpc_tools_path " + "#{Makit::Protoc::grpc_tools_path}".colorize(:green)
|
39
|
+
puts " grpc_csharp_plugin_path " + "#{Makit::Protoc::grpc_csharp_plugin_path}".colorize(:green)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# get the latest version of the grpc.tools package
|
44
|
+
def self.get_latest_grpc_tools_version
|
45
|
+
Makit::NuGet::get_latest_version("Grpc.Tools")
|
46
|
+
end
|
47
|
+
|
48
|
+
#GRPC_CSHARP_PLUGIN_PATH = Makit::Protoc::find_plugin_path("grpc_csharp_plugin")
|
49
|
+
def self.find_plugin_path(plugin_name)
|
50
|
+
# search for a file matching the pattern *grpc_csharp_plugin* in the grpc.tools directory
|
51
|
+
# should match on Windows and Linux
|
52
|
+
if (Makit::Environment.is_windows?)
|
53
|
+
plugin_name = plugin_name + ".exe"
|
54
|
+
end
|
55
|
+
|
56
|
+
plugin_path = Dir.glob("#{Makit::Directories::GRPC_TOOLS_PATH}/**/#{plugin_name}").first
|
57
|
+
|
58
|
+
raise "Plugin path #{plugin_path} not found" if !File.exist?(plugin_path)
|
59
|
+
plugin_path
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.get_proto_files_from_path(proto_source_dir)
|
63
|
+
proto_files = Array.new()
|
64
|
+
Dir.chdir(proto_source_dir) do
|
65
|
+
Dir.glob("**/*.proto").each do |proto|
|
66
|
+
proto_files << proto
|
67
|
+
end
|
68
|
+
end
|
69
|
+
proto_files
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.generate_source_file(proto_filename, language, csharp_output_dir)
|
73
|
+
FileUtils.mkdir_p(csharp_output_dir) unless Dir.exist?(csharp_output_dir)
|
74
|
+
"protoc --#{language}_out=#{csharp_output_dir} #{proto_filename}".run
|
75
|
+
end
|
76
|
+
|
77
|
+
# language: charp, ruby, go, etc.
|
78
|
+
def self.generate_source_files(proto_source_dir, language, output_dir)
|
79
|
+
debug = false
|
80
|
+
puts "Generating source files for #{language} in #{proto_source_dir} to #{output_dir}" if debug
|
81
|
+
FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir)
|
82
|
+
|
83
|
+
path_a = Pathname.new(proto_source_dir)
|
84
|
+
path_b = Pathname.new(output_dir)
|
85
|
+
output_relative_path = path_b.relative_path_from(path_a)
|
86
|
+
puts "output_relative_path: #{output_relative_path}" if debug
|
87
|
+
|
88
|
+
Dir.chdir(proto_source_dir) do
|
89
|
+
Dir.glob("**/*.proto").each do |proto|
|
90
|
+
puts "Generating #{proto}" if debug
|
91
|
+
if (language == "csharp")
|
92
|
+
namespace_dir = File.dirname(proto)
|
93
|
+
output_dir = "#{output_relative_path}/#{namespace_dir}"
|
94
|
+
puts "Generating #{proto} in #{output_dir}" if debug
|
95
|
+
FileUtils.mkdir_p("#{output_dir}") unless Dir.exist?("#{output_dir}")
|
96
|
+
"protoc --csharp_out=#{output_dir} #{proto}".run
|
97
|
+
else
|
98
|
+
"protoc --#{language}_out=#{output_relative_path} #{proto}".run
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
data/lib/makit/rake.rb
CHANGED
@@ -1,25 +1,25 @@
|
|
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 Rake
|
8
|
-
def self.rake_repository(url, task_name)
|
9
|
-
local_path = File.join(Makit::Directories::HOME, "code", "work", Makit::Directories.get_relative_directory(url))
|
10
|
-
if (Dir.exist?(local_path))
|
11
|
-
Dir.chdir(local_path) do
|
12
|
-
puts " " + "#{local_path}".colorize(:grey)
|
13
|
-
"git pull".run
|
14
|
-
end
|
15
|
-
else
|
16
|
-
puts "> ".colorize(:grey) + "git clone #{url} #{local_path}".colorize(:green)
|
17
|
-
`git clone #{url} #{local_path}`
|
18
|
-
end
|
19
|
-
Dir.chdir(local_path) do
|
20
|
-
puts " " + "#{local_path}".colorize(:grey)
|
21
|
-
"rake #{task_name}".run
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end # class Rake
|
25
|
-
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 working with the system Environment.
|
6
|
+
#
|
7
|
+
class Rake
|
8
|
+
def self.rake_repository(url, task_name)
|
9
|
+
local_path = File.join(Makit::Directories::HOME, "code", "work", Makit::Directories.get_relative_directory(url))
|
10
|
+
if (Dir.exist?(local_path))
|
11
|
+
Dir.chdir(local_path) do
|
12
|
+
puts " " + "#{local_path}".colorize(:grey)
|
13
|
+
"git pull".run
|
14
|
+
end
|
15
|
+
else
|
16
|
+
puts "> ".colorize(:grey) + "git clone #{url} #{local_path}".colorize(:green)
|
17
|
+
`git clone #{url} #{local_path}`
|
18
|
+
end
|
19
|
+
Dir.chdir(local_path) do
|
20
|
+
puts " " + "#{local_path}".colorize(:grey)
|
21
|
+
"rake #{task_name}".run
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end # class Rake
|
25
|
+
end # module Makit
|
data/lib/makit/secrets.rb
CHANGED
@@ -1,51 +1,51 @@
|
|
1
|
-
module Makit
|
2
|
-
class Secrets
|
3
|
-
def add(key, value)
|
4
|
-
secrets_hash = get_secrets_hash()
|
5
|
-
secrets_hash[key] = value
|
6
|
-
save_secrets_hash(secrets_hash)
|
7
|
-
end
|
8
|
-
|
9
|
-
def remove(key)
|
10
|
-
secrets_hash = get_secrets_hash()
|
11
|
-
secrets_hash.delete(key)
|
12
|
-
save_secrets_hash(secrets_hash)
|
13
|
-
end
|
14
|
-
|
15
|
-
def has_key?(key)
|
16
|
-
secrets_hash = get_secrets_hash()
|
17
|
-
return secrets_hash.key?(key)
|
18
|
-
end
|
19
|
-
|
20
|
-
def get(key)
|
21
|
-
secrets_hash = get_secrets_hash()
|
22
|
-
return secrets_hash[key]
|
23
|
-
end
|
24
|
-
|
25
|
-
def set(key, value)
|
26
|
-
secrets_hash = get_secrets_hash()
|
27
|
-
secrets_hash[key] = value
|
28
|
-
save_secrets_hash(secrets_hash)
|
29
|
-
end
|
30
|
-
|
31
|
-
def get_secrets_filename()
|
32
|
-
return "#{Makit::Directories::ROOT}/secrets.json"
|
33
|
-
end
|
34
|
-
|
35
|
-
def get_secrets_hash()
|
36
|
-
secrets_file = get_secrets_filename()
|
37
|
-
if File.exist?(secrets_file)
|
38
|
-
text = IO.read(secrets_file)
|
39
|
-
return JSON.parse(text)
|
40
|
-
else
|
41
|
-
return {}
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def save_secrets_hash(hash)
|
46
|
-
secrets_file = get_secrets_filename()
|
47
|
-
# pretty print the hash
|
48
|
-
File.open(secrets_file, "w") { |f| f.puts JSON.pretty_generate(hash) }
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
1
|
+
module Makit
|
2
|
+
class Secrets
|
3
|
+
def add(key, value)
|
4
|
+
secrets_hash = get_secrets_hash()
|
5
|
+
secrets_hash[key] = value
|
6
|
+
save_secrets_hash(secrets_hash)
|
7
|
+
end
|
8
|
+
|
9
|
+
def remove(key)
|
10
|
+
secrets_hash = get_secrets_hash()
|
11
|
+
secrets_hash.delete(key)
|
12
|
+
save_secrets_hash(secrets_hash)
|
13
|
+
end
|
14
|
+
|
15
|
+
def has_key?(key)
|
16
|
+
secrets_hash = get_secrets_hash()
|
17
|
+
return secrets_hash.key?(key)
|
18
|
+
end
|
19
|
+
|
20
|
+
def get(key)
|
21
|
+
secrets_hash = get_secrets_hash()
|
22
|
+
return secrets_hash[key]
|
23
|
+
end
|
24
|
+
|
25
|
+
def set(key, value)
|
26
|
+
secrets_hash = get_secrets_hash()
|
27
|
+
secrets_hash[key] = value
|
28
|
+
save_secrets_hash(secrets_hash)
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_secrets_filename()
|
32
|
+
return "#{Makit::Directories::ROOT}/secrets.json"
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_secrets_hash()
|
36
|
+
secrets_file = get_secrets_filename()
|
37
|
+
if File.exist?(secrets_file)
|
38
|
+
text = IO.read(secrets_file)
|
39
|
+
return JSON.parse(text)
|
40
|
+
else
|
41
|
+
return {}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def save_secrets_hash(hash)
|
46
|
+
secrets_file = get_secrets_filename()
|
47
|
+
# pretty print the hash
|
48
|
+
File.open(secrets_file, "w") { |f| f.puts JSON.pretty_generate(hash) }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
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
|