Class: Tebako::OptionsManager

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

Overview

Cli helpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ OptionsManager

rubocop:disable Metrics/ClassLength



43
44
45
46
47
48
# File 'lib/tebako/options_manager.rb', line 43

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.



50
51
52
# File 'lib/tebako/options_manager.rb', line 50

def ruby_ver
  @ruby_ver
end

#rvObject (readonly)

Returns the value of attribute rv.



50
51
52
# File 'lib/tebako/options_manager.rb', line 50

def rv
  @rv
end

Instance Method Details

#cfg_optionsObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/tebako/options_manager.rb', line 52

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}\" " \
    "#{remove_glibc_private} -DTEBAKO_VERSION:STRING=\"#{v_parts[0]}.#{v_parts[1]}.#{v_parts[2]}\""
end

#cwdObject



63
64
65
66
# File 'lib/tebako/options_manager.rb', line 63

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

#cwd_announceObject



68
69
70
# File 'lib/tebako/options_manager.rb', line 68

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

#data_app_fileObject



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

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)



74
75
76
# File 'lib/tebako/options_manager.rb', line 74

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


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

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)



98
99
100
# File 'lib/tebako/options_manager.rb', line 98

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)



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

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

#data_stub_fileObject



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

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

#depsObject



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

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

#deps_bin_dirObject



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

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

#deps_lib_dirObject



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

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

#folder_within_root?(folder) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
# File 'lib/tebako/options_manager.rb', line 120

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



129
130
131
132
133
134
135
136
137
# File 'lib/tebako/options_manager.rb', line 129

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



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

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

#handle_nil_prefixObject



143
144
145
146
147
148
149
150
151
152
# File 'lib/tebako/options_manager.rb', line 143

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



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

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

#modeObject



158
159
160
# File 'lib/tebako/options_manager.rb', line 158

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

#output_folderObject



162
163
164
# File 'lib/tebako/options_manager.rb', line 162

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

#output_type_firstObject



166
167
168
# File 'lib/tebako/options_manager.rb', line 166

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

#output_type_secondObject



170
171
172
# File 'lib/tebako/options_manager.rb', line 170

def output_type_second
  "application package"
end

#packageObject



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

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_within_root?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/tebako/options_manager.rb', line 187

def package_within_root?
  folder_within_root?(package)
end

#patchelf?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/tebako/options_manager.rb', line 191

def patchelf?
  @options["patchelf"]
end

#prefixObject



195
196
197
198
199
200
201
202
203
# File 'lib/tebako/options_manager.rb', line 195

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)


205
206
207
# File 'lib/tebako/options_manager.rb', line 205

def prefix_within_root?
  folder_within_root?(prefix)
end

#press_announce(is_msys) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/tebako/options_manager.rb', line 209

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



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/tebako/options_manager.rb', line 230

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



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

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



256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/tebako/options_manager.rb', line 256

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



222
223
224
225
226
227
228
# File 'lib/tebako/options_manager.rb', line 222

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

#press_announce_runtimeObject



269
270
271
272
273
274
275
276
277
# File 'lib/tebako/options_manager.rb', line 269

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



279
280
281
# File 'lib/tebako/options_manager.rb', line 279

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

#process_gemfile(gemfile_path) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/tebako/options_manager.rb', line 283

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



300
301
302
# File 'lib/tebako/options_manager.rb', line 300

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

#relative?(path) ⇒ Boolean

Returns:

  • (Boolean)


296
297
298
# File 'lib/tebako/options_manager.rb', line 296

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

#remove_glibc_privateObject



304
305
306
307
308
309
310
# File 'lib/tebako/options_manager.rb', line 304

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

#rootObject



312
313
314
315
316
317
318
319
# File 'lib/tebako/options_manager.rb', line 312

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



321
322
323
# File 'lib/tebako/options_manager.rb', line 321

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

#sourceObject



325
326
327
328
# File 'lib/tebako/options_manager.rb', line 325

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

#stash_dir(rver = nil) ⇒ Object



330
331
332
# File 'lib/tebako/options_manager.rb', line 330

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

#stash_dir_allObject



334
335
336
# File 'lib/tebako/options_manager.rb', line 334

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