Makefile and .NET Core

building-blocks-456616_1280

Overview

In this post you will learn what is build script or build tool, what products are out there and why it’s helpful during whole life cycle of you application.

It it a part of “Get Noticed!” series, check out how to create your first .NET Core application. If you already know, check out this repository

What is build script ?

Build script tools automate certain task that you have to do repeatedly with your
code, data store, application and other tools.
For example if you would like to get a clean run of our application (.NET Core).
You have to invoke four commands – dotnet clean, dotnet restore, dotnet build, dotnet run that is pretty irritating if you have to type that N times, over and over again.

What if you would like to get clean database each run ? Or let’s say that you would like to patch some meta data file with version every time when you publish new version of application or library, again very problematic.

You don’t really want to do it every time by hand, to be honest, sometimes you cant!
Like if your publishing or deploying process is handled by external machine, then it’s impossible to do such a step manually. That’s why we want to script it!

You will see later that you can automate various things, not only building your application but further steps in life cycle of your project

What is make ?

 

GNU Make is a tool which controls the generation of executables
and other non-source files of a program from the program’s source files.

(source)

In simple way, GNU Make allows you to describe to create executable file, but you will see that not only!

Where that can be useful ?

Project building

We mentioned above that getting a clean run, required from us a lot of commands typing, OK but hold on! Let’s create Makefile.

That way you can describe how to build all your projects. As you can see we just simply wrap up our well known .NET building tool commands with Make clean, restore, build, run .

Moreover we chained up those with one command run-clean, so now if you run make run-clean Make should run all other commands in order, at the end you should get running application, isn’t simpler ?

Make and Docker

I assume that you know what Docker is. If not there will be post coming in next few days, so stay tuned!

Let’s say that your application depend on PostgresSQL, for development you want to have clean database each time you run your application.

As you can see we added three new steps in our Makefile. run-with-db ,database-cleanup and  database-prep , again we just wrap up some well known docker commands, like stop and remove container or pull image and run container. At the end we chained up to run-with-db so that clean database will be prepared and then our application  will be run.

It’s not end!

Those two cases are just few where you can use Makefile to make your life easier. Stay tuned there will be more scenarios where you can use build tools.

Make is not just one tool out there, check out also FAKE, CAKE.

Setting up .NET Core project

Overview

This tutorial will work on most of OS, but I encourage you to get a Ubuntu, to go out of your comfort zone. It’s very easy to start with Linux. Working with console is very important to me, so that all work is done via command line.

Article is part of “Get Noticed!”, project squadron, so we will try to recreate what I have already done to Squadron.TestApplication repository

Install .NET Core

Installation of .NET Core is super easy, and I don’t want to copy what is already written so check out dotnet core website, and follow their steps. If you have any problems let me know in a comment below, I will try to help you.

Validating of installation

Once you installed framework, open command line and invoke

dotnet --version

you should get at least 1.0.1 (depend what time you are reading this article)

dotnetversionresult

Create folder structure

Main difference between standard .NET framework tooling and .NET Core. Previously you didn’t have to have folder structure, your projects could live in different places in your system. That information was stored in solution file.

.NET Core moved towards folder structure where you are not forced to have solution file that keep all information about your projects, so that you can open it in different IDEs or editors.
Let’s kick off with creating a directory structure, you can download full script here
cd ~/
mkdir Squadron.TestApplication
cd Squadron.TestApplication
mkdir src
cd src

Convention says that you should have two folders src and tests placed at the top folder of your project. For a now we just stick with src folder, because we are not going to create any test project yet

Initializing project

Make sure that you are inside src folder, then invoke in order.

mkdir Squadron.TestApplication
cd Squadron.TestApplication
dotnet new console

That will initialize your first project.

Base commands

There are 4 base commands that you have to know, for more commands just invoke dotnet --help in console.

  • dotnet new, initialize new project and create base files
  • dotnet restore, restore all packages
  • dotnet build, compile your project
  • dotnet run, run your project

After you initialized your new project run those commands in a order

  • dotnet restore
  • dotnet build
  • dotnet run

After last command you should get your first “Hello world”

What is next ?

Stay tuned in the next post will carry on this project and implement API based on ASP.NET Core