Class: Tebako::OptionsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/tebako/options_manager.rb

Overview

Cli helpers

Constant Summary collapse

DEFAULT_COMPRESSION_LEVEL =

rubocop:disable Metrics/ClassLength

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ OptionsManager

Returns a new instance of OptionsManager.



46
47
48
49
50
51
# File 'lib/tebako/options_manager.rb', line 46

def initialize(options)
  @options = options
  @rv = Tebako::RubyVersion.new(@options["Ruby"])
  @ruby_ver, @ruby_hash = @rv.extend_ruby_version
  @scmb = ScenarioManagerBase.new
end

Instance Attribute Details

#ruby_verObject (readonly)

Returns the value of attribute ruby_ver.



53
54
55
# File 'lib/tebako/options_manager.rb', line 53

def ruby_ver
  @ruby_ver
end

#rvObject (readonly)

Returns the value of attribute rv.



53
54
55
# File 'lib/tebako/options_manager.rb', line 53

def rv
  @rv
end

Instance Method Details

#application_cache_dirObject



88
89
90
# File 'lib/tebako/options_manager.rb', line 88

def application_cache_dir
  @application_cache_dir ||= File.join(deps, "application-cache")
end

#bundle_cache_dirObject



84
85
86
# File 'lib/tebako/options_manager.rb', line 84

def bundle_cache_dir
  @bundle_cache_dir ||= File.join(deps, "bundle-cache")
end

#bundle_formatObject



218
219
220
# File 'lib/tebako/options_manager.rb', line 218

def bundle_format
  @options.fetch("bundle-format", "layered")
end

#cfg_optionsObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tebako/options_manager.rb', line 59

def cfg_options
  ## {v_parts[3]} may be something like rc1 that won't work with CMake
  v_parts = Tebako::VERSION.split(".")
  # Cannot use 'xxx' as parameters because it does not work in Windows shells
  # So we have to use \"xxx\"
  @cfg_options ||=
    "-DCMAKE_BUILD_TYPE=Release -DRUBY_VER:STRING=\"#{@ruby_ver}\" -DRUBY_HASH:STRING=\"#{@ruby_hash}\" " \
    "-DDEPS:STRING=\"#{deps}\" -G \"#{@scmb.m_files}\" -B \"#{output_folder}\" -S \"#{source}\" " \
    "-DPACKAGE_MANIFEST:FILEPATH=\"#{package_manifest}\" " \
    "#{remove_glibc_private} -DTEBAKO_VERSION:STRING=\"#{v_parts[0]}.#{v_parts[1]}.#{v_parts[2]}\""
end

#compression_levelObject



80
81
82
# File 'lib/tebako/options_manager.rb', line 80

def compression_level
  @compression_level ||= @options.fetch("compression-level", DEFAULT_COMPRESSION_LEVEL)
end

#cwdObject



71
72
73
74
# File 'lib/tebako/options_manager.rb', line 71

def cwd
  f_cwd = @options["cwd"]&.gsub("\\", "/")
  @cwd ||= f_cwd
end

#cwd_announceObject



76
77
78
# File 'lib/tebako/options_manager.rb', line 76

def cwd_announce
  @cwd_announce ||= cwd.nil? ? "<Host current directory>" : cwd
end

#data_app_fileObject



148
149
150
# File 'lib/tebako/options_manager.rb', line 148

def data_app_file
  @data_app_file ||= File.join(data_bin_dir, "fs2.bin")
end

#data_bin_dirObject

DATA_BIN_DIR folder is used to create packaged filesystem set(DATA_BIN_DIR $CMAKE_CURRENT_BINARY_DIR/p)



130
131
132
# File 'lib/tebako/options_manager.rb', line 130

def data_bin_dir
  @data_bin_dir ||= File.join(output_folder, "p")
end

#data_bundle_fileObject

Mode File(s) Content bundle fs.bin Application both fs.bin, fs2.bin Stub, application respectively runtime fs.bin Stub app fs2.bin Application



140
141
142
# File 'lib/tebako/options_manager.rb', line 140

def data_bundle_file
  @data_bundle_file ||= File.join(data_bin_dir, "fs.bin")
end

#data_pre_dirObject

DATA_PRE_DIR folder is used to build gems that need to be packaged set(DATA_PRE_DIR $CMAKE_CURRENT_BINARY_DIR/r)



154
155
156
# File 'lib/tebako/options_manager.rb', line 154

def data_pre_dir
  @data_pre_dir ||= File.join(output_folder, "r")
end

#data_src_dirObject

DATA_SRC_DIR folder is used to collect all files that need to be packaged set(DATA_SRC_DIR $CMAKE_CURRENT_BINARY_DIR/s)



160
161
162
# File 'lib/tebako/options_manager.rb', line 160

def data_src_dir
  @data_src_dir ||= File.join(output_folder, "s")
end

#data_stub_fileObject



144
145
146
# File 'lib/tebako/options_manager.rb', line 144

def data_stub_file
  @data_stub_file ||= File.join(data_bin_dir, "fs.bin")
end

#deployment_cache?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/tebako/options_manager.rb', line 112

def deployment_cache?
  @options.fetch("deployment-cache", true)
end

#deployment_cache_dirObject



96
97
98
# File 'lib/tebako/options_manager.rb', line 96

def deployment_cache_dir
  @deployment_cache_dir ||= File.join(deps, "deployment-cache")
end

#depsObject



164
165
166
# File 'lib/tebako/options_manager.rb', line 164

def deps
  @deps ||= File.join(prefix, "deps")
end

#deps_bin_dirObject



168
169
170
# File 'lib/tebako/options_manager.rb', line 168

def deps_bin_dir
  @deps_bin_dir ||= File.join(deps, "bin")
end

#deps_lib_dirObject



172
173
174
# File 'lib/tebako/options_manager.rb', line 172

def deps_lib_dir
  @deps_lib_dir ||= File.join(deps, "lib")
end

#derive(overrides) ⇒ Object



55
56
57
# File 'lib/tebako/options_manager.rb', line 55

def derive(overrides)
  Tebako::OptionsManager.new(@options.merge(overrides))
end

#explain?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/tebako/options_manager.rb', line 116

def explain?
  @options["explain"] || false
end

#filesystem_cache_dirObject



92
93
94
# File 'lib/tebako/options_manager.rb', line 92

def filesystem_cache_dir
  @filesystem_cache_dir ||= File.join(deps, "filesystem-cache")
end

#finalized_runtime_cache_dirObject



108
109
110
# File 'lib/tebako/options_manager.rb', line 108

def finalized_runtime_cache_dir
  @finalized_runtime_cache_dir ||= File.join(deps, "finalized-runtime-cache")
end

#folder_within_root?(folder) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
179
180
181
182
183
# File 'lib/tebako/options_manager.rb', line 176

def folder_within_root?(folder)
  folder_path = Pathname.new(folder.chomp("/"))
  root_path = Pathname.new(root.chomp("/"))
  folder_path.ascend do |path|
    return true if path == root_path
  end
  false
end

#fs_currentObject



185
186
187
188
189
190
191
192
193
# File 'lib/tebako/options_manager.rb', line 185

def fs_current
  fs_current = Dir.pwd
  if @scmb.msys?
    fs_current, cygpath_res = Open3.capture2e("cygpath", "-w", fs_current)
    Tebako.packaging_error(101) unless cygpath_res.success?
    fs_current.strip!
  end
  @fs_current ||= fs_current&.gsub("\\", "/")
end

#fs_entranceObject



195
196
197
# File 'lib/tebako/options_manager.rb', line 195

def fs_entrance
  @fs_entrance ||= @options["entry-point"]&.gsub("\\", "/")
end

#handle_nil_prefixObject



199
200
201
202
203
204
205
206
207
208
# File 'lib/tebako/options_manager.rb', line 199

def handle_nil_prefix
  env_prefix = ENV.fetch("TEBAKO_PREFIX", nil)
  if env_prefix.nil?
    puts "No prefix specified, using ~/.tebako"
    File.expand_path("~/.tebako")
  else
    puts "Using TEBAKO_PREFIX environment variable as prefix"
    File.expand_path(env_prefix.gsub("\\", "/"))
  end
end

#l_levelObject



210
211
212
# File 'lib/tebako/options_manager.rb', line 210

def l_level
  @l_level ||= @options["log-level"].nil? ? "error" : @options["log-level"]
end

#layer_plan_dirObject



222
223
224
# File 'lib/tebako/options_manager.rb', line 222

def layer_plan_dir
  @layer_plan_dir ||= File.join(output_folder, "layer-plan")
end

#layer_strategyObject



214
215
216
# File 'lib/tebako/options_manager.rb', line 214

def layer_strategy
  @options.fetch("layer-strategy", "coarse")
end

#modeObject



226
227
228
# File 'lib/tebako/options_manager.rb', line 226

def mode
  @mode ||= @options["mode"].nil? ? "bundle" : @options["mode"]
end

#native_gem_cache_dirObject



104
105
106
# File 'lib/tebako/options_manager.rb', line 104

def native_gem_cache_dir
  @native_gem_cache_dir ||= File.join(deps, "native-gem-cache")
end

#output_folderObject



230
231
232
# File 'lib/tebako/options_manager.rb', line 230

def output_folder
  @output_folder ||= File.join(prefix, "o")
end

#output_type_firstObject



234
235
236
# File 'lib/tebako/options_manager.rb', line 234

def output_type_first
  @output_type_first ||= %w[both runtime].include?(mode) ? "runtime package" : "package"
end

#output_type_secondObject



238
239
240
# File 'lib/tebako/options_manager.rb', line 238

def output_type_second
  "application package"
end

#packageObject



242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/tebako/options_manager.rb', line 242

def package
  package = if @options["output"].nil?
              File.join(Dir.pwd, mode == "runtime" ? "tebako-runtime" : File.basename(fs_entrance, ".*"))
            else
              @options["output"]&.gsub("\\", "/")
            end
  @package ||= if relative?(package)
                 File.join(fs_current, package)
               else
                 package
               end
end

#package_manifestObject



124
125
126
# File 'lib/tebako/options_manager.rb', line 124

def package_manifest
  @package_manifest ||= File.join(output_folder, "package.manifest")
end

#package_within_root?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/tebako/options_manager.rb', line 255

def package_within_root?
  folder_within_root?(package)
end

#patchelf?Boolean

Returns:

  • (Boolean)


259
260
261
# File 'lib/tebako/options_manager.rb', line 259

def patchelf?
  @options["patchelf"]
end

#prefixObject



263
264
265
266
267
268
269
270
271
# File 'lib/tebako/options_manager.rb', line 263

def prefix
  @prefix ||= if @options["prefix"].nil?
                handle_nil_prefix
              elsif @options["prefix"] == "PWD"
                Dir.pwd
              else
                File.expand_path(@options["prefix"]&.gsub("\\", "/"))
              end
end

#prefix_within_root?Boolean

Returns:

  • (Boolean)


273
274
275
# File 'lib/tebako/options_manager.rb', line 273

def prefix_within_root?
  folder_within_root?(prefix)
end

#press_announce(is_msys) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/tebako/options_manager.rb', line 277

def press_announce(is_msys)
  case mode
  when "application"
    press_announce_application(is_msys)
  when "both"
    press_announce_both
  when "bundle"
    press_announce_bundle
  when "runtime"
    press_announce_runtime
  end
end

#press_announce_application(is_msys) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
# File 'lib/tebako/options_manager.rb', line 298

def press_announce_application(is_msys)
  <<~ANN
    Running tebako press at #{prefix}
       Mode:                      'application'
       Ruby version:              '#{@ruby_ver}'
       Project root:              '#{root}'
       Application entry point:   '#{fs_entrance}'
       Package file name:         '#{package}.tebako'#{press_announce_ref(is_msys)}
       Package working directory: '#{cwd_announce}'
  ANN
end

#press_announce_bothObject



310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/tebako/options_manager.rb', line 310

def press_announce_both
  <<~ANN
    Running tebako press at #{prefix}
       Mode:                      'both'
       Ruby version:              '#{@ruby_ver}'
       Project root:              '#{root}'
       Application entry point:   '#{fs_entrance}'
       Runtime file name:         '#{package}'
       Package file name:         '#{package}.tebako'
       Loging level:              '#{l_level}'
       Package working directory: '#{cwd_announce}'
  ANN
end

#press_announce_bundleObject



324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/tebako/options_manager.rb', line 324

def press_announce_bundle
  <<~ANN
    Running tebako press at #{prefix}
       Mode:                      'bundle'
       Ruby version:              '#{@ruby_ver}'
       Project root:              '#{root}'
       Application entry point:   '#{fs_entrance}'
       Package file name:         '#{package}'
       Loging level:              '#{l_level}'
       Package working directory: '#{cwd_announce}'
  ANN
end

#press_announce_ref(is_msys) ⇒ Object



290
291
292
293
294
295
296
# File 'lib/tebako/options_manager.rb', line 290

def press_announce_ref(is_msys)
  if is_msys
    " referencing runtime at '#{ref}'"
  else
    ""
  end
end

#press_announce_runtimeObject



337
338
339
340
341
342
343
344
345
# File 'lib/tebako/options_manager.rb', line 337

def press_announce_runtime
  <<~ANN
    Running tebako press at #{prefix}
       Mode:                      'runtime'
       Ruby version:              '#{@ruby_ver}'
       Runtime file name:         '#{package}'
       Loging level:              '#{l_level}'
  ANN
end

#press_optionsObject



347
348
349
# File 'lib/tebako/options_manager.rb', line 347

def press_options
  @press_options ||= "-DPCKG:STRING='#{package}' -DLOG_LEVEL:STRING='#{l_level}' " \
end

#process_gemfile(gemfile_path) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/tebako/options_manager.rb', line 351

def process_gemfile(gemfile_path)
  folder = File.dirname(gemfile_path)
  filename = File.basename(gemfile_path)
  # Change directory to the folder containing the Gemfile
  # Because Bundler::Definition.build *sometimes* requires to be in
  # the Gemfile directory
  Dir.chdir(folder) do
    @rv = Tebako::RubyVersionWithGemfile.new(@options["Ruby"], filename)
  end
  @ruby_ver, @ruby_hash = @rv.extend_ruby_version
  @ruby_src_dir = nil
end

#refObject



368
369
370
# File 'lib/tebako/options_manager.rb', line 368

def ref
  @ref ||= @options["ref"].nil? ? "tebako-runtime" : @options["ref"].gsub("\\", "/")
end

#relative?(path) ⇒ Boolean

Returns:

  • (Boolean)


364
365
366
# File 'lib/tebako/options_manager.rb', line 364

def relative?(path)
  Pathname.new(path).relative?
end

#remove_glibc_privateObject



372
373
374
375
376
377
378
# File 'lib/tebako/options_manager.rb', line 372

def remove_glibc_private
  @remove_glibc_private ||= if RUBY_PLATFORM.end_with?("linux") || RUBY_PLATFORM.end_with?("linux-gnu")
                              "-DREMOVE_GLIBC_PRIVATE=#{@options["patchelf"] ? "ON" : "OFF"}"
                            else
                              ""
                            end
end

#report_formatObject



120
121
122
# File 'lib/tebako/options_manager.rb', line 120

def report_format
  @options["report"]
end

#rootObject



380
381
382
383
384
385
386
387
# File 'lib/tebako/options_manager.rb', line 380

def root
  f_root = @options["root"].nil? ? "" : @options["root"].gsub("\\", "/")
  @root ||= if relative?(f_root)
              File.join(fs_current, f_root)
            else
              File.join(f_root, "")
            end
end

#ruby_src_dirObject



389
390
391
# File 'lib/tebako/options_manager.rb', line 389

def ruby_src_dir
  @ruby_src_dir ||= File.join(deps, "src", "_ruby_#{@ruby_ver}")
end

#runtime_deployment_cache_dirObject



100
101
102
# File 'lib/tebako/options_manager.rb', line 100

def runtime_deployment_cache_dir
  @runtime_deployment_cache_dir ||= File.join(deps, "runtime-deployment-cache")
end

#sourceObject



393
394
395
396
# File 'lib/tebako/options_manager.rb', line 393

def source
  c_path = Pathname.new(__FILE__).realpath
  @source ||= File.expand_path("../../..", c_path)
end

#stash_dir(rver = nil) ⇒ Object



398
399
400
# File 'lib/tebako/options_manager.rb', line 398

def stash_dir(rver = nil)
  @stash_dir ||= "#{stash_dir_all}_#{rver || @ruby_ver}"
end

#stash_dir_allObject



402
403
404
# File 'lib/tebako/options_manager.rb', line 402

def stash_dir_all
  @stash_dir_all ||= File.join(deps, "stash")
end