using System.Collections.Generic; using System.IO; using System.Text; using UnityEditor; using UnityEditor.Build; using UnityModule.Settings; namespace XcodeArchiver { public class PostprocessBuild : IPostprocessBuild { public const int POSTPROCESS_BUILD_CALLBACK_ORDER = 100; /// /// xcodebuild コマンドのパス /// private const string PATH_XCODEBUILD_BIN = "/usr/bin/xcodebuild"; /// /// 出力オプション種別 /// public enum ExportOptionType { AppStore, AdHoc, Enterprise, Development, } /// /// 出力オプションと plist のファイル名のマップ /// private static readonly Dictionary EXPORT_OPTION_MAP = new Dictionary() { { ExportOptionType.AppStore , "app-store" }, { ExportOptionType.AdHoc , "ad-hoc" }, { ExportOptionType.Enterprise , "enterprise" }, { ExportOptionType.Development, "development" }, }; /// /// 出力オプションと出力先ディレクトリ名のマップ /// private static readonly Dictionary EXPORT_DIRECTORY_MAP = new Dictionary() { { ExportOptionType.AppStore, "export-app-store" }, { ExportOptionType.AdHoc , "export-ad-hoc" }, }; /// /// パスの実体 /// private string exportedPath; /// /// パス /// public string ExportedPath { get { return this.exportedPath; } set { this.exportedPath = value; } } public int callbackOrder { get { return POSTPROCESS_BUILD_CALLBACK_ORDER; } } public void OnPostprocessBuild(BuildTarget target, string path) { if (target != BuildTarget.iOS) { return; } if (!EnvironmentSetting.Instance.ShouldRunXcodeArchive) { return; } this.ExportedPath = path; this.Prepare(); this.ExecuteBuildAndArchive(); this.ExecuteExport(ExportOptionType.AdHoc); if (EnvironmentSetting.Instance.ShouldExportAppStoreArchive && !EditorUserBuildSettings.development) { this.ExecuteExport(ExportOptionType.AppStore); } } private void Prepare() { this.GenerateExportOptionsPlist(ExportOptionType.AppStore); this.GenerateExportOptionsPlist(ExportOptionType.AdHoc); } private void ExecuteBuildAndArchive() { StringBuilder sb = new StringBuilder(); // xcworkspace 利用可否は当該ディレクトリの有無で判定 sb.AppendFormat( Directory.Exists(string.Format("{0}/Unity-iPhone.xcworkspace", this.ExportedPath)) ? " -workspace \"{0}/Unity-iPhone.xcworkspace\"" : " -project \"{0}/Unity-iPhone.xcodeproj\"", this.ExportedPath ); sb.AppendFormat(" -scheme \"Unity-iPhone\""); sb.AppendFormat(" -archivePath \"{0}/Unity-iPhone.xcarchive\" ", this.ExportedPath); sb.AppendFormat(" -sdk iphoneos"); sb.AppendFormat(" -configuration Release"); sb.AppendFormat(" -allowProvisioningUpdates"); sb.AppendFormat(" clean archive"); sb.AppendFormat(" CODE_SIGN_IDENTITY=\"iPhone Developer\""); sb.AppendFormat(" DEVELOPMENT_TEAM=\"{0}\"", PlayerSettings.iOS.appleDeveloperTeamID); if (!string.IsNullOrEmpty(PlayerSettings.iOS.iOSManualProvisioningProfileID)) { sb.AppendFormat(" PROVISIONING_PROFILE=\"{0}\"", PlayerSettings.iOS.iOSManualProvisioningProfileID); } System.Diagnostics.Process process = new System.Diagnostics.Process { StartInfo = { FileName = PATH_XCODEBUILD_BIN, Arguments = sb.ToString(), CreateNoWindow = true } }; process.Start(); process.WaitForExit(); process.Close(); } private void ExecuteExport(ExportOptionType exportOptionType) { StringBuilder sb = new StringBuilder(); sb.AppendFormat(" -exportArchive"); sb.AppendFormat(" -archivePath \"{0}/Unity-iPhone.xcarchive\" ", this.ExportedPath); sb.AppendFormat(" -exportPath \"{0}/{1}\"", this.ExportedPath, EXPORT_DIRECTORY_MAP[exportOptionType]); sb.AppendFormat(" -exportOptionsPlist \"{0}/{1}.plist\"", this.ExportedPath, EXPORT_OPTION_MAP[exportOptionType]); sb.AppendFormat(" -allowProvisioningUpdates"); System.Diagnostics.Process process = new System.Diagnostics.Process { StartInfo = { FileName = PATH_XCODEBUILD_BIN, Arguments = sb.ToString(), CreateNoWindow = true } }; process.Start(); process.WaitForExit(); process.Close(); } private void GenerateExportOptionsPlist(ExportOptionType exportOptionType) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("\n"); sb.AppendFormat("\n"); sb.AppendFormat("\n"); sb.AppendFormat("\n"); sb.AppendFormat("\tmethod\n"); sb.AppendFormat("\t{0}\n", EXPORT_OPTION_MAP[exportOptionType]); // 一部の広告 SDK が Bitcode サポートしていないことがあったりする。 sb.AppendFormat("\tcompileBitcode\n"); sb.AppendFormat("\t\n"); // iOS のオンデマンドリソース対応はデフォルト Off で良いかと。 sb.AppendFormat("\tembedOnDemandResourcesAssetPacksInBundle\n"); sb.AppendFormat("\t\n"); sb.AppendFormat("\n"); sb.AppendFormat("\n"); StreamWriter w = new StreamWriter(string.Format("{0}/{1}.plist", this.ExportedPath, EXPORT_OPTION_MAP[exportOptionType])); w.Write(sb.ToString()); w.Close(); } } }