Class: MRubyPortable::Docker

Inherits:
Object
  • Object
show all
Defined in:
lib/mruby_portable/docker.rb

Constant Summary collapse

IMAGE_REPOSITORY =
"mruby-portable-builder"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner: Runner.new, image: "#{IMAGE_REPOSITORY}:#{VERSION}") ⇒ Docker

Returns a new instance of Docker.



9
10
11
12
# File 'lib/mruby_portable/docker.rb', line 9

def initialize(runner: Runner.new, image: "#{IMAGE_REPOSITORY}:#{VERSION}")
  @runner = runner
  @image = image
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



7
8
9
# File 'lib/mruby_portable/docker.rb', line 7

def image
  @image
end

#runnerObject (readonly)

Returns the value of attribute runner.



7
8
9
# File 'lib/mruby_portable/docker.rb', line 7

def runner
  @runner
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/mruby_portable/docker.rb', line 14

def available?
  runner.success?(["docker", "version"])
end

#build_imageObject



28
29
30
# File 'lib/mruby_portable/docker.rb', line 28

def build_image
  runner.run!(build_image_command)
end

#build_image_commandObject



36
37
38
# File 'lib/mruby_portable/docker.rb', line 36

def build_image_command
  ["docker", "build", "-t", image, Paths.template("docker")]
end

#ensure_image!Object

Raises:



22
23
24
25
26
# File 'lib/mruby_portable/docker.rb', line 22

def ensure_image!
  raise Error, "Docker is not available" unless available?

  build_image unless image_exists?
end

#image_exists?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/mruby_portable/docker.rb', line 18

def image_exists?
  runner.success?(["docker", "image", "inspect", image])
end

#run_build(workspace) ⇒ Object



32
33
34
# File 'lib/mruby_portable/docker.rb', line 32

def run_build(workspace)
  runner.run!(run_build_command(workspace))
end

#run_build_command(workspace) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mruby_portable/docker.rb', line 40

def run_build_command(workspace)
  script = [
    "if [ -f app_bundle.rb ]; then /usr/local/mruby/build/host/bin/mrbc -B mruby_portable_app_irep -o generated_irep.c app_bundle.rb; fi",
    "cmake -S . -B build -DMRUBY_BUILD_DIR=/usr/local/mruby/build/psp_without_gems",
    "cmake --build build"
  ].join(" && ")

  [
    "docker", "run", "--rm",
    "-v", "#{File.expand_path(workspace)}:/workspace",
    "-w", "/workspace",
    image,
    "bash", "-lc", script
  ]
end