Class: Protod::RakeTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/protod/rake_task.rb

Defined Under Namespace

Classes: Builder

Instance Method Summary collapse

Constructor Details

#initialize(name: :protod) ⇒ RakeTask

Returns a new instance of RakeTask.



18
19
20
21
22
23
24
25
26
# File 'lib/protod/rake_task.rb', line 18

def initialize(name: :protod)
  super()

  @name = name

  define_generate_proto_task
  define_generate_pb_task
  define_run_gruf
end

Instance Method Details

#define_generate_pb_taskObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/protod/rake_task.rb', line 46

def define_generate_pb_task
  desc 'Generate protocol buffers files'
  task("#{@name}:generate:pb": :environment) do
    Protod.setup!

    proto_dir = Pathname.new(Protod.configuration.proto_root_dir)
    pb_dir    = File.absolute_path(Pathname.new(Protod.configuration.pb_root_dir))

    FileUtils.mkdir_p(pb_dir) unless File.exist?(pb_dir)

    Dir.mktmpdir do |dir|
      dir = Pathname(dir)

      Protod::Proto::Package.roots.flat_map(&:all_packages).filter { _1.url.present? }.each.with_index(1) do |package, i|
        args    = [package.url, dir.join("_ext#{i}_#{package.url.split('/').last}")]
        options = { depth: 1, branch: package.branch }.compact

        option_part = options.map { |k, v| "--#{k} #{v}" }.join(' ')
        arg_part    = args.map { Shellwords.shellescape(_1) }.join(' ')
        cmd         = "git clone #{option_part} #{arg_part}"

        puts "#{cmd}"
        system(cmd) or raise "Failed to generate pb!"
      end

      include_option = Dir.glob("#{dir}/*").map { "-I#{_1}" }.join(' ')
      cmd            = "bundle exec grpc_tools_ruby_protoc #{include_option} -I#{proto_dir} --ruby_out=#{pb_dir} --grpc_out=#{pb_dir} `find #{proto_dir} -type f -name '*.proto'`"

      puts "#{cmd}"
      system(cmd) or raise "Failed to generate pb!"
    end

    puts "Finished to generate pb."
  end
end

#define_generate_proto_taskObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/protod/rake_task.rb', line 28

def define_generate_proto_task
  desc 'Generate proto files'
  task("#{@name}:generate:proto": :environment) do
    Protod.setup!

    root = Pathname.new(Protod.configuration.proto_root_dir)

    Protod::Proto::Package.roots.flat_map { _1.all_packages.reject(&:external?).reject(&:empty?) }.each do |package|
      path = root.join(*package.full_ident.split('.').tap { _1.last << '.proto' })

      FileUtils.mkdir_p(path.parent) unless File.exist?(path.parent)

      puts "Start to generate #{path} ..."
      File.write(path, package.to_proto)
    end
  end
end

#define_run_grufObject



82
83
84
85
86
87
88
89
# File 'lib/protod/rake_task.rb', line 82

def define_run_gruf
  desc 'Run gruf'
  task("#{@name}:gruf": :environment) do
    require 'protod/protocol_buffers'

    ::Gruf::Cli::Executor.new.run
  end
end