-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathGetPackOutputItemsTask.cs
More file actions
111 lines (89 loc) · 4.42 KB
/
GetPackOutputItemsTask.cs
File metadata and controls
111 lines (89 loc) · 4.42 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable disable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using NuGet.Commands;
using NuGet.Common;
using NuGet.Versioning;
namespace NuGet.Build.Tasks.Pack
{
public class GetPackOutputItemsTask : Microsoft.Build.Utilities.Task
{
[Required]
public string PackageId { get; set; }
[Required]
public string PackageVersion { get; set; }
[Required]
public string PackageOutputPath { get; set; }
[Required]
public string NuspecOutputPath { get; set; }
public string NuspecFile { get; set; }
public string[] NuspecProperties { get; set; }
public bool IncludeSymbols { get; set; }
public bool IncludeSource { get; set; }
public string SymbolPackageFormat { get; set; }
public bool OutputFileNamesWithoutVersion { get; set; }
/// <summary>
/// Output items
/// </summary>
[Output]
public ITaskItem[] OutputPackItems { get; set; }
public override bool Execute()
{
var packageId = PackageId;
var packageVersion = PackageVersion;
if (!string.IsNullOrWhiteSpace(NuspecFile))
{
bool hasVersionInNuspecProperties = false;
if (NuspecProperties != null && NuspecProperties.Length > 0)
{
PackArgs packArgs = new PackArgs();
PackTaskLogic.SetPackArgsPropertiesFromNuspecProperties(packArgs, MSBuildStringUtility.TrimAndExcludeNullOrEmpty(NuspecProperties));
if (packArgs.Properties.ContainsKey("version"))
{
packageVersion = packArgs.Version;
hasVersionInNuspecProperties = true;
}
if (packArgs.Properties.TryGetValue("id", out var idTemp))
{
packageId = idTemp;
}
}
var nuspecReader = new NuGet.Packaging.NuspecReader(NuspecFile);
packageId = nuspecReader.GetId();
if (!hasVersionInNuspecProperties)
{
packageVersion = nuspecReader.GetVersion().ToNormalizedString();
}
}
if (!NuGetVersion.TryParse(packageVersion, out var versionTemp))
{
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
Strings.InvalidPackageVersion,
packageVersion));
}
NuGetVersion version = versionTemp!;
var symbolPackageFormat = PackArgs.GetSymbolPackageFormat(MSBuildStringUtility.TrimAndGetNullForEmpty(SymbolPackageFormat));
var nupkgFileName = PackCommandRunner.GetOutputFileName(packageId, version!, isNupkg: true, symbols: false, symbolPackageFormat: symbolPackageFormat, excludeVersion: OutputFileNamesWithoutVersion);
var nuspecFileName = PackCommandRunner.GetOutputFileName(packageId, version!, isNupkg: false, symbols: false, symbolPackageFormat: symbolPackageFormat, excludeVersion: OutputFileNamesWithoutVersion);
var outputs = new List<ITaskItem>();
outputs.Add(new TaskItem(Path.Combine(PackageOutputPath, nupkgFileName)));
outputs.Add(new TaskItem(Path.Combine(NuspecOutputPath, nuspecFileName)));
if (IncludeSource || IncludeSymbols)
{
var nupkgSymbolsFileName = PackCommandRunner.GetOutputFileName(packageId, version, isNupkg: true, symbols: true, symbolPackageFormat: symbolPackageFormat, excludeVersion: OutputFileNamesWithoutVersion);
var nuspecSymbolsFileName = PackCommandRunner.GetOutputFileName(packageId, version, isNupkg: false, symbols: true, symbolPackageFormat: symbolPackageFormat, excludeVersion: OutputFileNamesWithoutVersion);
outputs.Add(new TaskItem(Path.Combine(PackageOutputPath, nupkgSymbolsFileName)));
outputs.Add(new TaskItem(Path.Combine(NuspecOutputPath, nuspecSymbolsFileName)));
}
OutputPackItems = outputs.ToArray();
return true;
}
}
}