-
Notifications
You must be signed in to change notification settings - Fork 18
/
Directory.Build.targets
101 lines (83 loc) · 5.99 KB
/
Directory.Build.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
<Project>
<!-- Package to Project replacement: Based on https://github.com/dotnet/sdk/issues/1151#issuecomment-459275750 -->
<PropertyGroup>
<ReplacePackageReferences Condition="'$(ReplacePackageReferences)' == ''">true</ReplacePackageReferences>
<PackageReferenceFilter Condition="'$(PackageReferenceFilter)' == ''"></PackageReferenceFilter> <!-- Performance optimization: only look at certain package references -->
<ReplaceProjectReferences Condition="'$(ReplaceProjectReferences)' == ''">false</ReplaceProjectReferences>
</PropertyGroup>
<!-- MarkusH 2020/02/10: nuget restore does not set a proper solution path: fix it up -->
<PropertyGroup Condition=" ('$(SolutionPath)' == '' OR !Exists('$(SolutionPath)'))AND '$(SolutionDir)' != '' AND '$(SolutionDir)' != '*undefined*' ">
<SolutionPath>$(SolutionDir)$(SolutionName).SLN</SolutionPath>
</PropertyGroup>
<Choose>
<When Condition="'$(SolutionPath)' != '' AND '$(SolutionPath)' != '*undefined*' AND Exists('$(SolutionPath)') " >
<PropertyGroup>
<!-- <SolutionFileContent>$([System.IO.File]::ReadAllText($(SolutionPath)))</SolutionFileContent> --> <!-- Markush: 2020/02/10: This exhausts memory for large SLN files: read multiple times - less efficient but preserves memory-->
<SmartSolutionDir>$([System.IO.Path]::GetDirectoryName( $(SolutionPath) ))</SmartSolutionDir>
<RegexPattern>(?<="[PackageName]", ")(.*)(?=", ")</RegexPattern>
<HasSolution>true</HasSolution>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<HasSolution>false</HasSolution>
</PropertyGroup>
</Otherwise>
</Choose>
<Choose>
<When Condition="$(ReplacePackageReferences) AND $(HasSolution)">
<ItemGroup>
<!-- Keep the identity of the packagereference -->
<SmartPackageReference Include="@(PackageReference)">
<PackageName>%(Identity)</PackageName>
<Pattern>$(RegexPattern.Replace('[PackageName]','%(Identity)') )</Pattern>
<SmartPath></SmartPath>
<!--The RegEx gets the path to the project from the SLN file, using the package name as a look behind key
To use projects that are not named as the package (ie. Opc.Ua.Core.csproj) edit the SLN file to use the Package name after adding the project reference.
Example:
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Opc.Ua.Core", "..\..\UA-.NETStandard\Stack\Opc.Ua.Core\Opc.Ua.Core.csproj", "{7226CBF4-6AC8-44C4-A24B-D65C8EA52B97}"
change to:
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OPCFoundation.NetStandard.Opc.Ua.Core", "..\..\UA-.NETStandard\Stack\Opc.Ua.Core\Opc.Ua.Core.csproj", "{7226CBF4-6AC8-44C4-A24B-D65C8EA52B97}"-->
<SmartPath Condition = " '$(PackageReferenceFilter)' == '' OR '$(PackageReferenceFilter.Contains(%(Identity)))' == 'True' ">$([System.Text.RegularExpressions.Regex]::Match( '$([System.IO.File]::ReadAllText($(SolutionPath)))', '%(Pattern)' ))</SmartPath>
<InSolution>false</InSolution>
<InSolution Condition = " %(SmartPath) != '' ">True</InSolution>
</SmartPackageReference>
<!--Filter them by mapping them to another itemGroup using the WithMetadataValue item function-->
<PackageInSolution Include="@(SmartPackageReference -> WithMetadataValue('InSolution', True) )">
<!--<Pattern>$(RegexPattern.Replace('[PackageName]','%(PackageName)') )</Pattern>
<SmartPath>$([System.Text.RegularExpressions.Regex]::Match( '$([System.IO.File]::ReadAllText($(SolutionPath)))', '%(Pattern)' ))</SmartPath>-->
</PackageInSolution>
<!--Original approach-->
<!--<SmartPackageReference Include="@(PackageReference)">
<PackageName>%(Identity)</PackageName>
<InSolution>false</InSolution>
<InSolution Condition = " '$(PackageReferenceFilter)' == '' OR '$(PackageReferenceFilter.Contains(%(Identity)))' == 'True' ">$([System.IO.File]::ReadAllText($(SolutionPath)).Contains('\%(Identity).csproj'))</InSolution>
</SmartPackageReference>
<PackageInSolution Include="@(SmartPackageReference -> WithMetadataValue('InSolution', True) )">
<Pattern>$(RegexPattern.Replace('[PackageName]','%(PackageName)') )</Pattern>
<SmartPath>$([System.Text.RegularExpressions.Regex]::Match( '$([System.IO.File]::ReadAllText($(SolutionPath)))', '%(Pattern)' ))</SmartPath>
</PackageInSolution>-->
<ProjectReference Include="@(PackageInSolution -> '$(SmartSolutionDir)\%(SmartPath)' )"/>
<!-- Remove the package references that are now referenced as projects -->
<PackageReference Remove="@(PackageInSolution -> '%(PackageName)' )"/>
</ItemGroup>
</When>
<When Condition="$(ReplaceProjectReferences) AND '$(_RestoreSolutionFileUsed)' == ''">
<ItemGroup>
<!-- Keep the identity of the project reference (relative path), determine the project name and whether the project is contained in the current solution -->
<SmartProjectReference Include="@(ProjectReference)">
<OriginalIdentity>%(Identity)</OriginalIdentity>
<ProjectName>$([System.IO.Path]::GetFileNameWithoutExtension( $([System.IO.Path]::GetFullPath( '%(OriginalIdentity)' )) ))</ProjectName>
<InSolution>$([System.IO.File]::ReadAllText($(SolutionPath)).Contains('\%(ProjectName).csproj'))</InSolution>
</SmartProjectReference>
<!-- Filter them by mapping them to another itemGroup using the WithMetadataValue item function -->
<ProjectNotInSolution Include="@(SmartProjectReference -> WithMetadataValue('InSolution', False) )">
</ProjectNotInSolution>
<!--Reference the latest version of the package (why not * ? > see https://github.com/NuGet/Home/issues/7328-->
<PackageReference Include="@(ProjectNotInSolution -> '%(ProjectName)' )" Version="[*,9999.0]"/>
<!-- Remove the project references that are now referenced as packages -->
<ProjectReference Remove="@(ProjectNotInSolution -> '%(OriginalIdentity)' )"/>
</ItemGroup>
</When>
</Choose>
</Project>