Class: Ocran::DirBuilder

Inherits:
Object
  • Object
show all
Includes:
BuildConstants
Defined in:
lib/ocran/dir_builder.rb

Overview

Builder that outputs all files to a plain directory instead of a self-extracting executable. A launch script (.sh on POSIX, .bat on Windows) is written at the root of the directory so the packaged app can be started directly.

Constant Summary collapse

WINDOWS =
Gem.win_platform?

Constants included from BuildConstants

BuildConstants::BINDIR, BuildConstants::EXTRACT_ROOT, BuildConstants::GEMDIR, BuildConstants::LIBDIR, BuildConstants::SRCDIR

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) {|_self| ... } ⇒ DirBuilder

Returns a new instance of DirBuilder.

Yields:

  • (_self)

Yield Parameters:



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ocran/dir_builder.rb', line 20

def initialize(path)
  @path = Pathname(path)
  @path.mkpath
  @env = {}
  @exec_args = nil
  @symlinks = []
  @data_size = 0

  yield(self) if block_given?

  finalize
end

Instance Attribute Details

#data_sizeObject (readonly)

Returns the value of attribute data_size.



15
16
17
# File 'lib/ocran/dir_builder.rb', line 15

def data_size
  @data_size
end

#envObject (readonly)

Recorded launch data (environment variables and exec arguments), used by Direction to build the optional wrapper executable.



18
19
20
# File 'lib/ocran/dir_builder.rb', line 18

def env
  @env
end

#exec_argsObject (readonly)

Recorded launch data (environment variables and exec arguments), used by Direction to build the optional wrapper executable.



18
19
20
# File 'lib/ocran/dir_builder.rb', line 18

def exec_args
  @exec_args
end

Class Method Details

.create_zip(zip_path, source_dir) ⇒ Object

Create a zip archive from a source directory. Uses the zip command on POSIX and PowerShell on Windows.



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ocran/dir_builder.rb', line 60

def self.create_zip(zip_path, source_dir)
  zip_path = File.expand_path(zip_path.to_s)
  if Gem.win_platform?
    system("powershell", "-NoProfile", "-Command",
           "Compress-Archive -Path '#{source_dir}\\*' -DestinationPath '#{zip_path}'",
           exception: true)
  else
    Dir.chdir(source_dir) do
      system("zip", "-r", zip_path, ".", exception: true)
    end
  end
end

Instance Method Details

#cp(source, target) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/ocran/dir_builder.rb', line 37

def cp(source, target)
  dest = @path / target
  dest.dirname.mkpath
  src = source.to_s
  FileUtils.cp(src, dest.to_s)
  @data_size += File.size(src)
end

#exec(image, script, *argv) ⇒ Object



53
54
55
56
# File 'lib/ocran/dir_builder.rb', line 53

def exec(image, script, *argv)
  raise "Script is already set" if @exec_args
  @exec_args = [image.to_s, script.to_s, argv.map(&:to_s)]
end

#export(name, value) ⇒ Object



49
50
51
# File 'lib/ocran/dir_builder.rb', line 49

def export(name, value)
  @env[name.to_s] = value.to_s
end

#mkdir(target) ⇒ Object



33
34
35
# File 'lib/ocran/dir_builder.rb', line 33

def mkdir(target)
  (@path / target).mkpath
end


45
46
47
# File 'lib/ocran/dir_builder.rb', line 45

def symlink(link_path, target)
  @symlinks << [link_path.to_s, target.to_s]
end