Datei: NDOEnhancer/NDOEnhancer/ProjectAssets.cs
Last Commit (7d77e26)
| 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 | public string GetPackageDir( string packageName ) |
| 21 | ········{ |
| 22 | ············var json = new JSON(); |
| 23 | ············JSON.Object root; |
| 24 | ············var objPath = Path.Combine(this.projectDescription.ProjPath, "obj"); |
| 25 | ············var assetsPath = Path.Combine( objPath, "project.assets.json" ); |
| 26 | if ( assetsPath == null) |
| 27 | ············{ |
| 28 | ················Console.WriteLine( $"Can't find assets file: {assetsPath}" ); |
| 29 | ················return null; |
| 30 | ············} |
| 31 | ············using (TextReader tr = new StreamReader( assetsPath )) |
| 32 | root = json. Parse( tr ) . Value. AsObject( ) ; |
| 33 | |
| 34 | ············var targets = root["targets"].AsObject(); |
| 35 | ············var standardKey = targets.FirstOrDefault(o => o.StartsWith( ".NETStandard,Version=" ) || o.StartsWith( ".NETCoreApp,Version=" )); |
| 36 | ············if (standardKey == null) |
| 37 | ················standardKey = targets.First(); |
| 38 | ············var standard = targets[standardKey].AsObject(); |
| 39 | ············var packageKey = standard.FirstOrDefault(o=>o.StartsWith(packageName + '/', StringComparison.OrdinalIgnoreCase)); |
| 40 | ············if (packageKey == null) |
| 41 | ················return null; |
| 42 | ············var package = standard[packageKey].AsObject(); |
| 43 | ············var compile = package["compile"].AsObject().First(); |
| 44 | ············var relPath = packageKey + "/" + compile; |
| 45 | ············if (Path.DirectorySeparatorChar != '/') |
| 46 | ················relPath = relPath.Replace( '/', Path.DirectorySeparatorChar ); |
| 47 | ············//Regex regex = new Regex(@"netstandard\d\.\d"); |
| 48 | ············//relPath = regex.Replace( relPath, "net452" ); |
| 49 | return relPath; |
| 50 | ········} |
| 51 | |
| 52 | public IEnumerable<string> GetPackageDirectories( string pattern ) |
| 53 | ········{ |
| 54 | var json = new JSON( ) ; |
| 55 | JSON. Object root; |
| 56 | ············var objPath = Path.Combine(projectDescription.ProjPath, "obj"); |
| 57 | ············var assetsPath = Path.Combine( objPath, "project.assets.json" ); |
| 58 | ············if (assetsPath == null) |
| 59 | ············{ |
| 60 | ················Console.WriteLine( $"Can't find assets file: {assetsPath}" ); |
| 61 | ················return Enumerable.Empty<string>(); |
| 62 | ············} |
| 63 | ············using (TextReader tr = new StreamReader( assetsPath )) |
| 64 | ················root = json.Parse( tr ).Value.AsObject(); |
| 65 | |
| 66 | ············var targets = root["targets"].AsObject(); |
| 67 | ············var standardKey = targets.FirstOrDefault( o => o.StartsWith( ".NETStandard,Version=" ) || o.StartsWith( ".NETCoreApp,Version=" ) || o.StartsWith( "net" ) ); |
| 68 | ············if (standardKey == null) |
| 69 | ············{ |
| 70 | ················Console.WriteLine("Can't find any .NETStandard or .NETCoreApp target in the project.assets.json file"); |
| 71 | ················return Enumerable.Empty<string>(); |
| 72 | ············} |
| 73 | ············var standard = targets[standardKey].AsObject(); |
| 74 | Regex regex = new Regex( pattern + '/') ; |
| 75 | var packageKeys = standard. Where( o=>regex. Match( o) . Success) ; |
| 76 | ············var paths = packageKeys.Select(packageKey=> |
| 77 | ············{ |
| 78 | ················var package = standard[packageKey].AsObject(); |
| 79 | ················var compile = package["compile"].AsObject().First(); |
| 80 | ················var relPath = packageKey + "/" + compile; |
| 81 | ················if (Path.DirectorySeparatorChar != '/') |
| 82 | ····················relPath = relPath.Replace( '/', Path.DirectorySeparatorChar ); |
| 83 | ················//Regex regx = new Regex(@"netstandard\d\.\d"); |
| 84 | ················//relPath = regx.Replace( relPath, "net452" ); |
| 85 | return relPath; |
| 86 | } ) ; |
| 87 | ············return paths; |
| 88 | ········} |
| 89 | ····} |
| 90 | } |
| 91 |
New Commit (4bf6f30)
| 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 |