emeraldwalk / vscode-runonsave

Visual Studio Code extension to run commands whenever a file is saved.
Apache License 2.0
182 stars 57 forks source link

Request: Environment variable for relative directory #33

Open ehoogeveen-medweb opened 5 years ago

ehoogeveen-medweb commented 5 years ago

Run on Save currently has ${relativeFile} for the path relative to the workspace folder and ${fileDirname} for the absolute directory of the file, but what I need is actually something like ${relativeDirname} for the directory relative to the workspace folder.

I need this because I want to process some files and save the result in a dist/ subdirectory. I'm on Windows so my options for splitting the directory are very limited - I can strip off say the first 2 parts using the awkward for /F "tokens=2* delims=\" %A in ("${fileDirname}") do echo %B, but I would like the command to not be dependent on knowing the nesting level of the workspace folder. Approaching this from the other direction - stripping off the file basename from ${relativeFile} - doesn't appear to be possible on a single line.

ehoogeveen-medweb commented 5 years ago

Visual Studio Code appears to use ${relativeFileDirname} for this. It doesn't seem to be exposed by Run on Save at the moment though.

ehoogeveen-medweb commented 5 years ago

FWIW, I managed to work around this limitation with a batch file. It looks something like this:

setlocal enabledelayedexpansion
set "WorkspaceFolder=%~1"
set "FileDirname=%~2"
set "FileBasename=%~3"
set "FileExtname=%~4"
call :strLen WorkspaceFolder length
set /A "length+=1"
set "RelativeDirname=!FileDirname:~%length%!"
rem Do other stuff.
endlocal
exit /b

:strLen
setlocal enabledelayedexpansion
if "!%1!" == "" (
    set /A "len=0"
    goto :strLen_End
)
:strLen_Loop
if not "!%1:~%len%,1!" == "" (
    set /A "len+=1" & goto :strLen_Loop
)
:strLen_End
endlocal & set %2=%len%
exit /b
RyanEwen commented 4 years ago

I have a very similar problem and ${relativeFileDirname} would help me out as well

RyanEwen commented 4 years ago

My workaround for now us to use dirname dist/${relativeFile}:

    "emeraldwalk.runonsave": {
        "commands": [
            {
                "match": "\\.js$",
                "cmd": "npx babel ${relativeFile} --out-dir `dirname dist/${relativeFile}` --source-type script --source-maps --minified"
            }
        ]
    },
ehoogeveen-medweb commented 4 years ago

FWIW, I eventually switched from using a batch command to a PowerShell script, which is a bit less awkward (with commandlets like Split-Path). Of course, I then found myself having to get it working on Linux as well and ended up writing a dispatcher script based on https://nastytester.com/posts/script-that-works-in-windows-and-linux.html (so I'm back to using a batch command for at least part of the process).