Class: Makit::Protoc

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/protoc.rb

Overview

This class provide methods for working with the system Environment.

Class Method Summary collapse

Class Method Details

.find_plugin_path(plugin_name) ⇒ Object

GRPC_CSHARP_PLUGIN_PATH = Makit::Protoc::find_plugin_path(“grpc_csharp_plugin”)



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/makit/protoc.rb', line 53

def self.find_plugin_path(plugin_name)
  # search for a file matching the pattern *grpc_csharp_plugin* in the grpc.tools directory

  # should match on Windows and Linux

  plugin_name += ".exe" if Makit::Environment.is_windows?

  plugin_path = Dir.glob("#{Makit::Directories::GRPC_TOOLS_PATH}/**/#{plugin_name}").first

  raise "Plugin path #{plugin_path} not found" unless File.exist?(plugin_path)

  plugin_path
end

.generate_source_file(proto_filename, language, csharp_output_dir) ⇒ Object



75
76
77
78
# File 'lib/makit/protoc.rb', line 75

def self.generate_source_file(proto_filename, language, csharp_output_dir)
  FileUtils.mkdir_p(csharp_output_dir)
  "protoc --#{language}_out=#{csharp_output_dir} #{proto_filename}".run
end

.generate_source_files(proto_source_dir, language, output_dir) ⇒ Object

language: charp, ruby, go, etc.



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
# File 'lib/makit/protoc.rb', line 81

def self.generate_source_files(proto_source_dir, language, output_dir)
  debug = false
  puts "Generating source files for #{language} in #{proto_source_dir} to #{output_dir}" if debug
  FileUtils.mkdir_p(output_dir)

  path_a = Pathname.new(proto_source_dir)
  path_b = Pathname.new(output_dir)
  output_relative_path = path_b.relative_path_from(path_a)
  puts "output_relative_path: #{output_relative_path}" if debug

  Dir.chdir(proto_source_dir) do
    Dir.glob("**/*.proto").each do |proto|
      puts "Generating #{proto}" if debug
      if language == "csharp"
        namespace_dir = File.dirname(proto)
        output_dir = "#{output_relative_path}/#{namespace_dir}"
        puts "Generating #{proto} in #{output_dir}" if debug
        FileUtils.mkdir_p(output_dir.to_s)
        "protoc --csharp_out=#{output_dir} #{proto}".run
      else
        # Use grpc_tools_ruby_protoc for Ruby to generate compatible code

        if language == "ruby"
          "grpc_tools_ruby_protoc --ruby_out=#{output_relative_path} --grpc_out=#{output_relative_path} #{proto}".run
        else
          "protoc --#{language}_out=#{output_relative_path} #{proto}".run
        end
      end
    end
  end
end

.get_latest_grpc_tools_versionObject

get the latest version of the grpc.tools package



48
49
50
# File 'lib/makit/protoc.rb', line 48

def self.get_latest_grpc_tools_version
  Makit::NuGet.get_latest_version("Grpc.Tools")
end

.get_proto_files_from_path(proto_source_dir) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/makit/protoc.rb', line 65

def self.get_proto_files_from_path(proto_source_dir)
  proto_files = []
  Dir.chdir(proto_source_dir) do
    Dir.glob("**/*.proto").each do |proto|
      proto_files << proto
    end
  end
  proto_files
end

.grpc_csharp_plugin_pathObject



14
15
16
# File 'lib/makit/protoc.rb', line 14

def self.grpc_csharp_plugin_path
  Makit::Protoc.find_plugin_path("grpc_csharp_plugin")
end

.grpc_tools_pathObject



10
11
12
# File 'lib/makit/protoc.rb', line 10

def self.grpc_tools_path
  File.join(Makit::Directories::NUGET_PACKAGE_CACHE, "grpc.tools", Makit::Protoc.get_latest_grpc_tools_version)
end

.setupObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/makit/protoc.rb', line 18

def self.setup
  if Dir.exist?(Makit::Directories::NUGET_PACKAGE_CACHE)
    Makit::NuGet.cache("Grpc.Tools", "2.66.0")
    unless Dir.exist?(Makit::Directories::NUGET_PACKAGE_CACHE)
      raise "Nuget package path #{Makit::Directories::NUGET_PACKAGE_CACHE} not found"
    end

    # test if protoc is installed by running >protoc --version

    which_protoc = Makit::Environment.which("protoc")
    if Makit::Environment.which("protoc").nil?
      puts "  protoc not found, installing...".colorize(:red)
      "go version".run
      "go install google.golang.org/protobuf/cmd/protoc-gen-go@latest".run
      "go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest".run
      "go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest".run
    else
      puts "  protoc found at #{which_protoc}".colorize(:green)
    end
    raise "protoc not found" unless Makit::Environment.which("protoc")

    # "git clone https://github.com/googleapis/googleapis.git third_party".run unless File.directory?("third_party/google")

    puts "  grpc_tools_path #{Makit::Protoc.grpc_tools_path.to_s.colorize(:green)}"
    puts "  grpc_csharp_plugin_path #{Makit::Protoc.grpc_csharp_plugin_path.to_s.colorize(:green)}"
  else
    puts "  warning: Nuget package cache directory #{Makit::Directories::NUGET_PACKAGE_CACHE} not found".colorize(:red)
    puts "  warning: Unable to proceed with protoc setup".colorize(:red)
  end
end