Azure DevOps, Scrum, & .NET Software Leadership and Consulting Services

Free course! Predicting the Future, Estimating, and Running Your Projects with Flow Metrics

Script: Create new ASP.NET Core Project with Class Library & Tests


Building on my post about how to do some common Solution and Project tasks using the dotnet command, here’s a sample script to create a complete solution that can be used from Visual Studio 2017 or Visual Studio Code.

I wanted to be able to create a complete solution and project structure all from the command line.  Yes yes yes, I know that I could do something like “yo aspnet” and that that would pretty much do the same thing.  Well, it only pretty much does the same thing and I wanted to be able to do everything start to finish and only using the dotnet command.  I wanted to create the solution file (*.sln), create the folder structure, create a project for the ASP.NET Core code, create a class library project that would hold the back-end code that would be used by the ASP.NET Core project, create an MSTest-based unit test project, and then create all the references between projects so that this would compile right away.  I wanted to be able to call my script, pass in an argument for the Solution Name and then have it create everything.

Assuming a script file called Create.bat and a solution name of MySolution, here’s the example call:

Create.bat MySolution

Here’s the directory structure that I wanted:

/MySolution
    MySolution.sln
  /src
    /MySolution.Api
      MySolution.Api.csproj
    /MySolution.WebUi
      MySolution.WebUi.csproj
  /test
    /MySolution.Tests
      MySolution.Tests.csproj

Here’s the batch script for doing this on Windows:

set "base=%cd%"
set "workspaceroot=%base%\%1"

echo %base%
echo %workspaceroot%


mkdir %workspaceroot%
mkdir %workspaceroot%\src
mkdir %workspaceroot%\src\%1.Api
mkdir %workspaceroot%\src\%1.WebUi

mkdir %workspaceroot%\test
mkdir %workspaceroot%\test\%1.Tests

cd %workspaceroot%\src\%1.Api

dotnet new classlib -f netcoreapp1.1

cd %workspaceroot%\src\%1.WebUi

dotnet new mvc -f netcoreapp1.1

cd %workspaceroot%\test\%1.Tests

dotnet new mstest -f netcoreapp1.1

cd %workspaceroot%

dotnet new sln

dotnet sln .\%1.sln add .\src\%1.Api\%1.Api.csproj

dotnet sln .\%1.sln add .\src\%1.WebUi\%1.WebUi.csproj

dotnet sln .\%1.sln add .\test\%1.Tests\%1.Tests.csproj

cd %workspaceroot%\src\%1.WebUi

dotnet add reference ..\%1.Api\%1.Api.csproj

cd %workspaceroot%\test\%1.Tests

dotnet add reference ..\..\src\%1.Api\%1.Api.csproj
dotnet add reference ..\..\src\%1.WebUi\%1.WebUi.csproj

cd %workspaceroot%

dotnet restore

dotnet build

Here’s the shell script for doing this on Mac OS / Linux:

#!/bin/bash        

base="$PWD"
workspaceroot="$base/$1"

echo  $workspaceroot

mkdir $workspaceroot
mkdir $workspaceroot/src
mkdir $workspaceroot/src/$1.Api
mkdir $workspaceroot/src/$1.WebUi

mkdir $workspaceroot/test
mkdir $workspaceroot/test/$1.Tests

cd $workspaceroot/src/$1.Api

dotnet new classlib -f netcoreapp1.1

cd $workspaceroot/src/$1.WebUi

dotnet new mvc -f netcoreapp1.1

cd $workspaceroot/test/$1.Tests

dotnet new mstest -f netcoreapp1.1

cd $workspaceroot

dotnet new sln

dotnet sln ./$1.sln add ./src/$1.Api/$1.Api.csproj

dotnet sln ./$1.sln add ./src/$1.WebUi/$1.WebUi.csproj

dotnet sln ./$1.sln add ./test/$1.Tests/$1.Tests.csproj

cd $workspaceroot/src/$1.WebUi

dotnet add reference ../$1.Api/$1.Api.csproj

cd $workspaceroot/test/$1.Tests

dotnet add reference ../../src/$1.Api/$1.Api.csproj
dotnet add reference ../../src/$1.WebUi/$1.WebUi.csproj

cd $workspaceroot

dotnet restore

dotnet build

Download the scripts

Here’s the script for Windows.  Here’s the script for Mac OS & Linux.

-Ben

SUBSCRIBE TO THE BLOG


2 responses to “Script: Create new ASP.NET Core Project with Class Library & Tests”

  1. […] few days ago, I blogged about how to create a solution and project structure entirely from the command line.  We’ll start by using my create script from that blog […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.