From b2d05c7cabed8a745cdd27e60fcb8a09db37e735 Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Tue, 4 Mar 2025 23:26:15 +0800 Subject: [PATCH 1/2] feat: move patcher to thor command `patch` --- CMakeLists.txt | 4 +- README.adoc | 8 ++++ exe/tebako-packager | 77 ----------------------------------- lib/tebako/cli.rb | 4 ++ lib/tebako/cli_helpers.rb | 8 ++-- lib/tebako/patch_commands.rb | 79 ++++++++++++++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 83 deletions(-) delete mode 100755 exe/tebako-packager create mode 100644 lib/tebako/patch_commands.rb diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ba10046..f24c10b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -367,7 +367,7 @@ ExternalProject_Add(${RUBY_PRJ} SOURCE_DIR ${RUBY_SOURCE_DIR} BUILD_IN_SOURCE true CONFIGURE_COMMAND "" - BUILD_COMMAND ruby ${EXE}/tebako-packager pass1 ${OSTYPE_TXT} ${RUBY_SOURCE_DIR} ${FS_MOUNT_POINT} ${DATA_SRC_DIR} ${RUBY_VER} + BUILD_COMMAND ruby -r${CMAKE_CURRENT_SOURCE_DIR}/lib/tebako -e "Tebako::Cli.start(['patch', 'pass1', '--ostype=${OSTYPE_TXT}', '--ruby-source-dir=${RUBY_SOURCE_DIR}', '--fs-mount-point=${FS_MOUNT_POINT}', '--data-src-dir=${DATA_SRC_DIR}', '--ruby-ver=${RUBY_VER}'])" # Make it for MacOS otherwise LDFLAGS are invalid # COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_LIB_DIR} COMMAND ${GNU_BASH} -c "${RUBY_SOURCE_DIR}/configure ${OPENSSL_RUBY_OPTION} ${LIBYAML_RUBY_OPTION} \ @@ -381,7 +381,7 @@ ExternalProject_Add(${RUBY_PRJ} --prefix=${DATA_SRC_DIR} \ ${C_FLAGS_DEST}=\"${RUBY_C_FLAGS}\" \ LDFLAGS=\"${RUBY_L_FLAGS}\"" - COMMAND ruby ${EXE}/tebako-packager pass2 ${OSTYPE_TXT} ${RUBY_SOURCE_DIR} ${DEPS_LIB_DIR} ${DATA_SRC_DIR} ${RUBY_STASH_DIR} ${RUBY_VER} + COMMAND ruby -r${CMAKE_CURRENT_SOURCE_DIR}/lib/tebako -e "Tebako::Cli.start(['patch', 'pass2', '--ostype=${OSTYPE_TXT}', '--ruby-source-dir=${RUBY_SOURCE_DIR}', '--deps-lib-dir=${DEPS_LIB_DIR}', '--data-src-dir=${DATA_SRC_DIR}', '--ruby-stash-dir=${RUBY_STASH_DIR}', '--ruby-ver=${RUBY_VER}'])" INSTALL_COMMAND "" ) diff --git a/README.adoc b/README.adoc index 01cb534e..1e1588ce 100644 --- a/README.adoc +++ b/README.adoc @@ -332,6 +332,14 @@ Removes Tebako Ruby artifacts. `hash`:: Calculates the Tebako script hash for use as a cache key in CI/CD environments. +`patch`:: +Ruby patching commands for low-level operations. ++ +Subcommands include: ++ +* `pass1`: Run pass1 Ruby patching operations for preparing the build environment +* `pass2`: Run pass2 Ruby patching operations for creating the packaging environment + `version`:: Displays the Tebako version. diff --git a/exe/tebako-packager b/exe/tebako-packager deleted file mode 100755 index b4fa0572..00000000 --- a/exe/tebako-packager +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -# Copyright (c) 2023-2024 [Ribose Inc](https://www.ribose.com). -# All rights reserved. -# This file is a part of tebako -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS -# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -# POSSIBILITY OF SUCH DAMAGE. - -require_relative "../lib/tebako/packager" -require_relative "../lib/tebako/ruby_version" -require_relative "../lib/tebako/stripper" -require_relative "../lib/tebako/build_helpers" - -begin - unless ARGV.length.positive? - raise Tebako::Error, "tebako-packager needs at least 1 argument (command), none has been provided." - end - - case ARGV[0] - when "pass1" - # ARGV[0] -- command - # ARGV[1] -- OSTYPE - # ARGV[2] -- RUBY_SOURCE_DIR - # ARGV[3] -- FS_MOUNT_POINT - # ARGV[4] -- DATA_SRC_DIR - # ARGV[5] -- RUBY_VER - unless ARGV.length == 6 - raise Tebako::Error, - "tebako-packager pass1 command expects 6 arguments, #{ARGV.length} has been provided." - end - ruby_ver = Tebako::RubyVersion.new(ARGV[5]) - Tebako::Packager.pass1(ARGV[1], ARGV[2], ARGV[3], ARGV[4], ruby_ver) - - when "pass2" - # ARGV[0] -- command - # ARGV[1] -- OSTYPE - # ARGV[2] -- RUBY_SOURCE_DIR - # ARGV[3] -- DEPS_LIB_DIR - # ARGV[4] -- DATA_SRC_DIR - # ARGV[5] -- RUBY_STASH_DIR - # ARGV[6] -- RUBY_VER - unless ARGV.length == 7 - raise Tebako::Error, - "tebako-packager pass2 command expects 7 arguments, #{ARGV.length} has been provided." - end - ruby_ver = Tebako::RubyVersion.new(ARGV[6]) - Tebako::Packager.pass1a(ARGV[2]) - Tebako::Packager.stash(ARGV[4], ARGV[5], ARGV[2], ruby_ver) - Tebako::Packager.pass2(ARGV[1], ARGV[2], ARGV[3], ruby_ver) - else - raise Tebako::Error, "tebako-packager cannot process #{ARGV[0]} command" - end -rescue Tebako::Error => e - puts "tebako-packager failed: #{e.message} [#{e.error_code}]" - exit(e.error_code) -end -exit(0) diff --git a/lib/tebako/cli.rb b/lib/tebako/cli.rb index 5f776208..a621bbb7 100755 --- a/lib/tebako/cli.rb +++ b/lib/tebako/cli.rb @@ -37,6 +37,7 @@ require_relative "cache_manager" require_relative "cli_helpers" require_relative "error" +require_relative "patch_commands" require_relative "ruby_version" require_relative "scenario_manager" require_relative "version" @@ -138,6 +139,9 @@ def setup exit e.error_code end + desc "patch SUBCOMMAND ...ARGS", "Ruby patching commands" + subcommand "patch", PatchCommands + def self.exit_on_failure? true end diff --git a/lib/tebako/cli_helpers.rb b/lib/tebako/cli_helpers.rb index 670f654e..f39c9e37 100644 --- a/lib/tebako/cli_helpers.rb +++ b/lib/tebako/cli_helpers.rb @@ -49,7 +49,7 @@ module CliHelpers * It is not an error but we do not recommend it because it is a way to keep packaging old versions recrsively. * * * * For example, ensure that `--root=` differs from `--output=` as described in README.adoc: * - * tebako press --root='~/projects/myproject' --entry=start.rb --output=/temp/myproject.tebako * + * `tebako press --root='~/projects/myproject' --entry=start.rb --output=/temp/myproject.tebako` * * * ****************************************************************************************************************** @@ -60,11 +60,11 @@ module CliHelpers ****************************************************************************************************************** * * * WARNING: You are creating packaging environment inside application root. * - * It is not an error but it means that all build-time artifacts will ne included in tebako package. * + * It is not an error but it means that all build-time artifacts will be included in tebako package. * * You do not need it unless under very special circumstances like tebako packaging tebako itself. * * * - * Please consider removing your exisitng `--prefix` folder abd use another one that points outside of `--root` * - * like tebako press --r ~/projects/myproject -e start.rb -o /temp/myproject.tebako -p ~/.tebako * + * Please consider removing your existing `--prefix` folder and use another one that points outside of `--root` * + * like `tebako press --r ~/projects/myproject -e start.rb -o /temp/myproject.tebako -p ~/.tebako` * * * ****************************************************************************************************************** diff --git a/lib/tebako/patch_commands.rb b/lib/tebako/patch_commands.rb new file mode 100644 index 00000000..aa8f6d4a --- /dev/null +++ b/lib/tebako/patch_commands.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +# Copyright (c) 2025 [Ribose Inc](https://www.ribose.com). +# All rights reserved. +# This file is a part of tebako +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +require "thor" +require_relative "packager" +require_relative "ruby_version" +require_relative "stripper" +require_relative "build_helpers" + +module Tebako + # Thor-based command set for Ruby patching operations + class PatchCommands < Thor + desc "pass1", "Run pass1 Ruby patching operations" + method_option :ostype, type: :string, required: true, desc: "Operating system type" + method_option :ruby_source_dir, type: :string, required: true, desc: "Ruby source directory" + method_option :fs_mount_point, type: :string, required: true, desc: "Filesystem mount point" + method_option :data_src_dir, type: :string, required: true, desc: "Data source directory" + method_option :ruby_ver, type: :string, required: true, desc: "Ruby version string" + + # Method to run pass1 operations + def pass1 + ostype = options[:ostype] + ruby_source_dir = options[:ruby_source_dir] + mount_point = options[:fs_mount_point] + data_src_dir = options[:data_src_dir] + ruby_ver_str = options[:ruby_ver] + + ruby_ver = Tebako::RubyVersion.new(ruby_ver_str) + Tebako::Packager.pass1(ostype, ruby_source_dir, mount_point, data_src_dir, ruby_ver) + end + + desc "pass2", "Run pass2 Ruby patching operations" + method_option :ostype, type: :string, required: true, desc: "Operating system type" + method_option :ruby_source_dir, type: :string, required: true, desc: "Ruby source directory" + method_option :deps_lib_dir, type: :string, required: true, desc: "Dependencies library directory" + method_option :data_src_dir, type: :string, required: true, desc: "Data source directory" + method_option :ruby_stash_dir, type: :string, required: true, desc: "Ruby stash directory" + method_option :ruby_ver, type: :string, required: true, desc: "Ruby version string" + + # Method to run pass2 operations + def pass2 # rubocop:disable Metrics/AbcSize + ostype = options[:ostype] + ruby_source_dir = options[:ruby_source_dir] + deps_lib_dir = options[:deps_lib_dir] + data_src_dir = options[:data_src_dir] + ruby_stash_dir = options[:ruby_stash_dir] + ruby_ver_str = options[:ruby_ver] + + ruby_ver = Tebako::RubyVersion.new(ruby_ver_str) + Tebako::Packager.pass1a(ruby_source_dir) + Tebako::Packager.stash(data_src_dir, ruby_stash_dir, ruby_source_dir, ruby_ver) + Tebako::Packager.pass2(ostype, ruby_source_dir, deps_lib_dir, ruby_ver) + end + end +end From 8cb2a15f95835a66054516c3803d22c5a814b19a Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Wed, 5 Mar 2025 00:48:58 +0800 Subject: [PATCH 2/2] fix: load cli issue --- CMakeLists.txt | 4 ++-- lib/tebako.rb | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f24c10b2..93c23c46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -367,7 +367,7 @@ ExternalProject_Add(${RUBY_PRJ} SOURCE_DIR ${RUBY_SOURCE_DIR} BUILD_IN_SOURCE true CONFIGURE_COMMAND "" - BUILD_COMMAND ruby -r${CMAKE_CURRENT_SOURCE_DIR}/lib/tebako -e "Tebako::Cli.start(['patch', 'pass1', '--ostype=${OSTYPE_TXT}', '--ruby-source-dir=${RUBY_SOURCE_DIR}', '--fs-mount-point=${FS_MOUNT_POINT}', '--data-src-dir=${DATA_SRC_DIR}', '--ruby-ver=${RUBY_VER}'])" + BUILD_COMMAND ruby ${EXE}/tebako patch pass1 --ostype=${OSTYPE_TXT} --ruby-source-dir=${RUBY_SOURCE_DIR} --fs-mount-point=${FS_MOUNT_POINT} --data-src-dir=${DATA_SRC_DIR} --ruby-ver=${RUBY_VER} # Make it for MacOS otherwise LDFLAGS are invalid # COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_LIB_DIR} COMMAND ${GNU_BASH} -c "${RUBY_SOURCE_DIR}/configure ${OPENSSL_RUBY_OPTION} ${LIBYAML_RUBY_OPTION} \ @@ -381,7 +381,7 @@ ExternalProject_Add(${RUBY_PRJ} --prefix=${DATA_SRC_DIR} \ ${C_FLAGS_DEST}=\"${RUBY_C_FLAGS}\" \ LDFLAGS=\"${RUBY_L_FLAGS}\"" - COMMAND ruby -r${CMAKE_CURRENT_SOURCE_DIR}/lib/tebako -e "Tebako::Cli.start(['patch', 'pass2', '--ostype=${OSTYPE_TXT}', '--ruby-source-dir=${RUBY_SOURCE_DIR}', '--deps-lib-dir=${DEPS_LIB_DIR}', '--data-src-dir=${DATA_SRC_DIR}', '--ruby-stash-dir=${RUBY_STASH_DIR}', '--ruby-ver=${RUBY_VER}'])" + COMMAND ruby ${EXE}/tebako patch pass2 --ostype=${OSTYPE_TXT} --ruby-source-dir=${RUBY_SOURCE_DIR} --deps-lib-dir=${DEPS_LIB_DIR} --data-src-dir=${DATA_SRC_DIR} --ruby-stash-dir=${RUBY_STASH_DIR} --ruby-ver=${RUBY_VER} INSTALL_COMMAND "" ) diff --git a/lib/tebako.rb b/lib/tebako.rb index 3278ae79..2451f167 100644 --- a/lib/tebako.rb +++ b/lib/tebako.rb @@ -32,3 +32,4 @@ module Tebako require_relative "tebako/version" require_relative "tebako/error" +require_relative "tebako/cli"