private static ImmutableArray <PackageReference> InitializeAssemblyRegistry(string functionDirectory) { var builder = ImmutableArray <PackageReference> .Empty.ToBuilder(); string fileName = Path.Combine(functionDirectory, DotNetConstants.ProjectLockFileName); if (File.Exists(fileName)) { LockFile lockFile = null; try { var reader = new LockFileFormat(); lockFile = reader.Read(fileName); var target = lockFile.Targets .FirstOrDefault(t => string.Equals(t.TargetFramework.DotNetFrameworkName, FrameworkConstants.CommonFrameworks.NetStandard20.DotNetFrameworkName)); if (target != null) { string nugetCachePath = PackageManager.GetNugetPackagesPath(); // Get all the referenced libraries for the target, excluding the framework references we add by default var libraries = target.Libraries.Where(s => !DotNetConstants.FrameworkReferences.Contains(s.Name)); foreach (var library in libraries) { var package = new PackageReference(library.Name, library.Version.ToFullString()); var assemblies = GetAssembliesList(library.CompileTimeAssemblies, nugetCachePath, package); package.CompileTimeAssemblies.AddRange(assemblies); assemblies = GetAssembliesList(library.RuntimeAssemblies, nugetCachePath, package); package.RuntimeAssemblies.AddRange(assemblies); var frameworkAssemblies = library.FrameworkAssemblies.ToDictionary(a => new AssemblyName(a)); package.FrameworkAssemblies.AddRange(frameworkAssemblies); builder.Add(package); } } } catch (FileFormatException) { return(ImmutableArray <PackageReference> .Empty); } } return(builder.ToImmutableArray()); }
private static ImmutableArray <PackageReference> InitializeAssemblyRegistry(FunctionMetadata metadata) { var builder = ImmutableArray <PackageReference> .Empty.ToBuilder(); string fileName = Path.Combine(Path.GetDirectoryName(metadata.ScriptFile), DotNetConstants.ProjectLockFileName); if (File.Exists(fileName)) { var jobject = JObject.Parse(File.ReadAllText(fileName)); var target = jobject.SelectTokens(string.Format(CultureInfo.InvariantCulture, "$.targets['{0}']", FrameworkTargetName)).FirstOrDefault(); if (target != null) { string nugetHome = PackageManager.GetNugetPackagesPath(); foreach (JProperty token in target) { var referenceNameParts = token.Name.Split('/'); if (referenceNameParts.Length != 2) { throw new FormatException(string.Format(CultureInfo.InvariantCulture, "The package name '{0}' is not correctly formatted.", token.Name)); } var package = new PackageReference(referenceNameParts[0], referenceNameParts[1]); var references = token.SelectTokens("$..compile").FirstOrDefault(); if (references != null) { foreach (JProperty reference in references) { if (!reference.Name.EndsWith(EmptyFolderFileMarker, StringComparison.Ordinal)) { string path = Path.Combine(nugetHome, token.Name, reference.Name); path = path.Replace('/', '\\'); if (File.Exists(path)) { package.Assemblies.Add(AssemblyName.GetAssemblyName(path), path); } } } } var frameworkAssemblies = token.SelectTokens("$..frameworkAssemblies").FirstOrDefault(); if (frameworkAssemblies != null) { foreach (var assembly in frameworkAssemblies) { string assemblyName = assembly.ToString(); package.FrameworkAssemblies.Add(new AssemblyName(assemblyName), assemblyName); } } builder.Add(package); } } } return(builder.ToImmutableArray()); }