Skip to content

Commit fb6f7a0

Browse files
authored
[msbuild] Enable nullability in the Codesign task. (#22334)
1 parent eebba2d commit fb6f7a0

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

msbuild/Xamarin.MacDev.Tasks/Tasks/Codesign.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
using Xamarin.Messaging.Build.Client;
1414
using Xamarin.Utils;
1515

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

1918
namespace Xamarin.MacDev.Tasks {
2019
public class Codesign : XamarinParallelTask, ITaskCallback, ICancelableTask {
2120
const string ToolName = "codesign";
2221
const string MacOSDirName = "MacOS";
2322
const string CodeSignatureDirName = "_CodeSignature";
24-
string toolExe;
23+
string? toolExe;
2524

2625
#region Inputs
2726

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

3332
// Can also be specified per resource using the 'CodesignStampFile' metadata
34-
public string StampFile { get; set; }
33+
public string StampFile { get; set; } = string.Empty;
3534

3635
// Can also be specified per resource using the 'CodesignAllocate' metadata
37-
public string CodesignAllocate { get; set; }
36+
public string CodesignAllocate { get; set; } = string.Empty;
3837

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

4241
// Can also be specified per resource using the 'CodesignEntitlements' metadata
43-
public string Entitlements { get; set; }
42+
public string Entitlements { get; set; } = string.Empty;
4443

4544
// Can also be specified per resource using the 'CodesignKeychain' metadata
46-
public string Keychain { get; set; }
45+
public string Keychain { get; set; } = string.Empty;
4746

4847
[Required]
49-
public ITaskItem [] Resources { get; set; }
48+
public ITaskItem [] Resources { get; set; } = Array.Empty<ITaskItem> ();
5049

5150
// Can also be specified per resource using the 'CodesignResourceRules' metadata
52-
public string ResourceRules { get; set; }
51+
public string ResourceRules { get; set; } = string.Empty;
5352

5453
// Can also be specified per resource using the 'CodesignSigningKey' metadata
55-
public string SigningKey { get; set; }
54+
public string SigningKey { get; set; } = string.Empty;
5655

5756
// Can also be specified per resource using the 'CodesignExtraArgs' metadata
58-
public string ExtraArgs { get; set; }
57+
public string ExtraArgs { get; set; } = string.Empty;
5958

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

74-
public string ToolPath { get; set; }
73+
public string ToolPath { get; set; } = string.Empty;
7574

7675
#endregion
7776

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

8584
#endregion
8685

@@ -197,8 +196,8 @@ string ResolvePath (ITaskItem item, string path)
197196
if (Path.IsPathRooted (path))
198197
return path;
199198

200-
var sourceProjectPath = GetNonEmptyStringOrFallback (item, "SourceProjectPath", null);
201-
if (sourceProjectPath is null)
199+
var sourceProjectPath = GetNonEmptyStringOrFallback (item, "SourceProjectPath", out var foundSourceProjectPath, "");
200+
if (!foundSourceProjectPath)
202201
return path;
203202

204203
return Path.Combine (sourceProjectPath, path);
@@ -299,18 +298,18 @@ void Sign (SignInfo info)
299298
var item = info.Item;
300299
var fileName = GetFullPathToTool ();
301300
var arguments = info.GetCommandLineArguments (this);
302-
var environment = new Dictionary<string, string> () {
301+
var environment = new Dictionary<string, string?> () {
303302
{ "CODESIGN_ALLOCATE", GetCodesignAllocate (item) },
304303
};
305304
var rv = ExecuteAsync (fileName, arguments, null, environment, mergeOutput: false).Result;
306305
var exitCode = rv.ExitCode;
307-
var messages = rv.StandardOutput.ToString ();
306+
var messages = rv.StandardOutput?.ToString () ?? string.Empty;
308307

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

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

@@ -423,7 +422,7 @@ bool ExecuteUnsafe ()
423422
var itemsToSign = new List<SignInfo> ();
424423
for (var i = 0; i < resourcesToSign.Length; i++) {
425424
var item = resourcesToSign [i];
426-
var info = new SignInfo { Item = item };
425+
var info = new SignInfo (item);
427426
if (!Validate (info))
428427
continue;
429428
if (NeedsCodesign (resourcesToSign, i, info.GetStampFileContents (this)))
@@ -580,7 +579,13 @@ IEnumerable<ITaskItem> GetCodesignedFiles (ITaskItem item)
580579
class SignInfo {
581580
public ITaskItem Item;
582581

583-
IList<string> arguments;
582+
IList<string>? arguments;
583+
584+
public SignInfo (ITaskItem item)
585+
{
586+
Item = item;
587+
}
588+
584589
public IList<string> GetCommandLineArguments (Codesign task)
585590
{
586591
if (arguments is null)

0 commit comments

Comments
 (0)