Class: ProtocAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/twirp_rails/generators/twirp/protoc_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_path, dst_path, swagger_out_path = nil) ⇒ ProtocAdapter

Returns a new instance of ProtocAdapter.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/twirp_rails/generators/twirp/protoc_adapter.rb', line 6

def initialize(src_path, dst_path, swagger_out_path = nil)
  @src_path = src_path
  @dst_path = dst_path
  @swagger_out_path = swagger_out_path

  go_path = ENV.fetch('GOPATH') { File.expand_path('~/go') }
  go_bin_path = File.join(go_path, 'bin')
  @twirp_plugin_path = ENV.fetch('TWIRP_PLUGIN_PATH') { File.join(go_bin_path, 'protoc-gen-twirp_ruby') }
  @swagger_plugin_path = ENV.fetch('SWAGGER_PLUGIN_PATH') { File.join(go_bin_path, 'protoc-gen-twirp_swagger') }
  @protoc_path = `which protoc`.chomp
end

Instance Attribute Details

#dst_pathObject (readonly)

Returns the value of attribute dst_path.



2
3
4
# File 'lib/twirp_rails/generators/twirp/protoc_adapter.rb', line 2

def dst_path
  @dst_path
end

#protoc_pathObject (readonly)

Returns the value of attribute protoc_path.



4
5
6
# File 'lib/twirp_rails/generators/twirp/protoc_adapter.rb', line 4

def protoc_path
  @protoc_path
end

#src_pathObject (readonly)

Returns the value of attribute src_path.



2
3
4
# File 'lib/twirp_rails/generators/twirp/protoc_adapter.rb', line 2

def src_path
  @src_path
end

#swagger_out_pathObject (readonly)

Returns the value of attribute swagger_out_path.



2
3
4
# File 'lib/twirp_rails/generators/twirp/protoc_adapter.rb', line 2

def swagger_out_path
  @swagger_out_path
end

#swagger_plugin_pathObject (readonly)

Returns the value of attribute swagger_plugin_path.



4
5
6
# File 'lib/twirp_rails/generators/twirp/protoc_adapter.rb', line 4

def swagger_plugin_path
  @swagger_plugin_path
end

#twirp_plugin_pathObject (readonly)

Returns the value of attribute twirp_plugin_path.



4
5
6
# File 'lib/twirp_rails/generators/twirp/protoc_adapter.rb', line 4

def twirp_plugin_path
  @twirp_plugin_path
end

Instance Method Details

#check_requirements(check_swagger: false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/twirp_rails/generators/twirp/protoc_adapter.rb', line 35

def check_requirements(check_swagger: false)
  unless File.exist?(protoc_path)
    yield 'protoc not found - install protobuf (brew/apt/yum install protobuf)'
  end

  unless File.exist?(twirp_plugin_path)
    yield <<~TEXT
      protoc-gen-twirp_ruby not found - install go (brew install go)
      and run "go get github.com/twitchtv/twirp-ruby/protoc-gen-twirp_ruby
      or set TWIRP_PLUGIN_PATH environment variable to right location.
    TEXT
  end

  if check_swagger && !File.exist?(swagger_plugin_path)
    yield <<~TEXT
      protoc-gen-twirp_swagger not found - install go (brew install go)
      and run "go get github.com/elliots/protoc-gen-twirp_swagger
      or set SWAGGER_PLUGIN_PATH environment variable to right location.
    TEXT
  end
end

#cmd(files, gen_swagger) ⇒ Object



31
32
33
# File 'lib/twirp_rails/generators/twirp/protoc_adapter.rb', line 31

def cmd(files, gen_swagger)
  "#{protoc_path} #{flags(gen_swagger)} #{files}"
end

#rm_old_twirp_filesObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/twirp_rails/generators/twirp/protoc_adapter.rb', line 18

def rm_old_twirp_files
  return unless File.exists? dst_path

  remove_mask = File.join dst_path, '**/*_{twirp,pb}.rb'
  files_to_remove = Dir.glob remove_mask

  return if files_to_remove.empty?

  files_to_remove.each do |file|
    File.unlink file
  end
end