Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhelle committed Dec 9, 2024
1 parent e8e43bf commit 0a02017
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 2 deletions.
156 changes: 156 additions & 0 deletions articles/msbuild.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> </title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="title" content=" ">


<link rel="icon" href="../favicon.ico">
<link rel="stylesheet" href="../public/docfx.min.css">
<link rel="stylesheet" href="../public/main.css">
<meta name="docfx:navrel" content="../toc.html">
<meta name="docfx:tocrel" content="toc.html">




<meta name="docfx:docurl" content="https://github.com/christianhelle/refitter/blob/main/docs/docfx_project/articles/msbuild.md/#L1">
<meta name="loc:inThisArticle" content="In this article">
<meta name="loc:searchResultsCount" content="{count} results for &quot;{query}&quot;">
<meta name="loc:searchNoResults" content="No results for &quot;{query}&quot;">
<meta name="loc:tocFilter" content="Filter by title">
<meta name="loc:nextArticle" content="Next">
<meta name="loc:prevArticle" content="Previous">
<meta name="loc:themeLight" content="Light">
<meta name="loc:themeDark" content="Dark">
<meta name="loc:themeAuto" content="Auto">
<meta name="loc:changeTheme" content="Change theme">
<meta name="loc:copy" content="Copy">
<meta name="loc:downloadPdf" content="Download PDF">

<script type="module" src="./../public/docfx.min.js"></script>

<script>
const theme = localStorage.getItem('theme') || 'auto'
document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme)
</script>

</head>

<body class="tex2jax_ignore" data-layout="" data-yaml-mime="">
<header class="bg-body border-bottom">
<nav id="autocollapse" class="navbar navbar-expand-md" role="navigation">
<div class="container-xxl flex-nowrap">
<a class="navbar-brand" href="../index.html">
<img id="logo" class="svg" src="../images/logo.png" alt="">

</a>
<button class="btn btn-lg d-md-none border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navpanel" aria-controls="navpanel" aria-expanded="false" aria-label="Toggle navigation">
<i class="bi bi-three-dots"></i>
</button>
<div class="collapse navbar-collapse" id="navpanel">
<div id="navbar">
</div>
</div>
</div>
</nav>
</header>

<main class="container-xxl">
<div class="toc-offcanvas">
<div class="offcanvas-md offcanvas-start" tabindex="-1" id="tocOffcanvas" aria-labelledby="tocOffcanvasLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#tocOffcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<nav class="toc" id="toc"></nav>
</div>
</div>
</div>

<div class="content">
<div class="actionbar">
<button class="btn btn-lg border-0 d-md-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#tocOffcanvas" aria-controls="tocOffcanvas" aria-expanded="false" aria-label="Show table of contents">
<i class="bi bi-list"></i>
</button>

<nav id="breadcrumb"></nav>
</div>

<article data-uid="">

<h2 id="msbuild">MSBuild</h2>
<p>A common scenario for generating code from OpenAPI specifications is to do it at build time. This can be achieved using MSBuild tasks. An example of such an approach would be to include a <code>.refitter</code> file in the projects directory and execute the Refitter CLI from pre-build events</p>
<pre><code class="lang-xml">&lt;Target Name=&quot;Refitter&quot; AfterTargets=&quot;PreBuildEvent&quot;&gt;
&lt;Exec WorkingDirectory=&quot;$(ProjectDir)&quot; Command=&quot;refitter --settings-file .refitter --skip-validation&quot; /&gt;
&lt;/Target&gt;
</code></pre>
<p>The snippet above requires that Refitter is installed on the machine as a globally available dotnet tool. This might not be the case if you're running on a build agent from a CI/CD environment. In this case you might want to install Refitter as a local tool using a manifest file, as described in <a href="https://learn.microsoft.com/en-us/dotnet/core/tools/local-tools-how-to-use?WT.mc_id=DT-MVP-5004822">this tutorial</a></p>
<pre><code class="lang-xml">&lt;Target Name=&quot;Refitter&quot; AfterTargets=&quot;PreBuildEvent&quot;&gt;
&lt;Exec WorkingDirectory=&quot;$(ProjectDir)&quot; Command=&quot;dotnet tool restore&quot; /&gt;
&lt;Exec WorkingDirectory=&quot;$(ProjectDir)&quot; Command=&quot;refitter --settings-file .refitter --skip-validation&quot; /&gt;
&lt;/Target&gt;
</code></pre>
<p>The <code>dotnet build</code> process does will probably not have access to the package repository in which to download Refitter from, this is at least the case with Azure Pipelines and Azure Artifacts. To workaround this, you can provide a separate <code>nuget.config</code> that only uses <code>nuget.org</code> as a <code>&lt;packageSource&gt;</code>.</p>
<p>Something like this:</p>
<pre><code class="lang-xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;configuration&gt;
&lt;packageSources&gt;
&lt;clear /&gt;
&lt;add key=&quot;NuGet&quot; value=&quot;https://api.nuget.org/v3/index.json&quot; /&gt;
&lt;/packageSources&gt;
&lt;/configuration&gt;
</code></pre>
<p>You might want to place the <code>nuget.config</code> file in another folder to avoid using it to build the .NET project, then you can specify this when executing <code>dotnet tool restore</code></p>
<pre><code class="lang-xml">&lt;Target Name=&quot;Refitter&quot; AfterTargets=&quot;PreBuildEvent&quot;&gt;
&lt;Exec WorkingDirectory=&quot;$(ProjectDir)&quot; Command=&quot;dotnet tool restore --configfile refitter/nuget.config&quot; /&gt;
&lt;Exec WorkingDirectory=&quot;$(ProjectDir)&quot; Command=&quot;refitter --settings-file .refitter --skip-validation&quot; /&gt;
&lt;/Target&gt;
</code></pre>
<p>In the example above, the <code>nuget.config</code> file is placed under the <code>refitter</code> folder.</p>
<h3 id="refittermsbuild-package">Refitter.MSBuild package</h3>
<p>Refitter ships with an MSBuild custom task that is distributed as a NuGet package and includes the Refitter CLI binary. This will simplify generating code from OpenAPI specifications at build time.</p>
<p>To use the package, install <code>Refitter.MSBuild</code></p>
<pre><code class="lang-xml">&lt;ItemGroup&gt;
&lt;PackageReference Include=&quot;Refitter.MSBuild&quot; Version=&quot;1.5.0&quot; /&gt;
&lt;/ItemGroup&gt;
</code></pre>
<p>The MSBuild package includes a custom <code>.target</code> file which executes the <code>RefitterGenerateTask</code> custom task and looks something like this:</p>
<pre><code class="lang-xml">&lt;Target Name=&quot;Refitter&quot; BeforeTargets=&quot;BeforeBuild&quot;&gt;
&lt;RefitterGenerateTask ProjectFileDirectory=&quot;$(MSBuildProjectDirectory)&quot;
DisableLogging=&quot;$(RefitterNoLogging)&quot;/&gt;
&lt;ItemGroup&gt;
&lt;Compile Include=&quot;**/*.cs&quot; /&gt;
&lt;/ItemGroup&gt;
&lt;/Target&gt;
</code></pre>
<p>The <code>RefitterGenerateTask</code> task will scan the project folder for <code>.refitter</code> files and executes them all. By default, telemetry collection is enabled, and to opt-out of it you must specify <code>&lt;RefitterNoLogging&gt;true&lt;/RefitterNoLogging&gt;</code> in the <code>.csproj</code> <code>&lt;PropertyGroup&gt;</code></p>

</article>

<div class="contribution d-print-none">
<a href="https://github.com/christianhelle/refitter/blob/main/docs/docfx_project/articles/msbuild.md/#L1" class="edit-link">Edit this page</a>
</div>

<div class="next-article d-print-none border-top" id="nextArticle"></div>

</div>

<div class="affix">
<nav id="affix"></nav>
</div>
</main>


<footer class="border-top text-secondary">
<div class="container-xxl">
<div class="flex-fill">
<span>Made with <a href="https://dotnet.github.io/docfx">docfx</a></span>
</div>
</div>
</footer>
</body>
</html>
2 changes: 1 addition & 1 deletion articles/refitter-file-format.html
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ <h3 id="descriptions">Descriptions</h3>
<ul>
<li><code>baseUrl</code> - Used as the HttpClient base address. Leave this blank to manually set the base URL</li>
<li><code>httpMessageHandlers</code> - A collection of <code>HttpMessageHandler</code> that is added to the HttpClient pipeline</li>
<li><code>usePolly</code> - (DEPRECATED) Set this to true to configure the HttpClient to use Polly using a retry policy with a jittered backoff</li>
<li><code>usePolly</code> - Set this to <code>true</code> to configure the HttpClient to use Polly using a retry policy with a jittered backoff. This is <strong>DEPRECATED</strong>, use <code>transientErrorHandler</code> instead</li>
<li><code>transientErrorHandler</code>: Set this to configure transient error handling with a retry policy that uses a jittered backoff. See <a href="https://refitter.github.io/api/Refitter.Core.TransientErrorHandler.html">https://refitter.github.io/api/Refitter.Core.TransientErrorHandler.html</a></li>
<li><code>maxRetryCount</code> - This is the max retry count used in the Polly retry policy. Default is 6</li>
<li><code>firstBackoffRetryInSeconds</code> - This is the duration of the initial retry backoff. Default is 1 second</li>
Expand Down
3 changes: 3 additions & 0 deletions articles/toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<li>
<a href="source-generator.html" name="" title="Source Generator">Source Generator</a>
</li>
<li>
<a href="msbuild.html" name="" title="MSBuild">MSBuild</a>
</li>
<li>
<a href="refitter-file-format.html" name="" title=".refitter file format">.refitter file format</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion articles/toc.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

{"items":[{"name":"CLI Tool","href":"cli-tool.html","topicHref":"cli-tool.html"},{"name":"Source Generator","href":"source-generator.html","topicHref":"source-generator.html"},{"name":".refitter file format","href":"refitter-file-format.html","topicHref":"refitter-file-format.html"},{"name":"Using the generated code","href":"using-the-generated-code.html","topicHref":"using-the-generated-code.html"},{"name":"Examples","href":"examples.html","topicHref":"examples.html"},{"name":"XML Code Documentation","href":"xml-code-docs.html","topicHref":"xml-code-docs.html"}]}
{"items":[{"name":"CLI Tool","href":"cli-tool.html","topicHref":"cli-tool.html"},{"name":"Source Generator","href":"source-generator.html","topicHref":"source-generator.html"},{"name":"MSBuild","href":"msbuild.html","topicHref":"msbuild.html"},{"name":".refitter file format","href":"refitter-file-format.html","topicHref":"refitter-file-format.html"},{"name":"Using the generated code","href":"using-the-generated-code.html","topicHref":"using-the-generated-code.html"},{"name":"Examples","href":"examples.html","topicHref":"examples.html"},{"name":"XML Code Documentation","href":"xml-code-docs.html","topicHref":"xml-code-docs.html"}]}
10 changes: 10 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@
},
"version": ""
},
{
"type": "Conceptual",
"source_relative_path": "articles/msbuild.md",
"output": {
".html": {
"relative_path": "articles/msbuild.html"
}
},
"version": ""
},
{
"type": "Conceptual",
"source_relative_path": "articles/refitter-file-format.md",
Expand Down

0 comments on commit 0a02017

Please sign in to comment.