Class: Slugbuilder::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/slugbuilder/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo:, git_ref:, stdout: $stdout) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/slugbuilder/builder.rb', line 9

def initialize(repo:, git_ref:, stdout: $stdout)
  @stdout = stdout
  @base_dir = Shellwords.escape(Slugbuilder.config.base_dir)
  @cache_dir = Shellwords.escape(Slugbuilder.config.cache_dir)
  @output_dir = Shellwords.escape(Slugbuilder.config.output_dir)
  @buildpacks_dir = File.join(@base_dir, 'buildpacks')
  @env_dir = File.join(@base_dir, 'environment')
  repo_matches = parse_git_url(repo)
  @repo = "#{repo_matches[:org]}/#{repo_matches[:name]}"
  @git_url = normalize_git_url(repo)
  @git_ref = git_ref
  @git_dir = File.join(@base_dir, 'git', @repo)
  @build_dir = File.join(@base_dir, @repo, git_ref)

  setup

  if block_given?
    yield(repo: @repo, git_ref: git_ref, git_url: @git_url)
  end
end

Instance Method Details

#build(clear_cache: false, env: {}, prebuild: nil, postbuild: nil, slug_name: nil, buildpacks: Slugbuilder.config.buildpacks) ⇒ Object



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
# File 'lib/slugbuilder/builder.rb', line 30

def build(clear_cache: false, env: {}, prebuild: nil, postbuild: nil, slug_name: nil, buildpacks: Slugbuilder.config.buildpacks)
  @old_env = ENV.to_h
  # clear environment from previous builds
  FileUtils.rm_rf(@env_dir)
  FileUtils.mkdir_p(@env_dir)

  @buildpacks = buildpacks
  @env = env.map { |k, v| [k.to_s, v.to_s] }.to_h
  @slug_file = slug_name ? "#{slug_name}.tgz" : Shellwords.escape("#{@repo.gsub('/', '.')}.#{@git_ref}.#{@git_sha}.tgz")
  wipe_cache if clear_cache

  prebuild.call(repo: @repo, git_ref: @git_ref, git_url: @git_url) if prebuild

  with_clean_env do
    build_and_release
  end
  stitle("Setup completed in #{@setup_time} seconds")
  stitle("Build completed in #{@build_time} seconds")
  stext("Application compiled in #{@compile_time} seconds")
  stext("Slug compressed in #{@slug_time} seconds")
  stitle("Slug built to #{File.join(@output_dir, @slug_file)}")
  stats = {
    setup: @setup_time,
    build: @build_time,
    compile: @compile_time,
    slug: @slug_time,
    output: build_output.join('')
  }

  postbuild.call(repo: @repo, git_ref: @git_ref, git_sha: @git_sha, git_url: @git_url, request_id: @request_id, stats: stats, slug: File.join(@output_dir, @slug_file)) if postbuild
  if block_given?
    yield(repo: @repo, git_ref: @git_ref, git_sha: @git_sha, git_url: @git_url, request_id: @request_id, stats: stats, slug: File.join(@output_dir, @slug_file))
  end
  return true
rescue => e
  stitle("Failed: #{e}\n")
  return false
ensure
  restore_env
end