Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scan all .deps.json files and determine root project #217

Merged
merged 7 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -235,27 +236,43 @@ private string GetRuntimePath(string functionAppDirectory, bool appendBin)
// This method relies on the dependency manifest file to find the function app runtime dll file.
// It can be either <your_runtime>.deps.json or function.deps.json. In most cases, at least the
// function.deps.json should exist, but in case no manifest exists, it will throw the exception.
// In case there are multiple .deps.json files, the root project will be picked, based on the
// dependencies mentioned in the .deps.json files.
private async Task<string> GetRuntimeFilenameAsync(string functionAppDirectory)
{
var files = Directory.GetFiles(functionAppDirectory, "*.deps.json", SearchOption.AllDirectories);
var file = files.FirstOrDefault();
if (file.IsNullOrWhiteSpace())
if (!files.Any())
{
throw new InvalidOperationException("Invalid function app directory");
}

var dependencyManifests = new List<DependencyManifest>();
foreach (var file in files) {
dependencyManifests.Add(await GetDependencyManifestAsync(file));
}
justinyoo marked this conversation as resolved.
Show resolved Hide resolved

var runtimes = dependencyManifests
.Select(manifest => manifest.Targets[manifest.RuntimeTarget.Name].First())
.Select(target => new
{
Name = target.Key.Split("/").First(),
FileName = target.Value.Runtime.First().Key,
Dependencies = target.Value.Dependencies.Keys
});

var referencedRuntimes = runtimes.SelectMany(d => d.Dependencies);
return runtimes.FirstOrDefault(r => !referencedRuntimes.Contains(r.Name))?.FileName;
}

private static async Task<DependencyManifest> GetDependencyManifestAsync(string file)
{
var serialised = default(string);
using (var reader = File.OpenText(file))
{
serialised = await reader.ReadToEndAsync();
}

var manifesto = JsonConvert.DeserializeObject<DependencyManifest>(serialised);
var runtimeTarget = manifesto.RuntimeTarget.Name;
var runtimes = manifesto.Targets[runtimeTarget].Values;
var runtime = runtimes.First().Runtime.First().Key;

return runtime;
return JsonConvert.DeserializeObject<DependencyManifest>(serialised);
}

private Assembly GetAssembly(object instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<None Update="local.settings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="TestData\FakeDependencyFile.deps.json" Link="\%(Filename)%(Extension)">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ public async Task Given_Type_When_Initiated_Then_It_Should_Return_ApplicationAss
assembly.DefinedTypes.Select(p => p.FullName).Should().Contain(ti.FullName);
}

[TestMethod]
public async Task Given_Type_With_Referenced_Project_When_Initiated_Then_It_Should_Return_ApplicationAssemblyOfRootAssembly()
{
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
var context = new OpenApiHttpTriggerContext();

var assembly = (await context.SetApplicationAssemblyAsync(location, false))
.ApplicationAssembly;

assembly.FullName.Should().Be(typeof(OpenApiHttpTriggerContextTests).Assembly.FullName);
}

[DataTestMethod]
[DataRow(typeof(IOpenApiHttpTriggerContext))]
[DataRow(typeof(OpenApiHttpTriggerContext))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v5.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v5.0": {
"Microsoft.Azure.Functions.Worker.Extensions.OpenApi/1.0.0": {
"dependencies": {
"Microsoft.Azure.Core.NewtonsoftJson": "1.0.0",
"Microsoft.Azure.Functions.Worker.Core": "1.1.0",
"Microsoft.Azure.Functions.Worker.Extensions.Http": "3.0.12",
"Microsoft.Azure.WebJobs.Extensions.OpenApi.Core": "1.0.0"
},
"runtime": {
"Microsoft.Azure.Functions.Worker.Extensions.OpenApi.dll": {}
}
}
}
}
}