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.



18
19
20
21
22
23
24
# File 'lib/pkgr/buildpack.rb', line 18

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

Class Attribute Details

.buildpacks_cache_dirObject



9
10
11
12
13
# File 'lib/pkgr/buildpack.rb', line 9

def buildpacks_cache_dir
  @buildpacks_cache_dir ||= File.expand_path("~/.pkgr/buildpacks").tap do |dir|
    FileUtils.mkdir_p(dir)
  end
end

Instance Attribute Details

Returns the value of attribute banner.



16
17
18
# File 'lib/pkgr/buildpack.rb', line 16

def banner
  @banner
end

#branchObject (readonly)

Returns the value of attribute branch.



16
17
18
# File 'lib/pkgr/buildpack.rb', line 16

def branch
  @branch
end

#envObject (readonly)

Returns the value of attribute env.



16
17
18
# File 'lib/pkgr/buildpack.rb', line 16

def env
  @env
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/pkgr/buildpack.rb', line 16

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url.



16
17
18
# File 'lib/pkgr/buildpack.rb', line 16

def url
  @url
end

#uuidObject (readonly)

Returns the value of attribute uuid.



16
17
18
# File 'lib/pkgr/buildpack.rb', line 16

def uuid
  @uuid
end

Instance Method Details

#buildpack_cache_dirObject



26
27
28
# File 'lib/pkgr/buildpack.rb', line 26

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pkgr/buildpack.rb', line 38

def compile(path, compile_cache_dir, compile_env_dir)
  cmd = %{env -i PATH="$PATH"#{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

  true
end

#detect(path) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/pkgr/buildpack.rb', line 30

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

#dirObject



62
63
64
# File 'lib/pkgr/buildpack.rb', line 62

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

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  File.directory?(dir)
end

#installObject



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

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



75
76
77
78
79
80
81
82
83
# File 'lib/pkgr/buildpack.rb', line 75

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} )")
    buildpack_refresh.logger = Pkgr.logger
    buildpack_refresh.run_command
    buildpack_refresh.error!
  end
end

#release(path) ⇒ Object



55
56
57
58
59
60
# File 'lib/pkgr/buildpack.rb', line 55

def release(path)
  buildpack_release = Mixlib::ShellOut.new("#{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



99
100
101
102
103
104
105
106
# File 'lib/pkgr/buildpack.rb', line 99

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



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

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