Module: Headdesk::CliCommands::Unpack

Includes:
Headdesk::CliCommand
Defined in:
lib/headdesk/cli_commands/unpack.rb

Overview

Unpack an APK/IPA

Class Method Summary collapse

Methods included from Headdesk::CliCommand

each

Class Method Details

.included(thor) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/headdesk/cli_commands/unpack.rb', line 15

def self.included(thor)
  thor.class_eval do
    desc 'unpack FILE [DESTINATION]', 'Unpack an APK or IPA to [DESTINATION] or to the current directory'
    method_option :analyze, type: :boolean, aliases: '-a'
    def unpack(file, destination = nil)
      # Make sure the input file exists
      unless File.exist?(file)
        STDERR.puts "Could not find: #{file}"
        CLI.command_help(Thor::Base.shell.new, 'unpack')
        exit 1
      end

      # Make sure destination exists, if specified
      unless !destination || Dir.exist?(destination)
        STDERR.puts "Could not find destination path: #{destination}"
        CLI.command_help(Thor::Base.shell.new, 'unpack')
        exit 1
      end

      begin
        stdout = nil
        output_path = destination

        if !destination
          # Output to tempdir, then copy to cwd if no destination specified
          Dir.mktmpdir do |tmp_dir|
            output_path = tmp_dir
            stdout = Headdesk::ApkTool.unpack_to(file, tmp_dir)
            FileUtils.cp_r("#{tmp_dir}/.", Dir.pwd)
          end
        else
          stdout = Headdesk::ApkTool.unpack_to(file, destination)
        end

        # analyze if requested
        Headdesk::Analyze.at(output_path) if options[:analyze]

        print_update_message unless Headdesk::Versions.latest_version?
      rescue CliError => cli_err
        STDERR.puts cli_err.message
        CLI.command_help(Thor::Base.shell.new, 'unpack')
        exit 1
      rescue StandardError => rb_err
        STDERR.puts rb_err.message.red
        STDERR.puts rb_err.backtrace.ai
        exit 1
      end
    end
  end
end