class Makit::Protoc


This class provide methods for working with the system Environment.

def self.find_plugin_path(plugin_name)

GRPC_CSHARP_PLUGIN_PATH = Makit::Protoc::find_plugin_path("grpc_csharp_plugin")
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

  if (Makit::Environment.is_windows?)
    plugin_name = plugin_name + ".exe"
  end
  plugin_path = Dir.glob("#{Makit::Directories::GRPC_TOOLS_PATH}/**/#{plugin_name}").first
  raise "Plugin path #{plugin_path} not found" if !File.exist?(plugin_path)
  plugin_path
end

def self.generate_source_file(proto_filename, language, csharp_output_dir)

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

def self.generate_source_files(proto_source_dir, language, output_dir)

language: charp, ruby, go, etc.
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) unless Dir.exist?(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}") unless Dir.exist?("#{output_dir}")
        "protoc --csharp_out=#{output_dir} #{proto}".run
      else
        "protoc --#{language}_out=#{output_relative_path} #{proto}".run
      end
    end
  end
end

def self.get_latest_grpc_tools_version

get the latest version of the grpc.tools package
def self.get_latest_grpc_tools_version
  Makit::NuGet::get_latest_version("Grpc.Tools")
end

def self.get_proto_files_from_path(proto_source_dir)

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

def self.grpc_csharp_plugin_path

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

def self.grpc_tools_path

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

def self.setup

def self.setup
  if (!Dir.exist?(Makit::Directories::NUGET_PACKAGE_CACHE))
    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)
  else
    Makit::NuGet::cache("Grpc.Tools", "2.66.0")
    raise "Nuget package path #{Makit::Directories::NUGET_PACKAGE_CACHE} not found" if !Dir.exist?(Makit::Directories::NUGET_PACKAGE_CACHE)
    # 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" if !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}".colorize(:green)
    puts "  grpc_csharp_plugin_path " + "#{Makit::Protoc::grpc_csharp_plugin_path}".colorize(:green)
  end
end