Skip to content

Instantly share code, notes, and snippets.

@btastic
Last active October 11, 2018 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btastic/cc84c7b9418be66db7fd78a49aebcf62 to your computer and use it in GitHub Desktop.
Save btastic/cc84c7b9418be66db7fd78a49aebcf62 to your computer and use it in GitHub Desktop.
A simple script to generate a new dotnet project with a default project hierarchy
param(
[Parameter(Mandatory = $true)]
[string]$ProjectName,
[Parameter(Mandatory = $true)]
[string]$SolutionName,
[Parameter(Mandatory = $true)]
[string]$DotNetTemplateName
)
$ErrorActionPreference = "Stop"
Function Get-Gitignore {
param(
[Parameter(Mandatory = $true)]
[string[]]$list,
[Parameter(Mandatory = $true)]
[string] $path
)
$params = ($list | ForEach-Object { [uri]::EscapeDataString($_) }) -join ","
Invoke-WebRequest -Uri "https://www.gitignore.io/api/$params" | Select-Object -ExpandProperty content | Out-File -FilePath $(Join-Path -path $path -ChildPath ".gitignore") -Encoding ascii
}
Write-Host "Creating project $SolutionName"
if(Test-Path $SolutionName) {
Write-Error "Project folder does already exists. $SolutionName"
exit
}
# Folder structure
mkdir $SolutionName
$ProjectDir = Get-Item .\$SolutionName
mkdir $ProjectDir\src
mkdir $ProjectDir\tests
$SourceDir = Get-Item $ProjectDir\src
$TestsDir = Get-Item $ProjectDir\tests
# Default items
New-Item $ProjectDir\README.md
New-Item $ProjectDir\LICENSE
New-Item $TestsDir\.gitkeep
# .gitignore for visual studio
Get-Gitignore visualstudio $ProjectDir
# dotnet stuff
dotnet new $DotNetTemplateName -n $ProjectName -o $SourceDir\$ProjectName
dotnet new sln -o $ProjectDir\src -n $SolutionName
dotnet sln $SourceDir add $SourceDir\$ProjectName\
dotnet build $SourceDir\$ProjectName
# git
git init $ProjectDir
git -C $ProjectDir\ add .
git -C $ProjectDir\ commit -m "Initial project creation"
#prompt to open the solution
$reply = Read-Host -Prompt "Open solution now? [y/n]"
if ( $reply -match "[yY]" ) {
Invoke-Item $SourceDir\$SolutionName.sln
}
@btastic
Copy link
Author

btastic commented Oct 11, 2018

Usage:

.\New-DotNet ProjectName SolutionName DotNetTemplate

ProjectName = your project name
DotNetTemplate = the template name for dotnet scaffolding (https://docs.microsoft.com/de-de/dotnet/core/tools/dotnet-new?tabs=netcore21#arguments)
SolutionName = name of the solution

Example:
.\New-DotNet CoolProject.Api CoolProject webapi

Generates:
2018-10-12_00h19_00

2018-10-12_00h17_32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment