CompileAppManifest CreateTask(string?tmpdir = null, ApplePlatform platform = ApplePlatform.iOS)
        {
            if (string.IsNullOrEmpty(tmpdir))
            {
                tmpdir = Cache.CreateTemporaryDirectory();
            }

            var task = CreateTask <CompileAppManifest> ();

            task.AssemblyName           = "AssemblyName";
            task.AppBundleName          = "AppBundleName";
            task.CompiledAppManifest    = new TaskItem(Path.Combine(tmpdir, "TemporaryAppManifest.plist"));
            task.DefaultSdkVersion      = Sdks.GetAppleSdk(platform).GetInstalledSdkVersions(false).First().ToString();
            task.SdkPlatform            = PlatformFrameworkHelper.GetSdkPlatform(platform, false);
            task.SdkVersion             = task.DefaultSdkVersion;
            task.TargetFrameworkMoniker = TargetFramework.GetTargetFramework(platform, true).ToString();

            return(task);
        }
Example #2
0
        public override bool Execute()
        {
            PDictionary plist = null;

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

            var minimumOSVersionInManifest = plist?.Get <PString> (PlatformFrameworkHelper.GetMinimumOSVersionKey(Platform))?.Value;

            if (string.IsNullOrEmpty(minimumOSVersionInManifest))
            {
                MinimumOSVersion = SdkVersion;
            }
            else if (!IAppleSdkVersion_Extensions.TryParse(minimumOSVersionInManifest, out var _))
            {
                Log.LogError(null, null, null, AppManifest.ItemSpec, 0, 0, 0, 0, MSBStrings.E0011, minimumOSVersionInManifest);
                return(false);
            }
            else
            {
                MinimumOSVersion = minimumOSVersionInManifest;
            }

            if (Platform == ApplePlatform.MacCatalyst)
            {
                // Convert the min macOS version to the min iOS version, which the rest of our tooling expects.
                if (!MacCatalystSupport.TryGetiOSVersion(Sdks.GetAppleSdk(Platform).GetSdkPath(SdkVersion, false), MinimumOSVersion, out var convertedVersion))
                {
                    Log.LogError(MSBStrings.E0187, MinimumOSVersion);
                }
                MinimumOSVersion = convertedVersion;
            }

            return(true);
        }
Example #3
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);
        }
        public override bool Execute()
        {
            PDictionary?plist = null;

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

            CFBundleExecutable   = plist.GetCFBundleExecutable();
            CFBundleDisplayName  = plist?.GetCFBundleDisplayName();
            CFBundleIdentifier   = plist?.GetCFBundleIdentifier();
            CFBundleVersion      = plist?.GetCFBundleVersion();
            CLKComplicationGroup = plist?.Get <PString> (ManifestKeys.CLKComplicationGroup)?.Value;

            MinimumOSVersion = plist?.Get <PString> (PlatformFrameworkHelper.GetMinimumOSVersionKey(Platform))?.Value;
            if (Platform == ApplePlatform.MacCatalyst)
            {
                // The minimum version in the Info.plist is the macOS version. However, the rest of our tooling
                // expects the iOS version, so expose that.
                if (!MacCatalystSupport.TryGetiOSVersion(Sdks.GetAppleSdk(Platform).GetSdkPath(SdkVersion, false), MinimumOSVersion !, out var convertedVersion, out var knownMacOSVersions))
                {
                    Log.LogError(MSBStrings.E0187, MinimumOSVersion, string.Join(", ", knownMacOSVersions));
                }
                MinimumOSVersion = convertedVersion;
            }

            NSExtensionPointIdentifier = plist?.GetNSExtensionPointIdentifier();
            UIDeviceFamily             = plist?.GetUIDeviceFamily().ToString();
            WKWatchKitApp       = plist?.GetWKWatchKitApp() == true;
            XSAppIconAssets     = plist?.Get <PString> (ManifestKeys.XSAppIconAssets)?.Value;
            XSLaunchImageAssets = plist?.Get <PString> (ManifestKeys.XSLaunchImageAssets)?.Value;

            return(!Log.HasLoggedErrors);
        }
        bool SetMinimumOSVersion(PDictionary plist)
        {
            var    minimumVersionKey          = PlatformFrameworkHelper.GetMinimumOSVersionKey(Platform);
            var    minimumOSVersionInManifest = plist?.Get <PString> (minimumVersionKey)?.Value;
            string convertedSupportedOSPlatformVersion;
            string minimumOSVersion;

            if (Platform == ApplePlatform.MacCatalyst && !string.IsNullOrEmpty(SupportedOSPlatformVersion))
            {
                // SupportedOSPlatformVersion is the iOS version for Mac Catalyst.
                // But we need to store the macOS version in the app manifest, so convert it to the macOS version here.
                if (!MacCatalystSupport.TryGetMacOSVersion(Sdks.GetAppleSdk(Platform).GetSdkPath(SdkVersion, false), SupportedOSPlatformVersion, out var convertedVersion, out var knowniOSVersions))
                {
                    Log.LogError(MSBStrings.E0188, SupportedOSPlatformVersion, string.Join(", ", knowniOSVersions));
                }
                convertedSupportedOSPlatformVersion = convertedVersion;
            }
            else
            {
                convertedSupportedOSPlatformVersion = SupportedOSPlatformVersion;
            }

            if (Platform == ApplePlatform.MacCatalyst && string.IsNullOrEmpty(minimumOSVersionInManifest))
            {
                // If there was no value for the macOS min version key, then check the iOS min version key.
                var minimumiOSVersionInManifest = plist?.Get <PString> (ManifestKeys.MinimumOSVersion)?.Value;
                if (!string.IsNullOrEmpty(minimumiOSVersionInManifest))
                {
                    // Convert to the macOS version
                    if (!MacCatalystSupport.TryGetMacOSVersion(Sdks.GetAppleSdk(Platform).GetSdkPath(SdkVersion, false), minimumiOSVersionInManifest, out var convertedVersion, out var knowniOSVersions))
                    {
                        Log.LogError(MSBStrings.E0188, minimumiOSVersionInManifest, string.Join(", ", knowniOSVersions));
                    }
                    minimumOSVersionInManifest = convertedVersion;
                }
            }

            if (string.IsNullOrEmpty(minimumOSVersionInManifest))
            {
                // Nothing is specified in the Info.plist - use SupportedOSPlatformVersion, and if that's not set, then use the sdkVersion
                if (!string.IsNullOrEmpty(convertedSupportedOSPlatformVersion))
                {
                    minimumOSVersion = convertedSupportedOSPlatformVersion;
                }
                else
                {
                    minimumOSVersion = SdkVersion;
                }
            }
            else if (!IAppleSdkVersion_Extensions.TryParse(minimumOSVersionInManifest, out var _))
            {
                LogAppManifestError(MSBStrings.E0011, minimumOSVersionInManifest);
                return(false);
            }
            else if (!string.IsNullOrEmpty(convertedSupportedOSPlatformVersion) && convertedSupportedOSPlatformVersion != minimumOSVersionInManifest)
            {
                // SupportedOSPlatformVersion and the value in the Info.plist are not the same. This is an error.
                LogAppManifestError(MSBStrings.E7082, minimumVersionKey, minimumOSVersionInManifest, SupportedOSPlatformVersion);
                return(false);
            }
            else
            {
                minimumOSVersion = minimumOSVersionInManifest;
            }

            // Write out our value
            plist [minimumVersionKey] = minimumOSVersion;

            return(true);
        }
        protected override bool Compile(PDictionary plist)
        {
            var currentSDK = Sdks.GetAppleSdk(Platform);

            sdkVersion = AppleSdkVersion.Parse(DefaultSdkVersion);
            if (!currentSDK.SdkIsInstalled(sdkVersion, SdkIsSimulator))
            {
                Log.LogError(null, null, null, null, 0, 0, 0, 0, MSBStrings.E0013, Platform, sdkVersion);
                return(false);
            }

            supportedDevices = plist.GetUIDeviceFamily();

            if (!IsWatchApp)
            {
                var version = Sdks.XamIOS.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.GetAppleDTSettings();

            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");

            if (IsIOS)
            {
                var executable = plist.GetCFBundleExecutable();
                if (executable.EndsWith(".app", StringComparison.Ordinal))
                {
                    LogAppManifestError(MSBStrings.E0014, executable);
                }
            }

            if (!string.IsNullOrEmpty(ResourceRules))
            {
                plist.SetIfNotPresent(ManifestKeys.CFBundleResourceSpecification, Path.GetFileName(ResourceRules));
            }
            if (!plist.ContainsKey(ManifestKeys.CFBundleSupportedPlatforms))
            {
                plist[ManifestKeys.CFBundleSupportedPlatforms] = new PArray {
                    SdkPlatform
                }
            }
            ;

            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;
            }

            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);

            if (IsWatchExtension)
            {
                // Note: Only watchOS1 Extensions target Xamarin.iOS
                if (Platform == ApplePlatform.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);
                }
            }

            SetRequiredArchitectures(plist);

            if (IsIOS)
            {
                Validation(plist);
            }

            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 (Platform)
            {
            case ApplePlatform.iOS:
                SetIOSDeviceFamily(plist);
                break;

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

            case ApplePlatform.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 (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, MSBStrings.M0017);
            }
            else
            {
                Log.LogMessage(MessageImportance.Low, MSBStrings.M0018);
                ats.SetBooleanOrRemove(ManifestKeys.NSAllowsArbitraryLoads, true);
            }
        }

        void Validation(PDictionary plist)
        {
            if (!Validate)
            {
                return;
            }

            var supportsIPhone = (supportedDevices & IPhoneDeviceType.IPhone) != 0 ||
                                 supportedDevices == IPhoneDeviceType.NotSet;
            var supportsIPad = (supportedDevices & IPhoneDeviceType.IPad) != 0;

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

                if (supportsIPhone)
                {
                    orientation = plist.GetUISupportedInterfaceOrientations(false);
                    if (orientation == IPhoneOrientation.None)
                    {
                        LogAppManifestWarning(MSBStrings.W0019);
                    }
                    else if (!orientation.IsValidPair())
                    {
                        LogAppManifestWarning(MSBStrings.W0020);
                    }
                }

                if (supportsIPad)
                {
                    orientation = plist.GetUISupportedInterfaceOrientations(true);
                    if (orientation == IPhoneOrientation.None)
                    {
                        LogAppManifestWarning(MSBStrings.W0021);
                    }
                    else if (!orientation.IsValidPair())
                    {
                        LogAppManifestWarning(MSBStrings.W0022);
                    }
                }
            }
        }
    }