File: build

package info (click to toggle)
ruby-redis 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,160 kB
  • sloc: ruby: 11,445; makefile: 117; sh: 24
file content (77 lines) | stat: -rwxr-xr-x 1,607 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env ruby
# frozen_string_literal: true

TARBALL = ARGV[0]

require 'digest/sha1'
require 'English'
require 'fileutils'

class Builder
  TARBALL_CACHE_EXPIRATION = 60 * 10

  def initialize(redis_branch, tmp_dir)
    @redis_branch = redis_branch
    @tmp_dir = tmp_dir
    @build_dir = File.join(@tmp_dir, "cache", "redis-#{redis_branch}")
  end

  def run
    download_tarball_if_needed
    if old_checkum != checksum
      build
      update_checksum
    end
    0
  end

  private

  def download_tarball_if_needed
    return if File.exist?(tarball_path) && File.mtime(tarball_path) > Time.now - TARBALL_CACHE_EXPIRATION

    command!('wget', '-q', tarball_url, '-O', tarball_path)
  end

  def tarball_path
    File.join(@tmp_dir, "redis-#{@redis_branch}.tar.gz")
  end

  def tarball_url
    "https://github.com/antirez/redis/archive/#{@redis_branch}.tar.gz"
  end

  def build
    FileUtils.rm_rf(@build_dir)
    FileUtils.mkdir_p(@build_dir)
    command!('tar', 'xf', tarball_path, '-C', File.expand_path('../', @build_dir))
    Dir.chdir(@build_dir) do
      command!('make')
    end
  end

  def update_checksum
    File.write(checksum_path, checksum)
  end

  def old_checkum
    File.read(checksum_path)
  rescue Errno::ENOENT
    nil
  end

  def checksum_path
    File.join(@build_dir, 'build.checksum')
  end

  def checksum
    @checksum ||= Digest::SHA1.file(tarball_path).hexdigest
  end

  def command!(*args)
    puts "$ #{args.join(' ')}"
    raise "Command failed with status #{$CHILD_STATUS.exitstatus}" unless system(*args)
  end
end

exit Builder.new(ARGV[0], ARGV[1]).run