33
44using System ;
55using System . IO ;
6- using Microsoft . Build . Execution ;
7-
8- namespace Microsoft . Build . Prediction . Predictors
9- {
10- /// <summary>
11- /// Predicts inputs and outputs when using the built-in feature of the Sdk to create a NuGet package during the build.
12- /// </summary>
13- public sealed class GeneratePackageOnBuildPredictor : IProjectPredictor
14- {
15- internal const string GeneratePackageOnBuildPropertyName = "GeneratePackageOnBuild" ;
16-
17- internal const string PackageIdPropertyName = "PackageId" ;
18-
19- internal const string PackageVersionPropertyName = "PackageVersion" ;
20-
21- internal const string PackageOutputPathPropertyName = "PackageOutputPath" ;
22-
6+ using Microsoft . Build . Execution ;
7+
8+ namespace Microsoft . Build . Prediction . Predictors
9+ {
10+ /// <summary>
11+ /// Predicts inputs and outputs when using the built-in feature of the Sdk to create a NuGet package during the build.
12+ /// </summary>
13+ public sealed class GeneratePackageOnBuildPredictor : IProjectPredictor
14+ {
15+ internal const string GeneratePackageOnBuildPropertyName = "GeneratePackageOnBuild" ;
16+
17+ internal const string IsPackablePropertyName = "IsPackable" ;
18+
19+ internal const string PackageIdPropertyName = "PackageId" ;
20+
21+ internal const string PackageVersionPropertyName = "PackageVersion" ;
22+
23+ internal const string PackageOutputPathPropertyName = "PackageOutputPath" ;
24+
2325 internal const string OutputPathPropertyName = "OutputPath" ;
2426
25- internal const string OutputFileNamesWithoutVersionPropertyName = "OutputFileNamesWithoutVersion" ;
26-
27- internal const string NuspecOutputPathPropertyName = "NuspecOutputPath" ;
28-
29- internal const string IncludeSourcePropertyName = "IncludeSource" ;
30-
31- internal const string IncludeSymbolsPropertyName = "IncludeSymbols" ;
32-
33- internal const string SymbolPackageFormatPropertyName = "SymbolPackageFormat" ;
34-
35- internal const string NuspecFilePropertyName = "NuspecFile" ;
36-
37- /// <inheritdoc/>
38- public void PredictInputsAndOutputs (
39- ProjectInstance projectInstance ,
40- ProjectPredictionReporter predictionReporter )
41- {
42- // This is based on NuGet.Build.Tasks.Pack.targets and GetPackOutputItemsTask
43- // See: https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Build.Tasks.Pack/NuGet.Build.Tasks.Pack.targets
44- // See: https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Build.Tasks.Pack/GetPackOutputItemsTask.cs
45- var generatePackageOnBuild = projectInstance . GetPropertyValue ( GeneratePackageOnBuildPropertyName ) ;
46- if ( ! generatePackageOnBuild . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) )
47- {
48- return ;
49- }
50-
51- var packageId = projectInstance . GetPropertyValue ( PackageIdPropertyName ) ;
52- var packageVersion = projectInstance . GetPropertyValue ( PackageVersionPropertyName ) ;
53- var packageOutputPath = projectInstance . GetPropertyValue ( PackageOutputPathPropertyName ) ;
54- var nuspecOutputPath = projectInstance . GetPropertyValue ( NuspecOutputPathPropertyName ) ;
55- var includeSource = projectInstance . GetPropertyValue ( IncludeSourcePropertyName ) . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) ;
27+ internal const string OutputFileNamesWithoutVersionPropertyName = "OutputFileNamesWithoutVersion" ;
28+
29+ internal const string NuspecOutputPathPropertyName = "NuspecOutputPath" ;
30+
31+ internal const string IncludeSourcePropertyName = "IncludeSource" ;
32+
33+ internal const string IncludeSymbolsPropertyName = "IncludeSymbols" ;
34+
35+ internal const string SymbolPackageFormatPropertyName = "SymbolPackageFormat" ;
36+
37+ internal const string NuspecFilePropertyName = "NuspecFile" ;
38+
39+ /// <inheritdoc/>
40+ public void PredictInputsAndOutputs (
41+ ProjectInstance projectInstance ,
42+ ProjectPredictionReporter predictionReporter )
43+ {
44+ // This is based on NuGet.Build.Tasks.Pack.targets and GetPackOutputItemsTask
45+ // See: https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Build.Tasks.Pack/NuGet.Build.Tasks.Pack.targets
46+ // See: https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Build.Tasks.Pack/GetPackOutputItemsTask.cs
47+ var generatePackageOnBuild = projectInstance . GetPropertyValue ( GeneratePackageOnBuildPropertyName ) ;
48+ if ( ! generatePackageOnBuild . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) )
49+ {
50+ return ;
51+ }
52+
53+ var isPackable = projectInstance . GetPropertyValue ( IsPackablePropertyName ) ;
54+ if ( ! isPackable . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) )
55+ {
56+ return ;
57+ }
58+
59+ var packageId = projectInstance . GetPropertyValue ( PackageIdPropertyName ) ;
60+ var packageVersion = projectInstance . GetPropertyValue ( PackageVersionPropertyName ) ;
61+ var packageOutputPath = projectInstance . GetPropertyValue ( PackageOutputPathPropertyName ) ;
62+ var nuspecOutputPath = projectInstance . GetPropertyValue ( NuspecOutputPathPropertyName ) ;
63+ var includeSource = projectInstance . GetPropertyValue ( IncludeSourcePropertyName ) . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) ;
5664 var includeSymbols = projectInstance . GetPropertyValue ( IncludeSymbolsPropertyName ) . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) ;
57- var outputFileNamesWithoutVersion = projectInstance . GetPropertyValue ( OutputFileNamesWithoutVersionPropertyName ) . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) ;
58-
59- var symbolPackageFormat = projectInstance . GetPropertyValue ( SymbolPackageFormatPropertyName ) ;
60-
61- // PackageOutputPath defaults to OutputPath in the _CalculateInputsOutputsForPack target, not statically.
62- if ( string . IsNullOrEmpty ( packageOutputPath ) )
63- {
64- packageOutputPath = projectInstance . GetPropertyValue ( OutputPathPropertyName ) ;
65- }
66-
67- // All params are effectively required
68- if ( ! string . IsNullOrEmpty ( packageId )
69- && ! string . IsNullOrEmpty ( packageVersion )
70- && ! string . IsNullOrEmpty ( packageOutputPath )
71- && ! string . IsNullOrEmpty ( nuspecOutputPath )
72- && ! string . IsNullOrEmpty ( symbolPackageFormat ) )
73- {
74- var fileBaseName = outputFileNamesWithoutVersion ? packageId : $ "{ packageId } .{ packageVersion } ";
75-
76- // Nuspec files can also be provided instead of generated, in which case we should treat it like an input, not an output.
77- var nuspecFile = projectInstance . GetPropertyValue ( NuspecFilePropertyName ) ;
78-
79- predictionReporter . ReportOutputFile ( Path . Combine ( packageOutputPath , fileBaseName + ".nupkg" ) ) ;
80- if ( string . IsNullOrEmpty ( nuspecFile ) )
81- {
82- predictionReporter . ReportOutputFile ( Path . Combine ( nuspecOutputPath , fileBaseName + ".nuspec" ) ) ;
83- }
84- else
85- {
86- predictionReporter . ReportInputFile ( nuspecFile ) ;
87- }
88-
89- if ( includeSource || includeSymbols )
90- {
91- predictionReporter . ReportOutputFile ( Path . Combine ( packageOutputPath , fileBaseName + ( symbolPackageFormat . Equals ( "snupkg" , StringComparison . OrdinalIgnoreCase ) ? ".snupkg" : ".symbols.nupkg" ) ) ) ;
92- if ( string . IsNullOrEmpty ( nuspecFile ) )
93- {
94- predictionReporter . ReportOutputFile ( Path . Combine ( nuspecOutputPath , fileBaseName + ".symbols.nuspec" ) ) ;
95- }
96- }
97- }
98- }
99- }
65+ var outputFileNamesWithoutVersion = projectInstance . GetPropertyValue ( OutputFileNamesWithoutVersionPropertyName ) . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) ;
66+
67+ var symbolPackageFormat = projectInstance . GetPropertyValue ( SymbolPackageFormatPropertyName ) ;
68+
69+ // PackageOutputPath defaults to OutputPath in the _CalculateInputsOutputsForPack target, not statically.
70+ if ( string . IsNullOrEmpty ( packageOutputPath ) )
71+ {
72+ packageOutputPath = projectInstance . GetPropertyValue ( OutputPathPropertyName ) ;
73+ }
74+
75+ // All params are effectively required
76+ if ( ! string . IsNullOrEmpty ( packageId )
77+ && ! string . IsNullOrEmpty ( packageVersion )
78+ && ! string . IsNullOrEmpty ( packageOutputPath )
79+ && ! string . IsNullOrEmpty ( nuspecOutputPath )
80+ && ! string . IsNullOrEmpty ( symbolPackageFormat ) )
81+ {
82+ var fileBaseName = outputFileNamesWithoutVersion ? packageId : $ "{ packageId } .{ packageVersion } ";
83+
84+ // Nuspec files can also be provided instead of generated, in which case we should treat it like an input, not an output.
85+ var nuspecFile = projectInstance . GetPropertyValue ( NuspecFilePropertyName ) ;
86+
87+ predictionReporter . ReportOutputFile ( Path . Combine ( packageOutputPath , fileBaseName + ".nupkg" ) ) ;
88+ if ( string . IsNullOrEmpty ( nuspecFile ) )
89+ {
90+ predictionReporter . ReportOutputFile ( Path . Combine ( nuspecOutputPath , fileBaseName + ".nuspec" ) ) ;
91+ }
92+ else
93+ {
94+ predictionReporter . ReportInputFile ( nuspecFile ) ;
95+ }
96+
97+ if ( includeSource || includeSymbols )
98+ {
99+ predictionReporter . ReportOutputFile ( Path . Combine ( packageOutputPath , fileBaseName + ( symbolPackageFormat . Equals ( "snupkg" , StringComparison . OrdinalIgnoreCase ) ? ".snupkg" : ".symbols.nupkg" ) ) ) ;
100+ if ( string . IsNullOrEmpty ( nuspecFile ) )
101+ {
102+ predictionReporter . ReportOutputFile ( Path . Combine ( nuspecOutputPath , fileBaseName + ".symbols.nuspec" ) ) ;
103+ }
104+ }
105+ }
106+ }
107+ }
100108}
0 commit comments