@@ -9,19 +9,19 @@ namespace Common
9
9
{
10
10
public class ProjectFile
11
11
{
12
- private readonly string lineEndings ;
12
+ public readonly string LineEndings ;
13
13
14
14
public readonly string FilePath ;
15
15
public readonly XmlDocument Document ;
16
- private bool newFormat ;
16
+ private readonly bool newFormat ;
17
17
private ILog log ;
18
18
19
19
public ProjectFile ( string csprojFilePath )
20
20
{
21
21
var fileContent = File . ReadAllText ( csprojFilePath ) ;
22
22
log = LogManager . GetLogger ( typeof ( ProjectFile ) ) ;
23
23
24
- lineEndings = fileContent . Contains ( "\r \n " ) ? "\r \n " : "\n " ;
24
+ LineEndings = fileContent . Contains ( "\r \n " ) ? "\r \n " : "\n " ;
25
25
FilePath = csprojFilePath ;
26
26
Document = XmlDocumentHelper . Create ( fileContent ) ;
27
27
newFormat = ! string . IsNullOrEmpty ( Document . DocumentElement ? . GetAttribute ( "Sdk" ) ) ;
@@ -100,6 +100,13 @@ public XmlNode CreateReference(string refName, string refPath)
100
100
101
101
return elementToInsert ;
102
102
}
103
+ public XmlNode CreateNuGetReference ( string refName , string version )
104
+ {
105
+ var elementToInsert = Document . CreateElement ( "PackageReference" , Document . DocumentElement . NamespaceURI ) ;
106
+ elementToInsert . SetAttribute ( "Include" , refName ) ;
107
+ elementToInsert . SetAttribute ( "Version" , version ) ;
108
+ return elementToInsert ;
109
+ }
103
110
104
111
public void AddRef ( string refName , string refPath )
105
112
{
@@ -200,7 +207,7 @@ public XmlDocument CreateCsProjWithNugetReferences(List<Dep> deps, string module
200
207
201
208
public void Save ( )
202
209
{
203
- XmlDocumentHelper . Save ( Document , FilePath , lineEndings ) ;
210
+ XmlDocumentHelper . Save ( Document , FilePath , LineEndings ) ;
204
211
}
205
212
206
213
private XmlNode CreateAnalyzerGroup ( )
@@ -221,6 +228,12 @@ private bool IsReferenceGroup(XmlNode xmlNode)
221
228
. Cast < XmlNode > ( )
222
229
. Any ( childNode => childNode . Name == "Reference" ) ;
223
230
}
231
+ private bool IsPackageReferenceGroup ( XmlNode xmlNode )
232
+ {
233
+ return xmlNode . ChildNodes
234
+ . Cast < XmlNode > ( )
235
+ . Any ( childNode => childNode . Name == "PackageReference" ) ;
236
+ }
224
237
225
238
private XmlNode CreateItemGroup ( )
226
239
{
@@ -239,5 +252,55 @@ private XmlNode CreateItemGroup()
239
252
240
253
return itemGroup ;
241
254
}
255
+
256
+ public void InstallNuGetPackages ( List < string > nuGetPackages )
257
+ {
258
+ if ( newFormat )
259
+ {
260
+ foreach ( var package in nuGetPackages )
261
+ {
262
+ var splitted = package . Split ( '/' ) ;
263
+ if ( splitted . Length != 2 )
264
+ {
265
+ log . Error ( "package version is not defined: " + package ) ;
266
+ }
267
+ else
268
+ {
269
+ InstallNuGetPackage ( splitted [ 0 ] , splitted [ 1 ] ) ;
270
+ }
271
+ }
272
+ }
273
+ else
274
+ {
275
+ var currentModuleDirectory = Helper . GetModuleDirectory ( Directory . GetCurrentDirectory ( ) ) ;
276
+ var packagesDirectory = Path . Combine ( currentModuleDirectory , "packages" ) ;
277
+ new NuGetPackageHepler ( log ) . InstallPackages ( nuGetPackages , packagesDirectory , this ) ;
278
+ }
279
+ }
280
+
281
+ private void InstallNuGetPackage ( string packageName , string packageVersion )
282
+ {
283
+ try
284
+ {
285
+ var referenceGroup = Document
286
+ . GetElementsByTagName ( "ItemGroup" )
287
+ . Cast < XmlNode > ( )
288
+ . FirstOrDefault ( IsPackageReferenceGroup ) ?? CreateItemGroup ( ) ;
289
+ var packageNode = Document . SelectNodes ( "*/ItemGroup/PackageReference" ) ? . Cast < XmlElement > ( )
290
+ . FirstOrDefault ( el => el . Attributes [ "Include" , Document . DocumentElement . NamespaceURI ] . Value . Equals ( packageName , StringComparison . InvariantCultureIgnoreCase ) ) ;
291
+ if ( packageNode == null )
292
+ referenceGroup ? . AppendChild ( CreateNuGetReference ( packageName , packageVersion ) ) ;
293
+ else
294
+ {
295
+ packageNode . ParentNode ? . RemoveChild ( packageNode ) ;
296
+ packageNode . SetAttribute ( "Version" , packageVersion ) ;
297
+ referenceGroup ? . AppendChild ( packageNode ) ;
298
+ }
299
+ }
300
+ catch ( Exception e )
301
+ {
302
+ throw new Exception ( $ "Failed to add ref { packageName } to { FilePath } ", e ) ;
303
+ }
304
+ }
242
305
}
243
306
}
0 commit comments