Class: Mason::Buildpacks

Inherits:
Object
  • Object
show all
Defined in:
lib/mason/buildpacks.rb

Class Method Summary collapse

Class Method Details

.ad_hoc_root(expand = true) ⇒ Object



48
49
50
51
# File 'lib/mason/buildpacks.rb', line 48

def self.ad_hoc_root(expand=true)
  dir = "~/.mason/buildpacks-ad-hoc"
  expand ? File.expand_path(dir) : dir
end

.buildpacksObject



53
54
55
56
57
58
59
# File 'lib/mason/buildpacks.rb', line 53

def self.buildpacks
  @buildpacks ||= begin
    Dir[File.join(root, "*")].map do |buildpack_dir|
      Mason::Buildpack.new(buildpack_dir)
    end
  end
end

.detect(app, buildpack_url) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mason/buildpacks.rb', line 61

def self.detect(app, buildpack_url)
  if buildpack_url
    puts "Using $BUILDPACK_URL: #{buildpack_url}"
    buildpack_dir = install(buildpack_url, true)
    return Mason::Buildpack.new(buildpack_dir)
  else
    buildpacks.each do |buildpack|
      ret = buildpack.detect(app)
      return [buildpack, ret] if ret
    end
  end
  nil
end

.install(url, ad_hoc = false) ⇒ Object



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
# File 'lib/mason/buildpacks.rb', line 9

def self.install(url, ad_hoc=false)
  root_dir = ad_hoc ? ad_hoc_root : root
  FileUtils.mkdir_p root_dir

  Dir.chdir(root_dir) do
    uri = URI.parse(url)
    if uri.path =~ /buildpack-(\w+)/
      name = $1
      name += "-#{Digest::SHA1.hexdigest(url).to_s[0 .. 8]}" if ad_hoc
      branch = uri.fragment || "master"
      if File.exists?(name)
        # Can't do a fetch here as it won't update local branches
        system "cd #{name} && git checkout master && git pull"
        raise "failed to update buildpack checkout" unless $?.exitstatus.zero?
      else
        system "git clone #{url.split('#').first} #{name} >/dev/null 2>&1"
        raise "failed to clone buildpack" unless $?.exitstatus.zero?
      end
      system "cd #{name} && git checkout #{branch} 2> /dev/null"
      raise "failed to check out branch #{branch}" unless $?.exitstatus.zero?
      File.expand_path(root_dir + "/" + name)
    else
      raise "BUILDPACK should be a url containing buildpack-NAME.git"
    end
  end
end

.root(ad_hoc = false, expand = true) ⇒ Object



43
44
45
46
# File 'lib/mason/buildpacks.rb', line 43

def self.root(ad_hoc=false, expand=true)
  dir = "~/.mason/buildpacks"
  expand ? File.expand_path(dir) : dir
end

.uninstall(name) ⇒ Object



36
37
38
39
40
41
# File 'lib/mason/buildpacks.rb', line 36

def self.uninstall(name)
  Dir.chdir(root) do
    raise "#{name} buildpack is not installed" unless File.exists?(name)
    FileUtils.rm_rf name
  end
end