This repository has been archived by the owner on Oct 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathTrimDeps.cs
60 lines (50 loc) · 1.65 KB
/
TrimDeps.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
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace RepoTasks
{
public class TrimDeps : Task
{
[Required]
public ITaskItem[] DepsFiles { get; set; }
public override bool Execute()
{
foreach (var depsFile in DepsFiles)
{
ChangeEntryPointLibraryName(depsFile.GetMetadata("Identity"));
}
// Parse input
return true;
}
private void ChangeEntryPointLibraryName(string depsFile)
{
JToken deps;
using (var file = File.OpenText(depsFile))
using (JsonTextReader reader = new JsonTextReader(file))
{
deps = JObject.ReadFrom(reader);
}
foreach (JProperty target in deps["targets"])
{
var targetLibrary = target.Value.Children<JProperty>().FirstOrDefault();
if (targetLibrary == null)
{
continue;
}
targetLibrary.Remove();
}
var library = deps["libraries"].Children<JProperty>().First();
library.Remove();
using (var file = File.CreateText(depsFile))
using (var writer = new JsonTextWriter(file) { Formatting = Formatting.Indented })
{
deps.WriteTo(writer);
}
}
}
}