Datei: NDOEnhancer/NDOEnhancer/ProjectAssets.cs

Last Commit (70a7a80)
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Text;
6 using System.Text.RegularExpressions;
7 using System.Threading.Tasks;
8 using TinyJSON;
9
10 namespace NDOEnhancer
11 {
12 ····class ProjectAssets
13 ····{
14 ········private readonly ProjectDescription projectDescription;
15 ········public ProjectAssets( ProjectDescription projectDescription )
16 ········{
17 ············this.projectDescription = projectDescription;
18 ········}
19
20 ········JSON.Object LoadAssets()
21 ········{
22 ············var json = new JSON();
23 ············var objPath = Path.Combine(this.projectDescription.ProjPath, "obj");
24 ············var assetsPath = Path.Combine( objPath, "project.assets.json" );
25 ············if (!File.Exists( assetsPath ))
26 ············{
27 ················Console.WriteLine( $"Can't find assets file: {assetsPath}" );
28 ················return null;
29 ············}
30 ············using (TextReader tr = new StreamReader( assetsPath ))
31 ················return json.Parse( tr ).Value.AsObject();
32 ········}
33
34 ········/// <summary>
35 ········/// Computes the path of a compile asset relative to the NuGet package folder.
36 ········/// </summary>
37 ········/// <remarks>
38 ········/// The keys in the targets section (e.g. "NDO.Mapping/6.0.0") contain the original casing
39 ········/// of the package id, but NuGet stores the packages in lower case folders
40 ········/// (e.g. "ndo.mapping/6.0.0"). On case sensitive file systems (Linux) we have to use
41 ········/// the folder name provided in the libraries section.
42 ········/// </remarks>
43 ········static string GetRelativeCompilePath( JSON.Object root, string packageKey, string compile )
44 ········{
45 ············string packagePath = null;
46 ············JSON.Object libraries = null;
47 ············JSON.Object library = null;
48 ············if (root.Get( "libraries", ref libraries ) && libraries.Get( packageKey, ref library ))
49 ················library.Get( "path", ref packagePath );
50 ············if (string.IsNullOrEmpty( packagePath ))
51 ················packagePath = packageKey.ToLowerInvariant();
52
53 ············var relPath = packagePath.TrimEnd( '/' ) + "/" + compile;
54 ············if (Path.DirectorySeparatorChar != '/')
55 ················relPath = relPath.Replace( '/', Path.DirectorySeparatorChar );
56 ············return relPath;
57 ········}
58
59 ········public string GetPackageDir( string packageName )
60 ········{
61 ············var root = LoadAssets();
62 ············if (root == null)
63 ················return null;
64
65 ············var targets = root["targets"].AsObject();
66 ············var standardKey = targets.FirstOrDefault(o => o.StartsWith( ".NETStandard,Version=" ) || o.StartsWith( ".NETCoreApp,Version=" ));
67 ············if (standardKey == null)
68 ················standardKey = targets.First();
69 ············var standard = targets[standardKey].AsObject();
70 ············var packageKey = standard.FirstOrDefault(o=>o.StartsWith(packageName + '/', StringComparison.OrdinalIgnoreCase));
71 ············if (packageKey == null)
72 ················return null;
73 ············var package = standard[packageKey].AsObject();
74 ············var compile = package["compile"].AsObject().First();
75 ············//Regex regex = new Regex(@"netstandard\d\.\d");
76 ············//relPath = regex.Replace( relPath, "net452" );
77 ············return GetRelativeCompilePath( root, packageKey, compile );
78 ········}
79
80 ········/// <summary>
81 ········/// Gets the relative paths of the compile assets of all packages with ids matching the pattern.
82 ········/// </summary>
83 ········/// <param name="pattern">Regular expression for the package id. Package ids are case insensitive.</param>
84 ········/// <param name="excludedPackageIds">Package ids which should not be part of the result.</param>
85 ········public IEnumerable<string> GetPackageDirectories( string pattern, params string[] excludedPackageIds )
86 ········{
87 ············var root = LoadAssets();
88 ············if (root == null)
89 ················return Enumerable.Empty<string>();
90
91 ············var targets = root["targets"].AsObject();
92 ············var standardKey = targets.FirstOrDefault( o => o.StartsWith( ".NETStandard,Version=" ) || o.StartsWith( ".NETCoreApp,Version=" ) || o.StartsWith( "net" ) );
93 ············if (standardKey == null)
94 ············{
95 ················Console.WriteLine("Can't find any .NETStandard or .NETCoreApp target in the project.assets.json file");
96 ················return Enumerable.Empty<string>();
97 ············}
98 ············var standard = targets[standardKey].AsObject();
99 ············Regex regex = new Regex( "^" + pattern + '/', RegexOptions.IgnoreCase );
100 ············var packageKeys = standard.Where(o=>regex.Match(o).Success)
101 ················.Where(o=>!excludedPackageIds.Any(id=>o.StartsWith(id + '/', StringComparison.OrdinalIgnoreCase)));
102 ············var paths = packageKeys.Select(packageKey=>
103 ············{
104 ················var package = standard[packageKey].AsObject();
105 ················var compile = package["compile"].AsObject().First();
106 ················//Regex regx = new Regex(@"netstandard\d\.\d");
107 ················//relPath = regx.Replace( relPath, "net452" );
108 ················return GetRelativeCompilePath( root, packageKey, compile );
109 ············} ).ToList();
110 ············return paths;
111 ········}
112 ····}
113 }
114
New Commit (249dae5)
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Text;
6 using System.Text.RegularExpressions;
7 using System.Threading.Tasks;
8 using TinyJSON;
9
10 namespace NDOEnhancer
11 {
12 ····class ProjectAssets
13 ····{
14 ········private readonly ProjectDescription projectDescription;
15 ········public ProjectAssets( ProjectDescription projectDescription )
16 ········{
17 ············this.projectDescription = projectDescription;
18 ········}
19
20 ········JSON.Object LoadAssets()
21 ········{
22 ············var json = new JSON();
23 ············var objPath = Path.Combine(this.projectDescription.ProjPath, "obj");
24 ············var assetsPath = Path.Combine( objPath, "project.assets.json" );
25 ············if (!File.Exists( assetsPath ))
26 ············{
27 ················Console.WriteLine( $"Can't find assets file: {assetsPath}" );
28 ················return null;
29 ············}
30 ············using (TextReader tr = new StreamReader( assetsPath ))
31 ················return json.Parse( tr ).Value.AsObject();
32 ········}
33
34 ········/// <summary>
35 ········/// Computes the path of a compile asset relative to the NuGet package folder.
36 ········/// </summary>
37 ········/// <remarks>
38 ········/// The keys in the targets section (e.g. "NDO.Mapping/6.0.0") contain the original casing
39 ········/// of the package id, but NuGet stores the packages in lower case folders
40 ········/// (e.g. "ndo.mapping/6.0.0"). On case sensitive file systems (Linux) we have to use
41 ········/// the folder name provided in the libraries section.
42 ········/// </remarks>
43 ········static string GetRelativeCompilePath( JSON.Object root, string packageKey, string compile )
44 ········{
45 ············string packagePath = null;
46 ············JSON.Object libraries = null;
47 ············JSON.Object library = null;
48 ············if (root.Get( "libraries", ref libraries ) && libraries.Get( packageKey, ref library ))
49 ················library.Get( "path", ref packagePath );
50 ············if (string.IsNullOrEmpty( packagePath ))
51 ················packagePath = packageKey.ToLowerInvariant();
52
53 ············var relPath = packagePath.TrimEnd( '/' ) + "/" + compile;
54 ············if (Path.DirectorySeparatorChar != '/')
55 ················relPath = relPath.Replace( '/', Path.DirectorySeparatorChar );
56 ············return relPath;
57 ········}
58
59 ········public string GetPackageDir( string packageName )
60 ········{
61 ············var root = LoadAssets();
62 ············if (root == null)
63 ················return null;
64
65 ············var targets = root["targets"].AsObject();
66 ············var standardKey = targets.FirstOrDefault(o => o.StartsWith( ".NETStandard,Version=" ) || o.StartsWith( ".NETCoreApp,Version=" ));
67 ············if (standardKey == null)
68 ················standardKey = targets.First();
69 ············var standard = targets[standardKey].AsObject();
70 ············var packageKey = standard.FirstOrDefault(o=>o.StartsWith(packageName + '/', StringComparison.OrdinalIgnoreCase));
71 ············if (packageKey == null)
72 ················return null;
73 ············var package = standard[packageKey].AsObject();
74 ············var compile = package["compile"].AsObject().First();
 
 
75 ············return GetRelativeCompilePath( root, packageKey, compile );
76 ········}
77
78 ········/// <summary>
79 ········/// Gets the relative paths of the compile assets of all packages with ids matching the pattern.
80 ········/// </summary>
81 ········/// <param name="pattern">Regular expression for the package id. Package ids are case insensitive.</param>
82 ········/// <param name="excludedPackageIds">Package ids which should not be part of the result.</param>
83 ········public IEnumerable<string> GetPackageDirectories( string pattern, params string[] excludedPackageIds )
84 ········{
85 ············var root = LoadAssets();
86 ············if (root == null)
87 ················return Enumerable.Empty<string>();
88
89 ············var targets = root["targets"].AsObject();
90 ············var standardKey = targets.FirstOrDefault( o => o.StartsWith( ".NETStandard,Version=" ) || o.StartsWith( ".NETCoreApp,Version=" ) || o.StartsWith( "net" ) );
91 ············if (standardKey == null)
92 ············{
93 ················Console.WriteLine("Can't find any .NETStandard or .NETCoreApp target in the project.assets.json file");
94 ················return Enumerable.Empty<string>();
95 ············}
96 ············var standard = targets[standardKey].AsObject();
97 ············Regex regex = new Regex( "^" + pattern + '/', RegexOptions.IgnoreCase );
98 ············var packageKeys = standard.Where(o=>regex.Match(o).Success)
99 ················.Where(o=>!excludedPackageIds.Any(id=>o.StartsWith(id + '/', StringComparison.OrdinalIgnoreCase)));
100 ············var paths = packageKeys.Select(packageKey=>
101 ············{
102 ················var package = standard[packageKey].AsObject();
103 ················var compile = package["compile"].AsObject().First();
 
 
104 ················return GetRelativeCompilePath( root, packageKey, compile );
105 ············} ).ToList();
106 ············return paths;
107 ········}
108 ····}
109 }
110