Skip to content

Commit 66af84a

Browse files
committed
Stop using mkdir -Force
1 parent 08df6bf commit 66af84a

3 files changed

Lines changed: 46 additions & 10 deletions

File tree

Build.ps1

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ function init {
4646
# Calculate Paths
4747
# The output path is just a temporary output and logging location
4848
$Script:OutputPath = Join-Path $Path output
49-
$null = mkdir $OutputPath -Force
49+
50+
if(Test-Path $OutputPath -PathType Leaf) {
51+
throw "Cannot create folder for Configuration because there's a file in the way at '$OutputPath'"
52+
}
53+
if(!(Test-Path $OutputPath -PathType Container)) {
54+
$null = New-Item $OutputPath -Type Directory -Force
55+
}
5056

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

153164
# Remember, as of now, only nuget actually supports the -Destination flag
154165
foreach($Package in ([xml](gc .\packages.config)).packages.package) {
@@ -210,7 +221,13 @@ function build {
210221
$RootModule = "${ModuleName}.psm1"
211222
}
212223
}
213-
$null = mkdir $ReleasePath -Force
224+
if(Test-Path $ReleasePath -PathType Leaf) {
225+
throw "Cannot create folder for Configuration because there's a file in the way at '$ReleasePath'"
226+
}
227+
if(!(Test-Path $ReleasePath -PathType Container)) {
228+
$null = New-Item $ReleasePath -Type Directory -Force
229+
}
230+
214231
$ReleaseModule = Join-Path $ReleasePath ${RootModule}
215232
Trace-Message " Setting content for $ReleaseModule"
216233

@@ -244,7 +261,13 @@ function build {
244261
$ReadMe = Join-Path $Path Readme.md
245262
if(Test-Path $ReadMe -PathType Leaf) {
246263
$LanguagePath = Join-Path $ReleasePath $DefaultLanguage
247-
$null = mkdir $LanguagePath -Force
264+
if(Test-Path $LanguagePath -PathType Leaf) {
265+
throw "Cannot create folder for Configuration because there's a file in the way at '$LanguagePath'"
266+
}
267+
if(!(Test-Path $LanguagePath -PathType Container)) {
268+
$null = New-Item $LanguagePath -Type Directory -Force
269+
}
270+
248271
$about_module = Join-Path $LanguagePath "about_${ModuleName}.help.txt"
249272
if(!(Test-Path $about_module)) {
250273
Trace-Message "Turn readme into about_module"

Source/Configuration.psm1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,13 @@ function Get-StoragePath {
141141
$PathRoot = Join-Path $PathRoot $Version
142142
}
143143

144-
# Write-Debug "Storage Path: $PathRoot"
145-
144+
if(Test-Path $PathRoot -PathType Leaf) {
145+
throw "Cannot create folder for Configuration because there's a file in the way at $PathRoot"
146+
}
147+
if(!(Test-Path $PathRoot -PathType Container)) {
148+
$null = New-Item $PathRoot -Type Directory -Force
149+
}
146150
# Note: avoid using Convert-Path because drives aliases like "TestData:" get converted to a C:\ file system location
147-
$null = mkdir $PathRoot -Force
148151
(Resolve-Path $PathRoot).Path
149152
}
150153
}

Specs/Configuration.Steps.ps1

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ Given "a module with(?:\s+\w+ name '(?<name>.+?)'|\s+\w+ the company '(?<company
4343
$ModulePath = "TestDrive:\Modules\$name"
4444
Remove-Module $name -ErrorAction Ignore
4545
Remove-Item $ModulePath -Recurse -ErrorAction Ignore
46-
$null = mkdir $ModulePath -Force
46+
47+
48+
if(Test-Path $ModulePath -PathType Leaf) {
49+
throw "Cannot create folder for Configuration because there's a file in the way at '$ModulePath'"
50+
}
51+
if(!(Test-Path $ModulePath -PathType Container)) {
52+
$null = New-Item $ModulePath -Type Directory -Force
53+
}
4754
$Env:PSModulePath = $Env:PSModulePath + ";TestDrive:\Modules" -replace "(;TestDrive:\\Modules)+?$", ";TestDrive:\Modules"
4855

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

156163
$Parent = Split-Path $SettingsFile
157-
if(!(Test-Path $Parent)) {
158-
$null = mkdir $Parent -Force -EA 0
164+
if(Test-Path $Parent -PathType Leaf) {
165+
throw "Cannot create folder for Configuration because there's a file in the way at '$Parent'"
166+
}
167+
if(!(Test-Path $Parent -PathType Container)) {
168+
$null = New-Item $Parent -Type Directory -Force
159169
}
160170
Set-Content $SettingsFile -Value $hashtable
161171
}

0 commit comments

Comments
 (0)