-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add Edit-File command #19
Comments
OK, I'm taking a swag at importing the PSCX EditFileCommand.cs. Hopefully will have a PR soon-ish. |
Should it just be |
I needed this once or twice and used something like (gc {file}) -replace {pattern}, {replacement} | out-file ...to do that. This would be a nice shortcut. |
I'm fine with
Where you have the file locking problem (trying to write to file while reading it) as well as this doesn't preserve the file's original encoding which should be a goal of this command. Anway, the above pattern is common enough to warrant a simpler form:
|
Hmm, the PSCX implementation has an elaborate way of supporting Path/LiteralPath that won't port over. @SteveL-MSFT is there a canonical sample of Path/LiteralPath C# code I can use? |
@SteveL-MSFT Do you think this cmdlet should derive from To be honest, the implementation of the functionality isn't too bad but dealing with Path/LiteralPath parameters has been such a PITA that @oising added easier handling for these parameters in PSCX in the form of |
@rkeithhill sorry, pointed you to the wrong thing. Perhaps do something similar to https://github.com/PowerShell/PowerShellGet/blob/f1ddcb3bc602c61f9b98f79b1ab02b00331da8f5/src/code/PublishPSResource.cs#L95 |
@SteveL-MSFT Thanks. That got me past the Path/LiteralPath parameter issue. I have something that compiles and runs. However, I had to copy over some crufty PSCX code for the Encoding parameter (to do string conversion, checks, etc). I'm hopeful I can find some more modern and hopefully SMA built-in support. Do you have any pointers for that? I could submit a PR now but ... I'd rather have better Encoding parameter support implemented first. FYI the command's default behavior is to try to preserve the file's original encoding so the |
@SteveL-MSFT OK found some code in Out-File.cs but ... the Encoding parameter support required me to copy a number of FWIW I'm not a big fan of how some of this is implemented e.g. deriving from ContentCommandBase.cs to get Path/LiteralPath handling. We did something similar in PSCX and when it worked it was nice but there were a number of cases where it didn't work. So, the answer probably isn't to just make existing internal classes public. It would be better to provide "parameter implementations" that folks can use & configure via attributes (or interfaces). Two very good parameters to start with would be Anyway, I should have an initial WIP PR out in a few days. |
We may choose to include Jim's version and call it |
@SteveL-MSFT is there any status on this being included? Other than Jim's brief demo during one of the PowerShell Community calls I seem to have lost track of this. |
I would really love to see this command added. PS has been missing an easy way to modify files since v1. This comes in so handy for build systems. Our builds, first thing, edit a number of files to modify version numbers using this command e.g.:
And here's the implementation - feel free to use as much (or little) as you want. https://github.com/Pscx/Pscx/blob/master/Src/Pscx/Commands/IO/EditFileCommand.cs
The text was updated successfully, but these errors were encountered: