rioj7 / command-variable

Visual Studio Code extension for variable substitution via ${command:commandID}
51 stars 10 forks source link

fileDirnameForwardSlashes #47

Closed nabelekt closed 1 year ago

nabelekt commented 1 year ago

Hello, @rioj7. Thanks for your work on this extension!

I am trying to use it to pass paths to make on from tasks.json on Windows. If I use ${fileDirname}, the paths get into make with backslashes. If I use ${command:extension.commandvariable.file.fileDirnamePosix}, the slashes are fixed, but the Windows drive specification is not. I need to maintain the s:/ format. Would it be possible for you to add a fileDirnameForwardSlashes command that just fixes the slashes? Thanks!

nabelekt commented 1 year ago

I have been able to use an input to do what I need:

    "inputs": [
      {
        "id": "fileDirnameForwardSlashes",
        "type": "command",
        "command": "extension.commandvariable.transform",
        "args": {
          "text": "${fileDirname}",
          "find": "\\\\",  // Reason for four '\': https://stackoverflow.com/a/4025505/2909854
          "replace": "/",
          "flags": "g"
        }
      }
    ],  

And now I can use SRC_DIR=${input:fileDirnameForwardSlashes}" in my task.

I do still think that a command to do this would be useful.

rioj7 commented 1 year ago

@nabelekt If I would do this for ${fileDirname} I have to do it for all file and workspace commands that could have a back slash separator. Before the existence of transform command that would be the only solution. If next month somebody else has a different manipulation request I have to add an XYZ version of all file and workspace commands including the new *ForwardSlash commands.

I will add this example to the README to show how to manipulate a command result.

What is the problem if you use SRC_DIR=${fileDirname}?

What if you add " around the argument: "\"SRC_DIR=${fileDirname}\""

nabelekt commented 1 year ago

I'm not able to find documentation on it, but the problem with SRC_DIR=${fileDirname} is that make treats backslashes as escape characters. It expects / as the path separator, regardless of OS, it seems. I suspect that replacing \ with \\ would work on Windows, but haven't tried it, and want an OS-agnostic solution.

rioj7 commented 1 year ago

@nabelekt Is it the shell or make that treats a backslash as escape, then what if you escape the backslash with a backslash, but that would also mean a transform of the variable

Or you quote the argument, weak or strong, using " or '

Anyway if this / substitution works => problem solved