Sublime Forum

Build System - remove $folder from $file for relative path

#1

Trying to make the file paths in the build output window relative to the current folder, but I can’t make it work properly

The way I call it now: (this works fine but has full paths)
“cmd”: “moai”, “$file”]

My test to see if it would work: (works great, relative paths, but have to hardcode the folder path)
“cmd”: “moai”, “${file/\/path\/to\/folder\///}”]

Non-Working call:
“cmd”: “moai”, “${file/$folder//}”]

I’m assuming that it’s either a escaping issue or some issue with calling a variable in the regex part of the call. Any help would be appreciated.

0 Likes

#2
$file_name

docs.sublimetext.info/en/latest/ … -variables

The substitution feature (regex replace) does not substitute variables in the pattern (or in the replace string?).

1 Like

#3

Thanks for the reply.

$file-name works fine for files in the root of the project but fails as soon as I’m in a sub dir, hence the need for relative paths.

So you can’t use a variable in a substitution? That’s what I was thinking.

Guess I’ll have to call a batch file and do the substitution in the shell. Bit of a pain.

0 Likes

#4

So I sorted it using a bash script, I thought I’d post it in case anyone else has a similar problem.

You pass it the executable to run, the file path, & the folder path.

Build.sh

#!/bin/bash
# sublime relative paths for builds - v.1
#########################################
exectuable=$1
filePath=$2
folderPath=$3 # optional

if  $folderPath ]]; then
	# escape special characters
	folderPath=$(echo $folderPath | sed -e 's/\/&]/\\&/g')

	relativePath=$(echo $filePath | sed "s/$folderPath\///g")

	$exectuable $relativePath
else
	$exectuable $filePath
fi

sublime-build:

"cmd": "/path/to/build.sh", 
		"/path/to/exec    execArg1    execArg2",
		"$file",
		"$folder"],
"working_dir": "$folder",

In case you were wondering why: the exec I was using was shortening file paths in the error output after a certain length so ‘/path/to/file.lua’ was becoming ‘…th/to/file.lua’ (obviously the paths were much longer) so when I pressed F4 to cycle through the errors lots of blank tabs would open up.

That’s fixed now & as a side effect my output is much cleaner.

0 Likes

#5

[quote]$file-name works fine for files in the root of the project but fails as soon as I’m in a sub dir, hence the need for relative paths.

So you can’t use a variable in a substitution? That’s what I was thinking.

Guess I’ll have to call a batch file and do the substitution in the shell. Bit of a pain.[/quote]

True, in that case you either need to write a custom (build target) command (which then redirects the “real” command to exec) or call an external script that does adjustments.

Using variables for substitutions, if it was possible, would also be very error prone because of the escaping that is required for the substitution in a regular expression.

0 Likes

#6

I just reported this on github: git.io/vseVU

0 Likes

#7

Thanks, @FichteFoll
$file_name works for me:

{
    "shell_cmd": "jest --config ../jest.config.js $file_name"
}
0 Likes