简体   繁体   English

生成 NSwag 客户端作为构建的一部分

[英]Generate NSwag client as part of the build

I have a project that uses NSwag to generate a client and the contracts from a swagger file.我有一个项目,它使用 NSwag 从 swagger 文件生成客户端和合同。 I don't want these generated files to be tracked by git, so that when the project is built on the build server, it generates them as part of the build.我不希望 git 跟踪这些生成的文件,因此当项目在构建服务器上构建时,它会将它们作为构建的一部分生成。

I've been playing around with MSBuild targets to try getting this to work, and it generates the files but then subsequently fails the build, because there's some other classes that reference the generated classes.我一直在尝试使用 MSBuild 目标来尝试让它工作,它生成文件但随后构建失败,因为还有一些其他类引用了生成的类。

This is what I have in the csproj file at the moment:这是我目前在 csproj 文件中的内容:

<Target Name="NSwag" BeforeTargets="BeforeBuild;BeforeRebuild">
    <Exec Command="$(NSwagExe_Core21) run nswag.json /variables:Configuration=$(Configuration)" IgnoreExitCode="true" />
</Target>

Is this possible somehow?这有可能吗?

Edit编辑

Nswag spec here: 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"
    }
  }
}

I manage to resolve same issue playing with clean target and include sources...我设法解决了使用干净目标播放的相同问题并包含源...

<!--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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM