pucelle / vscode-run-on-save

Run configured shell commands when a file is saved in vscode, and output configured messages on status bar.
https://marketplace.visualstudio.com/items?itemName=pucelle.run-on-save
MIT License
50 stars 11 forks source link

One backslash is removed when saving on windows share #2

Closed thomasba closed 5 years ago

thomasba commented 5 years ago

When saving a file on a Windows Share (Path begins with "\\file...") one of the leading backslashes gets removed.

E.g. saving file "\server\folder\test.ps1" passes "\server\folder\test.ps1" to my script.

Config:

{
    "runOnSave.statusMessageTimeout": 3000,
    "runOnSave.commands": [
            {
                "match": ".*\\.ps1$",
                "command": "Update-CodeSigning.ps1 -FilePath ${file}",
                "runIn": "terminal"
            }
        ]
}
pucelle commented 5 years ago

Hi, thomasba, Sorry to reply late because I'm on vocation these days. I will begin to process issues since tomorrow.

pucelle commented 5 years ago

Hi, thomasba, I've released V1.1.1, and it will quote paths which includes \\ with double quote. Please help to try it, Thanks.

thomasba commented 5 years ago

Sadly this didn't resolve the problem. Could this be the issue?

> path.normalize('Update-CodeSigning.ps1 -FilePath \\\\test.domain.int\\path\\file.ext')
'Update-CodeSigning.ps1 -FilePath \\test.domain.int\\path\\file.ext'
>> path.normalize('\\\\test.domain.int\\path\\file.ext')
'\\\\test.domain.int\\path\\file.ext'

So it seems like two backslashes are only allowed at the beginning of a string. Do we have to normalize every piece (or path) by its own?

pucelle commented 5 years ago

Yes, path.normalize will remove double back slashes. I just released V1.1.2, please try again, Thanks.

thomasba commented 5 years ago

Now there are to many backslashes. I looked into it (I've never user nodejs before or even built a vscode extension, but your edits gave me an idea, what happening where :)) and I think back in Version 1.1.0 this would be a sufficient solution:

diff --git a/src/run-on-save.ts b/src/run-on-save.ts
index 31415ba..3ef80b5 100644
--- a/src/run-on-save.ts
+++ b/src/run-on-save.ts
@@ -73,7 +73,7 @@ export class CommandProcessor {
                        else {
                                return <TerminalCommand>{
                                        runIn: 'terminal',
-                                       command: path.normalize(this.formatVariables(command.command, fileName, true))
+                                       command: this.formatVariables(command.command, fileName, true)
                                }
                        }
                })
@@ -131,7 +131,7 @@ export class CommandProcessor {
                                piece = '"' + piece + '"'
                        }

-                       return piece
+                       return path.normalize(piece)
                })
        }
 }

By normalizing every piece the intial "\" of the path is at the front of it piece and doesn't get converted to "\".

Based on Version 1.1.2 (in case the use of path.normalize is desired, otherwise don't use it):

diff --git a/src/run-on-save.ts b/src/run-on-save.ts
index da36d69..07f02f7 100644
--- a/src/run-on-save.ts
+++ b/src/run-on-save.ts
@@ -123,12 +123,12 @@ export class CommandProcessor {
                                return envName ? String(process.env[envName]) : ''
                        })

-                       // If piece includes spaces or `\\`, then it must be encoded
-                       if (isCommand && piece !== oldPiece && /[\s"]|\\\\/.test(piece)) {
+                       // If piece includes spaces or quotation marks, then it must be encoded
+                       if (isCommand && piece !== oldPiece && /[\s"]/.test(piece)) {
                                piece = '"' + encodeCommandLineToBeQuoted(piece) + '"'
                        }

-                       return piece
+                       return path.normalize(piece)
                })
        }

diff --git a/src/util.ts b/src/util.ts
index 21a8874..0ebf69f 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -1,8 +1,8 @@
 export function encodeCommandLineToBeQuoted(command: string) {
-       return command.replace(/[\\"]/g, '\\$&')
+       return command.replace(/["]/g, '\\$&')
 }

 export function decodeQuotedCommandLine(command: string) {
-       return command.replace(/\\(.)/g, '$1')
+       return command.replace(/\\(["])/g, '$1')
 }
pucelle commented 5 years ago

Hi, thomasba, sorry to reply you late. Yes, you are right, I made a mistake here, \ should not escape to double when been quoted. I made another fix just now. Thanks.

thomasba commented 5 years ago

Hi pucelle, absolutely no problem! Now it workes as expected, thanks a lot!