static void TestExecuteTask(PDictionary input, PropertyListEditorAction action, string entry, string type, string value, PObject expected)
        {
            var task = new PropertyListEditor {
                PropertyList = Path.GetTempFileName(),
                BuildEngine  = new TestEngine(),
                Action       = action.ToString(),
                Entry        = entry,
                Type         = type,
                Value        = value
            };

            input.Save(task.PropertyList);

            try {
                if (expected == null)
                {
                    Assert.IsFalse(task.Execute(), "Task was expected to fail.");
                    return;
                }

                Assert.IsTrue(task.Execute(), "Task was expected to execute successfully.");

                var output = PObject.FromFile(task.PropertyList);

                Assert.AreEqual(expected.Type, output.Type, "Task produced the incorrect plist output.");

                CheckValue(output, expected);
            } finally {
                File.Delete(task.PropertyList);
            }
        }
Example #2
0
        void TestExecuteTask(PDictionary input, PropertyListEditorAction action, string entry, string type, string value, PObject expected)
        {
            var task = CreateTask <PropertyListEditor> ();

            task.PropertyList = Path.Combine(Cache.CreateTemporaryDirectory(), "propertyList.plist");
            task.Action       = action.ToString();
            task.Entry        = entry;
            task.Type         = type;
            task.Value        = value;
            input.Save(task.PropertyList);

            if (expected == null)
            {
                Assert.IsFalse(task.Execute(), "Task was expected to fail.");
                return;
            }

            Assert.IsTrue(task.Execute(), "Task was expected to execute successfully.");

            var output = PObject.FromFile(task.PropertyList);

            Assert.AreEqual(expected.Type, output.Type, "Task produced the incorrect plist output.");

            CheckValue(output, expected);
        }
Example #3
0
        public static void SetConfiguredSdkLocation(string location)
        {
            PDictionary plist;
            bool        binary;

            try {
                plist = PDictionary.FromFile(SettingsPath, out binary);
            } catch (FileNotFoundException) {
                plist  = new PDictionary();
                binary = false;
            }

            if (!string.IsNullOrEmpty(location))
            {
                plist.SetString("AppleSdkRoot", location);
            }
            else
            {
                plist.Remove("AppleSdkRoot");
            }

            plist.Save(SettingsPath, true, binary);

            //Init ();
            //var changed = Changed;
            //if (changed != null)
            //	changed ();
        }
 static void Save(PDictionary plist)
 {
     try {
         Directory.CreateDirectory(Path.GetDirectoryName(IndexFileName));
         plist.Save(IndexFileName, true, true);
     } catch (Exception ex) {
         LoggingService.LogWarning("Failed to save '{0}': {1}", IndexFileName, ex);
     }
 }
        void SaveMetaFile(int count, long size)
        {
            var meta = new PDictionary();

            meta.Add("RecordCount", new PNumber(count));
            meta.Add("StandardDirectoryPerms", new PNumber(16877));
            meta.Add("StandardFilePerms", new PNumber(-32348));
            meta.Add("TotalUncompressedBytes", new PNumber((int)size));
            meta.Add("Version", new PNumber(2));

            Directory.CreateDirectory(Path.Combine(Source.ItemSpec, "META-INF"));
            meta.Save(Path.Combine(Source.ItemSpec, "META-INF", "com.apple.ZipMetadata.plist"), true, true);
        }
Example #6
0
        public virtual void ConfigureTask()
        {
            Task = CreateTask <CompileAppManifest> ();

            Task.AppBundleName    = appBundleName;
            Task.AppBundleDir     = "AppBundlePath";
            Task.AssemblyName     = assemblyName;
            Task.AppManifest      = CreateTempFile("foo.plist");
            Task.BundleIdentifier = bundleIdentifier;
            Task.SdkPlatform      = "iPhoneSimulator";

            Plist = new PDictionary();
            Plist ["CFBundleDisplayName"] = displayName;
            Plist ["CFBundleIdentifier"]  = identifier;
            Plist.Save(Task.AppManifest);
        }
Example #7
0
        public virtual void ConfigureTask()
        {
            Task = CreateTask <CompileAppManifest> ();

            Task.ApplicationId       = identifier;
            Task.AppBundleName       = appBundleName;
            Task.CompiledAppManifest = new TaskItem(Path.Combine(Cache.CreateTemporaryDirectory(), "AppBundlePath", "Info.plist"));
            Task.AssemblyName        = assemblyName;
            Task.AppManifest         = new TaskItem(CreateTempFile("foo.plist"));
            Task.SdkPlatform         = "iPhoneSimulator";
            Task.SdkVersion          = "10.0";

            Plist = new PDictionary();
            Plist ["CFBundleDisplayName"] = displayName;
            Plist ["CFBundleIdentifier"]  = bundleIdentifier;
            Plist.Save(Task.AppManifest.ItemSpec);
        }
        public void ErrorWithMismatchedInfoPlistMinimumOSVersion()
        {
            var dir  = Cache.CreateTemporaryDirectory();
            var task = CreateTask(dir);

            var plist = new PDictionary();

            plist.SetMinimumOSVersion("10.0");
            var manifest = Path.Combine(dir, "Info.plist");

            plist.Save(manifest);
            task.AppManifest = new TaskItem(manifest);
            task.SupportedOSPlatformVersion = "11.0";

            ExecuteTask(task, expectedErrorCount: 1);
            Assert.AreEqual("The MinimumOSVersion value in the Info.plist (10.0) does not match the SupportedOSPlatformVersion value (11.0) in the project file (if there is no SupportedOSPlatformVersion value in the project file, then a default value has been assumed). Either change the value in the Info.plist to match the SupportedOSPlatformVersion value, or remove the value in the Info.plist (and add a SupportedOSPlatformVersion value to the project file if it doesn't already exist).", Engine.Logger.ErrorEvents [0].Message);
        }
Example #9
0
        public virtual void ConfigureTask()
        {
            Task = CreateTask <CompileAppManifest> ();

            Task.AppBundleName = appBundleName;
            Task.AppManifestBundleDirectory = Path.Combine(Cache.CreateTemporaryDirectory(), "AppBundlePath");
            Task.AssemblyName     = assemblyName;
            Task.AppManifest      = CreateTempFile("foo.plist");
            Task.BundleIdentifier = bundleIdentifier;
            Task.MinimumOSVersion = string.Empty;
            Task.SdkPlatform      = "iPhoneSimulator";

            Plist = new PDictionary();
            Plist ["CFBundleDisplayName"] = displayName;
            Plist ["CFBundleIdentifier"]  = identifier;
            Plist.Save(Task.AppManifest);
        }
Example #10
0
        public void NotMatching_VersionBuildNumbers()
        {
            var warningCount = Engine.Logger.WarningsEvents.Count;

            // Warning: The App Extension has a CFBundleShortVersionString
            // that does not match the main app bundle's CFBundleShortVersionString.
            sourcePlist.SetCFBundleShortVersionString("1");
            warningCount++;

            // Warning: The App Extension has a CFBundleVersion
            // that does not match the main app bundle's CFBundleVersion.
            sourcePlist.SetCFBundleVersion("1");
            warningCount++;

            sourcePlist.Save(mainAppPlistPath);
            Assert.True(task.Execute(), "#1");               // No build error.
            Assert.AreEqual(warningCount, Engine.Logger.WarningsEvents.Count, "#2");
        }
        public void MainMinimumOSVersions()
        {
            var dir  = Cache.CreateTemporaryDirectory();
            var task = CreateTask(dir);

            var mainPath = Path.Combine(dir, "Info.plist");
            var main     = new PDictionary();

            main.SetMinimumOSVersion("14.0");
            main.Save(mainPath);

            task.AppManifest = new TaskItem(mainPath);

            ExecuteTask(task);

            var plist = PDictionary.FromFile(task.CompiledAppManifest.ItemSpec);

            Assert.AreEqual("14.0", plist.GetMinimumOSVersion(), "MinimumOSVersion");
        }
Example #12
0
        ReadAppManifest CreateTask(ApplePlatform platform = ApplePlatform.iOS, Action <PDictionary>?createDictionary = null)
        {
            var tmpdir = Cache.CreateTemporaryDirectory();

            var plistPath = Path.Combine(tmpdir, "TemporaryAppManifest.plist");
            var plist     = new PDictionary();

            if (createDictionary != null)
            {
                createDictionary(plist);
            }
            plist.Save(plistPath);

            var task = CreateTask <ReadAppManifest> ();

            task.AppManifest            = new TaskItem(plistPath);
            task.SdkVersion             = Sdks.GetAppleSdk(platform).GetInstalledSdkVersions(false).First().ToString();
            task.TargetFrameworkMoniker = TargetFramework.GetTargetFramework(platform, true).ToString();

            return(task);
        }
Example #13
0
        public override bool Execute()
        {
            Log.LogTaskName ("Archive");
            Log.LogTaskProperty ("AppBundleDir", AppBundleDir);
            Log.LogTaskProperty ("AppExtensionReferences", AppExtensionReferences);
            Log.LogTaskProperty ("ITunesSourceFiles", ITunesSourceFiles);
            Log.LogTaskProperty ("OutputPath", OutputPath);
            Log.LogTaskProperty ("ProjectName", ProjectName);
            Log.LogTaskProperty ("SigningKey", SigningKey);
            Log.LogTaskProperty ("SolutionPath", SolutionPath);

            var archiveDir = CreateArchiveDirectory ();

            try {
                var plist = PDictionary.FromFile (Path.Combine (AppBundleDir.ItemSpec, "Info.plist"));
                var productsDir = Path.Combine (archiveDir, "Products");

                // Archive the OnDemandResources...
                var resourcesDestDir = Path.Combine (productsDir, "OnDemandResources");
                var resourcesSrcDir = Path.Combine (OutputPath, "OnDemandResources");

                if (Directory.Exists (resourcesSrcDir))
                    Ditto (resourcesSrcDir, resourcesDestDir);

                // Archive the Applications...
                var appDestDir = Path.Combine (productsDir, "Applications", Path.GetFileName (AppBundleDir.ItemSpec));
                Ditto (AppBundleDir.ItemSpec, appDestDir);

                // Archive the dSYMs...
                var dsymsDestDir = Path.Combine (archiveDir, "dSYMs", Path.GetFileName (DSYMDir));
                Ditto (DSYMDir, dsymsDestDir);

                // Archive the Bitcode symbol maps
                var bcSymbolMaps = Directory.GetFiles (Path.GetDirectoryName (DSYMDir), "*.bcsymbolmap");
                if (bcSymbolMaps.Length > 0) {
                    var bcSymbolMapsDir = Path.Combine (archiveDir, "BCSymbolMaps");

                    Directory.CreateDirectory (bcSymbolMapsDir);

                    for (int i = 0; i < bcSymbolMaps.Length; i++)
                        File.Copy (bcSymbolMaps[i], Path.Combine (bcSymbolMapsDir, Path.GetFileName (bcSymbolMaps[i])));
                }

                if (AppExtensionReferences != null) {
                    // Archive the dSYMs for each of the referenced App Extensions as well...
                    for (int i = 0; i < AppExtensionReferences.Length; i++)
                        ArchiveAppExtension (AppExtensionReferences[i], archiveDir);
                }

                if (ITunesSourceFiles != null) {
                    // Archive the iTunesMetadata.plist and iTunesArtwork files...
                    var iTunesMetadataDir = Path.Combine (archiveDir, "iTunesMetadata", Path.GetFileName (AppBundleDir.ItemSpec));
                    for (int i = 0; i < ITunesSourceFiles.Length; i++) {
                        var archivedMetaFile = Path.Combine (iTunesMetadataDir, Path.GetFileName (ITunesSourceFiles[i].ItemSpec));

                        Directory.CreateDirectory (iTunesMetadataDir);
                        File.Copy (ITunesSourceFiles[i].ItemSpec, archivedMetaFile, true);
                    }
                }

                // Generate an archive Info.plist
                var arInfo = new PDictionary ();
                // FIXME: figure out this value
                //arInfo.Add ("AppStoreFileSize", new PNumber (65535));

                var props = new PDictionary ();
                props.Add ("ApplicationPath", new PString (string.Format ("Applications/{0}", Path.GetFileName (AppBundleDir.ItemSpec))));
                props.Add ("CFBundleIdentifier", new PString (plist.GetCFBundleIdentifier ()));
                if (plist.GetCFBundleShortVersionString () != null)
                    props.Add ("CFBundleShortVersionString", new PString (plist.GetCFBundleShortVersionString ()));
                else if (plist.GetCFBundleVersion () != null)
                    props.Add ("CFBundleShortVersionString", new PString (plist.GetCFBundleVersion ()));

                var iconFiles = plist.GetCFBundleIconFiles ();
                var iconDict = plist.GetCFBundleIcons ();
                var icons = new PArray ();

                if (iconFiles != null)
                    AddIconPaths (icons, iconFiles, Path.Combine (archiveDir, "Products"));

                if (iconDict != null) {
                    var primary = iconDict.Get<PDictionary> (ManifestKeys.CFBundlePrimaryIcon);
                    if (primary != null && (iconFiles = primary.GetCFBundleIconFiles ()) != null)
                        AddIconPaths (icons, iconFiles, Path.Combine (archiveDir, "Products"));
                }

                if (icons.Count > 0)
                    props.Add ("IconPaths", icons);

                props.Add ("SigningIdentity", new PString (SigningKey));

                arInfo.Add ("ApplicationProperties", props);
                arInfo.Add ("ArchiveVersion", new PNumber (2));
                arInfo.Add ("CreationDate", new PDate (Now.ToUniversalTime ()));
                arInfo.Add ("Name", new PString (plist.GetCFBundleName () ?? plist.GetCFBundleDisplayName ()));
                arInfo.Add ("SchemeName", new PString (ProjectName));

                if (!string.IsNullOrEmpty (SolutionPath)) {
                    arInfo.Add ("SolutionName", new PString (Path.GetFileNameWithoutExtension (SolutionPath)));
                    arInfo.Add ("SolutionPath", new PString (SolutionPath));
                }

                arInfo.Save (Path.Combine (archiveDir, "Info.plist"));

                ArchiveDir = archiveDir;
            } catch (Exception ex) {
                Log.LogErrorFromException (ex);
                Directory.Delete (archiveDir, true);
            }

            return !Log.HasLoggedErrors;
        }
Example #14
0
        bool Compile(PDictionary plist)
        {
            var currentSDK = IPhoneSdks.GetSdk(Framework);

            if (!currentSDK.SdkIsInstalled(sdkVersion, SdkIsSimulator))
            {
                Log.LogError(null, null, null, null, 0, 0, 0, 0, "The {0} SDK for '{1}' is not installed.", Framework, sdkVersion);
                return(false);
            }

            supportedDevices = plist.GetUIDeviceFamily();

            if (!IsWatchApp)
            {
                var version = IPhoneSdks.MonoTouch.ExtendedVersion;
                // This key is our supported way of determining if an app
                // was built with Xamarin, so it needs to be present in all apps.

                var dict = new PDictionary();
                dict.Add("Version", new PString(string.Format("{0} ({1}: {2})", version.Version, version.Branch, version.Hash)));
                plist.Add("com.xamarin.ios", dict);
            }

            var sdkSettings = currentSDK.GetSdkSettings(sdkVersion, SdkIsSimulator);
            var dtSettings  = currentSDK.GetDTSettings();

            SetValue(plist, ManifestKeys.BuildMachineOSBuild, dtSettings.BuildMachineOSBuild);
            plist.SetIfNotPresent(ManifestKeys.CFBundleDevelopmentRegion, "en");

            plist.SetIfNotPresent(ManifestKeys.CFBundleExecutable, AssemblyName);
            if (IsIOS)
            {
                var executable = plist.GetCFBundleExecutable();
                if (executable.EndsWith(".app", StringComparison.Ordinal))
                {
                    LogAppManifestError("The executable (CFBundleExecutable) name ({0}) cannot end with '.app', because iOS may fail to launch the app.", executable);
                }
            }

            if (IsIOS)
            {
                if (minimumOSVersion < IPhoneSdkVersion.V5_0 && plist.GetUIMainStoryboardFile(true) != null)
                {
                    LogAppManifestError("Applications using a storyboard as the Main Interface must have a deployment target greater than 5.0");
                }

                if (!plist.ContainsKey(ManifestKeys.CFBundleName))
                {
                    plist [ManifestKeys.CFBundleName] = plist.ContainsKey(ManifestKeys.CFBundleDisplayName) ? plist.GetString(ManifestKeys.CFBundleDisplayName).Clone() : new PString(AppBundleName);
                }
            }
            else
            {
                plist.SetIfNotPresent(ManifestKeys.CFBundleName, AppBundleName);
            }

            plist.SetIfNotPresent(ManifestKeys.CFBundleIdentifier, BundleIdentifier);
            plist.SetIfNotPresent(ManifestKeys.CFBundleInfoDictionaryVersion, "6.0");
            plist.SetIfNotPresent(ManifestKeys.CFBundlePackageType, IsAppExtension ? "XPC!" : "APPL");
            if (!string.IsNullOrEmpty(ResourceRules))
            {
                plist.SetIfNotPresent(ManifestKeys.CFBundleResourceSpecification, Path.GetFileName(ResourceRules));
            }
            plist.SetIfNotPresent(ManifestKeys.CFBundleSignature, "????");
            if (!plist.ContainsKey(ManifestKeys.CFBundleSupportedPlatforms))
            {
                plist[ManifestKeys.CFBundleSupportedPlatforms] = new PArray {
                    SdkPlatform
                }
            }
            ;
            plist.SetIfNotPresent(ManifestKeys.CFBundleVersion, "1.0");
            plist.SetIfNotPresent(ManifestKeys.CFBundleShortVersionString, plist.GetCFBundleVersion());

            if (!SdkIsSimulator)
            {
                SetValue(plist, "DTCompiler", sdkSettings.DTCompiler);
                SetValue(plist, "DTPlatformBuild", dtSettings.DTPlatformBuild);
                SetValue(plist, "DTSDKBuild", sdkSettings.DTSDKBuild);
            }

            plist.SetIfNotPresent("DTPlatformName", SdkPlatform.ToLowerInvariant());
            if (!SdkIsSimulator)
            {
                SetValue(plist, "DTPlatformVersion", dtSettings.DTPlatformVersion);
            }

            var sdkName = sdkSettings.CanonicalName;

            // older sdksettings didn't have a canonicalname for sim
            if (SdkIsSimulator && string.IsNullOrEmpty(sdkName))
            {
                var deviceSdkSettings = currentSDK.GetSdkSettings(sdkVersion, false);
                sdkName = deviceSdkSettings.AlternateSDK;
            }
            SetValue(plist, "DTSDKName", sdkName);

            if (!SdkIsSimulator)
            {
                SetValue(plist, "DTXcode", AppleSdkSettings.DTXcode);
                SetValue(plist, "DTXcodeBuild", dtSettings.DTXcodeBuild);
            }

            SetDeviceFamily(plist);

            plist.SetIfNotPresent(ManifestKeys.MinimumOSVersion, minimumOSVersion.ToString());

            // Remove any Xamarin Studio specific keys
            plist.Remove(ManifestKeys.XSLaunchImageAssets);
            plist.Remove(ManifestKeys.XSAppIconAssets);

            // Merge with any partial plists generated by the Asset Catalog compiler...
            MergePartialPlistTemplates(plist);

            if (IsIOS)
            {
                Validation(plist);
            }

            CompiledAppManifest = new TaskItem(Path.Combine(AppBundleDir, "Info.plist"));
            plist.Save(CompiledAppManifest.ItemSpec, true, true);

            return(!Log.HasLoggedErrors);
        }

        void SetDeviceFamily(PDictionary plist)
        {
            switch (Framework)
            {
            case PlatformFramework.iOS:
                SetIOSDeviceFamily(plist);
                break;

            case PlatformFramework.WatchOS:
                plist.SetUIDeviceFamily(IPhoneDeviceType.Watch);
                break;

            case PlatformFramework.TVOS:
                plist.SetUIDeviceFamily(IPhoneDeviceType.TV);
                break;
            }
        }

        void SetIOSDeviceFamily(PDictionary plist)
        {
            if (IsWatchApp)
            {
                if (SdkIsSimulator)
                {
                    plist.SetUIDeviceFamily(IPhoneDeviceType.IPhone | IPhoneDeviceType.Watch);
                }
                else
                {
                    plist.SetUIDeviceFamily(IPhoneDeviceType.Watch);
                }
            }
            else
            {
                if (!IsAppExtension)
                {
                    plist.SetIfNotPresent(ManifestKeys.LSRequiresIPhoneOS, true);
                }

                if (minimumOSVersion >= IPhoneSdkVersion.V3_2 && supportedDevices == IPhoneDeviceType.NotSet)
                {
                    plist.SetUIDeviceFamily(IPhoneDeviceType.IPhone);
                }
            }
        }

        void Validation(PDictionary plist)
        {
            var supportsIPhone = (supportedDevices & IPhoneDeviceType.IPhone) != 0 ||
                                 supportedDevices == IPhoneDeviceType.NotSet;
            var supportsIPad = (supportedDevices & IPhoneDeviceType.IPad) != 0;

            // Validation...
            if (!IsAppExtension && sdkVersion >= IPhoneSdkVersion.V3_2)
            {
                IPhoneOrientation orientation;

                if (supportsIPhone)
                {
                    orientation = plist.GetUISupportedInterfaceOrientations(false);
                    if (orientation == IPhoneOrientation.None)
                    {
                        LogAppManifestWarning("Supported iPhone orientations have not been set");
                    }
                    else if (!orientation.IsValidPair())
                    {
                        LogAppManifestWarning("Supported iPhone orientations are not matched pairs");
                    }
                }

                if (supportsIPad)
                {
                    orientation = plist.GetUISupportedInterfaceOrientations(true);
                    if (orientation == IPhoneOrientation.None)
                    {
                        LogAppManifestWarning("Supported iPad orientations have not been set");
                    }
                    else if (!orientation.IsValidPair())
                    {
                        LogAppManifestWarning("Supported iPad orientations are not matched pairs");
                    }
                }
            }
        }
    }
Example #15
0
        public override bool Execute()
        {
            var intermediate = Path.Combine (IntermediateOutputPath, ToolName);
            var intermediateBundleDir = Path.Combine (intermediate, "bundle");
            var manifest = new TaskItem (Path.Combine (intermediate, "asset-manifest.plist"));
            var bundleResources = new List<ITaskItem> ();
            var outputManifests = new List<ITaskItem> ();
            var catalogs = new List<ITaskItem> ();
            var unique = new HashSet<string> ();
            string bundleIdentifier = null;
            var knownSpecs = new HashSet<string> ();
            var specs = new PArray ();
            int rc;

            Log.LogTaskName ("ACTool");
            Log.LogTaskProperty ("AppManifest", AppManifest);
            Log.LogTaskProperty ("DeviceModel", DeviceModel);
            Log.LogTaskProperty ("DeviceOSVersion", DeviceOSVersion);
            Log.LogTaskProperty ("ImageAssets", ImageAssets);
            Log.LogTaskProperty ("IntermediateOutputPath", IntermediateOutputPath);
            Log.LogTaskProperty ("IsWatchApp", IsWatchApp);
            Log.LogTaskProperty ("OptimizePNGs", OptimizePNGs);
            Log.LogTaskProperty ("OutputPath", OutputPath);
            Log.LogTaskProperty ("ProjectDir", ProjectDir);
            Log.LogTaskProperty ("ResourcePrefix", ResourcePrefix);
            Log.LogTaskProperty ("SdkBinPath", SdkBinPath);
            Log.LogTaskProperty ("SdkPlatform", SdkPlatform);
            Log.LogTaskProperty ("SdkVersion", SdkVersion);

            switch (SdkPlatform) {
            case "iPhoneSimulator":
            case "iPhoneOS":
            case "MacOSX":
            case "WatchSimulator":
            case "WatchOS":
            case "AppleTVSimulator":
            case "AppleTVOS":
                break;
            default:
                Log.LogError ("Unrecognized platform: {0}", SdkPlatform);
                return false;
            }

            if (AppManifest != null) {
                try {
                    plist = PDictionary.FromFile (AppManifest.ItemSpec);
                } catch (Exception ex) {
                    Log.LogError (null, null, null, AppManifest.ItemSpec, 0, 0, 0, 0, "{0}", ex.Message);
                    return false;
                }

                bundleIdentifier = plist.GetCFBundleIdentifier ();
            }

            foreach (var asset in ImageAssets) {
                var vpath = BundleResource.GetVirtualProjectPath (ProjectDir, asset);
                if (Path.GetFileName (vpath) != "Contents.json")
                    continue;

                // get the parent (which will typically be .appiconset, .launchimage, .imageset, .iconset, etc)
                var catalog = Path.GetDirectoryName (vpath);

                // keep walking up the directory structure until we get to the .xcassets directory
                while (!string.IsNullOrEmpty (catalog) && Path.GetExtension (catalog) != ".xcassets")
                    catalog = Path.GetDirectoryName (catalog);

                if (string.IsNullOrEmpty (catalog)) {
                    Log.LogWarning (null, null, null, asset.ItemSpec, 0, 0, 0, 0, "Asset not part of an asset catalog: {0}", asset.ItemSpec);
                    continue;
                }

                if (unique.Add (catalog))
                    catalogs.Add (new TaskItem (catalog));

                if (AppleSdkSettings.XcodeVersion.Major >= 7 && !string.IsNullOrEmpty (bundleIdentifier) && SdkPlatform != "WatchSimulator") {
                    var text = File.ReadAllText (asset.ItemSpec);

                    if (string.IsNullOrEmpty (text))
                        continue;

                    var json = JsonConvert.DeserializeObject (text) as JObject;

                    if (json == null)
                        continue;

                    var properties = json.Property ("properties");

                    if (properties == null)
                        continue;

                    var resourceTags = properties.Value.ToObject<JObject> ().Property ("on-demand-resource-tags");

                    if (resourceTags == null || resourceTags.Value.Type != JTokenType.Array)
                        continue;

                    var tagArray = resourceTags.Value.ToObject<JArray> ();
                    var tags = new HashSet<string> ();
                    string hash;

                    foreach (var tag in tagArray.Select (token => token.ToObject<string> ()))
                        tags.Add (tag);

                    var tagList = tags.ToList ();
                    tagList.Sort ();

                    var path = AssetPackUtils.GetAssetPackDirectory (intermediate, bundleIdentifier, tagList, out hash);

                    if (knownSpecs.Add (hash)) {
                        var assetpack = new PDictionary ();
                        var ptags = new PArray ();

                        Directory.CreateDirectory (path);

                        for (int i = 0; i < tags.Count; i++)
                            ptags.Add (new PString (tagList[i]));

                        assetpack.Add ("bundle-id", new PString (string.Format ("{0}.asset-pack-{1}", bundleIdentifier, hash)));
                        assetpack.Add ("bundle-path", new PString (Path.GetFullPath (path)));
                        assetpack.Add ("tags", ptags);
                        specs.Add (assetpack);
                    }
                }
            }

            if (catalogs.Count == 0) {
                // There are no (supported?) asset catalogs
                return true;
            }

            partialAppManifest = new TaskItem (Path.Combine (intermediate, "partial-info.plist"));

            if (specs.Count > 0) {
                outputSpecs = Path.Combine (intermediate, "output-specifications.plist");
                specs.Save (outputSpecs, true);
            }

            var output = new TaskItem (intermediateBundleDir);

            Directory.CreateDirectory (intermediateBundleDir);

            // Note: Compile() will set the PartialAppManifest property if it is used...
            if ((rc = Compile (catalogs.ToArray (), output, manifest)) != 0) {
                if (File.Exists (manifest.ItemSpec)) {
                    try {
                        var log = PDictionary.FromFile (manifest.ItemSpec);

                        LogWarningsAndErrors (log, catalogs[0]);
                    } catch (FormatException) {
                        Log.LogError ("actool exited with code {0}", rc);
                    }

                    File.Delete (manifest.ItemSpec);
                }

                return false;
            }

            if (PartialAppManifest != null && !File.Exists (PartialAppManifest.GetMetadata ("FullPath")))
                Log.LogError ("Partial Info.plist file was not generated: {0}", PartialAppManifest.GetMetadata ("FullPath"));

            try {
                var manifestOutput = PDictionary.FromFile (manifest.ItemSpec);

                LogWarningsAndErrors (manifestOutput, catalogs[0]);

                bundleResources.AddRange (GetCompiledBundleResources (manifestOutput, intermediateBundleDir));
                outputManifests.Add (manifest);
            } catch (Exception ex) {
                Log.LogError ("Failed to load output manifest for {0} for the file {2}: {1}", ToolName, ex.Message, manifest.ItemSpec);
            }

            foreach (var assetpack in specs.OfType<PDictionary> ()) {
                var path = Path.Combine (assetpack.GetString ("bundle-path").Value, "Info.plist");
                var bundlePath = PathUtils.AbsoluteToRelative (intermediate, path);
                var outputPath = Path.Combine (OutputPath, bundlePath);
                var rpath = Path.Combine (intermediate, bundlePath);
                var dict = new PDictionary ();

                dict.SetCFBundleIdentifier (assetpack.GetString ("bundle-id").Value);
                dict.Add ("Tags", assetpack.GetArray ("tags").Clone ());

                dict.Save (path, true, true);

                var item = new TaskItem (rpath);
                item.SetMetadata ("LogicalName", bundlePath);
                item.SetMetadata ("OutputPath", outputPath);
                item.SetMetadata ("Optimize", "false");

                bundleResources.Add (item);
            }

            BundleResources = bundleResources.ToArray ();
            OutputManifests = outputManifests.ToArray ();

            return !Log.HasLoggedErrors;
        }
Example #16
0
        bool Compile(PDictionary plist)
        {
            var currentSDK = IPhoneSdks.GetSdk (Framework);

            if (!currentSDK.SdkIsInstalled (sdkVersion, SdkIsSimulator)) {
                Log.LogError (null, null, null, null, 0, 0, 0, 0, "The {0} SDK for '{1}' is not installed.", Framework, sdkVersion);
                return false;
            }

            supportedDevices = plist.GetUIDeviceFamily ();

            if (!IsWatchApp) {
                var version = IPhoneSdks.MonoTouch.ExtendedVersion;
                // This key is our supported way of determining if an app
                // was built with Xamarin, so it needs to be present in all apps.

                var dict = new PDictionary ();
                dict.Add ("Version", new PString (string.Format ("{0} ({1}: {2})", version.Version, version.Branch, version.Hash)));
                plist.Add ("com.xamarin.ios", dict);
            }

            var sdkSettings = currentSDK.GetSdkSettings (sdkVersion, SdkIsSimulator);
            var dtSettings = currentSDK.GetDTSettings ();

            SetValue (plist, ManifestKeys.BuildMachineOSBuild, dtSettings.BuildMachineOSBuild);
            plist.SetIfNotPresent (ManifestKeys.CFBundleDevelopmentRegion, "en");

            plist.SetIfNotPresent (ManifestKeys.CFBundleExecutable, AssemblyName);
            if (IsIOS) {
                var executable = plist.GetCFBundleExecutable ();
                if (executable.EndsWith (".app", StringComparison.Ordinal))
                    LogAppManifestError ("The executable (CFBundleExecutable) name ({0}) cannot end with '.app', because iOS may fail to launch the app.", executable);
            }

            if (IsIOS) {
                if (minimumOSVersion < IPhoneSdkVersion.V5_0 && plist.GetUIMainStoryboardFile (true) != null)
                    LogAppManifestError ("Applications using a storyboard as the Main Interface must have a deployment target greater than 5.0");

                if (!plist.ContainsKey (ManifestKeys.CFBundleName))
                    plist [ManifestKeys.CFBundleName] = plist.ContainsKey (ManifestKeys.CFBundleDisplayName) ? plist.GetString (ManifestKeys.CFBundleDisplayName).Clone () : new PString (AppBundleName);
            } else {
                plist.SetIfNotPresent (ManifestKeys.CFBundleName, AppBundleName);
            }

            plist.SetIfNotPresent (ManifestKeys.CFBundleIdentifier, BundleIdentifier);
            plist.SetIfNotPresent (ManifestKeys.CFBundleInfoDictionaryVersion, "6.0");
            plist.SetIfNotPresent (ManifestKeys.CFBundlePackageType, IsAppExtension ? "XPC!" : "APPL");
            if (!string.IsNullOrEmpty (ResourceRules))
                plist.SetIfNotPresent (ManifestKeys.CFBundleResourceSpecification, Path.GetFileName (ResourceRules));
            plist.SetIfNotPresent (ManifestKeys.CFBundleSignature, "????");
            if (!plist.ContainsKey (ManifestKeys.CFBundleSupportedPlatforms))
                plist[ManifestKeys.CFBundleSupportedPlatforms] = new PArray { SdkPlatform };
            plist.SetIfNotPresent (ManifestKeys.CFBundleVersion, "1.0");

            if (!SdkIsSimulator) {
                SetValue (plist, "DTCompiler", sdkSettings.DTCompiler);
                SetValue (plist, "DTPlatformBuild", dtSettings.DTPlatformBuild);
                SetValue (plist, "DTSDKBuild", sdkSettings.DTSDKBuild);
            }

            plist.SetIfNotPresent ("DTPlatformName", SdkPlatform.ToLowerInvariant ());
            if (!SdkIsSimulator)
                SetValue (plist, "DTPlatformVersion", dtSettings.DTPlatformVersion);

            var sdkName = sdkSettings.CanonicalName;
            // older sdksettings didn't have a canonicalname for sim
            if (SdkIsSimulator && string.IsNullOrEmpty (sdkName)) {
                var deviceSdkSettings = currentSDK.GetSdkSettings (sdkVersion, false);
                sdkName = deviceSdkSettings.AlternateSDK;
            }
            SetValue (plist, "DTSDKName", sdkName);

            if (!SdkIsSimulator) {
                SetValue (plist, "DTXcode", AppleSdkSettings.DTXcode);
                SetValue (plist, "DTXcodeBuild", dtSettings.DTXcodeBuild);
            }

            SetDeviceFamily (plist);

            if (IsWatchExtension) {
                PObject capabilities;

                if (!plist.TryGetValue (ManifestKeys.UIRequiredDeviceCapabilities, out capabilities))
                    plist[ManifestKeys.UIRequiredDeviceCapabilities] = capabilities = new PArray ();
            }

            plist.SetIfNotPresent (ManifestKeys.MinimumOSVersion, minimumOSVersion.ToString ());

            // Remove any Xamarin Studio specific keys
            plist.Remove (ManifestKeys.XSLaunchImageAssets);
            plist.Remove (ManifestKeys.XSAppIconAssets);

            // Merge with any partial plists generated by the Asset Catalog compiler...
            MergePartialPlistTemplates (plist);

            if (IsIOS)
                Validation (plist);

            CompiledAppManifest = new TaskItem (Path.Combine (AppBundleDir, "Info.plist"));
            plist.Save (CompiledAppManifest.ItemSpec, true, true);

            return !Log.HasLoggedErrors;
        }
        public override bool Execute()
        {
            Log.LogTaskName("Archive");
            Log.LogTaskProperty("AppBundleDir", AppBundleDir);
            Log.LogTaskProperty("AppExtensionReferences", AppExtensionReferences);
            Log.LogTaskProperty("InsightsApiKey", InsightsApiKey);
            Log.LogTaskProperty("ITunesSourceFiles", ITunesSourceFiles);
            Log.LogTaskProperty("OutputPath", OutputPath);
            Log.LogTaskProperty("ProjectName", ProjectName);
            Log.LogTaskProperty("SigningKey", SigningKey);
            Log.LogTaskProperty("SolutionPath", SolutionPath);
            Log.LogTaskProperty("WatchAppReferences", WatchAppReferences);

            var archiveDir = CreateArchiveDirectory();

            try {
                var plist       = PDictionary.FromFile(Path.Combine(AppBundleDir.ItemSpec, "Info.plist"));
                var productsDir = Path.Combine(archiveDir, "Products");

                // Archive the OnDemandResources...
                var resourcesDestDir = Path.Combine(productsDir, "OnDemandResources");
                var resourcesSrcDir  = Path.Combine(OutputPath, "OnDemandResources");

                if (Directory.Exists(resourcesSrcDir))
                {
                    Ditto(resourcesSrcDir, resourcesDestDir);
                }

                // Archive the Applications...
                var appDestDir = Path.Combine(productsDir, "Applications", Path.GetFileName(AppBundleDir.ItemSpec));
                Ditto(AppBundleDir.ItemSpec, appDestDir);

                // Archive the dSYMs...
                if (Directory.Exists(DSYMDir))
                {
                    var destDir = Path.Combine(archiveDir, "dSYMs", Path.GetFileName(DSYMDir));
                    Ditto(DSYMDir, destDir);
                }

                // Archive the mSYMs...
                if (Directory.Exists(MSYMDir))
                {
                    var destDir = Path.Combine(archiveDir, "mSYMs", Path.GetFileName(MSYMDir));
                    Ditto(MSYMDir, destDir);
                }

                // Archive the Bitcode symbol maps
                var bcSymbolMaps = Directory.GetFiles(Path.GetDirectoryName(DSYMDir), "*.bcsymbolmap");
                if (bcSymbolMaps.Length > 0)
                {
                    var bcSymbolMapsDir = Path.Combine(archiveDir, "BCSymbolMaps");

                    Directory.CreateDirectory(bcSymbolMapsDir);

                    for (int i = 0; i < bcSymbolMaps.Length; i++)
                    {
                        File.Copy(bcSymbolMaps[i], Path.Combine(bcSymbolMapsDir, Path.GetFileName(bcSymbolMaps[i])));
                    }
                }

                if (AppExtensionReferences != null)
                {
                    // Archive the dSYMs, mSYMs, etc for each of the referenced App Extensions as well...
                    for (int i = 0; i < AppExtensionReferences.Length; i++)
                    {
                        ArchiveAppExtension(AppExtensionReferences[i], archiveDir);
                    }
                }

                if (WatchAppReferences != null)
                {
                    // Archive the dSYMs, mSYMs, etc for each of the referenced WatchOS2 Apps as well...
                    for (int i = 0; i < WatchAppReferences.Length; i++)
                    {
                        ArchiveWatchApp(WatchAppReferences[i], archiveDir);
                    }
                }

                if (ITunesSourceFiles != null)
                {
                    // Archive the iTunesMetadata.plist and iTunesArtwork files...
                    var iTunesMetadataDir = Path.Combine(archiveDir, "iTunesMetadata", Path.GetFileName(AppBundleDir.ItemSpec));
                    for (int i = 0; i < ITunesSourceFiles.Length; i++)
                    {
                        var archivedMetaFile = Path.Combine(iTunesMetadataDir, Path.GetFileName(ITunesSourceFiles[i].ItemSpec));

                        Directory.CreateDirectory(iTunesMetadataDir);
                        File.Copy(ITunesSourceFiles[i].ItemSpec, archivedMetaFile, true);
                    }
                }

                // Generate an archive Info.plist
                var arInfo = new PDictionary();
                // FIXME: figure out this value
                //arInfo.Add ("AppStoreFileSize", new PNumber (65535));

                var props = new PDictionary();
                props.Add("ApplicationPath", new PString(string.Format("Applications/{0}", Path.GetFileName(AppBundleDir.ItemSpec))));
                props.Add("CFBundleIdentifier", new PString(plist.GetCFBundleIdentifier()));
                if (plist.GetCFBundleShortVersionString() != null)
                {
                    props.Add("CFBundleShortVersionString", new PString(plist.GetCFBundleShortVersionString()));
                }
                else if (plist.GetCFBundleVersion() != null)
                {
                    props.Add("CFBundleShortVersionString", new PString(plist.GetCFBundleVersion()));
                }

                var iconFiles = plist.GetCFBundleIconFiles();
                var iconDict  = plist.GetCFBundleIcons();
                var icons     = new PArray();

                if (iconFiles != null)
                {
                    AddIconPaths(icons, iconFiles, Path.Combine(archiveDir, "Products"));
                }

                if (iconDict != null)
                {
                    var primary = iconDict.Get <PDictionary> (ManifestKeys.CFBundlePrimaryIcon);
                    if (primary != null && (iconFiles = primary.GetCFBundleIconFiles()) != null)
                    {
                        AddIconPaths(icons, iconFiles, Path.Combine(archiveDir, "Products"));
                    }
                }

                if (icons.Count > 0)
                {
                    props.Add("IconPaths", icons);
                }

                props.Add("SigningIdentity", new PString(SigningKey));

                arInfo.Add("ApplicationProperties", props);
                arInfo.Add("ArchiveVersion", new PNumber(2));
                arInfo.Add("CreationDate", new PDate(Now.ToUniversalTime()));
                arInfo.Add("Name", new PString(plist.GetCFBundleName() ?? plist.GetCFBundleDisplayName()));
                arInfo.Add("SchemeName", new PString(ProjectName));

                if (!string.IsNullOrEmpty(SolutionPath))
                {
                    arInfo.Add("SolutionName", new PString(Path.GetFileNameWithoutExtension(SolutionPath)));
                    arInfo.Add("SolutionPath", new PString(SolutionPath));
                }

                if (!string.IsNullOrEmpty(InsightsApiKey))
                {
                    arInfo.Add("InsightsApiKey", new PString(InsightsApiKey));
                }

                arInfo.Save(Path.Combine(archiveDir, "Info.plist"));

                ArchiveDir = archiveDir;
            } catch (Exception ex) {
                Log.LogErrorFromException(ex);
                Directory.Delete(archiveDir, true);
            }

            return(!Log.HasLoggedErrors);
        }
        public override bool Execute()
        {
            var    intermediate          = Path.Combine(IntermediateOutputPath, ToolName);
            var    intermediateBundleDir = Path.Combine(intermediate, "bundle");
            var    manifest         = new TaskItem(Path.Combine(intermediate, "asset-manifest.plist"));
            var    bundleResources  = new List <ITaskItem> ();
            var    outputManifests  = new List <ITaskItem> ();
            var    catalogs         = new List <ITaskItem> ();
            var    unique           = new HashSet <string> ();
            string bundleIdentifier = null;
            var    knownSpecs       = new HashSet <string> ();
            var    specs            = new PArray();
            int    rc;

            Log.LogTaskName("ACTool");
            Log.LogTaskProperty("AppManifest", AppManifest);
            Log.LogTaskProperty("DeviceModel", DeviceModel);
            Log.LogTaskProperty("DeviceOSVersion", DeviceOSVersion);
            Log.LogTaskProperty("ImageAssets", ImageAssets);
            Log.LogTaskProperty("IntermediateOutputPath", IntermediateOutputPath);
            Log.LogTaskProperty("IsWatchApp", IsWatchApp);
            Log.LogTaskProperty("OptimizePNGs", OptimizePNGs);
            Log.LogTaskProperty("OutputPath", OutputPath);
            Log.LogTaskProperty("ProjectDir", ProjectDir);
            Log.LogTaskProperty("ResourcePrefix", ResourcePrefix);
            Log.LogTaskProperty("SdkBinPath", SdkBinPath);
            Log.LogTaskProperty("SdkPlatform", SdkPlatform);
            Log.LogTaskProperty("SdkVersion", SdkVersion);

            switch (SdkPlatform)
            {
            case "iPhoneSimulator":
            case "iPhoneOS":
            case "MacOSX":
            case "WatchSimulator":
            case "WatchOS":
            case "AppleTVSimulator":
            case "AppleTVOS":
                break;

            default:
                Log.LogError("Unrecognized platform: {0}", SdkPlatform);
                return(false);
            }

            if (AppManifest != null)
            {
                try {
                    plist = PDictionary.FromFile(AppManifest.ItemSpec);
                } catch (Exception ex) {
                    Log.LogError(null, null, null, AppManifest.ItemSpec, 0, 0, 0, 0, "{0}", ex.Message);
                    return(false);
                }

                bundleIdentifier = plist.GetCFBundleIdentifier();
            }

            foreach (var asset in ImageAssets)
            {
                var vpath = BundleResource.GetVirtualProjectPath(ProjectDir, asset);
                if (Path.GetFileName(vpath) != "Contents.json")
                {
                    continue;
                }

                // get the parent (which will typically be .appiconset, .launchimage, .imageset, .iconset, etc)
                var catalog = Path.GetDirectoryName(vpath);

                // keep walking up the directory structure until we get to the .xcassets directory
                while (!string.IsNullOrEmpty(catalog) && Path.GetExtension(catalog) != ".xcassets")
                {
                    catalog = Path.GetDirectoryName(catalog);
                }

                if (string.IsNullOrEmpty(catalog))
                {
                    Log.LogWarning(null, null, null, asset.ItemSpec, 0, 0, 0, 0, "Asset not part of an asset catalog: {0}", asset.ItemSpec);
                    continue;
                }

                if (unique.Add(catalog))
                {
                    catalogs.Add(new TaskItem(catalog));
                }

                if (AppleSdkSettings.XcodeVersion.Major >= 7 && !string.IsNullOrEmpty(bundleIdentifier) && SdkPlatform != "WatchSimulator")
                {
                    var text = File.ReadAllText(asset.ItemSpec);

                    if (string.IsNullOrEmpty(text))
                    {
                        continue;
                    }

                    var json = JsonConvert.DeserializeObject(text) as JObject;

                    if (json == null)
                    {
                        continue;
                    }

                    var properties = json.Property("properties");

                    if (properties == null)
                    {
                        continue;
                    }

                    var resourceTags = properties.Value.ToObject <JObject> ().Property("on-demand-resource-tags");

                    if (resourceTags == null || resourceTags.Value.Type != JTokenType.Array)
                    {
                        continue;
                    }

                    var    tagArray = resourceTags.Value.ToObject <JArray> ();
                    var    tags     = new HashSet <string> ();
                    string hash;

                    foreach (var tag in tagArray.Select(token => token.ToObject <string> ()))
                    {
                        tags.Add(tag);
                    }

                    var tagList = tags.ToList();
                    tagList.Sort();

                    var path = AssetPackUtils.GetAssetPackDirectory(intermediate, bundleIdentifier, tagList, out hash);

                    if (knownSpecs.Add(hash))
                    {
                        var assetpack = new PDictionary();
                        var ptags     = new PArray();

                        Directory.CreateDirectory(path);

                        for (int i = 0; i < tags.Count; i++)
                        {
                            ptags.Add(new PString(tagList[i]));
                        }

                        assetpack.Add("bundle-id", new PString(string.Format("{0}.asset-pack-{1}", bundleIdentifier, hash)));
                        assetpack.Add("bundle-path", new PString(Path.GetFullPath(path)));
                        assetpack.Add("tags", ptags);
                        specs.Add(assetpack);
                    }
                }
            }

            if (catalogs.Count == 0)
            {
                // There are no (supported?) asset catalogs
                return(true);
            }

            partialAppManifest = new TaskItem(Path.Combine(intermediate, "partial-info.plist"));

            if (specs.Count > 0)
            {
                outputSpecs = Path.Combine(intermediate, "output-specifications.plist");
                specs.Save(outputSpecs, true);
            }

            var output = new TaskItem(intermediateBundleDir);

            Directory.CreateDirectory(intermediateBundleDir);

            // Note: Compile() will set the PartialAppManifest property if it is used...
            if ((rc = Compile(catalogs.ToArray(), output, manifest)) != 0)
            {
                if (File.Exists(manifest.ItemSpec))
                {
                    try {
                        var log = PDictionary.FromFile(manifest.ItemSpec);

                        LogWarningsAndErrors(log, catalogs[0]);
                    } catch (FormatException) {
                        Log.LogError("actool exited with code {0}", rc);
                    }

                    File.Delete(manifest.ItemSpec);
                }

                return(false);
            }

            if (PartialAppManifest != null && !File.Exists(PartialAppManifest.GetMetadata("FullPath")))
            {
                Log.LogError("Partial Info.plist file was not generated: {0}", PartialAppManifest.GetMetadata("FullPath"));
            }

            try {
                var manifestOutput = PDictionary.FromFile(manifest.ItemSpec);

                LogWarningsAndErrors(manifestOutput, catalogs[0]);

                bundleResources.AddRange(GetCompiledBundleResources(manifestOutput, intermediateBundleDir));
                outputManifests.Add(manifest);
            } catch (Exception ex) {
                Log.LogError("Failed to load output manifest for {0} for the file {2}: {1}", ToolName, ex.Message, manifest.ItemSpec);
            }

            foreach (var assetpack in specs.OfType <PDictionary> ())
            {
                var path       = Path.Combine(assetpack.GetString("bundle-path").Value, "Info.plist");
                var bundlePath = PathUtils.AbsoluteToRelative(intermediate, path);
                var outputPath = Path.Combine(OutputPath, bundlePath);
                var rpath      = Path.Combine(intermediate, bundlePath);
                var dict       = new PDictionary();

                dict.SetCFBundleIdentifier(assetpack.GetString("bundle-id").Value);
                dict.Add("Tags", assetpack.GetArray("tags").Clone());

                dict.Save(path, true, true);

                var item = new TaskItem(rpath);
                item.SetMetadata("LogicalName", bundlePath);
                item.SetMetadata("OutputPath", outputPath);
                item.SetMetadata("Optimize", "false");

                bundleResources.Add(item);
            }

            BundleResources = bundleResources.ToArray();
            OutputManifests = outputManifests.ToArray();

            return(!Log.HasLoggedErrors);
        }
Example #19
0
        public override bool Execute()
        {
            var manifestPath = Path.Combine (AppBundleDir.ItemSpec, "AssetPackManifestTemplate.plist");
            var onDemandResourcesPath = Path.Combine (AppBundleDir.ItemSpec, "OnDemandResources.plist");
            var onDemandResourcesDir = Path.Combine (OutputPath, "OnDemandResources");
            var onDemandResourcesStamp = File.GetLastWriteTime (onDemandResourcesPath);
            var initialInstallTags = new HashSet<string> (AssetPackUtils.ParseTags (InitialInstallTags));
            var prefetchOrder = AssetPackUtils.ParseTags (PrefetchOrder);
            var manifestStamp = File.GetLastWriteTime (manifestPath);
            var onDemandResources = new PDictionary ();
            var requestTags = new PDictionary ();
            bool updateOnDemandResources = false;
            var assetPacks = new PDictionary ();
            var manifest = new PDictionary ();
            var resources = new PArray ();
            bool updateManifest = false;

            Log.LogTaskName ("CreateAssetPackManifest");
            Log.LogTaskProperty ("AppBundleDir", AppBundleDir);
            Log.LogTaskProperty ("InitialInstallTags", InitialInstallTags);
            Log.LogTaskProperty ("OutputPath", OutputPath);
            Log.LogTaskProperty ("PrefetchOrder", PrefetchOrder);

            if (!Directory.Exists (onDemandResourcesDir))
                return !Log.HasLoggedErrors;

            onDemandResources.Add ("NSBundleResourceRequestAssetPacks", assetPacks);
            onDemandResources.Add ("NSBundleResourceRequestTags", requestTags);

            manifest.Add ("resources", resources);

            foreach (var dir in Directory.EnumerateDirectories (onDemandResourcesDir)) {
                var path = Path.Combine (dir, "Info.plist");
                PDictionary info;

                if (!File.Exists (path))
                    continue;

                var mtime = File.GetLastWriteTime (path);

                updateOnDemandResources = updateOnDemandResources || mtime > onDemandResourcesStamp;
                updateManifest = updateManifest || mtime > manifestStamp;

                try {
                    info = PDictionary.FromFile (path);
                } catch {
                    continue;
                }

                var bundleIdentifier = info.GetCFBundleIdentifier ();
                var primaryContentHash = new PDictionary ();
                var resource = new PDictionary ();
                var items = new PArray ();
                long size = 0;

                // update OnDemandResources.plist:NSBundleResourceRequestAssetPacks
                foreach (var file in Directory.EnumerateFiles (dir)) {
                    var name = Path.GetFileName (file);

                    if (name != "Info.plist")
                        items.Add (new PString (name));

                    size += new FileInfo (file).Length;
                }

                assetPacks.Add (bundleIdentifier, items);

                // update OnDemandResources.plist:NSBundleResourceRequestTags
                var tags = info.GetArray ("Tags").OfType<PString> ().Select (x => x.Value);
                var priority = double.NaN;

                foreach (var tag in tags) {
                    PDictionary dict;
                    PArray packs;

                    if (initialInstallTags.Contains (tag)) {
                        priority = 1.0f;
                    } else {
                        for (int i = 0; i < prefetchOrder.Length; i++) {
                            if (tag == prefetchOrder[i]) {
                                var value = GetDownloadPriority (i, prefetchOrder.Length);

                                priority = double.IsNaN (priority) ? value : Math.Max (priority, value);
                                break;
                            }
                        }
                    }

                    if (!requestTags.TryGetValue (tag, out dict)) {
                        dict = new PDictionary ();
                        dict.Add ("NSAssetPacks", new PArray ());

                        requestTags.Add (tag, dict);
                    }

                    packs = dict.GetArray ("NSAssetPacks");

                    packs.Add (new PString (bundleIdentifier));
                }

                // update AssetPackManifestTemplate.plist
                resource.Add ("URL", new PString ("http://127.0.0.1" + Path.GetFullPath (dir)));
                resource.Add ("bundleKey", new PString (bundleIdentifier));

                if (!double.IsNaN (priority))
                    resource.Add ("downloadPriority", new PReal (priority));

                resource.Add ("isStreamable", new PBoolean (true));
                primaryContentHash.Add ("hash", mtime.ToString ("yyyy-MM-dd HH:mm:ss.000"));
                primaryContentHash.Add ("strategy", "modtime");
                resource.Add ("primaryContentHash", primaryContentHash);
                resource.Add ("uncompressedSize", new PNumber ((int) ((size + 8191) & ~8191)));

                resources.Add (resource);
            }

            if (updateOnDemandResources) {
                try {
                    onDemandResources.Save (onDemandResourcesPath, true, true);
                } catch (Exception ex) {
                    Log.LogError ("Error saving `{0}': {1}", onDemandResourcesPath, ex.Message);
                }
            }

            if (updateManifest) {
                try {
                    manifest.Save (manifestPath, true, true);
                } catch (Exception ex) {
                    Log.LogError ("Error saving `{0}': {1}", manifestPath, ex.Message);
                }
            }

            return !Log.HasLoggedErrors;
        }
        bool Compile(PDictionary plist)
        {
            var currentSDK = IPhoneSdks.GetSdk(Framework);

            if (!currentSDK.SdkIsInstalled(sdkVersion, SdkIsSimulator))
            {
                Log.LogError(null, null, null, null, 0, 0, 0, 0, "The {0} SDK for '{1}' is not installed.", Framework, sdkVersion);
                return(false);
            }

            supportedDevices = plist.GetUIDeviceFamily();

            if (!IsWatchApp)
            {
                var version = IPhoneSdks.MonoTouch.ExtendedVersion;
                // This key is our supported way of determining if an app
                // was built with Xamarin, so it needs to be present in all apps.

                var dict = new PDictionary();
                dict.Add("Version", new PString(string.Format("{0} ({1}: {2})", version.Version, version.Branch, version.Hash)));
                plist.Add("com.xamarin.ios", dict);
            }

            var sdkSettings = currentSDK.GetSdkSettings(sdkVersion, SdkIsSimulator);
            var dtSettings  = currentSDK.GetDTSettings();

            SetValue(plist, ManifestKeys.BuildMachineOSBuild, dtSettings.BuildMachineOSBuild);
            // We have an issue here, this is for consideration by the platform:
            // CFLocaleCopyCurrent(), used in the mono code to get the current locale (locale.c line 421), will return the value of the application's CFBundleDevelopmentRegion Info.plist key if all of the following conditions are true:
            //
            // * CFBundleDevelopmentRegion is present in the Info.plist
            // * The CFBundleDevelopmentRegion language is in the list of preferred languages on the iOS device, but isn't the first one
            // * There are no localized resources (i.e. no .lproj directory) in the app for the first preferred locale
            //
            // This differs from iOS 10 where the presence of the CFBundleDevelopmentRegion key had no effect. Commenting this line out, ensures that CurrentCulture is correct and behaves like the iOS 10 version.
            // plist.SetIfNotPresent (ManifestKeys.CFBundleDevelopmentRegion, "en");

            plist.SetIfNotPresent(ManifestKeys.CFBundleExecutable, AssemblyName);
            if (IsIOS)
            {
                var executable = plist.GetCFBundleExecutable();
                if (executable.EndsWith(".app", StringComparison.Ordinal))
                {
                    LogAppManifestError("The executable (CFBundleExecutable) name ({0}) cannot end with '.app', because iOS may fail to launch the app.", executable);
                }
            }

            if (IsIOS)
            {
                if (minimumOSVersion < IPhoneSdkVersion.V5_0 && plist.GetUIMainStoryboardFile(true) != null)
                {
                    LogAppManifestError("Applications using a storyboard as the Main Interface must have a deployment target greater than 5.0");
                }

                if (!plist.ContainsKey(ManifestKeys.CFBundleName))
                {
                    plist [ManifestKeys.CFBundleName] = plist.ContainsKey(ManifestKeys.CFBundleDisplayName) ? plist.GetString(ManifestKeys.CFBundleDisplayName).Clone() : new PString(AppBundleName);
                }
            }
            else
            {
                plist.SetIfNotPresent(ManifestKeys.CFBundleName, AppBundleName);
            }

            plist.SetIfNotPresent(ManifestKeys.CFBundleIdentifier, BundleIdentifier);
            plist.SetIfNotPresent(ManifestKeys.CFBundleInfoDictionaryVersion, "6.0");
            plist.SetIfNotPresent(ManifestKeys.CFBundlePackageType, IsAppExtension ? "XPC!" : "APPL");
            if (!string.IsNullOrEmpty(ResourceRules))
            {
                plist.SetIfNotPresent(ManifestKeys.CFBundleResourceSpecification, Path.GetFileName(ResourceRules));
            }
            plist.SetIfNotPresent(ManifestKeys.CFBundleSignature, "????");
            if (!plist.ContainsKey(ManifestKeys.CFBundleSupportedPlatforms))
            {
                plist[ManifestKeys.CFBundleSupportedPlatforms] = new PArray {
                    SdkPlatform
                }
            }
            ;
            plist.SetIfNotPresent(ManifestKeys.CFBundleVersion, "1.0");
            plist.SetIfNotPresent(ManifestKeys.CFBundleShortVersionString, plist.GetCFBundleVersion());

            string dtCompiler        = null;
            string dtPlatformBuild   = null;
            string dtSDKBuild        = null;
            string dtPlatformName    = null;
            string dtPlatformVersion = null;
            string dtXcode           = null;
            string dtXcodeBuild      = null;

            if (!SdkIsSimulator)
            {
                dtCompiler      = sdkSettings.DTCompiler;
                dtPlatformBuild = dtSettings.DTPlatformBuild;
                dtSDKBuild      = sdkSettings.DTSDKBuild;
            }

            dtPlatformName = SdkPlatform.ToLowerInvariant();
            if (!SdkIsSimulator)
            {
                dtPlatformVersion = dtSettings.DTPlatformVersion;
            }

            var dtSDKName = sdkSettings.CanonicalName;

            // older sdksettings didn't have a canonicalname for sim
            if (SdkIsSimulator && string.IsNullOrEmpty(dtSDKName))
            {
                var deviceSdkSettings = currentSDK.GetSdkSettings(sdkVersion, false);
                dtSDKName = deviceSdkSettings.AlternateSDK;
            }

            if (!SdkIsSimulator)
            {
                dtXcode      = AppleSdkSettings.DTXcode;
                dtXcodeBuild = dtSettings.DTXcodeBuild;
            }

            if (UseFakeWatchOS4_3Sdk)
            {
                // This is a workaround for https://github.com/xamarin/xamarin-macios/issues/4810
                if (Framework == PlatformFramework.WatchOS)
                {
                    if (dtPlatformBuild != null)
                    {
                        dtPlatformBuild = "15T212";
                    }
                    if (dtPlatformVersion != null)
                    {
                        dtPlatformVersion = "4.3";
                    }
                    if (dtSDKBuild != null)
                    {
                        dtSDKBuild = "15T212";
                    }
                    if (dtSDKName != null)
                    {
                        dtSDKName = "watchos4.3";
                    }
                    if (dtXcode != null)
                    {
                        dtXcode = "0940";
                    }
                    if (dtXcodeBuild != null)
                    {
                        dtXcodeBuild = "9F1027a";
                    }
                }
                else
                {
                    Log.LogWarning("Can only fake the watchOS 4.3 SDK when building for watchOS.");
                }
            }

            SetValueIfNotNull(plist, "DTCompiler", dtCompiler);
            SetValueIfNotNull(plist, "DTPlatformBuild", dtPlatformBuild);
            SetValueIfNotNull(plist, "DTSDKBuild", dtSDKBuild);
            plist.SetIfNotPresent("DTPlatformName", dtPlatformName);
            SetValueIfNotNull(plist, "DTPlatformVersion", dtPlatformVersion);
            SetValue(plist, "DTSDKName", dtSDKName);
            SetValueIfNotNull(plist, "DTXcode", dtXcode);
            SetValueIfNotNull(plist, "DTXcodeBuild", dtXcodeBuild);

            SetDeviceFamily(plist);

            plist.SetIfNotPresent(ManifestKeys.MinimumOSVersion, minimumOSVersion.ToString());

            if (IsWatchExtension)
            {
                // Note: Only watchOS1 Extensions target Xamarin.iOS
                if (Framework == PlatformFramework.iOS)
                {
                    PObject value;

                    if (!plist.TryGetValue(ManifestKeys.UIRequiredDeviceCapabilities, out value))
                    {
                        var capabilities = new PArray();
                        capabilities.Add(new PString("watch-companion"));

                        plist.Add(ManifestKeys.UIRequiredDeviceCapabilities, capabilities);
                    }
                    else if (value is PDictionary)
                    {
                        var capabilities = (PDictionary)value;

                        if (!capabilities.ContainsKey("watch-companion"))
                        {
                            capabilities.Add("watch-companion", new PBoolean(true));
                        }
                    }
                    else
                    {
                        var  capabilities = (PArray)value;
                        bool exists       = false;

                        foreach (var capability in capabilities.OfType <PString> ())
                        {
                            if (capability.Value != "watch-companion")
                            {
                                continue;
                            }

                            exists = true;
                            break;
                        }

                        if (!exists)
                        {
                            capabilities.Add(new PString("watch-companion"));
                        }
                    }
                }

                if (Debug)
                {
                    SetAppTransportSecurity(plist);
                }
            }

            // Remove any Xamarin Studio specific keys
            plist.Remove(ManifestKeys.XSLaunchImageAssets);
            plist.Remove(ManifestKeys.XSAppIconAssets);

            // Merge with any partial plists generated by the Asset Catalog compiler...
            MergePartialPlistTemplates(plist);

            SetRequiredArchitectures(plist);

            if (IsIOS)
            {
                Validation(plist);
            }

            CompiledAppManifest = new TaskItem(Path.Combine(AppBundleDir, "Info.plist"));
            plist.Save(CompiledAppManifest.ItemSpec, true, true);

            return(!Log.HasLoggedErrors);
        }

        void SetValueIfNotNull(PDictionary dict, string key, string value)
        {
            if (value == null)
            {
                return;
            }
            SetValue(dict, key, value);
        }

        void SetRequiredArchitectures(PDictionary plist)
        {
            PObject capabilities;

            if (plist.TryGetValue(ManifestKeys.UIRequiredDeviceCapabilities, out capabilities))
            {
                if (capabilities is PArray)
                {
                    var architectureValues = new HashSet <string> (new[] { "armv6", "armv7", "arm64" });
                    var array = (PArray)capabilities;

                    // Remove any architecture values
                    for (int i = 0; i < array.Count; i++)
                    {
                        var value = array[i] as PString;

                        if (value == null || !architectureValues.Contains(value.Value))
                        {
                            continue;
                        }

                        array.RemoveAt(i);
                    }

                    // If-and-only-if the TargetArchitecture is a single architecture, set it as a required device capability
                    switch (architectures)
                    {
                    case TargetArchitecture.ARM64:
                        array.Add(new PString("arm64"));
                        break;

                    case TargetArchitecture.ARMv7:
                        array.Add(new PString("armv7"));
                        break;
                    }
                }
                else if (capabilities is PDictionary)
                {
                    var dict = (PDictionary)capabilities;

                    switch (architectures)
                    {
                    case TargetArchitecture.ARM64:
                        dict["arm64"] = new PBoolean(true);
                        dict.Remove("armv6");
                        dict.Remove("armv7");
                        break;

                    case TargetArchitecture.ARMv7:
                        dict["armv7"] = new PBoolean(true);
                        dict.Remove("armv6");
                        dict.Remove("arm64");
                        break;

                    default:
                        dict.Remove("armv6");
                        dict.Remove("armv7");
                        dict.Remove("arm64");
                        break;
                    }
                }
            }
            else
            {
                var array = new PArray();

                // If-and-only-if the TargetArchitecture is a single architecture, set it as a required device capability
                switch (architectures)
                {
                case TargetArchitecture.ARM64:
                    array.Add(new PString("arm64"));
                    break;

                case TargetArchitecture.ARMv7:
                    array.Add(new PString("armv7"));
                    break;
                }

                if (array.Count > 0)
                {
                    plist.Add(ManifestKeys.UIRequiredDeviceCapabilities, array);
                }
            }
        }

        void SetDeviceFamily(PDictionary plist)
        {
            switch (Framework)
            {
            case PlatformFramework.iOS:
                SetIOSDeviceFamily(plist);
                break;

            case PlatformFramework.WatchOS:
                plist.SetUIDeviceFamily(IPhoneDeviceType.Watch);
                break;

            case PlatformFramework.TVOS:
                plist.SetUIDeviceFamily(IPhoneDeviceType.TV);
                break;
            }
        }

        void SetIOSDeviceFamily(PDictionary plist)
        {
            if (IsWatchApp)
            {
                if (SdkIsSimulator)
                {
                    plist.SetUIDeviceFamily(IPhoneDeviceType.IPhone | IPhoneDeviceType.Watch);
                }
                else
                {
                    plist.SetUIDeviceFamily(IPhoneDeviceType.Watch);
                }
            }
            else
            {
                if (!IsAppExtension)
                {
                    plist.SetIfNotPresent(ManifestKeys.LSRequiresIPhoneOS, true);
                }

                if (minimumOSVersion >= IPhoneSdkVersion.V3_2 && supportedDevices == IPhoneDeviceType.NotSet)
                {
                    plist.SetUIDeviceFamily(IPhoneDeviceType.IPhone);
                }
            }
        }

        void SetAppTransportSecurity(PDictionary plist)
        {
            // Debugging over http has a couple of gotchas:
            // * We can't use https, because that requires a valid server certificate,
            //   which we can't ensure.
            //   It would also require a hostname for the mac, which it might not have either.
            // * NSAppTransportSecurity/NSExceptionDomains does not allow exceptions based
            //   on IP address (only hostname).
            // * Which means the only way to make sure watchOS allows connections from
            //   the app on device to the mac is to disable App Transport Security altogether.
            // Good news: watchOS 3 will apparently not apply ATS when connecting
            // directly to IP addresses, which means we won't have to do this at all
            // (sometime in the future).

            PDictionary ats;

            if (!plist.TryGetValue(ManifestKeys.NSAppTransportSecurity, out ats))
            {
                plist.Add(ManifestKeys.NSAppTransportSecurity, ats = new PDictionary());
            }

            if (ats.GetBoolean(ManifestKeys.NSAllowsArbitraryLoads))
            {
                Log.LogMessage(MessageImportance.Low, "All http loads are already allowed.");
            }
            else
            {
                Log.LogMessage(MessageImportance.Low, "Allowed arbitrary HTTP loads to support debugging.");
                ats.SetBooleanOrRemove(ManifestKeys.NSAllowsArbitraryLoads, true);
            }
        }

        void Validation(PDictionary plist)
        {
            var supportsIPhone = (supportedDevices & IPhoneDeviceType.IPhone) != 0 ||
                                 supportedDevices == IPhoneDeviceType.NotSet;
            var supportsIPad = (supportedDevices & IPhoneDeviceType.IPad) != 0;

            // Validation...
            if (!IsAppExtension && sdkVersion >= IPhoneSdkVersion.V3_2)
            {
                IPhoneOrientation orientation;

                if (supportsIPhone)
                {
                    orientation = plist.GetUISupportedInterfaceOrientations(false);
                    if (orientation == IPhoneOrientation.None)
                    {
                        LogAppManifestWarning("Supported iPhone orientations have not been set");
                    }
                    else if (!orientation.IsValidPair())
                    {
                        LogAppManifestWarning("Supported iPhone orientations are not matched pairs");
                    }
                }

                if (supportsIPad)
                {
                    orientation = plist.GetUISupportedInterfaceOrientations(true);
                    if (orientation == IPhoneOrientation.None)
                    {
                        LogAppManifestWarning("Supported iPad orientations have not been set");
                    }
                    else if (!orientation.IsValidPair())
                    {
                        LogAppManifestWarning("Supported iPad orientations are not matched pairs");
                    }
                }
            }
        }
    }
Example #21
0
        public override bool Execute()
        {
            var intermediate          = Path.Combine(IntermediateOutputPath, ToolName);
            var intermediateBundleDir = Path.Combine(intermediate, "bundle");
            var intermediateCloneDir  = Path.Combine(intermediate, "cloned-assets");
            var manifest        = new TaskItem(Path.Combine(intermediate, "asset-manifest.plist"));
            var bundleResources = new List <ITaskItem> ();
            var outputManifests = new List <ITaskItem> ();
            var catalogs        = new List <ITaskItem> ();
            var unique          = new HashSet <string> ();

            var knownSpecs = new HashSet <string> ();
            var clones     = new HashSet <string> ();
            var items      = new List <ITaskItem> ();
            var specs      = new PArray();

            if (AppManifest != null)
            {
                try {
                    plist = PDictionary.FromFile(AppManifest.ItemSpec);
                } catch (Exception ex) {
                    Log.LogError(null, null, null, AppManifest.ItemSpec, 0, 0, 0, 0, "{0}", ex.Message);
                    return(false);
                }
            }

            for (int i = 0; i < ImageAssets.Length; i++)
            {
                var vpath = BundleResource.GetVirtualProjectPath(ProjectDir, ImageAssets[i], !string.IsNullOrEmpty(SessionId));

                // Ignore MacOS .DS_Store files...
                if (Path.GetFileName(vpath).Equals(".DS_Store", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                // get the parent (which will typically be .appiconset, .launchimage, .imageset, .iconset, etc)
                var catalog = Path.GetDirectoryName(vpath);

                // keep walking up the directory structure until we get to the .xcassets directory
                while (!string.IsNullOrEmpty(catalog) && Path.GetExtension(catalog) != ".xcassets")
                {
                    catalog = Path.GetDirectoryName(catalog);
                }

                if (string.IsNullOrEmpty(catalog))
                {
                    Log.LogWarning(null, null, null, ImageAssets[i].ItemSpec, 0, 0, 0, 0, MSBStrings.W0090, ImageAssets[i].ItemSpec);
                    continue;
                }

                if (ImageAssets[i].GetMetadata("Link") != null)
                {
                    // Note: if any of the files within a catalog are linked, we'll have to clone the *entire* catalog
                    clones.Add(catalog);
                    continue;
                }

                // filter out everything except paths containing a Contents.json file since our main processing loop only cares about these
                if (Path.GetFileName(vpath) != "Contents.json")
                {
                    continue;
                }

                items.Add(ImageAssets[i]);
            }

            // clone any *.xcassets dirs that need cloning
            if (clones.Count > 0)
            {
                if (Directory.Exists(intermediateCloneDir))
                {
                    Directory.Delete(intermediateCloneDir, true);
                }

                Directory.CreateDirectory(intermediateCloneDir);

                items.Clear();

                for (int i = 0; i < ImageAssets.Length; i++)
                {
                    var       vpath = BundleResource.GetVirtualProjectPath(ProjectDir, ImageAssets[i], !string.IsNullOrEmpty(SessionId));
                    var       clone = false;
                    ITaskItem item;

                    // Ignore MacOS .DS_Store files...
                    if (Path.GetFileName(vpath).Equals(".DS_Store", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    foreach (var catalog in clones)
                    {
                        if (vpath.Length > catalog.Length && vpath[catalog.Length] == '/' && vpath.StartsWith(catalog, StringComparison.Ordinal))
                        {
                            clone = true;
                            break;
                        }
                    }

                    if (clone)
                    {
                        var src = ImageAssets[i].GetMetadata("FullPath");

                        if (!File.Exists(src))
                        {
                            Log.LogError(null, null, null, src, 0, 0, 0, 0, MSBStrings.E0091, src);
                            return(false);
                        }

                        var dest = Path.Combine(intermediateCloneDir, vpath);
                        var dir  = Path.GetDirectoryName(dest);

                        Directory.CreateDirectory(dir);

                        File.Copy(src, dest, true);

                        // filter out everything except paths containing a Contents.json file since our main processing loop only cares about these
                        if (Path.GetFileName(vpath) != "Contents.json")
                        {
                            continue;
                        }

                        item = new TaskItem(dest);
                        ImageAssets[i].CopyMetadataTo(item);
                        item.SetMetadata("Link", vpath);
                    }
                    else
                    {
                        // filter out everything except paths containing a Contents.json file since our main processing loop only cares about these
                        if (Path.GetFileName(vpath) != "Contents.json")
                        {
                            continue;
                        }

                        item = ImageAssets[i];
                    }

                    items.Add(item);
                }
            }

            // Note: `items` contains only the Contents.json files at this point
            for (int i = 0; i < items.Count; i++)
            {
                var vpath = BundleResource.GetVirtualProjectPath(ProjectDir, items[i], !string.IsNullOrEmpty(SessionId));
                var path  = items[i].GetMetadata("FullPath");

                // get the parent (which will typically be .appiconset, .launchimage, .imageset, .iconset, etc)
                var catalog = Path.GetDirectoryName(vpath);
                path = Path.GetDirectoryName(path);

                // keep walking up the directory structure until we get to the .xcassets directory
                while (!string.IsNullOrEmpty(catalog) && Path.GetExtension(catalog) != ".xcassets")
                {
                    catalog = Path.GetDirectoryName(catalog);
                    path    = Path.GetDirectoryName(path);
                }

                if (unique.Add(catalog))
                {
                    var item = new TaskItem(path);
                    item.SetMetadata("Link", catalog);

                    catalogs.Add(item);
                }

                if (AppleSdkSettings.XcodeVersion.Major >= 7 && SdkPlatform != "WatchSimulator")
                {
                    var text = File.ReadAllText(items[i].ItemSpec);

                    if (string.IsNullOrEmpty(text))
                    {
                        continue;
                    }

                    JsonDocument json;
                    JsonElement  value;

                    try {
                        var options = new JsonDocumentOptions()
                        {
                            AllowTrailingCommas = true,
                        };
                        json = JsonDocument.Parse(text, options);
                    } catch (JsonException je) {
                        var line = (int)(je.LineNumber + 1 ?? 0);
                        var col  = (int)(je.BytePositionInLine + 1 ?? 0);
                        Log.LogError(null, null, null, items [i].ItemSpec, line, col, line, col, "{0}", je.Message);
                        return(false);
                    } catch (Exception e) {
                        Log.LogError(null, null, null, items[i].ItemSpec, 0, 0, 0, 0, MSBStrings.E0092, e.Message);
                        return(false);
                    }

                    if (!json.RootElement.TryGetProperty("properties", out value) || value.ValueKind != JsonValueKind.Object)
                    {
                        continue;
                    }

                    var properties = value;

                    if (!properties.TryGetProperty("on-demand-resource-tags", out value) || value.ValueKind != JsonValueKind.Array)
                    {
                        continue;
                    }

                    var    resourceTags = value;
                    var    tags         = new HashSet <string> ();
                    string hash;

                    foreach (var tag in resourceTags.EnumerateArray())
                    {
                        if (tag.ValueKind == JsonValueKind.String)
                        {
                            tags.Add(tag.GetString());
                        }
                    }

                    var tagList = tags.ToList();
                    tagList.Sort();

                    var assetDir = AssetPackUtils.GetAssetPackDirectory(intermediate, BundleIdentifier, tagList, out hash);

                    if (knownSpecs.Add(hash))
                    {
                        var assetpack = new PDictionary();
                        var ptags     = new PArray();

                        Directory.CreateDirectory(assetDir);

                        for (int j = 0; j < tagList.Count; j++)
                        {
                            ptags.Add(new PString(tagList[j]));
                        }

                        assetpack.Add("bundle-id", new PString(string.Format("{0}.asset-pack-{1}", BundleIdentifier, hash)));
                        assetpack.Add("bundle-path", new PString(Path.GetFullPath(assetDir)));
                        assetpack.Add("tags", ptags);
                        specs.Add(assetpack);
                    }
                }
            }

            if (catalogs.Count == 0)
            {
                // There are no (supported?) asset catalogs
                return(!Log.HasLoggedErrors);
            }

            partialAppManifest = new TaskItem(Path.Combine(intermediate, "partial-info.plist"));

            if (specs.Count > 0)
            {
                outputSpecs = Path.Combine(intermediate, "output-specifications.plist");
                specs.Save(outputSpecs, true);
            }

            Directory.CreateDirectory(intermediateBundleDir);

            // Note: Compile() will set the PartialAppManifest property if it is used...
            if ((Compile(catalogs.ToArray(), intermediateBundleDir, manifest)) != 0)
            {
                return(false);
            }

            if (PartialAppManifest != null && !File.Exists(PartialAppManifest.GetMetadata("FullPath")))
            {
                Log.LogError(MSBStrings.E0093, PartialAppManifest.GetMetadata("FullPath"));
            }

            try {
                var manifestOutput = PDictionary.FromFile(manifest.ItemSpec);

                LogWarningsAndErrors(manifestOutput, catalogs[0]);

                bundleResources.AddRange(GetCompiledBundleResources(manifestOutput, intermediateBundleDir));
                outputManifests.Add(manifest);
            } catch (Exception ex) {
                Log.LogError(MSBStrings.E0094, ToolName, manifest.ItemSpec, ex.Message);
            }

            foreach (var assetpack in specs.OfType <PDictionary> ())
            {
                var path       = Path.Combine(assetpack.GetString("bundle-path").Value, "Info.plist");
                var bundlePath = PathUtils.AbsoluteToRelative(intermediate, path);
                var outputPath = Path.Combine(OutputPath, bundlePath);
                var rpath      = Path.Combine(intermediate, bundlePath);
                var dict       = new PDictionary();

                dict.SetCFBundleIdentifier(assetpack.GetString("bundle-id").Value);
                dict.Add("Tags", assetpack.GetArray("tags").Clone());

                dict.Save(path, true, true);

                var item = new TaskItem(rpath);
                item.SetMetadata("LogicalName", bundlePath);
                item.SetMetadata("OutputPath", outputPath);
                item.SetMetadata("Optimize", "false");

                bundleResources.Add(item);
            }

            BundleResources = bundleResources.ToArray();
            OutputManifests = outputManifests.ToArray();

            return(!Log.HasLoggedErrors);
        }
        bool Compile(PDictionary plist)
        {
            var currentSDK = IPhoneSdks.GetSdk(Framework);

            if (!currentSDK.SdkIsInstalled(sdkVersion, SdkIsSimulator))
            {
                Log.LogError(null, null, null, null, 0, 0, 0, 0, "The {0} SDK for '{1}' is not installed.", Framework, sdkVersion);
                return(false);
            }

            supportedDevices = plist.GetUIDeviceFamily();

            if (!IsWatchApp)
            {
                var version = IPhoneSdks.MonoTouch.ExtendedVersion;
                // This key is our supported way of determining if an app
                // was built with Xamarin, so it needs to be present in all apps.

                var dict = new PDictionary();
                dict.Add("Version", new PString(string.Format("{0} ({1}: {2})", version.Version, version.Branch, version.Hash)));
                plist.Add("com.xamarin.ios", dict);
            }

            var sdkSettings = currentSDK.GetSdkSettings(sdkVersion, SdkIsSimulator);
            var dtSettings  = currentSDK.GetDTSettings();

            SetValue(plist, ManifestKeys.BuildMachineOSBuild, dtSettings.BuildMachineOSBuild);
            plist.SetIfNotPresent(ManifestKeys.CFBundleDevelopmentRegion, "en");

            plist.SetIfNotPresent(ManifestKeys.CFBundleExecutable, AssemblyName);
            if (IsIOS)
            {
                var executable = plist.GetCFBundleExecutable();
                if (executable.EndsWith(".app", StringComparison.Ordinal))
                {
                    LogAppManifestError("The executable (CFBundleExecutable) name ({0}) cannot end with '.app', because iOS may fail to launch the app.", executable);
                }
            }

            if (IsIOS)
            {
                if (minimumOSVersion < IPhoneSdkVersion.V5_0 && plist.GetUIMainStoryboardFile(true) != null)
                {
                    LogAppManifestError("Applications using a storyboard as the Main Interface must have a deployment target greater than 5.0");
                }

                if (!plist.ContainsKey(ManifestKeys.CFBundleName))
                {
                    plist [ManifestKeys.CFBundleName] = plist.ContainsKey(ManifestKeys.CFBundleDisplayName) ? plist.GetString(ManifestKeys.CFBundleDisplayName).Clone() : new PString(AppBundleName);
                }
            }
            else
            {
                plist.SetIfNotPresent(ManifestKeys.CFBundleName, AppBundleName);
            }

            plist.SetIfNotPresent(ManifestKeys.CFBundleIdentifier, BundleIdentifier);
            plist.SetIfNotPresent(ManifestKeys.CFBundleInfoDictionaryVersion, "6.0");
            plist.SetIfNotPresent(ManifestKeys.CFBundlePackageType, IsAppExtension ? "XPC!" : "APPL");
            if (!string.IsNullOrEmpty(ResourceRules))
            {
                plist.SetIfNotPresent(ManifestKeys.CFBundleResourceSpecification, Path.GetFileName(ResourceRules));
            }
            plist.SetIfNotPresent(ManifestKeys.CFBundleSignature, "????");
            if (!plist.ContainsKey(ManifestKeys.CFBundleSupportedPlatforms))
            {
                plist[ManifestKeys.CFBundleSupportedPlatforms] = new PArray {
                    SdkPlatform
                }
            }
            ;
            plist.SetIfNotPresent(ManifestKeys.CFBundleVersion, "1.0");
            plist.SetIfNotPresent(ManifestKeys.CFBundleShortVersionString, plist.GetCFBundleVersion());

            if (!SdkIsSimulator)
            {
                SetValue(plist, "DTCompiler", sdkSettings.DTCompiler);
                SetValue(plist, "DTPlatformBuild", dtSettings.DTPlatformBuild);
                SetValue(plist, "DTSDKBuild", sdkSettings.DTSDKBuild);
            }

            plist.SetIfNotPresent("DTPlatformName", SdkPlatform.ToLowerInvariant());
            if (!SdkIsSimulator)
            {
                SetValue(plist, "DTPlatformVersion", dtSettings.DTPlatformVersion);
            }

            var sdkName = sdkSettings.CanonicalName;

            // older sdksettings didn't have a canonicalname for sim
            if (SdkIsSimulator && string.IsNullOrEmpty(sdkName))
            {
                var deviceSdkSettings = currentSDK.GetSdkSettings(sdkVersion, false);
                sdkName = deviceSdkSettings.AlternateSDK;
            }
            SetValue(plist, "DTSDKName", sdkName);

            if (!SdkIsSimulator)
            {
                SetValue(plist, "DTXcode", AppleSdkSettings.DTXcode);
                SetValue(plist, "DTXcodeBuild", dtSettings.DTXcodeBuild);
            }

            SetDeviceFamily(plist);

            plist.SetIfNotPresent(ManifestKeys.MinimumOSVersion, minimumOSVersion.ToString());

            if (IsWatchExtension && Debug)
            {
                SetAppTransportSecurity(plist);
            }

            // Remove any Xamarin Studio specific keys
            plist.Remove(ManifestKeys.XSLaunchImageAssets);
            plist.Remove(ManifestKeys.XSAppIconAssets);

            // Merge with any partial plists generated by the Asset Catalog compiler...
            MergePartialPlistTemplates(plist);

            if (IsIOS)
            {
                Validation(plist);
            }

            CompiledAppManifest = new TaskItem(Path.Combine(AppBundleDir, "Info.plist"));
            plist.Save(CompiledAppManifest.ItemSpec, true, true);

            return(!Log.HasLoggedErrors);
        }

        void SetDeviceFamily(PDictionary plist)
        {
            switch (Framework)
            {
            case PlatformFramework.iOS:
                SetIOSDeviceFamily(plist);
                break;

            case PlatformFramework.WatchOS:
                plist.SetUIDeviceFamily(IPhoneDeviceType.Watch);
                break;

            case PlatformFramework.TVOS:
                plist.SetUIDeviceFamily(IPhoneDeviceType.TV);
                break;
            }
        }

        void SetIOSDeviceFamily(PDictionary plist)
        {
            if (IsWatchApp)
            {
                if (SdkIsSimulator)
                {
                    plist.SetUIDeviceFamily(IPhoneDeviceType.IPhone | IPhoneDeviceType.Watch);
                }
                else
                {
                    plist.SetUIDeviceFamily(IPhoneDeviceType.Watch);
                }
            }
            else
            {
                if (!IsAppExtension)
                {
                    plist.SetIfNotPresent(ManifestKeys.LSRequiresIPhoneOS, true);
                }

                if (minimumOSVersion >= IPhoneSdkVersion.V3_2 && supportedDevices == IPhoneDeviceType.NotSet)
                {
                    plist.SetUIDeviceFamily(IPhoneDeviceType.IPhone);
                }
            }
        }

        void SetAppTransportSecurity(PDictionary plist)
        {
            // Debugging over http has a couple of gotchas:
            // * We can't use https, because that requires a valid server certificate,
            //   which we can't ensure.
            //   It would also require a hostname for the mac, which it might not have either.
            // * NSAppTransportSecurity/NSExceptionDomains does not allow exceptions based
            //   on IP address (only hostname).
            // * Which means the only way to make sure watchOS allows connections from
            //   the app on device to the mac is to disable App Transport Security altogether.
            // Good news: watchOS 3 will apparently not apply ATS when connecting
            // directly to IP addresses, which means we won't have to do this at all
            // (sometime in the future).

            PDictionary ats;

            if (!plist.TryGetValue(ManifestKeys.NSAppTransportSecurity, out ats))
            {
                plist.Add(ManifestKeys.NSAppTransportSecurity, ats = new PDictionary());
            }

            if (ats.GetBoolean(ManifestKeys.NSAllowsArbitraryLoads))
            {
                Log.LogMessage(MessageImportance.Low, "All http loads are already allowed.");
            }
            else
            {
                Log.LogMessage(MessageImportance.Low, "Allowed arbitrary HTTP loads to support debugging.");
                ats.SetBooleanOrRemove(ManifestKeys.NSAllowsArbitraryLoads, true);
            }
        }

        void Validation(PDictionary plist)
        {
            var supportsIPhone = (supportedDevices & IPhoneDeviceType.IPhone) != 0 ||
                                 supportedDevices == IPhoneDeviceType.NotSet;
            var supportsIPad = (supportedDevices & IPhoneDeviceType.IPad) != 0;

            // Validation...
            if (!IsAppExtension && sdkVersion >= IPhoneSdkVersion.V3_2)
            {
                IPhoneOrientation orientation;

                if (supportsIPhone)
                {
                    orientation = plist.GetUISupportedInterfaceOrientations(false);
                    if (orientation == IPhoneOrientation.None)
                    {
                        LogAppManifestWarning("Supported iPhone orientations have not been set");
                    }
                    else if (!orientation.IsValidPair())
                    {
                        LogAppManifestWarning("Supported iPhone orientations are not matched pairs");
                    }
                }

                if (supportsIPad)
                {
                    orientation = plist.GetUISupportedInterfaceOrientations(true);
                    if (orientation == IPhoneOrientation.None)
                    {
                        LogAppManifestWarning("Supported iPad orientations have not been set");
                    }
                    else if (!orientation.IsValidPair())
                    {
                        LogAppManifestWarning("Supported iPad orientations are not matched pairs");
                    }
                }
            }
        }
    }
Example #23
0
        public override bool Execute()
        {
            var archiveDir = CreateArchiveDirectory();

            try {
                var plist       = PDictionary.FromFile(Path.Combine(AppBundleDir.ItemSpec, "Contents", "Info.plist"));
                var productsDir = Path.Combine(archiveDir, "Products");

                // Archive the Applications...
                var appDestDir = Path.Combine(productsDir, "Applications", Path.GetFileName(AppBundleDir.ItemSpec));
                Ditto(AppBundleDir.ItemSpec, appDestDir);

                // Archive the dSYMs...
                if (Directory.Exists(DSYMDir))
                {
                    var destDir = Path.Combine(archiveDir, "dSYMs", Path.GetFileName(DSYMDir));
                    Ditto(DSYMDir, destDir);
                }

                // Generate an archive Info.plist
                var arInfo = new PDictionary();
                var props  = new PDictionary();
                props.Add("ApplicationPath", new PString(string.Format("Applications/{0}", Path.GetFileName(AppBundleDir.ItemSpec))));
                props.Add("CFBundleIdentifier", new PString(plist.GetCFBundleIdentifier()));

                var version = plist.GetCFBundleShortVersionString();
                var build   = plist.GetCFBundleVersion();
                props.Add("CFBundleShortVersionString", new PString(version ?? (build ?? "1.0")));
                props.Add("CFBundleVersion", new PString(build ?? "1.0"));
                props.Add("SigningIdentity", SigningKey);

                arInfo.Add("ApplicationProperties", props);
                arInfo.Add("ArchiveVersion", new PNumber(2));
                arInfo.Add("CreationDate", new PDate(Now.ToUniversalTime()));
                arInfo.Add("Name", new PString(plist.GetCFBundleName() ?? plist.GetCFBundleDisplayName()));

                if (!string.IsNullOrEmpty(ProjectGuid))
                {
                    arInfo.Add("ProjectGuid", new PString(ProjectGuid));
                }

                if (!string.IsNullOrEmpty(ProjectTypeGuids))
                {
                    arInfo.Add("ProjectTypeGuids", new PString(ProjectTypeGuids));
                }

                if (!string.IsNullOrEmpty(SolutionPath))
                {
                    arInfo.Add("SolutionName", new PString(Path.GetFileNameWithoutExtension(SolutionPath)));
                    arInfo.Add("SolutionPath", new PString(SolutionPath));
                }

                if (!string.IsNullOrEmpty(InsightsApiKey))
                {
                    arInfo.Add("InsightsApiKey", new PString(InsightsApiKey));
                }

                arInfo.Save(Path.Combine(archiveDir, "Info.plist"));
                ArchiveDir = archiveDir;
            } catch (Exception ex) {
                Log.LogErrorFromException(ex);
                Directory.Delete(archiveDir, true);
            }

            return(!Log.HasLoggedErrors);
        }
        public override bool Execute()
        {
            var archiveDir = CreateArchiveDirectory();

            try {
                var plist       = PDictionary.FromFile(PlatformFrameworkHelper.GetAppManifestPath(Platform, AppBundleDir.ItemSpec));
                var productsDir = Path.Combine(archiveDir, "Products");

                // Archive the OnDemandResources...
                var resourcesDestDir = Path.Combine(productsDir, "OnDemandResources");
                var resourcesSrcDir  = Path.Combine(OutputPath, "OnDemandResources");

                if (Directory.Exists(resourcesSrcDir))
                {
                    Ditto(resourcesSrcDir, resourcesDestDir);
                }

                // Archive the Applications...
                var appDestDir = Path.Combine(productsDir, "Applications", Path.GetFileName(AppBundleDir.ItemSpec));
                Ditto(AppBundleDir.ItemSpec, appDestDir);

                // Archive the main dSYM...
                ArchiveDSym(DSYMDir, archiveDir);

                // for each `.dylib` (file) inside `MonoBundle` there could be a corresponding `.dSYM` - e.g. when using an AOT mode
                foreach (var dylib in Directory.GetFiles(UserDylibPath, "*.dylib"))
                {
                    var dsym = Path.Combine(AppBundleDir.ItemSpec, "..", Path.GetFileName(dylib) + ".dSYM");
                    ArchiveDSym(dsym, archiveDir);
                }

                // for each user framework (directory) that is bundled inside the app we must also archive their dSYMs, if available
                var fks = UserFrameworksPath;
                if (Directory.Exists(fks))
                {
                    foreach (var fx in Directory.GetDirectories(fks, "*.framework"))
                    {
                        var dsym = Path.Combine(AppBundleDir.ItemSpec, "..", Path.GetFileName(fx) + ".dSYM");
                        ArchiveDSym(dsym, archiveDir);
                    }
                }

                // Archive the mSYMs...
                ArchiveMSym(MSYMDir, archiveDir);

                // Archive the Bitcode symbol maps
                var bcSymbolMaps = Directory.GetFiles(Path.GetDirectoryName(DSYMDir), "*.bcsymbolmap");
                if (bcSymbolMaps.Length > 0)
                {
                    var bcSymbolMapsDir = Path.Combine(archiveDir, "BCSymbolMaps");

                    Directory.CreateDirectory(bcSymbolMapsDir);

                    for (int i = 0; i < bcSymbolMaps.Length; i++)
                    {
                        File.Copy(bcSymbolMaps [i], Path.Combine(bcSymbolMapsDir, Path.GetFileName(bcSymbolMaps [i])));
                    }
                }

                if (AppExtensionReferences != null)
                {
                    // Archive the dSYMs, mSYMs, etc for each of the referenced App Extensions as well...
                    for (int i = 0; i < AppExtensionReferences.Length; i++)
                    {
                        ArchiveAppExtension(AppExtensionReferences [i], archiveDir);
                    }
                }

                if (WatchAppReferences != null)
                {
                    // Archive the dSYMs, mSYMs, etc for each of the referenced WatchOS2 Apps as well...
                    for (int i = 0; i < WatchAppReferences.Length; i++)
                    {
                        ArchiveWatchApp(WatchAppReferences [i], archiveDir);
                    }
                }

                if (ITunesSourceFiles != null)
                {
                    // Archive the iTunesMetadata.plist and iTunesArtwork files...
                    var iTunesMetadataDir = Path.Combine(archiveDir, "iTunesMetadata", Path.GetFileName(AppBundleDir.ItemSpec));
                    for (int i = 0; i < ITunesSourceFiles.Length; i++)
                    {
                        var archivedMetaFile = Path.Combine(iTunesMetadataDir, Path.GetFileName(ITunesSourceFiles [i].ItemSpec));

                        Directory.CreateDirectory(iTunesMetadataDir);
                        File.Copy(ITunesSourceFiles [i].ItemSpec, archivedMetaFile, true);
                    }
                }

                // Generate an archive Info.plist
                var arInfo = new PDictionary();
                // FIXME: figure out this value
                //arInfo.Add ("AppStoreFileSize", new PNumber (65535));
                var props = new PDictionary();
                props.Add("ApplicationPath", new PString(string.Format("Applications/{0}", Path.GetFileName(AppBundleDir.ItemSpec))));
                props.Add("CFBundleIdentifier", new PString(plist.GetCFBundleIdentifier()));

                var version = plist.GetCFBundleShortVersionString();
                var build   = plist.GetCFBundleVersion();
                props.Add("CFBundleShortVersionString", new PString(version ?? (build ?? "1.0")));
                props.Add("CFBundleVersion", new PString(build ?? "1.0"));

                var iconFiles = plist.GetCFBundleIconFiles();
                var iconDict  = plist.GetCFBundleIcons();
                var icons     = new PArray();

                if (iconFiles != null)
                {
                    AddIconPaths(icons, iconFiles, Path.Combine(archiveDir, "Products"));
                }

                if (iconDict != null)
                {
                    var primary = iconDict.Get <PDictionary> (ManifestKeys.CFBundlePrimaryIcon);
                    if (primary != null && (iconFiles = primary.GetCFBundleIconFiles()) != null)
                    {
                        AddIconPaths(icons, iconFiles, Path.Combine(archiveDir, "Products"));
                    }
                }

                if (icons.Count > 0)
                {
                    props.Add("IconPaths", icons);
                }

                props.Add("SigningIdentity", SigningKey);

                arInfo.Add("ApplicationProperties", props);
                arInfo.Add("ArchiveVersion", new PNumber(2));
                arInfo.Add("CreationDate", new PDate(Now.ToUniversalTime()));
                arInfo.Add("Name", new PString(plist.GetCFBundleName() ?? plist.GetCFBundleDisplayName()));

                if (!string.IsNullOrEmpty(ProjectGuid))
                {
                    arInfo.Add("ProjectGuid", new PString(ProjectGuid));
                }

                if (!string.IsNullOrEmpty(ProjectTypeGuids))
                {
                    arInfo.Add("ProjectTypeGuids", new PString(ProjectTypeGuids));
                }

                if (!string.IsNullOrEmpty(SolutionPath))
                {
                    arInfo.Add("SolutionName", new PString(Path.GetFileNameWithoutExtension(SolutionPath)));
                    arInfo.Add("SolutionPath", new PString(SolutionPath));
                }

                if (!string.IsNullOrEmpty(InsightsApiKey))
                {
                    arInfo.Add("InsightsApiKey", new PString(InsightsApiKey));
                }

                arInfo.Save(Path.Combine(archiveDir, "Info.plist"));
                ArchiveDir = archiveDir;
            } catch (Exception ex) {
                Log.LogErrorFromException(ex);
                Directory.Delete(archiveDir, true);
            }

            return(!Log.HasLoggedErrors);
        }
Example #25
0
        void SaveMetaFile(int count, long size)
        {
            var meta = new PDictionary ();

            meta.Add ("RecordCount", new PNumber (count));
            meta.Add ("StandardDirectoryPerms", new PNumber (16877));
            meta.Add ("StandardFilePerms", new PNumber (-32348));
            meta.Add ("TotalUncompressedBytes", new PNumber ((int) size));
            meta.Add ("Version", new PNumber (2));

            Directory.CreateDirectory (Path.Combine (Source.ItemSpec, "META-INF"));
            meta.Save (Path.Combine (Source.ItemSpec, "META-INF", "com.apple.ZipMetadata.plist"), true, true);
        }
        public override bool Execute()
        {
            var  manifestPath            = Path.Combine(AppBundleDir.ItemSpec, "AssetPackManifestTemplate.plist");
            var  onDemandResourcesPath   = Path.Combine(AppBundleDir.ItemSpec, "OnDemandResources.plist");
            var  onDemandResourcesDir    = Path.Combine(OutputPath, "OnDemandResources");
            var  onDemandResourcesStamp  = File.GetLastWriteTime(onDemandResourcesPath);
            var  initialInstallTags      = new HashSet <string> (AssetPackUtils.ParseTags(InitialInstallTags));
            var  prefetchOrder           = AssetPackUtils.ParseTags(PrefetchOrder);
            var  manifestStamp           = File.GetLastWriteTime(manifestPath);
            var  onDemandResources       = new PDictionary();
            var  requestTags             = new PDictionary();
            bool updateOnDemandResources = false;
            var  assetPacks     = new PDictionary();
            var  manifest       = new PDictionary();
            var  resources      = new PArray();
            bool updateManifest = false;

            Log.LogTaskName("CreateAssetPackManifest");
            Log.LogTaskProperty("AppBundleDir", AppBundleDir);
            Log.LogTaskProperty("InitialInstallTags", InitialInstallTags);
            Log.LogTaskProperty("OutputPath", OutputPath);
            Log.LogTaskProperty("PrefetchOrder", PrefetchOrder);

            if (!Directory.Exists(onDemandResourcesDir))
            {
                return(!Log.HasLoggedErrors);
            }

            onDemandResources.Add("NSBundleResourceRequestAssetPacks", assetPacks);
            onDemandResources.Add("NSBundleResourceRequestTags", requestTags);

            manifest.Add("resources", resources);

            foreach (var dir in Directory.EnumerateDirectories(onDemandResourcesDir))
            {
                var         path = Path.Combine(dir, "Info.plist");
                PDictionary info;

                if (!File.Exists(path))
                {
                    continue;
                }

                var mtime = File.GetLastWriteTime(path);

                updateOnDemandResources = updateOnDemandResources || mtime > onDemandResourcesStamp;
                updateManifest          = updateManifest || mtime > manifestStamp;

                try {
                    info = PDictionary.FromFile(path);
                } catch {
                    continue;
                }

                var  bundleIdentifier   = info.GetCFBundleIdentifier();
                var  primaryContentHash = new PDictionary();
                var  resource           = new PDictionary();
                var  items = new PArray();
                long size  = 0;

                // update OnDemandResources.plist:NSBundleResourceRequestAssetPacks
                foreach (var file in Directory.EnumerateFiles(dir))
                {
                    var name = Path.GetFileName(file);

                    if (name != "Info.plist")
                    {
                        items.Add(new PString(name));
                    }

                    size += new FileInfo(file).Length;
                }

                assetPacks.Add(bundleIdentifier, items);

                // update OnDemandResources.plist:NSBundleResourceRequestTags
                var tags     = info.GetArray("Tags").OfType <PString> ().Select(x => x.Value);
                var priority = double.NaN;

                foreach (var tag in tags)
                {
                    PDictionary dict;
                    PArray      packs;

                    if (initialInstallTags.Contains(tag))
                    {
                        priority = 1.0f;
                    }
                    else
                    {
                        for (int i = 0; i < prefetchOrder.Length; i++)
                        {
                            if (tag == prefetchOrder[i])
                            {
                                var value = GetDownloadPriority(i, prefetchOrder.Length);

                                priority = double.IsNaN(priority) ? value : Math.Max(priority, value);
                                break;
                            }
                        }
                    }

                    if (!requestTags.TryGetValue(tag, out dict))
                    {
                        dict = new PDictionary();
                        dict.Add("NSAssetPacks", new PArray());

                        requestTags.Add(tag, dict);
                    }

                    packs = dict.GetArray("NSAssetPacks");

                    packs.Add(new PString(bundleIdentifier));
                }

                // update AssetPackManifestTemplate.plist
                resource.Add("URL", new PString("http://127.0.0.1" + Path.GetFullPath(dir)));
                resource.Add("bundleKey", new PString(bundleIdentifier));

                if (!double.IsNaN(priority))
                {
                    resource.Add("downloadPriority", new PReal(priority));
                }

                resource.Add("isStreamable", new PBoolean(true));
                primaryContentHash.Add("hash", mtime.ToString("yyyy-MM-dd HH:mm:ss.000"));
                primaryContentHash.Add("strategy", "modtime");
                resource.Add("primaryContentHash", primaryContentHash);
                resource.Add("uncompressedSize", new PNumber((int)((size + 8191) & ~8191)));

                resources.Add(resource);
            }

            if (updateOnDemandResources)
            {
                try {
                    onDemandResources.Save(onDemandResourcesPath, true, true);
                } catch (Exception ex) {
                    Log.LogError("Error saving `{0}': {1}", onDemandResourcesPath, ex.Message);
                }
            }

            if (updateManifest)
            {
                try {
                    manifest.Save(manifestPath, true, true);
                } catch (Exception ex) {
                    Log.LogError("Error saving `{0}': {1}", manifestPath, ex.Message);
                }
            }

            return(!Log.HasLoggedErrors);
        }
Example #27
0
        public override bool Execute()
        {
            Log.LogTaskName ("ComputeBundleResourceOutputPaths");
            Log.LogTaskProperty ("AppBundleDir", AppBundleDir);
            Log.LogTaskProperty ("BundleIdentifier", BundleIdentifier);
            Log.LogTaskProperty ("BundleResources", BundleResources);
            Log.LogTaskProperty ("IntermediateOutputPath", IntermediateOutputPath);
            Log.LogTaskProperty ("OutputPath", OutputPath);

            var intermediate = Path.Combine (IntermediateOutputPath, "assetpacks");
            var bundleResources = new List<ITaskItem> ();
            var packs = new HashSet<string> ();

            if (BundleResources != null) {
                foreach (var item in BundleResources) {
                    var logicalName = item.GetMetadata ("LogicalName");
                    var outputPath = item.GetMetadata ("OutputPath");
                    var tags = AssetPackUtils.GetResourceTags (item);
                    string hash;

                    if (tags != null) {
                        var assetpack = AssetPackUtils.GetAssetPackDirectory (OutputPath, BundleIdentifier, tags, out hash);

                        if (packs.Add (hash)) {
                            var path = Path.Combine (intermediate, hash + ".plist");

                            if (!File.Exists (path)) {
                                var plist = new PDictionary ();
                                var array = new PArray ();

                                for (int i = 0; i < tags.Count; i++)
                                    array.Add (new PString (tags[i]));

                                plist.SetCFBundleIdentifier (BundleIdentifier + ".asset-pack-" + hash);
                                plist.Add ("Tags", array);

                                Directory.CreateDirectory (intermediate);

                                plist.Save (path, true, true);
                            }

                            var manifest = new TaskItem (path);
                            manifest.SetMetadata ("OutputPath", Path.Combine (assetpack, "Info.plist"));
                            bundleResources.Add (manifest);
                        }

                        outputPath = Path.Combine (assetpack, logicalName);
                    } else if (string.IsNullOrEmpty (outputPath)) {
                        outputPath = Path.Combine (AppBundleDir.ItemSpec, logicalName);
                    }

                    var bundleResource = new TaskItem (item);

                    bundleResource.SetMetadata ("OutputPath", outputPath);

                    bundleResources.Add (bundleResource);
                }
            }

            BundleResourcesWithOutputPaths = bundleResources.ToArray ();

            return !Log.HasLoggedErrors;
        }
Example #28
0
        public virtual void ConfigureTask()
        {
            Task = CreateTask<CompileAppManifest> ();

            Task.AppBundleName = appBundleName;
            Task.AppBundleDir = "AppBundlePath";
            Task.AssemblyName = assemblyName;
            Task.AppManifest = CreateTempFile ("foo.plist");
            Task.BundleIdentifier = bundleIdentifier;
            Task.SdkPlatform = "iPhoneSimulator";

            Plist = new PDictionary ();
            Plist ["CFBundleDisplayName"] = displayName;
            Plist ["CFBundleIdentifier"] = identifier;
            Plist.Save (Task.AppManifest);
        }
        public override bool Execute()
        {
            var intermediate    = Path.Combine(IntermediateOutputPath, "assetpacks");
            var bundleResources = new List <ITaskItem> ();
            var packs           = new HashSet <string> ();

            if (BundleResources != null)
            {
                foreach (var item in BundleResources)
                {
                    var            logicalName = item.GetMetadata("LogicalName");
                    var            outputPath  = item.GetMetadata("OutputPath");
                    IList <string> tags;
                    string         hash;

                    if (EnableOnDemandResources && (tags = AssetPackUtils.GetResourceTags(item)) != null)
                    {
                        var assetpack = AssetPackUtils.GetAssetPackDirectory(OutputPath, BundleIdentifier, tags, out hash);

                        if (packs.Add(hash))
                        {
                            var path = Path.Combine(intermediate, hash + ".plist");

                            if (!File.Exists(path))
                            {
                                var plist = new PDictionary();
                                var array = new PArray();

                                for (int i = 0; i < tags.Count; i++)
                                {
                                    array.Add(new PString(tags[i]));
                                }

                                plist.SetCFBundleIdentifier(BundleIdentifier + ".asset-pack-" + hash);
                                plist.Add("Tags", array);

                                Directory.CreateDirectory(intermediate);

                                plist.Save(path, true, true);
                            }

                            var manifest = new TaskItem(path);
                            manifest.SetMetadata("OutputPath", Path.Combine(assetpack, "Info.plist"));
                            bundleResources.Add(manifest);
                        }

                        outputPath = Path.Combine(assetpack, logicalName);
                    }
                    else if (string.IsNullOrEmpty(outputPath))
                    {
                        outputPath = Path.Combine(AppBundleDir.ItemSpec, logicalName);
                    }

                    var bundleResource = new TaskItem(item);

                    bundleResource.SetMetadata("OutputPath", outputPath);

                    bundleResources.Add(bundleResource);
                }
            }

            BundleResourcesWithOutputPaths = bundleResources.ToArray();

            return(!Log.HasLoggedErrors);
        }
Example #30
0
        public override bool Execute()
        {
            var    intermediate          = Path.Combine(IntermediateOutputPath, ToolName);
            var    intermediateBundleDir = Path.Combine(intermediate, "bundle");
            var    intermediateCloneDir  = Path.Combine(intermediate, "cloned-assets");
            var    manifest         = new TaskItem(Path.Combine(intermediate, "asset-manifest.plist"));
            var    bundleResources  = new List <ITaskItem> ();
            var    outputManifests  = new List <ITaskItem> ();
            var    catalogs         = new List <ITaskItem> ();
            var    unique           = new HashSet <string> ();
            string bundleIdentifier = null;
            var    knownSpecs       = new HashSet <string> ();
            var    clones           = new HashSet <string> ();
            var    items            = new List <ITaskItem> ();
            var    specs            = new PArray();

            Log.LogTaskName("ACTool");
            Log.LogTaskProperty("AppManifest", AppManifest);
            Log.LogTaskProperty("DeviceModel", DeviceModel);
            Log.LogTaskProperty("DeviceOSVersion", DeviceOSVersion);
            Log.LogTaskProperty("EnableOnDemandResources", EnableOnDemandResources);
            Log.LogTaskProperty("ImageAssets", ImageAssets);
            Log.LogTaskProperty("IntermediateOutputPath", IntermediateOutputPath);
            Log.LogTaskProperty("IsWatchApp", IsWatchApp);
            Log.LogTaskProperty("OptimizePNGs", OptimizePNGs);
            Log.LogTaskProperty("OutputPath", OutputPath);
            Log.LogTaskProperty("ProjectDir", ProjectDir);
            Log.LogTaskProperty("ResourcePrefix", ResourcePrefix);
            Log.LogTaskProperty("SdkBinPath", SdkBinPath);
            Log.LogTaskProperty("SdkPlatform", SdkPlatform);
            Log.LogTaskProperty("SdkVersion", SdkVersion);

            switch (SdkPlatform)
            {
            case "iPhoneSimulator":
            case "iPhoneOS":
            case "MacOSX":
            case "WatchSimulator":
            case "WatchOS":
            case "AppleTVSimulator":
            case "AppleTVOS":
                break;

            default:
                Log.LogError("Unrecognized platform: {0}", SdkPlatform);
                return(false);
            }

            if (AppManifest != null)
            {
                try {
                    plist = PDictionary.FromFile(AppManifest.ItemSpec);
                } catch (Exception ex) {
                    Log.LogError(null, null, null, AppManifest.ItemSpec, 0, 0, 0, 0, "{0}", ex.Message);
                    return(false);
                }

                bundleIdentifier = plist.GetCFBundleIdentifier();
            }

            for (int i = 0; i < ImageAssets.Length; i++)
            {
                var vpath = BundleResource.GetVirtualProjectPath(ProjectDir, ImageAssets[i], !string.IsNullOrEmpty(SessionId));

                // Ignore MacOS .DS_Store files...
                if (Path.GetFileName(vpath).Equals(".DS_Store", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                // get the parent (which will typically be .appiconset, .launchimage, .imageset, .iconset, etc)
                var catalog = Path.GetDirectoryName(vpath);

                // keep walking up the directory structure until we get to the .xcassets directory
                while (!string.IsNullOrEmpty(catalog) && Path.GetExtension(catalog) != ".xcassets")
                {
                    catalog = Path.GetDirectoryName(catalog);
                }

                if (string.IsNullOrEmpty(catalog))
                {
                    Log.LogWarning(null, null, null, ImageAssets[i].ItemSpec, 0, 0, 0, 0, "Asset not part of an asset catalog: {0}", ImageAssets[i].ItemSpec);
                    continue;
                }

                if (ImageAssets[i].GetMetadata("Link") != null)
                {
                    // Note: if any of the files within a catalog are linked, we'll have to clone the *entire* catalog
                    clones.Add(catalog);
                    continue;
                }

                // filter out everything except paths containing a Contents.json file since our main processing loop only cares about these
                if (Path.GetFileName(vpath) != "Contents.json")
                {
                    continue;
                }

                items.Add(ImageAssets[i]);
            }

            // clone any *.xcassets dirs that need cloning
            if (clones.Count > 0)
            {
                if (Directory.Exists(intermediateCloneDir))
                {
                    Directory.Delete(intermediateCloneDir, true);
                }

                Directory.CreateDirectory(intermediateCloneDir);

                items.Clear();

                for (int i = 0; i < ImageAssets.Length; i++)
                {
                    var       vpath = BundleResource.GetVirtualProjectPath(ProjectDir, ImageAssets[i], !string.IsNullOrEmpty(SessionId));
                    var       clone = false;
                    ITaskItem item;

                    // Ignore MacOS .DS_Store files...
                    if (Path.GetFileName(vpath).Equals(".DS_Store", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    foreach (var catalog in clones)
                    {
                        if (vpath.Length > catalog.Length && vpath[catalog.Length] == '/' && vpath.StartsWith(catalog, StringComparison.Ordinal))
                        {
                            clone = true;
                            break;
                        }
                    }

                    if (clone)
                    {
                        var src = ImageAssets[i].GetMetadata("FullPath");

                        if (!File.Exists(src))
                        {
                            Log.LogError(null, null, null, src, 0, 0, 0, 0, "File not found: {0}", src);
                            return(false);
                        }

                        var dest = Path.Combine(intermediateCloneDir, vpath);
                        var dir  = Path.GetDirectoryName(dest);

                        Directory.CreateDirectory(dir);

                        File.Copy(src, dest, true);

                        // filter out everything except paths containing a Contents.json file since our main processing loop only cares about these
                        if (Path.GetFileName(vpath) != "Contents.json")
                        {
                            continue;
                        }

                        item = new TaskItem(dest);
                        ImageAssets[i].CopyMetadataTo(item);
                        item.SetMetadata("Link", vpath);
                    }
                    else
                    {
                        // filter out everything except paths containing a Contents.json file since our main processing loop only cares about these
                        if (Path.GetFileName(vpath) != "Contents.json")
                        {
                            continue;
                        }

                        item = ImageAssets[i];
                    }

                    items.Add(item);
                }
            }

            // Note: `items` contains only the Contents.json files at this point
            for (int i = 0; i < items.Count; i++)
            {
                var vpath = BundleResource.GetVirtualProjectPath(ProjectDir, items[i], !string.IsNullOrEmpty(SessionId));
                var path  = items[i].GetMetadata("FullPath");

                // get the parent (which will typically be .appiconset, .launchimage, .imageset, .iconset, etc)
                var catalog = Path.GetDirectoryName(vpath);
                path = Path.GetDirectoryName(path);

                // keep walking up the directory structure until we get to the .xcassets directory
                while (!string.IsNullOrEmpty(catalog) && Path.GetExtension(catalog) != ".xcassets")
                {
                    catalog = Path.GetDirectoryName(catalog);
                    path    = Path.GetDirectoryName(path);
                }

                if (unique.Add(catalog))
                {
                    var item = new TaskItem(path);
                    item.SetMetadata("Link", catalog);

                    catalogs.Add(item);
                }

                if (AppleSdkSettings.XcodeVersion.Major >= 7 && !string.IsNullOrEmpty(bundleIdentifier) && SdkPlatform != "WatchSimulator")
                {
                    var text = File.ReadAllText(items[i].ItemSpec);

                    if (string.IsNullOrEmpty(text))
                    {
                        continue;
                    }

                    var json = JsonConvert.DeserializeObject(text) as JObject;

                    if (json == null)
                    {
                        continue;
                    }

                    var properties = json.Property("properties");

                    if (properties == null)
                    {
                        continue;
                    }

                    var resourceTags = properties.Value.ToObject <JObject> ().Property("on-demand-resource-tags");

                    if (resourceTags == null || resourceTags.Value.Type != JTokenType.Array)
                    {
                        continue;
                    }

                    var    tagArray = resourceTags.Value.ToObject <JArray> ();
                    var    tags     = new HashSet <string> ();
                    string hash;

                    foreach (var tag in tagArray.Select(token => token.ToObject <string> ()))
                    {
                        tags.Add(tag);
                    }

                    var tagList = tags.ToList();
                    tagList.Sort();

                    var assetDir = AssetPackUtils.GetAssetPackDirectory(intermediate, bundleIdentifier, tagList, out hash);

                    if (knownSpecs.Add(hash))
                    {
                        var assetpack = new PDictionary();
                        var ptags     = new PArray();

                        Directory.CreateDirectory(assetDir);

                        for (int j = 0; j < tagList.Count; j++)
                        {
                            ptags.Add(new PString(tagList[j]));
                        }

                        assetpack.Add("bundle-id", new PString(string.Format("{0}.asset-pack-{1}", bundleIdentifier, hash)));
                        assetpack.Add("bundle-path", new PString(Path.GetFullPath(assetDir)));
                        assetpack.Add("tags", ptags);
                        specs.Add(assetpack);
                    }
                }
            }

            if (catalogs.Count == 0)
            {
                // There are no (supported?) asset catalogs
                return(true);
            }

            partialAppManifest = new TaskItem(Path.Combine(intermediate, "partial-info.plist"));

            if (specs.Count > 0)
            {
                outputSpecs = Path.Combine(intermediate, "output-specifications.plist");
                specs.Save(outputSpecs, true);
            }

            Directory.CreateDirectory(intermediateBundleDir);

            // Note: Compile() will set the PartialAppManifest property if it is used...
            if ((Compile(catalogs.ToArray(), intermediateBundleDir, manifest)) != 0)
            {
                return(false);
            }

            if (PartialAppManifest != null && !File.Exists(PartialAppManifest.GetMetadata("FullPath")))
            {
                Log.LogError("Partial Info.plist file was not generated: {0}", PartialAppManifest.GetMetadata("FullPath"));
            }

            try {
                var manifestOutput = PDictionary.FromFile(manifest.ItemSpec);

                LogWarningsAndErrors(manifestOutput, catalogs[0]);

                bundleResources.AddRange(GetCompiledBundleResources(manifestOutput, intermediateBundleDir));
                outputManifests.Add(manifest);
            } catch (Exception ex) {
                Log.LogError("Failed to load output manifest for {0} for the file {2}: {1}", ToolName, ex.Message, manifest.ItemSpec);
            }

            foreach (var assetpack in specs.OfType <PDictionary> ())
            {
                var path       = Path.Combine(assetpack.GetString("bundle-path").Value, "Info.plist");
                var bundlePath = PathUtils.AbsoluteToRelative(intermediate, path);
                var outputPath = Path.Combine(OutputPath, bundlePath);
                var rpath      = Path.Combine(intermediate, bundlePath);
                var dict       = new PDictionary();

                dict.SetCFBundleIdentifier(assetpack.GetString("bundle-id").Value);
                dict.Add("Tags", assetpack.GetArray("tags").Clone());

                dict.Save(path, true, true);

                var item = new TaskItem(rpath);
                item.SetMetadata("LogicalName", bundlePath);
                item.SetMetadata("OutputPath", outputPath);
                item.SetMetadata("Optimize", "false");

                bundleResources.Add(item);
            }

            BundleResources = bundleResources.ToArray();
            OutputManifests = outputManifests.ToArray();

            return(!Log.HasLoggedErrors);
        }
Example #31
0
        }                                                    // default is `MonoBundle` but that can be changed with `_CustomBundleName`

        public override bool Execute()
        {
            var archiveDir = CreateArchiveDirectory();

            try {
                var plist       = PDictionary.FromFile(PlatformFrameworkHelper.GetAppManifestPath(Platform, AppBundleDir.ItemSpec));
                var productsDir = Path.Combine(archiveDir, "Products");

                // Archive the Applications...
                var appDestDir = Path.Combine(productsDir, "Applications", Path.GetFileName(AppBundleDir.ItemSpec));
                Ditto(AppBundleDir.ItemSpec, appDestDir);

                // Archive the main dSYM...
                ArchiveDSym(DSYMDir, archiveDir);

                // for each `.dylib` (file) inside `MonoBundle` there could be a corresponding `.dSYM` - e.g. when using an AOT mode
                foreach (var dylib in Directory.GetFiles(Path.Combine(AppBundleDir.ItemSpec, "Contents", CustomBundleName), "*.dylib"))
                {
                    var dsym = Path.Combine(AppBundleDir.ItemSpec, "..", Path.GetFileName(dylib) + ".dSYM");
                    ArchiveDSym(dsym, archiveDir);
                }

                // for each user framework (directory) that is bundled inside the app we must also archive their dSYMs, if available
                var fks = Path.Combine(AppBundleDir.ItemSpec, "Contents", "Frameworks");
                if (Directory.Exists(fks))
                {
                    foreach (var fx in Directory.GetDirectories(fks, "*.framework"))
                    {
                        var dsym = Path.Combine(AppBundleDir.ItemSpec, "..", Path.GetFileName(fx) + ".dSYM");
                        ArchiveDSym(dsym, archiveDir);
                    }
                }

                // Generate an archive Info.plist
                var arInfo = new PDictionary();
                var props  = new PDictionary();
                props.Add("ApplicationPath", new PString(string.Format("Applications/{0}", Path.GetFileName(AppBundleDir.ItemSpec))));
                props.Add("CFBundleIdentifier", new PString(plist.GetCFBundleIdentifier()));

                var version = plist.GetCFBundleShortVersionString();
                var build   = plist.GetCFBundleVersion();
                props.Add("CFBundleShortVersionString", new PString(version ?? (build ?? "1.0")));
                props.Add("CFBundleVersion", new PString(build ?? "1.0"));
                props.Add("SigningIdentity", SigningKey);

                arInfo.Add("ApplicationProperties", props);
                arInfo.Add("ArchiveVersion", new PNumber(2));
                arInfo.Add("CreationDate", new PDate(Now.ToUniversalTime()));
                arInfo.Add("Name", new PString(plist.GetCFBundleName() ?? plist.GetCFBundleDisplayName()));

                if (!string.IsNullOrEmpty(ProjectGuid))
                {
                    arInfo.Add("ProjectGuid", new PString(ProjectGuid));
                }

                if (!string.IsNullOrEmpty(ProjectTypeGuids))
                {
                    arInfo.Add("ProjectTypeGuids", new PString(ProjectTypeGuids));
                }

                if (!string.IsNullOrEmpty(SolutionPath))
                {
                    arInfo.Add("SolutionName", new PString(Path.GetFileNameWithoutExtension(SolutionPath)));
                    arInfo.Add("SolutionPath", new PString(SolutionPath));
                }

                if (!string.IsNullOrEmpty(InsightsApiKey))
                {
                    arInfo.Add("InsightsApiKey", new PString(InsightsApiKey));
                }

                arInfo.Save(Path.Combine(archiveDir, "Info.plist"));
                ArchiveDir = archiveDir;
            } catch (Exception ex) {
                Log.LogErrorFromException(ex);
                Directory.Delete(archiveDir, true);
            }

            return(!Log.HasLoggedErrors);
        }