Class: Pkgr::Buildpack

Inherits:
Object
  • Object
show all
Defined in:
lib/pkgr/buildpack.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, type = :builtin, env = nil) ⇒ Buildpack

Returns a new instance of Buildpack.



24
25
26
27
28
29
30
# File 'lib/pkgr/buildpack.rb', line 24

def initialize(url, type = :builtin, env = nil)
  @uuid = Digest::SHA1.hexdigest(url)
  @url, @branch = url.split("#")
  @branch ||= "master"
  @type = type
  @env = env || Env.new
end

Class Attribute Details

.buildpacks_cache_dirObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pkgr/buildpack.rb', line 9

def buildpacks_cache_dir
  default_buildpacks_cache_dir = File.expand_path("~/.pkgr/buildpacks")
  @buildpacks_cache_dir ||= begin
    default_buildpacks_cache_dir.tap do |dir|
      FileUtils.mkdir_p(dir)
    end
  rescue
    Pkgr.warn "Unable to create directory at #{default_buildpacks_cache_dir.inspect}. Using temporary directory."
    Dir.mktmpdir
  end
end

Instance Attribute Details

Returns the value of attribute banner.



22
23
24
# File 'lib/pkgr/buildpack.rb', line 22

def banner
  @banner
end

#branchObject (readonly)

Returns the value of attribute branch.



22
23
24
# File 'lib/pkgr/buildpack.rb', line 22

def branch
  @branch
end

#envObject (readonly)

Returns the value of attribute env.



22
23
24
# File 'lib/pkgr/buildpack.rb', line 22

def env
  @env
end

#typeObject (readonly)

Returns the value of attribute type.



22
23
24
# File 'lib/pkgr/buildpack.rb', line 22

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url.



22
23
24
# File 'lib/pkgr/buildpack.rb', line 22

def url
  @url
end

#uuidObject (readonly)

Returns the value of attribute uuid.



22
23
24
# File 'lib/pkgr/buildpack.rb', line 22

def uuid
  @uuid
end

Instance Method Details

#buildpack_cache_dirObject



32
33
34
# File 'lib/pkgr/buildpack.rb', line 32

def buildpack_cache_dir
  File.join(self.class.buildpacks_cache_dir, type.to_s, uuid)
end

#compile(path, compile_cache_dir, compile_env_dir) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pkgr/buildpack.rb', line 44

def compile(path, compile_cache_dir, compile_env_dir)
  compile_home_dir = Dir.mktmpdir
  # Required to work around bundler forcing a writable home dir
  local_env = Env.new(["HOME=#{compile_home_dir}"])
  cmd = %{env -i #{compound_environment(path, local_env)} #{dir}/bin/compile "#{path}" "#{compile_cache_dir}" "#{compile_env_dir}" }
  Pkgr.debug "Running #{cmd.inspect}"

  Dir.chdir(path) do
    IO.popen(cmd) do |io|
      until io.eof?
        data = io.gets
        print data
      end
    end
    raise "compile failed" unless $?.exitstatus.zero?
  end
ensure
  FileUtils.remove_entry_secure compile_home_dir
  true
end

#detect(path) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/pkgr/buildpack.rb', line 36

def detect(path)
  buildpack_detect = Mixlib::ShellOut.new("env -i #{compound_environment(path)} #{dir}/bin/detect \"#{path}\"")
  buildpack_detect.logger = Pkgr.logger
  buildpack_detect.run_command
  @banner = buildpack_detect.stdout.chomp
  buildpack_detect.exitstatus == 0
end

#dirObject



72
73
74
# File 'lib/pkgr/buildpack.rb', line 72

def dir
  File.join(buildpack_cache_dir, File.basename(url, ".git"))
end

#exists?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/pkgr/buildpack.rb', line 76

def exists?
  File.directory?(dir)
end

#installObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pkgr/buildpack.rb', line 95

def install
  unless exists?
    FileUtils.mkdir_p(buildpack_cache_dir)
    Dir.chdir(buildpack_cache_dir) do
      puts "-----> Fetching buildpack #{url} at #{branch}"
      buildpack_install = Mixlib::ShellOut.new("git clone '#{url}'")
      buildpack_install.logger = Pkgr.logger
      buildpack_install.run_command
      buildpack_install.error!
    end
  end
  refresh(true)
end

#refresh(edge = true) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/pkgr/buildpack.rb', line 85

def refresh(edge = true)
  return if !edge
  Dir.chdir(dir) do
    buildpack_refresh = Mixlib::ShellOut.new("git fetch origin && ( git reset --hard #{branch} || git reset --hard origin/#{branch} ) && chmod -f +x bin/detect && chmod -f +x bin/compile && chmod -f +x bin/release")
    buildpack_refresh.logger = Pkgr.logger
    buildpack_refresh.run_command
    buildpack_refresh.error!
  end
end

#release(path) ⇒ Object



65
66
67
68
69
70
# File 'lib/pkgr/buildpack.rb', line 65

def release(path)
  buildpack_release = Mixlib::ShellOut.new("env -i #{compound_environment(path)} #{dir}/bin/release \"#{path}\" > #{path}/.release")
  buildpack_release.logger = Pkgr.logger
  buildpack_release.run_command
  buildpack_release.exitstatus == 0
end

#replace_app_with_app_home(app_home) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/pkgr/buildpack.rb', line 109

def replace_app_with_app_home(app_home)
  Dir.chdir(dir) do
    buildpack_replace = Mixlib::ShellOut.new("find . -type f -not -path '*/.git/*' -print0 | xargs -0 perl -pi -e s,/app/,#{app_home}/,g")
    buildpack_replace.logger = Pkgr.logger
    buildpack_replace.run_command
    buildpack_replace.error!
  end
end

#setup(edge, app_home) ⇒ Object



80
81
82
83
# File 'lib/pkgr/buildpack.rb', line 80

def setup(edge, app_home)
  exists? ? refresh(edge) : install
  replace_app_with_app_home(app_home)
end