Class: BoringBuilder::BoringCache

Inherits:
Object
  • Object
show all
Defined in:
lib/boringbuilder/boring_cache.rb

Constant Summary collapse

CLI_IMAGE =
"ghcr.io/boringcache/base:bookworm-v1.19.4@" \
"sha256:84d1e168a50fd086ecd4fdb2fd96f689982a6f3c61454acd8c7189e336f8f3ae"
BUILD_IMAGE =
"ghcr.io/boringcache/base:bookworm-build-v1.19.4@" \
"sha256:df4461581214c0052157041238a67b8da420e1399e4024bc857c68c74a9721cb"
TOKEN_NAMES =
%w[BORINGCACHE_RESTORE_TOKEN BORINGCACHE_SAVE_TOKEN].freeze
ENVIRONMENT_NAMES =
%w[BORINGCACHE_API_URL BORINGCACHE_DEFAULT_WORKSPACE BORINGCACHE_WORKSPACE].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, client, environment: ENV) ⇒ BoringCache

Returns a new instance of BoringCache.



14
15
16
17
18
# File 'lib/boringbuilder/boring_cache.rb', line 14

def initialize(project, client, environment: ENV)
  @project = project
  @client = client
  @environment = environment
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'lib/boringbuilder/boring_cache.rb', line 12

def client
  @client
end

#environmentObject (readonly)

Returns the value of attribute environment.



12
13
14
# File 'lib/boringbuilder/boring_cache.rb', line 12

def environment
  @environment
end

#projectObject (readonly)

Returns the value of attribute project.



12
13
14
# File 'lib/boringbuilder/boring_cache.rb', line 12

def project
  @project
end

Instance Method Details

#artifact_publishable?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/boringbuilder/boring_cache.rb', line 54

def artifact_publishable?
  save_token? && (workspace? || project_config?)
end

#cli_imageObject



65
66
67
# File 'lib/boringbuilder/boring_cache.rb', line 65

def cli_image
  environment.fetch("BORINGBUILDER_BORINGCACHE_IMAGE", CLI_IMAGE)
end

#enabled?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/boringbuilder/boring_cache.rb', line 20

def enabled?
  token? && (workspace? || project_config?)
end

#modeObject



50
51
52
# File 'lib/boringbuilder/boring_cache.rb', line 50

def mode
  enabled? ? :boringcache : :local
end

#prepare(container, workdir: project.application_path) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/boringbuilder/boring_cache.rb', line 24

def prepare(container, workdir: project.application_path)
  return container unless enabled?

  container = container.with_file("/usr/local/bin/boringcache", cli_file, permissions: 0o755)
  container = container.with_file("#{workdir}/.boringcache.toml", project_config_file) if project_config?
  container = with_environment(container)
  with_secrets(container)
end

#prepare_artifact_publisher(container, workdir: "/workspace") ⇒ Object



58
59
60
61
62
63
# File 'lib/boringbuilder/boring_cache.rb', line 58

def prepare_artifact_publisher(container, workdir: "/workspace")
  container = container.with_workdir(workdir)
  container = container.with_file("#{workdir}/.boringcache.toml", project_config_file) if project_config?
  container = with_environment(container)
  with_secrets(container)
end

#save_token?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/boringbuilder/boring_cache.rb', line 69

def save_token?
  !value("BORINGCACHE_SAVE_TOKEN").nil?
end

#workspace_configured?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/boringbuilder/boring_cache.rb', line 73

def workspace_configured?
  workspace? || project_config?
end

#wrap(command, entry: "bundler") ⇒ Object



33
34
35
36
37
# File 'lib/boringbuilder/boring_cache.rb', line 33

def wrap(command, entry: "bundler")
  return command unless enabled?

  wrapped(command, "--entry", entry)
end

#wrap_mount(command, tag:, path:) ⇒ Object



39
40
41
# File 'lib/boringbuilder/boring_cache.rb', line 39

def wrap_mount(command, tag:, path:)
  wrap_mounts(command, tag => path)
end

#wrap_mounts(command, mounts) ⇒ Object



43
44
45
46
47
48
# File 'lib/boringbuilder/boring_cache.rb', line 43

def wrap_mounts(command, mounts)
  return command unless enabled?

  cache_arguments = mounts.flat_map { |tag, path| ["--manual-entry", "#{tag}:#{path}"] }
  wrapped(command, *cache_arguments)
end