Skip to content

Commit 91ce0b4

Browse files
dupdobphmonte
andauthored
Add support for azure function (Buildalyzer#280)
Co-authored-by: Pablo Monteiro <[email protected]>
1 parent aa33cf5 commit 91ce0b4

File tree

7 files changed

+125
-21
lines changed

7 files changed

+125
-21
lines changed

src/Buildalyzer/Logging/EventProcessor.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -145,31 +145,36 @@ private void TargetFinished(object sender, TargetFinishedEventArgs e)
145145

146146
private void MessageRaised(object sender, BuildMessageEventArgs e)
147147
{
148-
AnalyzerResult result = _currentResult.Count == 0 ? null : _currentResult.Peek();
149-
if (result is object)
148+
var result = _currentResult.Count == 0 ? null : _currentResult.Peek();
149+
if (result is not object || !IsRelevant())
150150
{
151-
// Process the command line arguments for the Fsc task
152-
if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true
153-
&& !string.IsNullOrWhiteSpace(e.Message)
154-
&& _targetStack.Any(x => x.TargetName == "CoreCompile")
155-
&& result.CompilerCommand is null)
156-
{
157-
result.ProcessFscCommandLine(e.Message);
158-
}
151+
return;
152+
}
159153

160-
// Process the command line arguments for the Csc task
161-
if (e is TaskCommandLineEventArgs cmd
162-
&& string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase))
163-
{
164-
result.ProcessCscCommandLine(cmd.CommandLine, _targetStack.Any(x => x.TargetName == "CoreCompile"));
165-
}
154+
// Process the command line arguments for the Fsc task
155+
if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true
156+
&& !string.IsNullOrWhiteSpace(e.Message)
157+
&& _targetStack.Any(x => x.TargetName == "CoreCompile")
158+
&& result.CompilerCommand is null)
159+
{
160+
result.ProcessFscCommandLine(e.Message);
161+
}
166162

167-
if (e is TaskCommandLineEventArgs cmdVbc &&
168-
string.Equals(cmdVbc.TaskName, "Vbc", StringComparison.OrdinalIgnoreCase))
169-
{
170-
result.ProcessVbcCommandLine(cmdVbc.CommandLine);
171-
}
163+
// Process the command line arguments for the Csc task
164+
if (e is TaskCommandLineEventArgs cmd
165+
&& string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase))
166+
{
167+
result.ProcessCscCommandLine(cmd.CommandLine, _targetStack.Any(x => x.TargetName == "CoreCompile"));
172168
}
169+
170+
if (e is TaskCommandLineEventArgs cmdVbc &&
171+
string.Equals(cmdVbc.TaskName, "Vbc", StringComparison.OrdinalIgnoreCase))
172+
{
173+
result.ProcessVbcCommandLine(cmdVbc.CommandLine);
174+
}
175+
176+
bool IsRelevant() => string.IsNullOrEmpty(result.Command) || AnalyzerManager.NormalizePath(e.ProjectFile) == _projectFilePath;
177+
173178
}
174179

175180
private void BuildFinished(object sender, BuildFinishedEventArgs e)

tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,29 @@ public void WpfControlLibraryGetsSourceFiles()
224224
}.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x)), log.ToString());
225225
}
226226

227+
[Test]
228+
[Platform("win")]
229+
public void AzureFunctionSourceFiles()
230+
{
231+
// Given
232+
StringWriter log = new StringWriter();
233+
IProjectAnalyzer analyzer = GetProjectAnalyzer(@"AzureFunctionProject\AzureFunctionProject.csproj", log);
234+
235+
// When
236+
IAnalyzerResults results = analyzer.Build();
237+
238+
// Then
239+
IReadOnlyList<string> sourceFiles = results.SingleOrDefault()?.SourceFiles;
240+
sourceFiles.ShouldNotBeNull(log.ToString());
241+
new[]
242+
{
243+
"Program",
244+
"TestFunction",
245+
"AssemblyAttributes",
246+
"AssemblyInfo"
247+
}.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString());
248+
}
249+
227250
[Test]
228251
public void MultiTargetingBuildAllTargetFrameworksGetsSourceFiles()
229252
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net6.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0" />
9+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.10" />
10+
</ItemGroup>
11+
<ItemGroup>
12+
<None Update="host.json">
13+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
14+
</None>
15+
<None Update="local.settings.json">
16+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17+
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
18+
</None>
19+
</ItemGroup>
20+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.Extensions.Hosting;
2+
3+
namespace AzureFunctionProject;
4+
5+
public class Program
6+
{
7+
public static void Main()
8+
{
9+
var host = new HostBuilder()
10+
.ConfigureServices(_ =>
11+
{})
12+
.Build();
13+
14+
host.Run();
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.Azure.WebJobs;
3+
using Microsoft.Extensions.Logging;
4+
5+
namespace AzureFunctionProject;
6+
7+
public class TestFunction
8+
{
9+
[FunctionName(nameof(TestFunction))]
10+
public async Task Run([CosmosDBTrigger(
11+
databaseName: "databaseName",
12+
collectionName: "collectionName",
13+
ConnectionStringSetting = "",
14+
LeaseCollectionName = "leases")]string input,
15+
ILogger log)
16+
{
17+
if (input != null)
18+
{
19+
log.LogInformation("Document modified");
20+
}
21+
}
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "2.0",
3+
"logging": {
4+
"applicationInsights": {
5+
"samplingSettings": {
6+
"isEnabled": true,
7+
"excludedTypes": "Request"
8+
}
9+
}
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"AzureWebJobsStorage": "",
5+
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
6+
}
7+
}

0 commit comments

Comments
 (0)