Skip to content

Instantly share code, notes, and snippets.

@rcollette
Created May 31, 2019 18:33
Show Gist options
  • Save rcollette/03bdcc49ef7021cff4603723037bcf1b to your computer and use it in GitHub Desktop.
Save rcollette/03bdcc49ef7021cff4603723037bcf1b to your computer and use it in GitHub Desktop.
An opinionated bash script for creating a .NET Core webapi (microservice) solution.
#!/bin/bash
set -e
set -x
export SOLUTIONNAME=${1:-"SolutionName"}
export FRAMEWORK=${2:-"netcoreapp2.2"}
export LANGVERSION=${3:-"7.3"}
add_project() {
if [[ "${1}" = "classlib" ]]
then
dotnet new classlib --name "${2}" --framework "${FRAMEWORK}" --langVersion "${LANGVERSION}"
else
dotnet new "${1}" --name "${2}" --framework "${FRAMEWORK}"
fi
dotnet new xunit --name "${2}.Test" --framework "${FRAMEWORK}"
cd "${2}.Test"
dotnet add reference "../${2}"
dotnet add package FluentAssertions
dotnet add package Moq
dotnet add package StyleCop.Analyzers
dotnet add package xunit
dotnet add package xunit.runner.visualstudio
if [[ "${1}" = "webapi" ]]
then
dotnet add package Microsoft.AspNetCore.App
fi
cd ..
dotnet sln add "${2}"
dotnet sln add "${2}.Test"
if [[ "${1}" = "webapi" ]]
then
dotnet new xunit --name "${2}.IntegrationTest" --framework "${FRAMEWORK}"
cd "${2}.IntegrationTest"
dotnet add reference "../${2}"
dotnet add package FluentAssertions
dotnet add package Moq
dotnet add package StyleCop.Analyzers
dotnet add package xunit
dotnet add package xunit.runner.visualstudio
dotnet add package Microsoft.AspNetCore.App
dotnet add package Microsoft.AspNetCore.Mvc.Testing
cd ..
dotnet sln add "${2}.IntegrationTest"
fi
}
mkdir "${SOLUTIONNAME}"
cd "${SOLUTIONNAME}"
dotnet new sln
add_project webapi "${SOLUTIONNAME}.Web" #Web tier
add_project classlib "${SOLUTIONNAME}.Service" #Domain/Business Logic tier
add_project classlib "${SOLUTIONNAME}.Repository" #Data Access Tier
add_project classlib "${SOLUTIONNAME}.Security" #Security Tier
add_project classlib "${SOLUTIONNAME}.HttpClient" # Api Access
#Add dependencies to web project
cd "${SOLUTIONNAME}.Web"
dotnet add reference "../${SOLUTIONNAME}.Service"
dotnet add reference "../${SOLUTIONNAME}.Security"
# Normally the Web tier should not reference the data access tier directly, but we need to reference exception types
# for mapping from an exception to an HTTP error response.
dotnet add reference "../${SOLUTIONNAME}.Repository"
dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection
dotnet add package Microsoft.AspNetCore.Mvc.Api.Analyzers
dotnet add package Newtonsoft.Json
dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Filters.Expressions
dotnet add package Serilog.Formatting.Compact
dotnet add package Serilog.Settings.Configuration
dotnet add package Serilog.Sinks.Console
dotnet add package Serilog.Sinks.SumoLogic
dotnet add package StyleCop.Analyzers
dotnet add package Swashbuckle.AspNetCore
dotnet add package Swashbuckle.AspNetCore.SwaggerGen
dotnet add package Swashbuckle.AspNetCore.SwaggerUI
#Add dependencies to service project
cd "../${SOLUTIONNAME}.Service"
dotnet add package Automapper
dotnet add reference "../${SOLUTIONNAME}.Repository"
#Add dependencies to repository project
cd "../${SOLUTIONNAME}.Repository"
dotnet add package AutoMapper
dotnet add package EntityFramework
#Add dependencies to HttpClient project
cd "../${SOLUTIONNAME}.HttpClient"
dotnet add package Flurl.Http
dotnet add package AutoMapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment