-
-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce remove
, the helper for cleaning temporary build items
#123
Comments
I like this idea, and often use remove-item in my scripts. |
Collection - yes. I did not plan the pipeline input. Why would you need this? Can you give an example? |
I don't think I'd need the output pipelined, but some way to check success or failure would be great: |
For the moment I am not planning this. The command is going to be dumb simple, at least its first version. It removes items with given names in the current directory and subdirectories, normally it is the build root. |
Ah ok. I won't be able to use it without any sort of logging/output to know of success or failure, but it might be useful to others. :) |
You will be able to catch errors, endeed. The command is going to write errors as usual if it cannot remove something. But if it does not find something this is not an error. |
Here is the prototype, you may start playing and giving some feedback |
Here is a test in the dev branch: Remove.test.ps1 The task |
I have several doubts about the first version and I am thinking of the alternative approach foreach($_ in $Path) {
if (Test-Path $_) {
Remove-Item $_ -Force -Recurse
}
} So that
The parameter |
Turns out TODO: Investigate further and submit an issue to PowerShell. DONE |
v5.3.0 |
It would be good to oput what is done or not done ? Maybe with |
Maybe. Like this, perhaps? function Remove-BuildItem([Parameter(Mandatory=1)][string[]]$Path) {
if ($Path -match '^[.*/\\]*$') {*Die 'Not allowed paths.' 5}
$v = $PSBoundParameters['Verbose']
try {
foreach($_ in $Path) {
if (Get-Item $_ -Force -ErrorAction 0) {
if ($v) {Write-Verbose "remove: removing '$_'." -Verbose}
Remove-Item $_ -Force -Recurse
}
elseif ($v) {
Write-Verbose "remove: skipping '$_'." -Verbose
}
}
}
catch {
*Die $_
}
} |
LGTM |
I opened a separate issue. |
Build scripts often produce temporary files and folders and require commands like
What's wrong with the above command? It is verbose. And it has a flaw. If X
cannot be removed (locked files, permissions, etc.) the command ignores it.
If this is not desired, we should remove
-ErrorAction Ignore
. But thenthe command also fails on missing items. Thus, we should use
Test-Path
in addition and the simple frequent task becomes not so simple.
TODO
Introduce a straightforward helper
remove
, so that the above frequentoperation can be invoked as
remove X, Y
UPDATE: Using
Test-Path
is not that simple, too, see PowerShell/PowerShell#6473Thus, the proposed robust helper dealing with the issues would be really nice.
The text was updated successfully, but these errors were encountered: