|
| 1 | +require 'dry/cli' |
| 2 | +require 'cfpropertylist' |
| 3 | +require 'zip' |
| 4 | +require 'rbconfig' |
| 5 | + |
| 6 | +module EmergeCLI |
| 7 | + module Commands |
| 8 | + module Build |
| 9 | + module Distribution |
| 10 | + class ValidateApp < EmergeCLI::Commands::GlobalOptions |
| 11 | + desc 'Validate app for build distribution' |
| 12 | + |
| 13 | + option :path, type: :string, required: true, desc: 'Path to the xcarchive, IPA or APK to validate' |
| 14 | + |
| 15 | + # Constants |
| 16 | + PLIST_START = '<plist'.freeze |
| 17 | + PLIST_STOP = '</plist>'.freeze |
| 18 | + |
| 19 | + UTF8_ENCODING = 'UTF-8'.freeze |
| 20 | + STRING_FORMAT = 'binary'.freeze |
| 21 | + EMPTY_STRING = ''.freeze |
| 22 | + |
| 23 | + EXPECTED_ABI = 'arm64-v8a'.freeze |
| 24 | + |
| 25 | + def call(**options) |
| 26 | + @options = options |
| 27 | + before(options) |
| 28 | + |
| 29 | + Sync do |
| 30 | + file_extension = File.extname(@options[:path]) |
| 31 | + case file_extension |
| 32 | + when '.xcarchive' |
| 33 | + handle_xcarchive |
| 34 | + when '.ipa' |
| 35 | + handle_ipa |
| 36 | + when '.app' |
| 37 | + handle_app |
| 38 | + when '.apk' |
| 39 | + handle_apk |
| 40 | + else |
| 41 | + raise "Unknown file extension: #{file_extension}" |
| 42 | + end |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + private |
| 47 | + |
| 48 | + def handle_xcarchive |
| 49 | + raise 'Path must be an xcarchive' unless @options[:path].end_with?('.xcarchive') |
| 50 | + |
| 51 | + app_path = Dir.glob("#{@options[:path]}/Products/Applications/*.app").first |
| 52 | + run_codesign_check(app_path) |
| 53 | + read_provisioning_profile(app_path) |
| 54 | + end |
| 55 | + |
| 56 | + def handle_ipa |
| 57 | + raise 'Path must be an IPA' unless @options[:path].end_with?('.ipa') |
| 58 | + |
| 59 | + Dir.mktmpdir do |tmp_dir| |
| 60 | + Zip::File.open(@options[:path]) do |zip_file| |
| 61 | + zip_file.each do |entry| |
| 62 | + entry.extract(File.join(tmp_dir, entry.name)) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + app_path = File.join(tmp_dir, 'Payload/*.app') |
| 67 | + app_path = Dir.glob(app_path).first |
| 68 | + run_codesign_check(app_path) |
| 69 | + read_provisioning_profile(app_path) |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + def handle_app |
| 74 | + raise 'Path must be an app' unless @options[:path].end_with?('.app') |
| 75 | + |
| 76 | + app_path = @options[:path] |
| 77 | + run_codesign_check(app_path) |
| 78 | + read_provisioning_profile(app_path) |
| 79 | + end |
| 80 | + |
| 81 | + def handle_apk |
| 82 | + raise 'Path must be an APK' unless @options[:path].end_with?('.apk') |
| 83 | + |
| 84 | + apk_path = @options[:path] |
| 85 | + check_supported_abis(apk_path) |
| 86 | + end |
| 87 | + |
| 88 | + def run_codesign_check(app_path) |
| 89 | + unless RbConfig::CONFIG['host_os'] =~ /darwin/i |
| 90 | + Logger.info 'Skipping codesign check on non-macOS platform' |
| 91 | + return |
| 92 | + end |
| 93 | + |
| 94 | + command = "codesign -dvvv '#{app_path}'" |
| 95 | + Logger.debug command |
| 96 | + stdout, _, status = Open3.capture3(command) |
| 97 | + Logger.debug stdout |
| 98 | + raise '❌ Codesign check failed' unless status.success? |
| 99 | + |
| 100 | + Logger.info '✅ Codesign check passed' |
| 101 | + end |
| 102 | + |
| 103 | + def read_provisioning_profile(app_path) |
| 104 | + entitlements_path = File.join(app_path, 'embedded.mobileprovision') |
| 105 | + raise '❌ Entitlements file not found' unless File.exist?(entitlements_path) |
| 106 | + |
| 107 | + content = File.read(entitlements_path) |
| 108 | + lines = content.lines |
| 109 | + |
| 110 | + buffer = '' |
| 111 | + inside_plist = false |
| 112 | + lines.each do |line| |
| 113 | + inside_plist = true if line.include? PLIST_START |
| 114 | + if inside_plist |
| 115 | + buffer << line |
| 116 | + break if line.include? PLIST_STOP |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + encoded_plist = buffer.encode(UTF8_ENCODING, STRING_FORMAT, invalid: :replace, undef: :replace, |
| 121 | + replace: EMPTY_STRING) |
| 122 | + encoded_plist = encoded_plist.sub(/#{PLIST_STOP}.+/, PLIST_STOP) |
| 123 | + |
| 124 | + plist = CFPropertyList::List.new(data: encoded_plist) |
| 125 | + parsed_data = CFPropertyList.native_types(plist.value) |
| 126 | + |
| 127 | + expiration_date = parsed_data['ExpirationDate'] |
| 128 | + if expiration_date > Time.now |
| 129 | + Logger.info '✅ Provisioning profile hasn\'t expired' |
| 130 | + else |
| 131 | + Logger.info "❌ Provisioning profile is expired. Expiration date: #{expiration_date}" |
| 132 | + end |
| 133 | + |
| 134 | + provisions_all_devices = parsed_data['ProvisionsAllDevices'] |
| 135 | + if provisions_all_devices |
| 136 | + Logger.info 'Provisioning profile supports all devices (likely an enterprise profile)' |
| 137 | + else |
| 138 | + devices = parsed_data['ProvisionedDevices'] |
| 139 | + Logger.info 'Provisioning profile does not support all devices (likely a development profile).' |
| 140 | + Logger.info "Devices: #{devices.inspect}" |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + def check_supported_abis(apk_path) |
| 145 | + abis = [] |
| 146 | + |
| 147 | + Zip::File.open(apk_path) do |zip_file| |
| 148 | + zip_file.each do |entry| |
| 149 | + if entry.name.start_with?('lib/') && entry.name.count('/') == 2 |
| 150 | + abi = entry.name.split('/')[1] |
| 151 | + abis << abi unless abis.include?(abi) |
| 152 | + end |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + unless abis.include?(EXPECTED_ABI) |
| 157 | + raise "APK does not support #{EXPECTED_ABI} architecture, found: #{abis.join(', ')}" |
| 158 | + end |
| 159 | + |
| 160 | + Logger.info "✅ APK supports #{EXPECTED_ABI} architecture" |
| 161 | + end |
| 162 | + end |
| 163 | + end |
| 164 | + end |
| 165 | + end |
| 166 | +end |
0 commit comments