<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>C# on Huy Minh Ha</title><link>https://minhhh.github.io/tags/c%23/</link><description>Recent content in C# on Huy Minh Ha</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sun, 11 Sep 2016 00:00:00 +0700</lastBuildDate><atom:link href="https://minhhh.github.io/tags/c%23/index.xml" rel="self" type="application/rss+xml"/><item><title>Autoformat C# code from command line</title><link>https://minhhh.github.io/posts/autoformat-csharp-code-from-command-line/</link><pubDate>Sun, 11 Sep 2016 00:00:00 +0700</pubDate><guid>https://minhhh.github.io/posts/autoformat-csharp-code-from-command-line/</guid><description>&lt;p&gt;We all need a tool to format code automatically according to project&amp;rsquo;s convention. Using an IDE like Visual Studio, Xamarin or MonoDevelop, we can format code easily. However, it&amp;rsquo;s not always convenient to fireup the IDE just for formatting some code, and also having a tool on the command line makes it easier to integrate with other workflows. Therefore, it&amp;rsquo;s great if we have some tool which runs on the command line to auto format our code.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Requirements&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Customizable format options&lt;/li&gt;
&lt;li&gt;Format whole directory recursively&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The second requirement is relatively easy to implement if we have a tool to format a single file on the commandline because we can just use &lt;code&gt;find&lt;/code&gt; with some filter to iteratively apply the tool to a bunch of files.&lt;/p&gt;
&lt;h2 id="solution-1-astyle"&gt;Solution 1: Astyle
&lt;/h2&gt;&lt;p&gt;&lt;a class="link" href="http://astyle.sourceforge.net/" target="_blank" rel="noopener"
 &gt;Artistic Style&lt;/a&gt; is a source code indenter, formatter, and beautifier for the C, C++, C++/CLI, Objective‑C, C# and Java programming languages. We can download and install Astyle rather quickly without any problems on most platforms.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt;
We will use a simple script below to help us install AStyle on Mac OSX. Installation on other platforms can be done in a similar manner&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;
After we have &lt;code&gt;astyle&lt;/code&gt; available on the command line, applying it for our project is a matter of wrapping everything in a single Make command like so&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;# Supposed our Code is in the Code folder
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;format:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;	find Code -iname &amp;#34;*.cs&amp;#34; -not -path &amp;#34;Code/excludedpath/*&amp;#34; | xargs -n 1 -I {} bash -c &amp;#34;astyle --options=.astylerc \&amp;#34;{}\&amp;#34;&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Remember to create a file called &lt;code&gt;.astylerc&lt;/code&gt; and put whatever format options you want to customize for your project there.&lt;/p&gt;
&lt;h2 id="solution-2-nrefactory"&gt;Solution 2: NRefactory
&lt;/h2&gt;&lt;p&gt;&lt;a class="link" href="https://github.com/icsharpcode/NRefactory" target="_blank" rel="noopener"
 &gt;NRefactory&lt;/a&gt; is the C# analysis library used in the SharpDevelop and MonoDevelop IDEs. We can write a command line client with our customized parameters and format options, which uses &lt;code&gt;NRefactory&lt;/code&gt; internally to format code.&lt;/p&gt;
&lt;p&gt;TODO: Write a cli using &lt;code&gt;docopt&lt;/code&gt;&lt;/p&gt;</description></item></channel></rss>