Compiling .NET projects in Sublime Text

February 7, 2013 – sublime text

The subject of this post is the compilation of .NET projects and solutions in Sublime Text using the Build Systems feature.

Sublime Text supports custom Build Systems.

By adding MSBuild as a new Build System, it is possible to build Visual Studio projects and solutions without the Visual Studio IDE installed.

Adding the Source Code

The following steps require a folder with MSBuild project files. In order to be pragmatic, the custom Build System is going to compile the source code of AutoFixture.

Go to File menu, click Open Folder... and select the root folder of the project to be compiled.

Adding the new Build System

Go to Tools menu, Build System, and click New Build System...

Paste the following code to a file:

<pre><code class='json'>{
&quot;cmd&quot;: [&quot;C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe&quot;, &quot;AutoFixture.sln&quot;],
&quot;working_dir&quot;: &quot;${project_path:${folder:${file_path}}}\\Src&quot;

}

Save the file as AutoFixture.sublime-build.

Go to Tools menu, Build System, and select AutoFixture.

The above steps are per MBSuild project file. Repeat the above steps by creating a Build System per MSBuild project file.

Running the Build

Go to Tools menu, and select Build. Alternatively, use the Ctrl+B command.

The compiler detects a missing semicolon.

In the above screenshot, on line 38 a semicolon has been intentionally removed for the demo. After running a build, the compiler detects the missing semicolon.

What about the Tests?

Currently, the easiest way to run the tests is by using a command-line interface version of the test runner.

On my machine, attempting to add a new Build System for the tests resulted in high memory usage. This has been tested while Sublime Text 3 was in beta (Build 3010).

Commit to Revision Control with Care

January 1, 2013 – software

We should always commit to revision control with care.

Often, developers provide commit messages that are incomplete, lowercase, with orthographic permutations. Even worse, snapshots can contain code that doesn’t even compile.

In a signed document the rules and regulations can be altered and then another document is going to be created and signed, but the original document will always remain signed.

As signatures in documents can’t be erased so do recorded snapshots, at least it is not always an easy task.

After a snapshot has been recorded it acts like a signature for the developer – should we ever put our signature roughly?

ReSharper - The Good Parts (Static Code Analysis)

December 6, 2012 – microsoft visual studio

What I really like about ReSharper is the Static code analysis, and the quick-fixes. What I really don’t like is pretty much everything else.

Keeping only the Static code analysis

Go to ReSharper menu and clik Options…

In the Environment > General tab uncheck:

ReSharper, Options, Environment, General

In the Environment > Keyboard & Menus tab select:

ReSharper, Options, Environment, Keyboard and Menus

In the Environment > Editor tab uncheck:

From Braces and Parentheses uncheck:

ReSharper, Options, Environment, Editor

In the Environment > IntelliSense > General tab select:

ReSharper, Options, Environment, IntelliSense General

Uncheck everything in the IntelliSense > Completion Behavior tab.

ReSharper, Options, Environment, IntelliSense Completion Behavior

In the Environment > IntelliSense > Completion Appearance tab select:

ReSharper, Options, Environment, IntelliSense Completion Appearance

In the Environment > IntelliSense > Parameter Info tab select:

ReSharper, Options, Environment, IntelliSense Parameter Info

Enable Visual Studio IntelliSense

Since we customized ReSharper to use Visual Studio IntelliSense, we have to manually enable it from Visual Studio options.

In Visual Studio, go to Tools menu and click Options…

In the Text Editor > C# tab select:

Visual Studio, Tools, Options, Text Editor, C#

Suspending ReSharper

There are some projects where I don’t even want to have ReSharper’s Static code analysis and the quick-fixes.

In Visual Studio, go to Tools menu and click Options…

In the ReSharper tab click Suspend.

Visual Studio, Tools, Options, ReSharper

Empty ASP.NET Web API Project Template

November 2, 2012 – asp.net web api

Usually, every time you create a new Web API project you:

  1. Delete almost all the generated code.
  2. Uninstall NuGet packages that you don’t need (yet, or at all).
  3. Remove unused assembly references.
  4. Manually edit the Web.config file(s) to remove the elements that point to the assemblies which have been removed.

Solution

Use the Empty ASP.NET Web API Project Template. The Visual Studio extension can be downloaded from here.

It will add a new project template Empty Web API which includes the following:

Files

Assemblies

NuGet packages

Remarks

The Newtonsoft.Json package is included because it is lazy loaded inside the System.Net.Http.Formatting.JsonMediaTypeFormatter type.

If you completely remove the Newtonsoft.Json assembly a System.IO.FileNotFoundException exception is thrown with message “Could not load file or assembly ‘Newtonsoft.Json, ..’ or one of its dependencies. The system cannot find the file specified.”

A favicon.ico file is included because the browser requests it so it’s better not to respond with a 404 Not Found.

Exercising the SUT asynchronously

October 28, 2012 – unit testing

In unit testing, there are times were the SUT has to be exercised asynchronously.

How can we wait for the exercise to complete execution?

The SpinWait synchronization type contains a method named SpinUntil which works perfect for the described scenario.

<pre><code class='csharp'>// Fixture setup

var sut = new ObjectLocalStorage(); sut.Set(@object, expected); object result = null;

// Exercise system new Task(() => result = sut.Get(@object)).Start(); SpinWait.SpinUntil(() => result != null);

// Verify outcome Assert.Equal(expected, result); // Teardown

There is also a SpinUntil overload accepting a TimeSpan timeout.

Truly Constrained Non-Deterministic Numbers in AutoFixture

October 8, 2012 – autofixture

Numbers in AutoFixture are currently created using a strictly monotonically increasing sequence.

<pre><code class='csharp'>var fixture = new Fixture();

var i = fixture.CreateAnonymous<int>(); // Prints –> 1 var l = fixture.CreateAnonymous<long>(); // Prints –> 2 var f = fixture.CreateAnonymous<float>(); // Prints –> 3.0

Starting with version 2.13.0, by applying a specific customization numbers can also be created using a constrained non-deterministic sequence. The new customization is called RandomNumericSequenceCustomization.

<pre><code class='csharp'>var fixture = new Fixture()
.Customize(new RandomNumericSequenceCustomization());

var i = fixture.CreateAnonymous<int>(); // Prints –> 122 var l = fixture.CreateAnonymous<long>(); // Prints –> 38 var f = fixture.CreateAnonymous<float>(); // Prints –> 147.0

Once the customization has been applied to a Fixture instance subsequent requests for numeric types will yield random non-repeatable numbers in the range of [1, 255]. When requesting more than 255 numbers the range is automatically changed to [256, 32767] and so on.

The default ranges are [1, 255], [256, 32767], and [32768, 2147483647].

Supplying a custom range

To supply a custom range, customize an instance of the Fixture class with an instance of the RandomNumericSequenceGenerator and pass to its constructor a sequence of integer numbers (e.g. -100, 100, 255).

The sequence must be two positive or negative numbers optionally followed by a series of greater numbers.

<pre><code class='csharp'>var fixture = new Fixture();

fixture.Customizations.Add(

new RandomNumericSequenceGenerator(-100, 100, 255));

var i = fixture.CreateAnonymous<int>(); // Prints –> -95 var l = fixture.CreateAnonymous<long>(); // Prints –> 47 var f = fixture.CreateAnonymous<float>(); // Prints –> -82.0

After applying the customization, numbers are now created in the range of [-100, 100]. However, when requesting more numbers than the range size the range is automatically changed to [101, 255].

« Previous Entries | Next Entries »