Skip to content

Commit

Permalink
refactor(rmdir): Use 'Remove-Item' instead of 'rmdir' (ScoopInstaller…
Browse files Browse the repository at this point in the history
  • Loading branch information
niheaven authored and se35710 committed Mar 8, 2022
1 parent 9fc91d6 commit 28ca55c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- **depends:** Rewrite 'depends.ps1' ([#4638](https://github.com/ScoopInstaller/Scoop/issues/4638))
- **depends:** Keep bucket in 'Get-Dependency()' ([#4673](https://github.com/ScoopInstaller/Scoop/issues/4673))
- **mklink:** Use 'New-Item' instead of 'mklink' ([#4690](https://github.com/ScoopInstaller/Scoop/issues/4690))
- **rmdir:** Use 'Remove-Item' instead of 'rmdir' ([#4691](https://github.com/ScoopInstaller/Scoop/issues/4691))

### Builds

Expand Down
4 changes: 2 additions & 2 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1192,10 +1192,10 @@ function unlink_persist_data($dir) {
# remove read-only attribute on the link
attrib -R /L $filepath
# remove the junction
& "$env:COMSPEC" /c "rmdir /s /q `"$filepath`""
Remove-Item -Path $filepath -Recurse -Force -ErrorAction SilentlyContinue
} else {
# remove the hard link
& "$env:COMSPEC" /c "del `"$filepath`""
Remove-Item -Path $filepath -Force -ErrorAction SilentlyContinue
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions lib/psmodules.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,56 @@ $modulesdir = "$scoopdir\modules"

function install_psmodule($manifest, $dir, $global) {
$psmodule = $manifest.psmodule
if(!$psmodule) { return }
if (!$psmodule) { return }

if($global) {
abort "Installing PowerShell modules globally is not implemented!"
if ($global) {
abort 'Installing PowerShell modules globally is not implemented!'
}

$modulesdir = ensure $modulesdir
ensure_in_psmodulepath $modulesdir $global

$module_name = $psmodule.name
if(!$module_name) {
if (!$module_name) {
abort "Invalid manifest: The 'name' property is missing from 'psmodule'."
}

$linkfrom = "$modulesdir\$module_name"
write-host "Installing PowerShell module '$module_name'"
Write-Host "Installing PowerShell module '$module_name'"

write-host "Linking $(friendly_path $linkfrom) => $(friendly_path $dir)"
Write-Host "Linking $(friendly_path $linkfrom) => $(friendly_path $dir)"

if(test-path $linkfrom) {
if (Test-Path $linkfrom) {
warn "$(friendly_path $linkfrom) already exists. It will be replaced."
& "$env:COMSPEC" /c "rmdir `"$linkfrom`""
Remove-Item -Path $linkfrom -Force -ErrorAction SilentlyContinue
}

New-Item -Path $linkfrom -ItemType Junction -Value $dir | Out-Null
}

function uninstall_psmodule($manifest, $dir, $global) {
$psmodule = $manifest.psmodule
if(!$psmodule) { return }
if (!$psmodule) { return }

$module_name = $psmodule.name
write-host "Uninstalling PowerShell module '$module_name'."
Write-Host "Uninstalling PowerShell module '$module_name'."

$linkfrom = "$modulesdir\$module_name"
if(test-path $linkfrom) {
write-host "Removing $(friendly_path $linkfrom)"
$linkfrom = resolve-path $linkfrom
& "$env:COMSPEC" /c "rmdir `"$linkfrom`""
if (Test-Path $linkfrom) {
Write-Host "Removing $(friendly_path $linkfrom)"
$linkfrom = Resolve-Path $linkfrom
Remove-Item -Path $linkfrom -Force -ErrorAction SilentlyContinue
}
}

function ensure_in_psmodulepath($dir, $global) {
$path = env 'psmodulepath' $global
if(!$global -and $null -eq $path) {
if (!$global -and $null -eq $path) {
$path = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules"
}
$dir = fullpath $dir
if($path -notmatch [regex]::escape($dir)) {
write-output "Adding $(friendly_path $dir) to $(if($global){'global'}else{'your'}) PowerShell module path."
if ($path -notmatch [Regex]::Escape($dir)) {
Write-Output "Adding $(friendly_path $dir) to $(if($global){'global'}else{'your'}) PowerShell module path."

env 'psmodulepath' $global "$dir;$path" # for future sessions...
$env:psmodulepath = "$dir;$env:psmodulepath" # for this session
Expand Down

0 comments on commit 28ca55c

Please sign in to comment.