-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathAddPackageType.cs
More file actions
60 lines (51 loc) · 1.82 KB
/
AddPackageType.cs
File metadata and controls
60 lines (51 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Build.Framework;
using Task = Microsoft.Build.Utilities.Task;
using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.NET.Build.Tasks
{
[MSBuildMultiThreadableTask]
public class AddPackageType : Task
{
public string? CurrentPackageType { get; set; }
[Required]
public string? PackageTypeToAdd { get; set; }
[Output]
public string[]? UpdatedPackageType { get; set; }
public override bool Execute()
{
string current = CurrentPackageType ?? string.Empty;
string toAdd = (PackageTypeToAdd ?? string.Empty).Trim();
if (string.IsNullOrEmpty(toAdd))
{
UpdatedPackageType = current.Split(';');
return true;
}
// Split current types, trim, and filter out empty
var types = current.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.Trim())
.Where(t => !string.IsNullOrEmpty(t))
.ToList();
// Check if toAdd (case-insensitive, ignoring version) is already present
bool alreadyPresent = types.Any(t =>
{
var typeName = t.Split(',')[0].Trim();
return typeName.Equals(toAdd, StringComparison.InvariantCultureIgnoreCase);
});
if (alreadyPresent)
{
UpdatedPackageType = current.Split(';');
}
else
{
types.Insert(0, toAdd);
UpdatedPackageType = types.ToArray();
}
return true;
}
}
}