Sha256: 82321c6ca17de98f02432f3ef4f2ee6444789bca8aad1275666daeea62426391

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

namespace :proto do
  desc "Generate proto files"

  task :generate do
    def extract_license_terms(file_contents)
      text = []
      text << file_contents.shift while !file_contents.empty? && file_contents[0] =~ /^#/
      text << ""
      text
    end

    # adds the NewRelic License notice to the top of the generated files
    # Removes require lines since these are replicated in the proto.rb file.
    def add_license_preamble_and_remove_requires(output_path)
      gemspec_path = File.expand_path(File.join(output_path, '..', '..', '..', '..', '..'))
      license_terms = extract_license_terms(File.readlines(File.join(gemspec_path, "Gemfile")))
      Dir.glob(File.join(output_path, "*.rb")) do |filename|
        contents = File.readlines(filename)
        contents.reject! { |r| r =~ /^\s*require\s.*$/ }
        File.open(filename, 'w') do |output|
          output.puts license_terms
          output.puts contents
        end
      end
    end

    gem_folder = File.expand_path(File.join(File.dirname(__FILE__), ".."))
    proto_filename = File.join(gem_folder, "lib", "new_relic", "proto", "infinite_tracing.proto")
    output_path = File.join(gem_folder, "lib", "new_relic", "infinite_tracing", "proto")

    FileUtils.mkdir_p(output_path)
    cmd = [
      "grpc_tools_ruby_protoc",
      "-I#{gem_folder}/lib/new_relic/proto",
      "--ruby_out=#{output_path}",
      "--grpc_out=#{output_path} #{proto_filename}"
    ].join(" ")

    if system(cmd)
      puts "Proto file generated!"
      add_license_preamble_and_remove_requires output_path
    else
      puts "Failed to generate proto file."
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
newrelic-infinite_tracing-8.10.1 tasks/proto.rake
newrelic-infinite_tracing-8.10.0 tasks/proto.rake