我有一个项目,该项目使用NSwag来生成客户端和来自swagger文件的合同。我不希望git跟踪这些生成的文件,以便在构建服务器上构建项目时,它将生成它们作为构建的一部分。
我一直在尝试使用MSBuild目标,以使其正常工作,它会生成文件,但随后构建失败,因为还有其他一些类引用了生成的类。
这是我目前在csproj文件中的内容:
<Target Name="NSwag" BeforeTargets="BeforeBuild;BeforeRebuild">
<Exec Command="$(NSwagExe_Core21) run nswag.json /variables:Configuration=$(Configuration)" IgnoreExitCode="true" />
</Target>
这有可能吗?
修改
此处是Nswag规范:
{
"runtime": "NetCore21",
"defaultVariables": null,
"swaggerGenerator": {
"fromSwagger": {
"url": "swagger.json",
"output": null
}
},
"codeGenerators": {
"swaggerToCSharpClient": {
"clientBaseClass": "ClientBase", //name of your client base class
"configurationClass": null,
"generateClientClasses": true,
"generateClientInterfaces": true,
"generateDtoTypes": true,
"injectHttpClient": true,
"disposeHttpClient": true,
"protectedMethods": [],
"generateExceptionClasses": true,
"exceptionClass": "SwaggerException",
"wrapDtoExceptions": true,
"useHttpClientCreationMethod": false,
"httpClientType": "System.Net.Http.HttpClient",
"useHttpRequestMessageCreationMethod": true, //allows you to add headers to each message
"useBaseUrl": true,
"generateBaseUrlProperty": true,
"generateSyncMethods": false,
"exposeJsonSerializerSettings": false,
"clientClassAccessModifier": "internal", //make client generated client implementations internal
"typeAccessModifier": "public", //make your models and client interfaces public
"generateContractsOutput": true,
"contractsNamespace": "MyNamspace.Client.Contracts",
"contractsOutputFilePath": "Contracts.g.cs",
"parameterDateTimeFormat": "s",
"generateUpdateJsonSerializerSettingsMethod": true,
"serializeTypeInformation": false,
"queryNullValue": "",
"className": "CorvetteClient",
"operationGenerationMode": "MultipleClientsFromOperationId",
"additionalNamespaceUsages": [],
"additionalContractNamespaceUsages": [],
"generateOptionalParameters": false,
"generateJsonMethods": true,
"enforceFlagEnums": false,
"parameterArrayType": "System.Collections.Generic.IEnumerable",
"parameterDictionaryType": "System.Collections.Generic.IDictionary",
"responseArrayType": "System.Collections.Generic.ICollection",
"responseDictionaryType": "System.Collections.Generic.IDictionary",
"wrapResponses": false,
"wrapResponseMethods": [],
"generateResponseClasses": true,
"responseClass": "SwaggerResponse",
"namespace": "MyNamespace.Client",
"requiredPropertiesMustBeDefined": true,
"dateType": "System.DateTimeOffset",
"jsonConverters": null,
"dateTimeType": "System.DateTimeOffset",
"timeType": "System.TimeSpan",
"timeSpanType": "System.TimeSpan",
"arrayType": "System.Collections.Generic.ICollection",
"arrayInstanceType": "System.Collections.ObjectModel.Collection",
"dictionaryType": "System.Collections.Generic.IDictionary",
"dictionaryInstanceType": "System.Collections.Generic.Dictionary",
"arrayBaseType": "System.Collections.ObjectModel.Collection",
"dictionaryBaseType": "System.Collections.Generic.Dictionary",
"classStyle": "Poco",
"generateDefaultValues": true,
"generateDataAnnotations": true,
"excludedTypeNames": [],
"handleReferences": false,
"generateImmutableArrayProperties": false,
"generateImmutableDictionaryProperties": false,
"jsonSerializerSettingsTransformationMethod": null,
"inlineNamedDictionaries": false,
"inlineNamedTuples": true,
"templateDirectory": null,
"typeNameGeneratorType": null,
"propertyNameGeneratorType": null,
"enumNameGeneratorType": null,
"serviceHost": null,
"serviceSchemes": null,
"output": "Client.g.cs"
}
}
}
答案 0 :(得分:0)
我设法解决了使用干净目标的相同问题,并包含了源代码...
<!--Custom task to generate source code from OpenApi Specification before compilation-->
<Target Name="GenerateSources" BeforeTargets="Compile">
<Exec Command="$(NSwagExe_Core31) run config.nswag" />
</Target>
<!--Custom task to remove generate source code before clean project-->
<Target Name="RemoveGenerateSources" BeforeTargets="CoreClean">
<RemoveDir Directories="bin/debug/GeneratedSources" />
</Target>
<!--Register generated source code as project source code-->
<ItemGroup>
<Compile Include="bin/debug/GeneratedSources/**/*.cs" />
</ItemGroup>