Skip to content

Commit

Permalink
Stop using mkdir -Force
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaykul committed Nov 1, 2017
1 parent 08df6bf commit 66af84a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
31 changes: 27 additions & 4 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ function init {
# Calculate Paths
# The output path is just a temporary output and logging location
$Script:OutputPath = Join-Path $Path output
$null = mkdir $OutputPath -Force

if(Test-Path $OutputPath -PathType Leaf) {
throw "Cannot create folder for Configuration because there's a file in the way at '$OutputPath'"
}
if(!(Test-Path $OutputPath -PathType Container)) {
$null = New-Item $OutputPath -Type Directory -Force
}

# We expect the source for the module in a subdirectory called one of three things:
$Script:SourcePath = "src", "source", ${ModuleName} | ForEach { Join-Path $Path $_ -Resolve -ErrorAction Ignore } | Select -First 1
Expand Down Expand Up @@ -148,7 +154,12 @@ function update {
# force reinstall by cleaning the old ones
remove-item $Path\packages\ -Recurse -Force
}
$null = mkdir $Path\packages\ -Force
if(Test-Path $Path\packages\ -PathType Leaf) {
throw "Cannot create folder for Configuration because there's a file in the way at '$Path\packages\'"
}
if(!(Test-Path $Path\packages\ -PathType Container)) {
$null = New-Item $Path\packages\ -Type Directory -Force
}

# Remember, as of now, only nuget actually supports the -Destination flag
foreach($Package in ([xml](gc .\packages.config)).packages.package) {
Expand Down Expand Up @@ -210,7 +221,13 @@ function build {
$RootModule = "${ModuleName}.psm1"
}
}
$null = mkdir $ReleasePath -Force
if(Test-Path $ReleasePath -PathType Leaf) {
throw "Cannot create folder for Configuration because there's a file in the way at '$ReleasePath'"
}
if(!(Test-Path $ReleasePath -PathType Container)) {
$null = New-Item $ReleasePath -Type Directory -Force
}

$ReleaseModule = Join-Path $ReleasePath ${RootModule}
Trace-Message " Setting content for $ReleaseModule"

Expand Down Expand Up @@ -244,7 +261,13 @@ function build {
$ReadMe = Join-Path $Path Readme.md
if(Test-Path $ReadMe -PathType Leaf) {
$LanguagePath = Join-Path $ReleasePath $DefaultLanguage
$null = mkdir $LanguagePath -Force
if(Test-Path $LanguagePath -PathType Leaf) {
throw "Cannot create folder for Configuration because there's a file in the way at '$LanguagePath'"
}
if(!(Test-Path $LanguagePath -PathType Container)) {
$null = New-Item $LanguagePath -Type Directory -Force
}

$about_module = Join-Path $LanguagePath "about_${ModuleName}.help.txt"
if(!(Test-Path $about_module)) {
Trace-Message "Turn readme into about_module"
Expand Down
9 changes: 6 additions & 3 deletions Source/Configuration.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ function Get-StoragePath {
$PathRoot = Join-Path $PathRoot $Version
}

# Write-Debug "Storage Path: $PathRoot"

if(Test-Path $PathRoot -PathType Leaf) {
throw "Cannot create folder for Configuration because there's a file in the way at $PathRoot"
}
if(!(Test-Path $PathRoot -PathType Container)) {
$null = New-Item $PathRoot -Type Directory -Force
}
# Note: avoid using Convert-Path because drives aliases like "TestData:" get converted to a C:\ file system location
$null = mkdir $PathRoot -Force
(Resolve-Path $PathRoot).Path
}
}
Expand Down
16 changes: 13 additions & 3 deletions Specs/Configuration.Steps.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ Given "a module with(?:\s+\w+ name '(?<name>.+?)'|\s+\w+ the company '(?<company
$ModulePath = "TestDrive:\Modules\$name"
Remove-Module $name -ErrorAction Ignore
Remove-Item $ModulePath -Recurse -ErrorAction Ignore
$null = mkdir $ModulePath -Force


if(Test-Path $ModulePath -PathType Leaf) {
throw "Cannot create folder for Configuration because there's a file in the way at '$ModulePath'"
}
if(!(Test-Path $ModulePath -PathType Container)) {
$null = New-Item $ModulePath -Type Directory -Force
}
$Env:PSModulePath = $Env:PSModulePath + ";TestDrive:\Modules" -replace "(;TestDrive:\\Modules)+?$", ";TestDrive:\Modules"

Set-Content $ModulePath\${Name}.psm1 "
Expand Down Expand Up @@ -154,8 +161,11 @@ When "a (?:settings file|module manifest) named (\S+)(?:(?: in the (?<Scope>\S+)
$SettingsFile = Join-Path $folder $fileName

$Parent = Split-Path $SettingsFile
if(!(Test-Path $Parent)) {
$null = mkdir $Parent -Force -EA 0
if(Test-Path $Parent -PathType Leaf) {
throw "Cannot create folder for Configuration because there's a file in the way at '$Parent'"
}
if(!(Test-Path $Parent -PathType Container)) {
$null = New-Item $Parent -Type Directory -Force
}
Set-Content $SettingsFile -Value $hashtable
}
Expand Down

0 comments on commit 66af84a

Please sign in to comment.