8
8
using System . IO ;
9
9
using System . Linq ;
10
10
using System . Reflection ;
11
+ using Microsoft . Azure . WebJobs . Host ;
11
12
using Newtonsoft . Json . Linq ;
13
+ using NuGet . Frameworks ;
14
+ using NuGet . ProjectModel ;
12
15
13
16
namespace Microsoft . Azure . WebJobs . Script . Description
14
17
{
@@ -18,7 +21,6 @@ namespace Microsoft.Azure.WebJobs.Script.Description
18
21
public sealed class PackageAssemblyResolver
19
22
{
20
23
private const string EmptyFolderFileMarker = "_._" ;
21
- private const string FrameworkTargetName = ".NETFramework,Version=v4.6" ;
22
24
23
25
private readonly ImmutableArray < PackageReference > _packages ;
24
26
@@ -41,7 +43,7 @@ public IEnumerable<string> AssemblyReferences
41
43
{
42
44
return _packages . Aggregate ( new List < string > ( ) , ( assemblies , p ) =>
43
45
{
44
- assemblies . AddRange ( p . Assemblies . Values ) ;
46
+ assemblies . AddRange ( p . CompileTimeAssemblies . Values ) ;
45
47
assemblies . AddRange ( p . FrameworkAssemblies . Values ) ;
46
48
return assemblies ;
47
49
} ) . Distinct ( ) ;
@@ -55,68 +57,65 @@ private static ImmutableArray<PackageReference> InitializeAssemblyRegistry(strin
55
57
56
58
if ( File . Exists ( fileName ) )
57
59
{
58
- var jobject = JObject . Parse ( File . ReadAllText ( fileName ) ) ;
59
-
60
- var target = jobject . SelectTokens ( string . Format ( CultureInfo . InvariantCulture , "$.targets['{0}']" , FrameworkTargetName ) ) . FirstOrDefault ( ) ;
61
-
62
- if ( target != null )
60
+ LockFile lockFile = null ;
61
+ try
63
62
{
64
- string nugetHome = PackageManager . GetNugetPackagesPath ( ) ;
63
+ var reader = new LockFileFormat ( ) ;
64
+ lockFile = reader . Read ( fileName ) ;
65
+
66
+ var target = lockFile . Targets
67
+ . FirstOrDefault ( t => string . Equals ( t . TargetFramework . DotNetFrameworkName , FrameworkConstants . CommonFrameworks . NetStandard20 . DotNetFrameworkName ) ) ;
65
68
66
- foreach ( JProperty token in target )
69
+ if ( target != null )
67
70
{
68
- var referenceNameParts = token . Name . Split ( '/' ) ;
69
- if ( referenceNameParts . Length != 2 )
71
+ string nugetCachePath = PackageManager . GetNugetPackagesPath ( ) ;
72
+
73
+ // Get all the referenced libraries for the target, excluding the framework references we add by default
74
+ var libraries = target . Libraries . Where ( s => ! DotNetConstants . FrameworkReferences . Contains ( s . Name ) ) ;
75
+ foreach ( var library in libraries )
70
76
{
71
- throw new FormatException ( string . Format ( CultureInfo . InvariantCulture , "The package name '{0}' is not correctly formatted." , token . Name ) ) ;
72
- }
77
+ var package = new PackageReference ( library . Name , library . Version . ToFullString ( ) ) ;
73
78
74
- var package = new PackageReference ( referenceNameParts [ 0 ] , referenceNameParts [ 1 ] ) ;
79
+ var assemblies = GetAssembliesList ( library . CompileTimeAssemblies , nugetCachePath , package ) ;
75
80
76
- var references = token . SelectTokens ( "$..compile" ) . FirstOrDefault ( ) ;
77
- if ( references != null )
78
- {
79
- foreach ( JProperty reference in references )
80
- {
81
- if ( ! reference . Name . EndsWith ( EmptyFolderFileMarker , StringComparison . Ordinal ) )
82
- {
83
- string path = Path . Combine ( nugetHome , token . Name , reference . Name ) ;
84
- path = path . Replace ( '/' , '\\ ' ) ;
85
-
86
- if ( File . Exists ( path ) )
87
- {
88
- package . Assemblies . Add ( AssemblyName . GetAssemblyName ( path ) , path ) ;
89
- }
90
- }
91
- }
92
- }
81
+ package . CompileTimeAssemblies . AddRange ( assemblies ) ;
93
82
94
- var frameworkAssemblies = token . SelectTokens ( "$..frameworkAssemblies" ) . FirstOrDefault ( ) ;
95
- if ( frameworkAssemblies != null )
96
- {
97
- foreach ( var assembly in frameworkAssemblies )
98
- {
99
- string assemblyName = assembly . ToString ( ) ;
100
- package . FrameworkAssemblies . Add ( new AssemblyName ( assemblyName ) , assemblyName ) ;
101
- }
102
- }
83
+ assemblies = GetAssembliesList ( library . RuntimeAssemblies , nugetCachePath , package ) ;
103
84
104
- builder . Add ( package ) ;
85
+ package . RuntimeAssemblies . AddRange ( assemblies ) ;
86
+
87
+ var frameworkAssemblies = library . FrameworkAssemblies . ToDictionary ( a => new AssemblyName ( a ) ) ;
88
+ package . FrameworkAssemblies . AddRange ( frameworkAssemblies ) ;
89
+
90
+ builder . Add ( package ) ;
91
+ }
105
92
}
106
93
}
94
+ catch ( FileFormatException )
95
+ {
96
+ return ImmutableArray < PackageReference > . Empty ;
97
+ }
107
98
}
108
99
109
100
return builder . ToImmutableArray ( ) ;
110
101
}
111
102
103
+ private static IReadOnlyDictionary < AssemblyName , string > GetAssembliesList ( IList < LockFileItem > compileTimeAssemblies , string nugetCachePath , PackageReference package )
104
+ {
105
+ string packagePath = Path . Combine ( nugetCachePath , package . Name , package . Version . ToString ( ) ) ;
106
+ return compileTimeAssemblies . Select ( i => Path . Combine ( packagePath , i . Path ) )
107
+ . Where ( p => ! p . EndsWith ( EmptyFolderFileMarker ) && File . Exists ( p ) )
108
+ . ToDictionary ( p => AssemblyName . GetAssemblyName ( p ) , p => new Uri ( p ) . LocalPath ) ;
109
+ }
110
+
112
111
public bool TryResolveAssembly ( string name , out string path )
113
112
{
114
113
path = null ;
115
114
var assemblyName = new AssemblyName ( name ) ;
116
115
117
116
foreach ( var package in _packages )
118
117
{
119
- if ( package . Assemblies . TryGetValue ( assemblyName , out path ) ||
118
+ if ( package . RuntimeAssemblies . TryGetValue ( assemblyName , out path ) ||
120
119
package . FrameworkAssemblies . TryGetValue ( assemblyName , out path ) )
121
120
{
122
121
break ;
0 commit comments