Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active March 9, 2018 04:51
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 stknohg/3d86a34c0f3156c12f3c611cfbb15cef to your computer and use it in GitHub Desktop.
Save stknohg/3d86a34c0f3156c12f3c611cfbb15cef to your computer and use it in GitHub Desktop.
.NET Coreで単体テスト付きのプロジェクト(クラスライブラリ)を生成するスクリプト
# see : https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-dotnet-test
function New-CoreXUnitProject {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[string]$ProjectName
)
# Validations
try {
[void](Get-Command -Name 'dotnet' -ErrorAction Stop)
} catch {
Write-Error 'Failed to find dotnet command.'
return
}
$currentDir = Get-Location -PSProvider FileSystem
Push-Location -Path $currentDir
try {
# create solution root directory
$rootPath = Join-Path $currentDir $ProjectName
if (Test-Path $rootPath) {
Write-Error ("{0} is already exists." -f $rootPath)
return
}
[void](mkdir -Path $rootPath -Force)
# create solution
$currentDir = $rootPath
Set-Location $currentDir
dotnet new sln
# create source project
[void](mkdir -Path (Join-Path $currentDir $ProjectName) -Force)
Set-Location (Join-Path $currentDir $ProjectName)
dotnet new classlib
Set-Location $currentDir
dotnet sln add (Join-Path $currentDir ("{0}\{0}.csproj" -f $ProjectName))
# create test project
$testProjectName = "{0}.Tests" -f $ProjectName
[void](mkdir -Path (Join-Path $currentDir $testProjectName) -Force)
Set-Location (Join-Path $currentDir $testProjectName)
dotnet new xunit
dotnet add reference (Join-Path $currentDir ("{0}\{0}.csproj" -f $ProjectName))
Set-Location $currentDir
dotnet sln add (Join-Path $currentDir ("{0}\{0}.csproj" -f $testProjectName))
} finally {
Pop-Location
}
}
# usage :
# cd [rootdir]
# New-CoreXUnitProject -PorjectName Sample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment