77using System ;
88using System . Collections . Generic ;
99using System . Diagnostics . CodeAnalysis ;
10+ using System . Linq ;
11+ using System . Xml . Linq ;
1012
1113using Microsoft . Build . Evaluation ;
1214using Microsoft . Build . Utilities . ProjectCreation ;
@@ -15,6 +17,17 @@ namespace Ubiquity.Versioning.Build.Tasks.UT
1517{
1618 internal static class ProjectCreatorLibraryExtensions
1719 {
20+ public static PackageRepository SourceMapping ( this PackageRepository pkgRepo , string pkgSourceKey , string pattern )
21+ {
22+ var nugetConfig = XDocument . Load ( pkgRepo . NuGetConfigPath ) ;
23+ XElement configuration = GetOrCreateConfigurationElement ( nugetConfig ) ;
24+ XElement sourceMapping = GetOrCreateSourceMappingElement ( configuration ) ;
25+ XElement pkgSrcElement = GetOrCreatePackageSource ( sourceMapping , pkgSourceKey ) ;
26+ _ = GetOrCreatePackageElement ( pkgSrcElement , pattern ) ;
27+ nugetConfig . Save ( pkgRepo . NuGetConfigPath ) ;
28+ return pkgRepo ;
29+ }
30+
1831 [ SuppressMessage ( "Style" , "IDE0060:Remove unused parameter" , Justification = "Syntactical sugar" ) ]
1932 [ SuppressMessage ( "Style" , "IDE0046:Convert to conditional expression" , Justification = "Result is Less than 'simplified'" ) ]
2033 public static ProjectCreator VersioningProject (
@@ -30,9 +43,9 @@ public static ProjectCreator VersioningProject(
3043 string ? treatAsLocalProperty = null ,
3144 ProjectCollection ? projectCollection = null ,
3245 IDictionary < string , string > ? globalProperties = null ,
33- NewProjectFileOptions ? projectFileOptions = NewProjectFileOptions . None )
46+ NewProjectFileOptions ? projectFileOptions = NewProjectFileOptions . None )
3447 {
35- ArgumentException . ThrowIfNullOrWhiteSpace ( targetFramework ) ;
48+ ArgumentException . ThrowIfNullOrWhiteSpace ( targetFramework ) ;
3649
3750 return templates . SdkCsproj (
3851 path ,
@@ -46,13 +59,71 @@ public static ProjectCreator VersioningProject(
4659 projectCollection ,
4760 projectFileOptions ,
4861 globalProperties
49- ) . ItemPackageReference ( PackageUnderTestId , version : packageVersion , privateAssets : "All" )
50- . Property ( "Nullable" , "disable" )
51- . Property ( "ManagePackageVersionsCentrally" , "false" )
52- . Property ( "ImplicitUsings" , "disable" )
62+ ) . ItemPackageReference ( PackageUnderTestId , version : packageVersion , privateAssets : "All" )
63+ . Property ( "Nullable" , "disable" )
64+ . Property ( "ManagePackageVersionsCentrally" , "false" )
65+ . Property ( "ImplicitUsings" , "disable" )
5366 ;
5467 }
5568
69+ private static XElement GetOrCreateSourceMappingElement ( XElement configuration )
70+ {
71+ XElement ? sourceMapping = configuration . Element ( "packageSourceMapping" ) ;
72+ if ( sourceMapping is null )
73+ {
74+ sourceMapping = new XElement ( "packageSourceMapping" ) ;
75+ configuration . Add ( sourceMapping ) ;
76+ }
77+
78+ return sourceMapping ;
79+ }
80+
81+ private static XElement GetOrCreateConfigurationElement ( XDocument nugetConfig )
82+ {
83+ XElement ? configuration = nugetConfig . Element ( "configuration" ) ;
84+ if ( configuration is null )
85+ {
86+ configuration = new XElement ( "configuration" ) ;
87+ nugetConfig . Add ( configuration ) ;
88+ }
89+
90+ return configuration ;
91+ }
92+
93+ private static XElement GetOrCreatePackageSource ( XElement sourceMapping , string pkgSource )
94+ {
95+ XElement ? packageSource = ( from e in sourceMapping . Elements ( "packageSource" )
96+ from a in e . Attributes ( )
97+ where a . Name == "key" && a . Value == pkgSource
98+ select e
99+ ) . FirstOrDefault ( ) ;
100+
101+ if ( packageSource is null )
102+ {
103+ packageSource = new XElement ( "packageSource" , new XAttribute ( "key" , pkgSource ) ) ;
104+ sourceMapping . Add ( packageSource ) ;
105+ }
106+
107+ return packageSource ;
108+ }
109+
110+ private static XElement GetOrCreatePackageElement ( XElement pkgSrc , string pattern )
111+ {
112+ XElement ? package = ( from e in pkgSrc . Elements ( "package" )
113+ from a in e . Attributes ( )
114+ where a . Name == "pattern" && a . Value == pattern
115+ select e
116+ ) . FirstOrDefault ( ) ;
117+
118+ if ( package is null )
119+ {
120+ package = new XElement ( "package" , new XAttribute ( "pattern" , pattern ) ) ;
121+ pkgSrc . Add ( package ) ;
122+ }
123+
124+ return package ;
125+ }
126+
56127 private const string PackageUnderTestId = @"Ubiquity.NET.Versioning.Build.Tasks" ;
57128 }
58129}
0 commit comments