-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
NuGetizer.targets
238 lines (209 loc) · 13.8 KB
/
NuGetizer.targets
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!--
***********************************************************************************************
NuGetizer.targets
WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
created a backup copy. Incorrect changes to this file will make it
impossible to load or build your projects from the command-line or the IDE.
Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->
<Project InitialTargets="_SetPropertiesFromCapabilities" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="NuGetizer.Tasks.AssignPackagePath" AssemblyFile="NuGetizer.Tasks.dll" />
<UsingTask TaskName="NuGetizer.Tasks.WriteItemsToFile" AssemblyFile="NuGetizer.Tasks.dll" />
<Import Project="NuGetizer.props" Condition="'$(NuGetizerPropsImported)' == ''" />
<!-- Adds compatibility targets with Pack SDK -->
<Import Project="NuGetizer.Compatibility.props" />
<Import Project="NuGetizer.Shared.targets" />
<!-- We don't need to do the inference in the crosstargeting scenario, we'd infer in the inner build instead -->
<Import Project="NuGetizer.Inference.targets" Condition="'$(EnablePackInference)' != 'false' and '$(IsCrossTargetingBuild)' != 'true'" />
<!--
=================================================================================================
GetPackageContents
=================================================================================================
Returns the "virtual package" for the current project, containing
all items that would end up in a .nupkg if the project were
to be packed.
If the project has NuGet metadata to generate a .nupkg, a metadata
item containing the manifest information will also be returned, as
Identity=$(PackageId)
%(PackFolder)=Metadata
... all manifest values as metadata items ...
All items returned from this target contain a %(PackageId) metadata
with the project's $(PackageId), if any.
OUTPUTS:
@(_PackageContent) The contents that this project contributes to a package
=================================================================================================
-->
<PropertyGroup>
<GetPackageContentsDependsOn>
$(GetPackageContentsDependsOn);
GetPackageTargetPath;
_AddPackageManifest
</GetPackageContentsDependsOn>
<GetPackageContentsDependsOn Condition="'$(PackProjectReference)' == 'true'">
$(GetPackageContentsDependsOn);
_GetReferencedPackageContents
</GetPackageContentsDependsOn>
</PropertyGroup>
<Target Name="GetPackageContents" DependsOnTargets="$(GetPackageContentsDependsOn)" Returns="@(_PackageContent)">
<ItemGroup>
<!--
PackageId metadata on all PackageFile items means we can tell appart which ones came from which dependencies
NOTE: if PackageId is empty, we won't generate a manifest and it means the files need to be packed with the
current project.
-->
<PackageFile>
<PackageId Condition="'$(IsPackable)' == 'true'">$(PackageId)</PackageId>
<Platform Condition="'%(PackageFile.Platform)' == ''">$(Platform)</Platform>
<TargetFrameworkMoniker Condition="'%(PackageFile.TargetFrameworkMoniker)' == '' and '$(IsPackagingProject)' != 'true'">$(TargetFrameworkMoniker)</TargetFrameworkMoniker>
</PackageFile>
</ItemGroup>
<!-- We batch depending on the IsPackaging metadata, which is only specified for referenced content
if the current project is building a package, to force retargeting of the referenced content. -->
<AssignPackagePath Files="@(PackageFile)" KnownFolders="@(PackFolderKind)" IsPackaging="%(PackageFile.IsPackaging)">
<Output TaskParameter="AssignedFiles" ItemName="_PackageContent" />
</AssignPackagePath>
</Target>
<Target Name="_AddPackageManifest" Condition="'$(IsPackable)' == 'true'">
<!-- If packaging the project, provide the metadata as a non-file item -->
<ItemGroup>
<PackageFile Include="@(PackageMetadata)">
<PackFolder>Metadata</PackFolder>
<PackageId>$(PackageId)</PackageId>
<Platform>$(Platform)</Platform>
<TargetFrameworkMoniker Condition="'$(IsPackagingProject)' != 'true'">$(TargetFrameworkMoniker)</TargetFrameworkMoniker>
</PackageFile>
</ItemGroup>
</Target>
<Target Name="_GetReferencedPackageContents"
Condition="'$(_SupportsProjectReferences)' == 'true'"
DependsOnTargets="$(_GetReferencedPackageContentsDependsOn)"
Returns="@(PackageFile)">
<Error Condition="'@(_NonNuGetizedProjectReference)' != ''"
Code="NG0011"
Text="Some project references cannot be properly packaged. Please install the NuGetizer package on the following projects: @(_NonNuGetizedProjectReference)." />
<!--
PackageId metadata on all PackageFile items means we can tell appart which ones came from which dependencies
NOTE: if PackageId is empty, we won't generate a manifest and it means the files need to be packed with the
current project.
-->
<!-- Workaround for Xamarin.Android 9.4 requires DesignTimeBuild to be set for its FilterAssemblies target -->
<MSBuild Projects="@(_NuGetizedProjectReference)"
Targets="GetPackageContents"
BuildInParallel="$(BuildInParallel)"
Properties="%(_NuGetizedProjectReference.SetConfiguration);
%(_NuGetizedProjectReference.SetPlatform);
BuildingPackage=$(BuildingPackage);
DesignTimeBuild=false"
Condition="'@(ProjectReferenceWithConfiguration)' != '' and '@(_NuGetizedProjectReference)' != ''"
RemoveProperties="%(_NuGetizedProjectReference.GlobalPropertiesToRemove)">
<Output TaskParameter="TargetOutputs" ItemName="_ReferencedPackageContent" />
</MSBuild>
<ItemGroup>
<_ReferencedPackageDependency Include="@(_ReferencedPackageContent)"
Condition="'%(_ReferencedPackageContent.PackageId)' != '$(PackageId)' and
'%(_ReferencedPackageContent.PackFolder)' == 'Metadata'">
<!-- For consistency, annotate like the rest -->
<PackageId Condition="'$(IsPackable)' == 'true'">$(PackageId)</PackageId>
<TargetFrameworkMoniker Condition="'$(IsPackagingProject)' != 'true'">$(TargetFrameworkMoniker)</TargetFrameworkMoniker>
<PackFolder>Dependency</PackFolder>
</_ReferencedPackageDependency>
<!-- Remove the referenced package actual contents if it provides a manifest, since it will be a dependency in that case. -->
<_PackageContentFromDependency Include="@(_ReferencedPackageContent)"
Condition="'%(_ReferencedPackageContent.PackageId)' != '' and
'%(_ReferencedPackageContent.PackageId)' != '$(PackageId)'" />
<_ReferencedPackageContent Remove="@(_PackageContentFromDependency)" />
</ItemGroup>
<!-- Always annotate package contents with the original target framework and moniker -->
<CreateItem Include="@(_ReferencedPackageContent)" PreserveExistingMetadata="true"
Condition="'@(_ReferencedPackageContent)' != ''"
AdditionalMetadata="OriginalTargetFramework=%(_ReferencedPackageContent.TargetFramework);
OriginalTargetFrameworkMoniker=%(_ReferencedPackageContent.TargetFrameworkMoniker)">
<Output TaskParameter="Include" ItemName="_ReferencedPackageContentWithOriginalValues"/>
</CreateItem>
<ItemGroup Condition="'$(IsPackagingProject)' != 'true'">
<!-- Retarget content for the currently building package, if necessary -->
<_ReferencedPackageContentWithOriginalValues Condition="'%(_ReferencedPackageContentWithOriginalValues.PackageId)' == ''">
<PackageId Condition="'$(IsPackable)' == 'true'">$(PackageId)</PackageId>
<!-- Clear the target framework since it trumps the TFM in AsignPackagePath now -->
<!-- Only do this for assets that come from project references that don't build nugets (PackageId=='' above) -->
<TargetFramework></TargetFramework>
<!-- NOTE: we're always overwriting the TFM for framework-specific items in this case
since this item will end up making up the contents of this package project in its
current TFM configuration.
TBD: we might want to preserve it anyways and adjust later (i.e. net45 project
references netstandard1.6 project)
TODO: take into account cross-targeting.
-->
<TargetFrameworkMoniker Condition="'%(_ReferencedPackageContentWithOriginalValues.FrameworkSpecific)' == 'true'">$(TargetFrameworkMoniker)</TargetFrameworkMoniker>
</_ReferencedPackageContentWithOriginalValues>
</ItemGroup>
<!-- Otherwise, assign target paths based on the original TFM -->
<ItemGroup Condition="'$(IsPackagingProject)' == 'true'">
<!-- Retarget content for the currently building package, if necessary -->
<_ReferencedPackageContentWithOriginalValues Condition="'%(_ReferencedPackageContentWithOriginalValues.PackageId)' == ''">
<PackageId Condition="'$(IsPackable)' == 'true'">$(PackageId)</PackageId>
<!-- Clear the target framework since it trumps the TFM in AsignPackagePath now -->
<!-- Only do this for framework-specifc assets that come from project references that don't build nugets (PackageId=='' above) -->
<TargetFramework Condition="'%(_ReferencedPackageContentWithOriginalValues.FrameworkSpecific)' == 'true'"></TargetFramework>
<TargetFrameworkMoniker>%(_ReferencedPackageContentWithOriginalValues.OriginalTargetFrameworkMoniker)</TargetFrameworkMoniker>
</_ReferencedPackageContentWithOriginalValues>
</ItemGroup>
<ItemGroup>
<!-- Ensure referenced package content gets assigned a package path if it didn't provide one already.
This happens for project references' that don't have a PackageId, since their package path will
depend on the referencing project's TFM.
-->
<PackageFile Include="@(_ReferencedPackageContentWithOriginalValues);@(_ReferencedPackageDependency)">
<IsPackaging>$(BuildingPackage)</IsPackaging>
</PackageFile>
</ItemGroup>
</Target>
<!-- This target separates project references that have the packaging targets from those that don't -->
<Target Name="_SplitProjectReferencesByIsNuGetized"
Condition="'@(ProjectReferenceWithConfiguration)' != '' and '@(_MSBuildProjectReferenceExistent)' != ''"
Inputs="@(_MSBuildProjectReferenceExistent)"
Outputs="%(_MSBuildProjectReferenceExistent.Identity)-BATCH">
<MSBuild Projects="@(_MSBuildProjectReferenceExistent)"
Targets="GetTargetPathWithTargetPlatformMoniker"
BuildInParallel="$(BuildInParallel)"
Properties="%(_MSBuildProjectReferenceExistent.SetConfiguration); %(_MSBuildProjectReferenceExistent.SetPlatform)"
RemoveProperties="%(_MSBuildProjectReferenceExistent.GlobalPropertiesToRemove)">
<Output TaskParameter="TargetOutputs" ItemName="_ReferencedProjectTargetPath" />
</MSBuild>
<PropertyGroup>
<_ShouldPack>%(_MSBuildProjectReferenceExistent.Pack)</_ShouldPack>
<_IsNuGetized>%(_ReferencedProjectTargetPath.IsNuGetized)</_IsNuGetized>
<_PrivateAssets>%(_MSBuildProjectReferenceExistent.PrivateAssets)</_PrivateAssets>
</PropertyGroup>
<!-- Only process references that aren't excluded from Pack -->
<ItemGroup Condition="'$(_ShouldPack)' != 'false'">
<!-- We will fail for this first group: project references that aren't excluded from packaging, yet haven't been nugetized -->
<_NonNuGetizedProjectReference Include="@(_MSBuildProjectReferenceExistent)" Condition="'$(_IsNuGetized)' != 'true'" />
<!-- We will only process for packaging the project references that haven't been excluded from packaging and are nugetized -->
<_NuGetizedProjectReference Include="@(_MSBuildProjectReferenceExistent)" Condition="'$(_IsNuGetized)' == 'true'">
<!-- By flagging PrivateAssets=all nugetized references as IsPackable=false, we cause its assets to be
packed together with the build output, instead of referenced as a dependency.
This works because default value for IsPackable checks for '' before defaulting
to whether there is a PackageId property.
Also, passing our own PackFolder causes it to be the overriden default for the referenced project. -->
<AdditionalProperties Condition="'$(_PrivateAssets)' == 'all'">IsPackable=false;PackFolder=$(PackFolder)</AdditionalProperties>
</_NuGetizedProjectReference>
</ItemGroup>
</Target>
<Target Name="_SetPropertiesFromCapabilities">
<PropertyGroup>
<_AllProjectCapabilities>@(ProjectCapability)</_AllProjectCapabilities>
<!-- NOTE: this avoids taking dependencies on targets that are only available when the project supports the concept of project references -->
<_SupportsProjectReferences>$(_AllProjectCapabilities.Contains('ProjectReferences'))</_SupportsProjectReferences>
<_GetReferencedPackageContentsDependsOn Condition="'$(_SupportsProjectReferences)' == 'true'">
$(_GetReferencedPackageContentsDependsOn);
AssignProjectConfiguration;
_SplitProjectReferencesByFileExistence;
_SplitProjectReferencesByIsNuGetized
</_GetReferencedPackageContentsDependsOn>
</PropertyGroup>
</Target>
<Import Project="NuGetizer.Authoring.targets" Condition="'$(IsPackagingProject)' == 'true'" />
<Import Project="NuGetizer.MultiTargeting.targets" Condition="'$(IsCrossTargetingBuild)' == 'true' And '$(FromPackagingProject)' == 'true'" />
</Project>