-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonPackageReferenceTask.cs
35 lines (31 loc) · 1 KB
/
JsonPackageReferenceTask.cs
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace CustomPackageReferences
{
public class JsonPackageReferenceTask : CustomPackageReferenceTask
{
public override IEnumerable<PackageReference> GetPackages(string projectPath, string targetFramework)
{
var jsonPath = Path.Combine(projectPath, "project.json");
if (!File.Exists(jsonPath)) {
File.WriteAllText(jsonPath,
@"{
""dependencies"": {
}
}");
}
var json = JToken.Parse(File.ReadAllText(jsonPath));
var deps = json["dependencies"];
return deps.Children().OfType<JProperty>().Select(c => new PackageReference
{
Name = c.Name,
Version = c.First.Type == JTokenType.Object ? c.Value.Value<string>("version") : (string)((JValue)c.Value).Value
});
}
}
}