lib/gym/runner.rb in gym-0.1.0 vs lib/gym/runner.rb in gym-0.2.0

- old
+ new

@@ -7,10 +7,11 @@ def run clear_old_files build_app verify_archive package_app + swift_library_fix move_results end private @@ -53,11 +54,11 @@ # Builds the app and prepares the archive def build_app command = BuildCommandGenerator.generate print_command(command, "Generated Build Command") if $verbose - execute_command(command: command, print_all: true, error: proc do |output| + Gym::CommandsExecutor.execute(command: command, print_all: true, error: proc do |output| ErrorHandler.handle_build_error(output) end) Helper.log.info("Successfully stored the archive. You can find it in the Xcode Organizer.".green) Helper.log.info("Stored the archive in: ".green + BuildCommandGenerator.archive_path) if $verbose @@ -72,15 +73,51 @@ def package_app command = PackageCommandGenerator.generate print_command(command, "Generated Package Command") if $verbose - execute_command(command: command, print_all: false, error: proc do |output| + Gym::CommandsExecutor.execute(command: command, print_all: false, error: proc do |output| ErrorHandler.handle_package_error(output) end) end + # Determine whether it is a Swift project and, eventually, include all required libraries to copy from Xcode's toolchain directory. + # Since there's no "xcodebuild" target to do just that, it is done post-build when exporting an archived build. + def swift_library_fix + require 'fileutils' + + ipa_swift_frameworks = Dir["#{PackageCommandGenerator.appfile_path}/Frameworks/libswift*"] + + unless ipa_swift_frameworks.empty? + Dir.mktmpdir do |tmpdir| + # Copy all necessary Swift libraries to a temporary "SwiftSupport" directory so that we can + # easily add it to the .ipa later. + swift_support = File.join(tmpdir, "SwiftSupport") + + Dir.mkdir(swift_support) + + developer_dir = `xcode-select --print-path`.strip + ipa_swift_frameworks.each do |path| + framework = File.basename(path) + + FileUtils.copy_file("#{developer_dir}/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos/#{framework}", File.join(swift_support, framework)) + end + + # Add "SwiftSupport" to the .ipa archive + Dir.chdir(tmpdir) do + command_parts = ["zip --recurse-paths #{PackageCommandGenerator.ipa_path} SwiftSupport"] + command_parts << "> /dev/null" unless $verbose + print_command(command_parts, "Fix Swift embedded code if needed") if $verbose + + Gym::CommandsExecutor.execute(command: command_parts, print_all: false, error: proc do |output| + ErrorHandler.handle_package_error(output) + end) + end + end + end + end + # Moves over the binary and dsym file to the output directory # @return (String) The path to the resulting ipa file def move_results require 'fileutils' @@ -106,61 +143,8 @@ ipa_path = File.join(Gym.config[:output_directory], File.basename(PackageCommandGenerator.ipa_path)) Helper.log.info "Successfully exported and signed ipa file:".green Helper.log.info ipa_path ipa_path - end - - ##################################################### - # @!group Actually executing the commands - ##################################################### - - # @param command [String] The command to be executed - # @param print_all [Boolean] Do we want to print out the command output while building? - # If set to false, nothing will be printed - # @param error [Block] A block that's called if an error occurs - # @return [String] All the output as string - def execute_command(command: nil, print_all: false, error: nil) - print_all = true if $verbose - - output = [] - command = command.join(" ") - Helper.log.info command.yellow.strip unless Gym.config[:silent] - - puts "\n-----".cyan if print_all - - last_length = 0 - begin - PTY.spawn(command) do |stdin, stdout, pid| - stdin.each do |l| - line = l.strip # strip so that \n gets removed - output << line - - next unless print_all - - current_length = line.length - spaces = [last_length - current_length, 0].max - print((line + " " * spaces + "\r").cyan) - last_length = current_length - end - Process.wait(pid) - puts "-----\n".cyan if print_all - end - rescue => ex - # This could happen when the environment is wrong: - # > invalid byte sequence in US-ASCII (ArgumentError) - output << ex.to_s - o = output.join("\n") - puts o - error.call(o) - end - - # Exit status for build command, should be 0 if build succeeded - # Disabled Rubocop, since $CHILD_STATUS just is not the same - if $?.exitstatus != 0 # rubocop:disable Style/SpecialGlobalVars - o = output.join("\n") - puts o # the user has the right to see the raw output - error.call(o) - end end end end