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 Codesign task. #22334

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 26 additions & 21 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/Codesign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
using Xamarin.Messaging.Build.Client;
using Xamarin.Utils;

// Disable until we get around to enable + fix any issues.
#nullable disable
#nullable enable

namespace Xamarin.MacDev.Tasks {
public class Codesign : XamarinParallelTask, ITaskCallback, ICancelableTask {
const string ToolName = "codesign";
const string MacOSDirName = "MacOS";
const string CodeSignatureDirName = "_CodeSignature";
string toolExe;
string? toolExe;

#region Inputs

Expand All @@ -31,31 +30,31 @@ public class Codesign : XamarinParallelTask, ITaskCallback, ICancelableTask {
public bool DisallowResourcesSubdirectoryInAppBundle { get; set; }

// Can also be specified per resource using the 'CodesignStampFile' metadata
public string StampFile { get; set; }
public string StampFile { get; set; } = string.Empty;

// Can also be specified per resource using the 'CodesignAllocate' metadata
public string CodesignAllocate { get; set; }
public string CodesignAllocate { get; set; } = string.Empty;

// Can also be specified per resource using the 'CodesignDisableTimestamp' metadata
public bool DisableTimestamp { get; set; }

// Can also be specified per resource using the 'CodesignEntitlements' metadata
public string Entitlements { get; set; }
public string Entitlements { get; set; } = string.Empty;

// Can also be specified per resource using the 'CodesignKeychain' metadata
public string Keychain { get; set; }
public string Keychain { get; set; } = string.Empty;

[Required]
public ITaskItem [] Resources { get; set; }
public ITaskItem [] Resources { get; set; } = Array.Empty<ITaskItem> ();

// Can also be specified per resource using the 'CodesignResourceRules' metadata
public string ResourceRules { get; set; }
public string ResourceRules { get; set; } = string.Empty;

// Can also be specified per resource using the 'CodesignSigningKey' metadata
public string SigningKey { get; set; }
public string SigningKey { get; set; } = string.Empty;

// Can also be specified per resource using the 'CodesignExtraArgs' metadata
public string ExtraArgs { get; set; }
public string ExtraArgs { get; set; } = string.Empty;

// Can also be specified per resource using the 'CodesignDeep' metadata (yes, the naming difference is correct and due to historical reasons)
public bool IsAppExtension { get; set; }
Expand All @@ -71,7 +70,7 @@ public string ToolExe {
set { toolExe = value; }
}

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

#endregion

Expand All @@ -80,7 +79,7 @@ public string ToolExe {
// This output value is not observed anywhere in our targets, but it's required for building on Windows
// to make sure any codesigned files other tasks depend on are copied back to the windows machine.
[Output]
public ITaskItem [] CodesignedFiles { get; set; }
public ITaskItem [] CodesignedFiles { get; set; } = Array.Empty<ITaskItem> ();

#endregion

Expand Down Expand Up @@ -197,8 +196,8 @@ string ResolvePath (ITaskItem item, string path)
if (Path.IsPathRooted (path))
return path;

var sourceProjectPath = GetNonEmptyStringOrFallback (item, "SourceProjectPath", null);
if (sourceProjectPath is null)
var sourceProjectPath = GetNonEmptyStringOrFallback (item, "SourceProjectPath", out var foundSourceProjectPath, "");
if (!foundSourceProjectPath)
return path;

return Path.Combine (sourceProjectPath, path);
Expand Down Expand Up @@ -299,18 +298,18 @@ void Sign (SignInfo info)
var item = info.Item;
var fileName = GetFullPathToTool ();
var arguments = info.GetCommandLineArguments (this);
var environment = new Dictionary<string, string> () {
var environment = new Dictionary<string, string?> () {
{ "CODESIGN_ALLOCATE", GetCodesignAllocate (item) },
};
var rv = ExecuteAsync (fileName, arguments, null, environment, mergeOutput: false).Result;
var exitCode = rv.ExitCode;
var messages = rv.StandardOutput.ToString ();
var messages = rv.StandardOutput?.ToString () ?? string.Empty;

if (messages.Length > 0)
Log.LogMessage (MessageImportance.Normal, "{0}", messages.ToString ());

if (exitCode != 0) {
var errors = rv.StandardError.ToString ();
var errors = rv.StandardError?.ToString () ?? string.Empty;
if (errors.Length > 0)
Log.LogError (MSBStrings.E0004, item.ItemSpec, errors);
else
Expand All @@ -327,7 +326,7 @@ void Sign (SignInfo info)
File.WriteAllText (stampFile, info.GetStampFileContents (this));
} else {
Log.LogMessage (MessageImportance.Low, "The stamp file '{0}' does not exit for the item '{1}', and it will be created", stampFile, item.ItemSpec);
Directory.CreateDirectory (Path.GetDirectoryName (stampFile));
Directory.CreateDirectory (Path.GetDirectoryName (stampFile)!);
File.WriteAllText (stampFile, info.GetStampFileContents (this));
}

Expand Down Expand Up @@ -423,7 +422,7 @@ bool ExecuteUnsafe ()
var itemsToSign = new List<SignInfo> ();
for (var i = 0; i < resourcesToSign.Length; i++) {
var item = resourcesToSign [i];
var info = new SignInfo { Item = item };
var info = new SignInfo (item);
if (!Validate (info))
continue;
if (NeedsCodesign (resourcesToSign, i, info.GetStampFileContents (this)))
Expand Down Expand Up @@ -580,7 +579,13 @@ IEnumerable<ITaskItem> GetCodesignedFiles (ITaskItem item)
class SignInfo {
public ITaskItem Item;

IList<string> arguments;
IList<string>? arguments;

public SignInfo (ITaskItem item)
{
Item = item;
}

public IList<string> GetCommandLineArguments (Codesign task)
{
if (arguments is null)
Expand Down
Loading