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

Add support for azure function #280

Merged
merged 4 commits into from
Sep 8, 2024
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
47 changes: 26 additions & 21 deletions src/Buildalyzer/Logging/EventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,36 @@ private void TargetFinished(object sender, TargetFinishedEventArgs e)

private void MessageRaised(object sender, BuildMessageEventArgs e)
{
AnalyzerResult result = _currentResult.Count == 0 ? null : _currentResult.Peek();
if (result is object)
var result = _currentResult.Count == 0 ? null : _currentResult.Peek();
if (result is not object || !IsRelevant())
{
// Process the command line arguments for the Fsc task
if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true
&& !string.IsNullOrWhiteSpace(e.Message)
&& _targetStack.Any(x => x.TargetName == "CoreCompile")
&& result.CompilerCommand is null)
{
result.ProcessFscCommandLine(e.Message);
}
return;
}

// Process the command line arguments for the Csc task
if (e is TaskCommandLineEventArgs cmd
&& string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase))
{
result.ProcessCscCommandLine(cmd.CommandLine, _targetStack.Any(x => x.TargetName == "CoreCompile"));
}
// Process the command line arguments for the Fsc task
if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true
&& !string.IsNullOrWhiteSpace(e.Message)
&& _targetStack.Any(x => x.TargetName == "CoreCompile")
&& result.CompilerCommand is null)
{
result.ProcessFscCommandLine(e.Message);
}

if (e is TaskCommandLineEventArgs cmdVbc &&
string.Equals(cmdVbc.TaskName, "Vbc", StringComparison.OrdinalIgnoreCase))
{
result.ProcessVbcCommandLine(cmdVbc.CommandLine);
}
// Process the command line arguments for the Csc task
if (e is TaskCommandLineEventArgs cmd
&& string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase))
{
result.ProcessCscCommandLine(cmd.CommandLine, _targetStack.Any(x => x.TargetName == "CoreCompile"));
}

if (e is TaskCommandLineEventArgs cmdVbc &&
string.Equals(cmdVbc.TaskName, "Vbc", StringComparison.OrdinalIgnoreCase))
{
result.ProcessVbcCommandLine(cmdVbc.CommandLine);
}

bool IsRelevant() => string.IsNullOrEmpty(result.Command) || AnalyzerManager.NormalizePath(e.ProjectFile) == _projectFilePath;

}

private void BuildFinished(object sender, BuildFinishedEventArgs e)
Expand Down
21 changes: 21 additions & 0 deletions tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,27 @@ public void WpfControlLibraryGetsSourceFiles()
}.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x)), log.ToString());
}

[Test]
[Platform("win")]
public void AzureFunctionSourceFiles()
{
// Given
StringWriter log = new StringWriter();
IProjectAnalyzer analyzer = GetProjectAnalyzer(@"AzureFunctionProject\AzureFunctionProject.csproj", log);

// When
IAnalyzerResults results = analyzer.Build();

// Then
IReadOnlyList<string> sourceFiles = results.SingleOrDefault()?.SourceFiles;
sourceFiles.ShouldNotBeNull(log.ToString());
new[]
{
"Program.cs",
"Function1.cs",
}.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x)), log.ToString());
}

[Test]
public void MultiTargetingBuildAllTargetFrameworksGetsSourceFiles()
{
Expand Down
20 changes: 20 additions & 0 deletions tests/projects/AzureFunctionProject/AzureFunctionProject.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.10" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
16 changes: 16 additions & 0 deletions tests/projects/AzureFunctionProject/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.Hosting;

namespace AzureFunctionProject;

public class Program
{
public static void Main()
{
var host = new HostBuilder()
.ConfigureServices(_ =>
{})
.Build();

host.Run();
}
}
22 changes: 22 additions & 0 deletions tests/projects/AzureFunctionProject/TestFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace AzureFunctionProject;

public class TestFunction
{
[FunctionName(nameof(TestFunction))]
public async Task Run([CosmosDBTrigger(
databaseName: "databaseName",
collectionName: "collectionName",
ConnectionStringSetting = "",
LeaseCollectionName = "leases")]string input,
ILogger log)
{
if (input != null)
{
log.LogInformation("Document modified");
}
}
}
11 changes: 11 additions & 0 deletions tests/projects/AzureFunctionProject/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
}
}
7 changes: 7 additions & 0 deletions tests/projects/AzureFunctionProject/local.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
}
}