Skip to content

Commit 06aedf1

Browse files
committed
Kiota: Replace PowerShell script with inline MSBuild task for improved performance
1 parent 749e588 commit 06aedf1

File tree

3 files changed

+45
-50
lines changed

3 files changed

+45
-50
lines changed

src/JsonApiDotNetCore.OpenApi.Client.Kiota/Build/JsonApiDotNetCore.OpenApi.Client.Kiota.targets

+45-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
<!-- Loosely based on https://github.com/kimbell/Kiota.Testing, related to https://github.com/microsoft/kiota/issues/3005 -->
22
<Project>
3+
<UsingTask TaskName="KiotaPatchGeneratedCodeFiles" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
4+
<ParameterGroup>
5+
<StartDirectory ParameterType="System.String" Required="true" />
6+
</ParameterGroup>
7+
<Task>
8+
<Code Type="Class" Language="cs">
9+
<![CDATA[
10+
using System;
11+
using System.IO;
12+
using System.Text.RegularExpressions;
13+
using Microsoft.Build.Framework;
14+
using Microsoft.Build.Utilities;
15+
16+
public sealed class KiotaPatchGeneratedCodeFiles : Task
17+
{
18+
private static readonly Regex HeaderRegex = new(@"// <auto-generated/>(?:\r\n|\n|\r)(#pragma|using)", RegexOptions.Singleline | RegexOptions.Compiled);
19+
private static readonly Regex NullableRegex = new(@"(?s)#if NETSTANDARD2_1_OR_GREATER .*?(?:\r\n|\n|\r)#nullable enable(?:\r\n|\n|\r)(?<ifBody>.*?)(?:\r\n|\n|\r)#nullable restore(?:\r\n|\n|\r)#else(?:\r\n|\n|\r)(?<elseBody>.*?)(?:\r\n|\n|\r)#endif", RegexOptions.Singleline | RegexOptions.Compiled);
20+
private static readonly Regex LineBreaksRegex = new(@"}(?:\r\n|\n|\r)(?<lineIndent>[ ]+/// <summary>)", RegexOptions.Singleline | RegexOptions.Compiled);
21+
22+
public string StartDirectory { get; set; }
23+
24+
public override bool Execute()
25+
{
26+
string absoluteStartDirectory = Path.GetFullPath(StartDirectory);
27+
Log.LogMessage(MessageImportance.High, $"Patching kiota output files in {absoluteStartDirectory}");
28+
29+
foreach (string path in Directory.GetFiles(absoluteStartDirectory, "*.cs", SearchOption.AllDirectories))
30+
{
31+
string content = File.ReadAllText(path);
32+
content = HeaderRegex.Replace(content, $"// <auto-generated/>{Environment.NewLine}#nullable enable{Environment.NewLine}#pragma warning disable CS8625{Environment.NewLine}$1");
33+
content = NullableRegex.Replace(content, "$1");
34+
content = LineBreaksRegex.Replace(content, $"}}{Environment.NewLine}{Environment.NewLine}$1");
35+
36+
File.WriteAllText(path, content);
37+
Log.LogMessage(MessageImportance.Normal, $"Patched file: {path}");
38+
}
39+
40+
return true;
41+
}
42+
}
43+
]]>
44+
</Code>
45+
</Task>
46+
</UsingTask>
347

448
<!-- Restore local tools -->
549
<Target Name="_KiotaRestoreTools" Condition="'$(KiotaAutoRestoreTools)' == 'true'">
@@ -140,11 +184,6 @@
140184
<Exec Command="$(_KiotaCommand) %(KiotaReference.Arguments)" EnvironmentVariables="KIOTA_TUTORIAL_ENABLED=false;KIOTA_OFFLINE_ENABLED=true" />
141185

142186
<!-- Post-process output files -->
143-
<Message Importance="High" Condition="'$(KiotaPatchOutput)' == 'true'" Text="Patching kiota output files in %(KiotaReference.OutputPath)" />
144-
<Exec Condition="'$(KiotaPatchOutput)' == 'true'" ConsoleToMSBuild="true"
145-
Command="pwsh $(MSBuildThisFileDirectory)kiota-patch-generated-code.ps1 %(KiotaReference.OutputPath)">
146-
<Output TaskParameter="ConsoleOutput" ItemName="OutputLinesOfScript" />
147-
</Exec>
148-
<Error Condition="'@(OutputLinesOfScript)' != ''" Text="@(OutputLinesOfScript, '%0a')" />
187+
<KiotaPatchGeneratedCodeFiles StartDirectory="%(KiotaReference.OutputPath)" />
149188
</Target>
150189
</Project>

src/JsonApiDotNetCore.OpenApi.Client.Kiota/Build/kiota-patch-generated-code.ps1

-43
This file was deleted.

src/JsonApiDotNetCore.OpenApi.Client.Kiota/JsonApiDotNetCore.OpenApi.Client.Kiota.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<None Include="..\..\PackageReadme.md" Visible="false" Pack="True" PackagePath="" />
3030
<None Include="Build\*.props" Pack="True" PackagePath="build" />
3131
<None Include="Build\*.targets" Pack="True" PackagePath="build" />
32-
<None Include="Build\*.ps1" Pack="True" PackagePath="build" />
3332
</ItemGroup>
3433

3534
<ItemGroup>

0 commit comments

Comments
 (0)