require 'ruby_apk' Zip.warn_invalid_date = false module Fastlane module Actions module SharedValues APP_VERSION = :APP_VERSION BUILD_NUMBER = :BUILD_NUMBER end class ExtractVersionAction < Action def self.run(config) platform = Actions.lane_context[Actions::SharedValues::PLATFORM_NAME].to_sym # Gets version from ipa/apk app_version, build_number = extract_version(platform, config) Actions.lane_context[SharedValues::APP_VERSION] = app_version ENV[SharedValues::APP_VERSION.to_s] = app_version Actions.lane_context[SharedValues::BUILD_NUMBER] = build_number ENV[SharedValues::BUILD_NUMBER.to_s] = build_number.to_s UI.success("Successfully extracted app version: '#{Actions.lane_context[SharedValues::APP_VERSION]}', build number: #{Actions.lane_context[SharedValues::BUILD_NUMBER]}") return app_version, build_number end def self.extract_version(platform, config) case platform when :ios extract_version_from_ipa(config[:ipa]) if validate_ios(config) when :android extract_version_from_apk(config[:apk]) if validate_android(config) end end ######## ios specific def self.validate_ios(config) UI.user_error!("No IPA file path given, pass using `ipa: 'ipa path'`") unless config[:ipa].to_s.length > 0 true end def self.extract_version_from_ipa(ipa_file) info = FastlaneCore::IpaFileAnalyser.fetch_info_plist_file(ipa_file) return info['CFBundleShortVersionString'], info['CFBundleVersion'] end ######## android specific def self.validate_android(config) UI.user_error!("No APK file path given, pass using `apk: 'apk path'`") unless config[:apk].to_s.length > 0 true end def self.extract_version_from_apk(apk_file) apk = Android::Apk.new(apk_file) return apk.manifest.version_name, apk.manifest.version_code end def self.description "Extracts application version and build number from .ipa/.apk" end def self.available_options [ FastlaneCore::ConfigItem.new(key: :ipa, env_name: "", description: ".ipa file to extract icon", optional: true, default_value: Actions.lane_context[SharedValues::IPA_OUTPUT_PATH]), FastlaneCore::ConfigItem.new(key: :apk, env_name: "", description: ".apk file to extract icon", optional: true, default_value: Actions.lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]) ] end def self.output [ ['APP_VERSION', 'App version extracted from .ipa/.apk'], ['BUILD_NUMBER', 'Build number extracted from .ipa/.apk'] ] end def self.author "Piotrek Dubiel" end def self.is_supported?(platform) [:ios, :android].include? platform end end end end