-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathJsonApiDotNetCore.OpenApi.Client.Kiota.targets
More file actions
189 lines (171 loc) · 9.48 KB
/
JsonApiDotNetCore.OpenApi.Client.Kiota.targets
File metadata and controls
189 lines (171 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!-- Loosely based on https://github.com/kimbell/Kiota.Testing, related to https://github.com/microsoft/kiota/issues/3005 -->
<Project>
<UsingTask TaskName="KiotaPatchGeneratedCodeFiles" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<StartDirectory ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Code Type="Class" Language="cs">
<![CDATA[
using System;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public sealed class KiotaPatchGeneratedCodeFiles : Task
{
private static readonly Regex HeaderRegex = new(@"// <auto-generated/>(?:\r\n|\n|\r)(#pragma|using)", RegexOptions.Singleline | RegexOptions.Compiled);
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);
private static readonly Regex LineBreaksRegex = new(@"}(?:\r\n|\n|\r)(?<lineIndent>[ ]+/// <summary>)", RegexOptions.Singleline | RegexOptions.Compiled);
public string StartDirectory { get; set; }
public override bool Execute()
{
string absoluteStartDirectory = Path.GetFullPath(StartDirectory);
Log.LogMessage(MessageImportance.High, $"Patching kiota output files in {absoluteStartDirectory}");
foreach (string path in Directory.GetFiles(absoluteStartDirectory, "*.cs", SearchOption.AllDirectories))
{
string content = File.ReadAllText(path);
content = HeaderRegex.Replace(content, $"// <auto-generated/>{Environment.NewLine}#nullable enable{Environment.NewLine}#pragma warning disable CS8625{Environment.NewLine}$1");
content = NullableRegex.Replace(content, "$1");
content = LineBreaksRegex.Replace(content, $"}}{Environment.NewLine}{Environment.NewLine}$1");
File.WriteAllText(path, content);
Log.LogMessage(MessageImportance.Normal, $"Patched file: {path}");
}
return true;
}
}
]]>
</Code>
</Task>
</UsingTask>
<!-- Restore local tools -->
<Target Name="_KiotaRestoreTools" Condition="'$(KiotaAutoRestoreTools)' == 'true'">
<Exec Command="dotnet tool restore" />
</Target>
<!-- Build the arguments value based on the other values. This is then used by the actual CLI -->
<Target Name="_KiotaBuildArguments">
<ItemGroup>
<KiotaReference Condition="'%(Identity)' != ''">
<Arguments>%(Arguments) --openapi %(Identity)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(Language)' != ''">
<Arguments>%(Arguments) --language %(Language)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(Language)' == ''">
<Arguments>%(Arguments) --language csharp</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(ClassName)' != ''">
<Arguments>%(Arguments) --class-name %(ClassName)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(NamespaceName)' != ''">
<Arguments>%(Arguments) --namespace-name %(NamespaceName)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(OutputPath)' != ''">
<Arguments>%(Arguments) --output %(OutputPath)</Arguments>
<_NonEmptyOutputPath>%(OutputPath)</_NonEmptyOutputPath>
</KiotaReference>
<KiotaReference Condition="'%(OutputPath)' == ''">
<_NonEmptyOutputPath>./output</_NonEmptyOutputPath>
</KiotaReference>
<KiotaReference Condition="'%(LogLevel)' != ''">
<Arguments>%(Arguments) --log-level %(LogLevel)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(BackingStore)' == 'true'">
<Arguments>%(Arguments) --backing-store</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(ExcludeBackwardCompatible)' == 'true'">
<Arguments>%(Arguments) --exclude-backward-compatible</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(AdditionalData)' != ''">
<Arguments>%(Arguments) --additional-data %(AdditionalData)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(Serializer)' != ''">
<Arguments>%(Arguments) --serializer %(Serializer)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(Deserializer)' != ''">
<Arguments>%(Arguments) --deserializer %(Deserializer)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(CleanOutput)' == 'true'">
<Arguments>%(Arguments) --clean-output</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(ClearCache)' == 'true'">
<Arguments>%(Arguments) --clear-cache</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(MimeTypes)' != ''">
<Arguments>%(Arguments) --structured-mime-types %(MimeTypes)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(IncludePath)' != ''">
<Arguments>%(Arguments) --include-path %(IncludePath)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(ExcludePath)' != ''">
<Arguments>%(Arguments) --exclude-path %(ExcludePath)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(DisableValidationRules)' != ''">
<Arguments>%(Arguments) --disable-validation-rules %(DisableValidationRules)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(DisableSslValidation)' == 'true'">
<Arguments>%(Arguments) --disable-ssl-validation</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(TypeAccessModifier)' != ''">
<Arguments>%(Arguments) --type-access-modifier %(TypeAccessModifier)</Arguments>
</KiotaReference>
<KiotaReference Condition="'%(ExtraArguments)' != ''">
<Arguments>%(Arguments) %(ExtraArguments)</Arguments>
</KiotaReference>
</ItemGroup>
</Target>
<!-- Exclude **/*.cs from output directory upfront, so that MSBuild properly refreshes if .cs files appear/disappear when openapi.json file changed -->
<Target Name="_KiotaExcludeGeneratedCode" Condition="'$(DesignTimeBuild)' != 'true' And '$(BuildingProject)' == 'true'"
DependsOnTargets="_KiotaBuildArguments">
<ItemGroup>
<_WildcardGroup Include="%2A%2A/%2A.cs">
<GeneratedCodeDirectory>%(KiotaReference._NonEmptyOutputPath)</GeneratedCodeDirectory>
</_WildcardGroup>
</ItemGroup>
<CreateItem Include="@(_WildcardGroup->'%(GeneratedCodeDirectory)/%(Identity)')">
<Output TaskParameter="Include" ItemName="_FilesToExcludeGroup" />
</CreateItem>
<ItemGroup>
<_RelativeExcludePathGroup Include="@(_FilesToExcludeGroup)" Condition="'@(_FilesToExcludeGroup)' != ''">
<!-- Ensure that an output directory such as ./GeneratedCode gets normalized -->
<RelativePath>$([MSBuild]::MakeRelative($(MSBuildProjectDirectory), %(_FilesToExcludeGroup.FullPath)))</RelativePath>
</_RelativeExcludePathGroup>
</ItemGroup>
<ItemGroup>
<Compile Remove="%(_RelativeExcludePathGroup.RelativePath)" />
</ItemGroup>
</Target>
<!-- Include **/*.cs from output directory afterward, so that MSBuild properly refreshes if .cs files appear/disappear when openapi.json file changed -->
<Target Name="_KiotaIncludeGeneratedCode" Condition="'$(DesignTimeBuild)' != 'true' And '$(BuildingProject)' == 'true' And '@(KiotaReference)' != ''"
DependsOnTargets="_KiotaRunTool" BeforeTargets="BeforeCompile;CoreCompile">
<ItemGroup>
<_WildcardGroup Include="%2A%2A/%2A.cs">
<GeneratedCodeDirectory>%(KiotaReference._NonEmptyOutputPath)</GeneratedCodeDirectory>
</_WildcardGroup>
</ItemGroup>
<CreateItem Include="@(_WildcardGroup->'%(GeneratedCodeDirectory)/%(Identity)')">
<Output TaskParameter="Include" ItemName="_FilesToIncludeGroup" />
</CreateItem>
<ItemGroup>
<_RelativeIncludePathGroup Include="@(_FilesToIncludeGroup)">
<!-- Ensure that an output directory such as ./GeneratedCode gets normalized -->
<RelativePath>$([MSBuild]::MakeRelative($(MSBuildProjectDirectory), %(_FilesToIncludeGroup.FullPath)))</RelativePath>
</_RelativeIncludePathGroup>
</ItemGroup>
<ItemGroup>
<Compile Include="%(_RelativeIncludePathGroup.RelativePath)" />
</ItemGroup>
</Target>
<Target Name="_KiotaRunTool" Condition="'$(DesignTimeBuild)' != 'true' And '$(BuildingProject)' == 'true'"
DependsOnTargets="_KiotaRestoreTools;_KiotaBuildArguments;_KiotaExcludeGeneratedCode">
<!-- Run the kiota generate command, as global or local tool -->
<PropertyGroup>
<_KiotaCommand Condition="'$(KiotaAutoRestoreTools)' == 'true'">dotnet kiota generate</_KiotaCommand>
<_KiotaCommand Condition="'$(KiotaAutoRestoreTools)' != 'true'">kiota generate</_KiotaCommand>
</PropertyGroup>
<Message Importance="High" Text="$(_KiotaCommand) %(KiotaReference.Arguments)" />
<Exec Command="$(_KiotaCommand) %(KiotaReference.Arguments)" EnvironmentVariables="KIOTA_TUTORIAL_ENABLED=false;KIOTA_OFFLINE_ENABLED=true" />
<!-- Post-process output files -->
<KiotaPatchGeneratedCodeFiles StartDirectory="%(KiotaReference.OutputPath)" />
</Target>
</Project>