Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[msbuild] Enable nullability in the CompileEntitlements task. #15912

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 32 additions & 34 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/CompileEntitlementsTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Xamarin.Localization.MSBuild;
using Xamarin.Utils;

#nullable enable

namespace Xamarin.MacDev.Tasks
{
public abstract class CompileEntitlementsTaskBase : XamarinTask
Expand Down Expand Up @@ -46,42 +48,42 @@ public abstract class CompileEntitlementsTaskBase : XamarinTask
#region Inputs

[Required]
public string AppBundleDir { get; set; }
public string AppBundleDir { get; set; } = string.Empty;

[Required]
public string AppIdentifier { get; set; }
public string AppIdentifier { get; set; } = string.Empty;

[Required]
public string BundleIdentifier { get; set; }
public string BundleIdentifier { get; set; } = string.Empty;

[Required]
public ITaskItem CompiledEntitlements { get; set; }
public ITaskItem? CompiledEntitlements { get; set; }

public bool Debug { get; set; }

public string Entitlements { get; set; }
public string Entitlements { get; set; } = string.Empty;

[Required]
public bool IsAppExtension { get; set; }

public string ProvisioningProfile { get; set; }
public string ProvisioningProfile { get; set; } = string.Empty;

[Required]
public string SdkDevPath { get; set; }
public string SdkDevPath { get; set; } = string.Empty;

public bool SdkIsSimulator { get; set; }

[Required]
public string SdkPlatform { get; set; }
public string SdkPlatform { get; set; } = string.Empty;

[Required]
public string SdkVersion { get; set; }
public string SdkVersion { get; set; } = string.Empty;

[Output]
public ITaskItem EntitlementsInExecutable { get; set; }
public ITaskItem? EntitlementsInExecutable { get; set; }

[Output]
public ITaskItem EntitlementsInSignature { get; set; }
public ITaskItem? EntitlementsInSignature { get; set; }

#endregion

Expand Down Expand Up @@ -139,19 +141,15 @@ public abstract class CompileEntitlementsTaskBase : XamarinTask
}
}

protected virtual bool MergeProfileEntitlements {
get { return true; }
}

PString MergeEntitlementString (PString pstr, MobileProvision profile, bool expandWildcards)
PString MergeEntitlementString (PString pstr, MobileProvision? profile, bool expandWildcards)
{
string TeamIdentifierPrefix;
string AppIdentifierPrefix;

if (string.IsNullOrEmpty (pstr.Value))
return (PString) pstr.Clone ();

if (profile == null) {
if (profile is null) {
if (!warnedTeamIdentifierPrefix && pstr.Value.Contains ("$(TeamIdentifierPrefix)")) {
Log.LogWarning (null, null, null, Entitlements, 0, 0, 0, 0, MSBStrings.W0108);
warnedTeamIdentifierPrefix = true;
Expand All @@ -163,12 +161,12 @@ PString MergeEntitlementString (PString pstr, MobileProvision profile, bool expa
}
}

if (profile != null && profile.ApplicationIdentifierPrefix.Count > 0)
if (profile is not null && profile.ApplicationIdentifierPrefix.Count > 0)
AppIdentifierPrefix = profile.ApplicationIdentifierPrefix[0] + ".";
else
AppIdentifierPrefix = string.Empty;

if (profile != null && profile.TeamIdentifierPrefix.Count > 0)
if (profile is not null && profile.TeamIdentifierPrefix.Count > 0)
TeamIdentifierPrefix = profile.TeamIdentifierPrefix[0] + ".";
else
TeamIdentifierPrefix = AppIdentifierPrefix;
Expand Down Expand Up @@ -203,12 +201,12 @@ PString MergeEntitlementString (PString pstr, MobileProvision profile, bool expa
return new PString (expanded);
}

PArray MergeEntitlementArray (PArray array, MobileProvision profile)
PArray? MergeEntitlementArray (PArray array, MobileProvision? profile)
{
var result = new PArray ();

foreach (var item in array) {
PObject value;
PObject? value;

if (item is PDictionary)
value = MergeEntitlementDictionary ((PDictionary) item, profile);
Expand All @@ -219,7 +217,7 @@ PArray MergeEntitlementArray (PArray array, MobileProvision profile)
else
value = item.Clone ();

if (value != null)
if (value is not null)
result.Add (value);
}

Expand All @@ -229,12 +227,12 @@ PArray MergeEntitlementArray (PArray array, MobileProvision profile)
return null;
}

PDictionary MergeEntitlementDictionary (PDictionary dict, MobileProvision profile)
PDictionary MergeEntitlementDictionary (PDictionary dict, MobileProvision? profile)
{
var result = new PDictionary ();

foreach (var item in dict) {
PObject value = item.Value;
PObject? value = item.Value;

if (value is PDictionary)
value = MergeEntitlementDictionary ((PDictionary) value, profile);
Expand All @@ -245,7 +243,7 @@ PDictionary MergeEntitlementDictionary (PDictionary dict, MobileProvision profil
else
value = value.Clone ();

if (value != null)
if (value is not null)
result.Add (item.Key, value);
}

Expand Down Expand Up @@ -289,11 +287,11 @@ static void WriteXcent (PObject doc, string path)
}
}

protected virtual PDictionary GetCompiledEntitlements (MobileProvision profile, PDictionary template)
protected virtual PDictionary GetCompiledEntitlements (MobileProvision? profile, PDictionary template)
{
var entitlements = new PDictionary ();

if (profile != null && MergeProfileEntitlements) {
if (profile is not null) {
// start off with the settings from the provisioning profile
foreach (var item in profile.Entitlements) {
if (!AllowedProvisioningKeys.Contains (item.Key))
Expand All @@ -312,7 +310,7 @@ protected virtual PDictionary GetCompiledEntitlements (MobileProvision profile,
else
value = value.Clone ();

if (value != null)
if (value is not null)
entitlements.Add (item.Key, value);
}
}
Expand All @@ -325,15 +323,15 @@ protected virtual PDictionary GetCompiledEntitlements (MobileProvision profile,
item.Key == "com.apple.developer.icloud-container-identifiers" ||
item.Key == "com.apple.developer.icloud-container-environment" ||
item.Key == "com.apple.developer.icloud-services") {
if (profile == null)
if (profile is null)
Log.LogWarning (null, null, null, Entitlements, 0, 0, 0, 0, MSBStrings.W0110, item.Key);
else if (!profile.Entitlements.ContainsKey (item.Key))
Log.LogWarning (null, null, null, Entitlements, 0, 0, 0, 0, MSBStrings.W0111, item.Key);
} else if (item.Key == ApplicationIdentifierKey) {
var str = value as PString;

// Ignore ONLY if it is empty, otherwise take the user's value
if (str == null || string.IsNullOrEmpty (str.Value))
if (str is null || string.IsNullOrEmpty (str.Value))
continue;
}

Expand All @@ -346,7 +344,7 @@ protected virtual PDictionary GetCompiledEntitlements (MobileProvision profile,
else
value = value.Clone ();

if (value != null)
if (value is not null)
entitlements[item.Key] = value;
}

Expand Down Expand Up @@ -389,7 +387,7 @@ protected virtual MobileProvision GetMobileProvision (MobileProvisionPlatform pl
public override bool Execute ()
{
MobileProvisionPlatform platform;
MobileProvision profile;
MobileProvision? profile;
PDictionary template;
PDictionary compiled;
PDictionary archived;
Expand Down Expand Up @@ -418,7 +416,7 @@ public override bool Execute ()
}

if (!string.IsNullOrEmpty (ProvisioningProfile)) {
if ((profile = GetMobileProvision (platform, ProvisioningProfile)) == null) {
if ((profile = GetMobileProvision (platform, ProvisioningProfile)) is null) {
Log.LogError (MSBStrings.E0049, ProvisioningProfile);
return false;
}
Expand Down Expand Up @@ -448,7 +446,7 @@ public override bool Execute ()
archived = GetArchivedExpandedEntitlements (template, compiled);

try {
Directory.CreateDirectory (Path.GetDirectoryName (CompiledEntitlements.ItemSpec));
Directory.CreateDirectory (Path.GetDirectoryName (CompiledEntitlements!.ItemSpec));
WriteXcent (compiled, CompiledEntitlements.ItemSpec);
} catch (Exception ex) {
Log.LogError (MSBStrings.E0114, CompiledEntitlements, ex.Message);
Expand Down