|
1 | 1 | <!-- Loosely based on https://github.com/kimbell/Kiota.Testing, related to https://github.com/microsoft/kiota/issues/3005 -->
|
2 | 2 | <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> |
3 | 47 |
|
4 | 48 | <!-- Restore local tools -->
|
5 | 49 | <Target Name="_KiotaRestoreTools" Condition="'$(KiotaAutoRestoreTools)' == 'true'">
|
|
140 | 184 | <Exec Command="$(_KiotaCommand) %(KiotaReference.Arguments)" EnvironmentVariables="KIOTA_TUTORIAL_ENABLED=false;KIOTA_OFFLINE_ENABLED=true" />
|
141 | 185 |
|
142 | 186 | <!-- 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)" /> |
149 | 188 | </Target>
|
150 | 189 | </Project>
|
0 commit comments