Skip to content

Commit

Permalink
chore: issue warning when packaging in-place
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Jan 10, 2025
1 parent c1332b9 commit 230cc86
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 32 deletions.
66 changes: 41 additions & 25 deletions lib/tebako/cli_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,47 +41,52 @@
module Tebako
# Cli helpers
module CliHelpers
WARN = <<~WARN
******************************************************************************************************************
* *
* WARNING: You are packaging in-place, i.e.: tebako package will be placed inside application root. *
* It is not an error but we do not recommend it because it is a way to keep packaging old versions recrsively. *
* *
******************************************************************************************************************
WARN

def do_press(options_manager)
scenario_manager = Tebako::ScenarioManager.new(options_manager.root, options_manager.fs_entrance)
puts options_manager.press_announce(scenario_manager.msys?)

if options_manager.mode == "both" || options_manager.mode == "runtime" || options_manager.mode == "bundle"
do_press_runtime(options_manager, scenario_manager)
end

if options_manager.mode == "both" || options_manager.mode == "application"
do_press_application(options_manager, scenario_manager)
if options_manager.package_within_root?
puts WARN
sleep 5
end

true
do_press_runtime(options_manager, scenario_manager)
do_press_application(options_manager, scenario_manager)
end

def do_press_application(options_manager, scenario_manager)
return unless %w[both application].include?(options_manager.mode)

packager = Tebako::PackagerLite.new(options_manager, scenario_manager)
packager.create_package
end

def do_press_runtime(options_manager, scenario_manager)
return unless %w[both runtime bundle].include?(options_manager.mode)

generate_files(options_manager, scenario_manager)
cfg_cmd = "cmake -DSETUP_MODE:BOOLEAN=OFF #{options_manager.cfg_options} #{options_manager.press_options}"
build_cmd = "cmake --build #{options_manager.output_folder} --target tebako --parallel #{Etc.nprocessors}"
merged_env = ENV.to_h.merge(options_manager.b_env)
Tebako.packaging_error(103) unless system(merged_env, cfg_cmd)
Tebako.packaging_error(104) unless system(merged_env, build_cmd)
true
Tebako.packaging_error(103) unless system(merged_env, press_cfg_cmd(options_manager))
Tebako.packaging_error(104) unless system(merged_env, press_build_cmd(options_manager))
end

def do_setup(options_manager)
puts "Setting up tebako packaging environment"

cfg_cmd = "cmake -DSETUP_MODE:BOOLEAN=ON #{options_manager.cfg_options}"
build_cmd = "cmake --build \"#{options_manager.output_folder}\" --target setup --parallel #{Etc.nprocessors}"
merged_env = ENV.to_h.merge(options_manager.b_env)
Tebako.packaging_error(101) unless system(merged_env, cfg_cmd)
Tebako.packaging_error(102) unless system(merged_env, build_cmd)

fix_mkdwarfs_permissions(options_manager)
true
Tebako.packaging_error(101) unless system(merged_env, setup_cfg_cmd(options_manager))
Tebako.packaging_error(102) unless system(merged_env, setup_build_cmd(options_manager))
end

def generate_files(options_manager, scenario_manager)
Expand All @@ -93,16 +98,11 @@ def generate_files(options_manager, scenario_manager)
Tebako::Codegen.generate_tebako_fs_cpp(options_manager, scenario_manager)
Tebako::Codegen.generate_deploy_rb(options_manager, scenario_manager)

return unless options_manager.mode == "both" || options_manager.mode == "runtime"
return unless %w[both runtime].include?(options_manager.mode)

Tebako::Codegen.generate_stub_rb(options_manager)
end

def fix_mkdwarfs_permissions(options_manager)
scenario_manager = Tebako::ScenarioManager.new(options_manager.root, options_manager.fs_entrance)
FileUtils.chmod("+x", File.join(options_manager.deps_bin_dir, "mkdwarfs#{scenario_manager.exe_suffix}"))
end

def options_from_tebafile(tebafile)
::YAML.load_file(tebafile)["options"] || {}
rescue Psych::SyntaxError => e
Expand All @@ -114,5 +114,21 @@ def options_from_tebafile(tebafile)
puts e.message
{}
end

def press_build_cmd(options_manager)
"cmake --build #{options_manager.output_folder} --target tebako --parallel #{Etc.nprocessors}"
end

def press_cfg_cmd(options_manager)
"cmake -DSETUP_MODE:BOOLEAN=OFF #{options_manager.cfg_options} #{options_manager.press_options}"
end

def setup_build_cmd(options_manager)
"cmake --build \"#{options_manager.output_folder}\" --target setup --parallel #{Etc.nprocessors}"
end

def setup_cfg_cmd(options_manager)
"cmake -DSETUP_MODE:BOOLEAN=ON #{options_manager.cfg_options}"
end
end
end
2 changes: 1 addition & 1 deletion lib/tebako/deploy_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def initialize(fs_root, fs_entrance, target_dir, pre_dir)
@fs_entrance = fs_entrance
@target_dir = target_dir
@pre_dir = pre_dir
@verbose = ENV["VERBOSE"] == "yes" || ENV["VERBOSE"] == "true"
@verbose = %w[yes true].include?(ENV.fetch("VERBOSE", nil))
@ncores = BuildHelpers.ncores
end

Expand Down
9 changes: 9 additions & 0 deletions lib/tebako/options_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ def package
end
end

def package_within_root?
package_path = Pathname.new(package.chomp("/"))
root_path = Pathname.new(root.chomp("/"))
package_path.ascend do |path|
return true if path == root_path
end
false
end

def prefix
@prefix ||= if @options["prefix"].nil?
handle_nil_prefix
Expand Down
1 change: 1 addition & 0 deletions lib/tebako/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def init(stash_dir, src_dir, pre_dir, bin_dir)

def mkdwarfs(deps_bin_dir, data_bin_file, data_src_dir, descriptor = nil)
puts "-- Running mkdwarfs script"
FileUtils.chmod("a+x", Dir.glob(File.join(deps_bin_dir, "mkdwarfs*")))
params = [File.join(deps_bin_dir, "mkdwarfs"), "-o", data_bin_file, "-i", data_src_dir, "--no-progress"]
params << "--header" << descriptor if descriptor
BuildHelpers.run_with_capture_v(params)
Expand Down
32 changes: 26 additions & 6 deletions spec/cli_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
{ "output" => "/path/to/output", "deps" => "/path/to/deps", "entry-point" => "entrypoint",
"root" => "/tmp/path/to/root/" }
end
let(:ruby_ver) { "3.2.5" }
let(:ruby_hash) { Tebako::RubyVersion::RUBY_VERSIONS["3.2.5"] }
let(:ruby_ver) { "3.2.6" }
let(:ruby_hash) { Tebako::RubyVersion::RUBY_VERSIONS["3.2.6"] }

before do
allow_any_instance_of(Pathname).to receive(:realpath) { |instance| instance }
Expand All @@ -62,7 +62,7 @@

it "executes the press command successfully" do
allow_any_instance_of(Tebako::PackagerLite).to receive(:create_package).and_return(true)
expect(do_press(options_manager)).to be_truthy
expect { do_press(options_manager) }.not_to raise_error
end
end

Expand All @@ -79,7 +79,7 @@
allow(Tebako::Codegen).to receive(:generate_tebako_version_h).and_return(true)
allow(Tebako::Codegen).to receive(:generate_tebako_fs_cpp).and_return(true)

expect(do_press(options_manager)).to be_truthy
expect { do_press(options_manager) }.not_to raise_error
end

it "raises an error if the press command fails" do
Expand All @@ -103,7 +103,7 @@
allow(Tebako::Codegen).to receive(:generate_tebako_fs_cpp).and_return(true)
allow(Tebako::Codegen).to receive(:generate_package_header).and_return(true)

expect(do_press(options_manager)).to be_truthy
expect { do_press(options_manager) }.not_to raise_error
end

it "raises an error if the press command fails" do
Expand All @@ -112,6 +112,26 @@
expect { do_press(options_manager) }.to raise_error(Tebako::Error)
end
end

context "when package_within_root? is true" do
before do
options["mode"] = "bundle"
options["output"] = "/tmp/path/to/root/output"
end

let(:options_manager) { Tebako::OptionsManager.new(options) }

it "shows a warning and executes the press command successfully" do
allow(FileUtils).to receive(:rm_rf)
allow(self).to receive(:system).and_return(true)
allow(Tebako::Codegen).to receive(:generate_tebako_version_h).and_return(true)
allow(Tebako::Codegen).to receive(:generate_tebako_fs_cpp).and_return(true)
allow(Tebako::Codegen).to receive(:generate_package_header).and_return(true)

allow(self).to receive(:sleep).with(any_args).and_return(nil)
expect { do_press(options_manager) }.to output(/WARNING/).to_stdout
end
end
end

describe "#do_setup" do
Expand All @@ -125,7 +145,7 @@
it "executes the setup command successfully" do
allow(FileUtils).to receive(:rm_rf)
allow(self).to receive(:system).and_return(true)
expect(do_setup(options_manager)).to be_truthy
expect { do_setup(options_manager) }.not_to raise_error
end

it "raises an error if the setup command fails" do
Expand Down
Loading

0 comments on commit 230cc86

Please sign in to comment.