File: install-protoc

package info (click to toggle)
golang-gitaly-proto 1.37.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 372 kB
  • sloc: ruby: 216; makefile: 93
file content (61 lines) | stat: -rwxr-xr-x 1,512 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env ruby
require 'fileutils'
require 'digest'

require_relative 'run.rb'

PROTOC_DOWNLOAD = {
  'osx' => {
    'name' => 'protoc.zip',
    'url' => 'https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protoc-3.6.1-osx-x86_64.zip',
    'sha256' => '0decc6ce5beed07f8c20361ddeb5ac7666f09cf34572cca530e16814093f9c0c',
  },
  'linux' => {
    'name' => 'protoc.zip',
    'url' => 'https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protoc-3.6.1-linux-x86_64.zip',
    'sha256' => '6003de742ea3fcf703cfec1cd4a3380fd143081a2eb0e559065563496af27807',
  }
}
PROTOC_DIR = '_build/protoc'

def main
  current_platform = platform
  unless current_platform
    abort "Platform #{RUBY_PLATFORM} is not yet supported by #{$0}"
  end

  install_protoc(current_platform)
end

def install_protoc(current_platform)
  # protoc Protobuf compiler
  download(PROTOC_DOWNLOAD[current_platform])
  FileUtils.rm_rf(PROTOC_DIR)
  FileUtils.mkdir_p(PROTOC_DIR)
  run!(%W[unzip #{File.expand_path(PROTOC_DOWNLOAD[current_platform]['name'])}], PROTOC_DIR)
end

def platform
  case RUBY_PLATFORM
  when /darwin/ then 'osx'
  when /linux/  then 'linux'
  end
end

def download(source)
  file = source['name']
  sha = source['sha256']

  return if sha_match?(file, sha)

  run!(%W[curl -L -o #{file} #{source['url']}])

  abort "SHA256 check failed" unless sha_match?(file, sha)
end

def sha_match?(file, sha)
  File.exist?(file) && Digest::SHA256.file(file).hexdigest == sha
end

main
puts 'done'