diff --git a/compiler.go b/compiler.go index 348a072898..0729e0f52e 100644 --- a/compiler.go +++ b/compiler.go @@ -198,11 +198,13 @@ func (c *Compiler) ResetCache() { func (c *Compiler) getSpecialVars(t *ast.Task, call *Call) (map[string]string, error) { allVars := map[string]string{ - "TASK_EXE": filepath.ToSlash(os.Args[0]), - "ROOT_TASKFILE": filepathext.SmartJoin(c.Dir, c.Entrypoint), - "ROOT_DIR": c.Dir, - "USER_WORKING_DIR": c.UserWorkingDir, - "TASK_VERSION": version.GetVersion(), + "TASK_EXE": filepath.ToSlash(os.Args[0]), + "ROOT_TASKFILE": filepathext.SmartJoin(c.Dir, c.Entrypoint), + "ROOT_DIR": c.Dir, + "USER_WORKING_DIR": c.UserWorkingDir, + "TASK_VERSION": version.GetVersion(), + "PATH_LIST_SEPARATOR": os.PathListSeparator, + "FILE_PATH_SEPARATOR": os.PathSeparator, } if t != nil { allVars["TASK"] = t.Task diff --git a/internal/templater/funcs.go b/internal/templater/funcs.go index c3fdc469b6..630f682646 100644 --- a/internal/templater/funcs.go +++ b/internal/templater/funcs.go @@ -3,6 +3,8 @@ package templater import ( "maps" "math/rand/v2" + "os" + "path" "path/filepath" "runtime" "strings" @@ -21,8 +23,8 @@ var templateFuncs template.FuncMap func init() { taskFuncs := template.FuncMap{ - "OS": os, - "ARCH": arch, + "OS": goos, + "ARCH": goarch, "numCPU": runtime.NumCPU, "catLines": catLines, "splitLines": splitLines, @@ -33,6 +35,8 @@ func init() { "splitArgs": splitArgs, "IsSH": IsSH, // Deprecated "joinPath": filepath.Join, + "joinEnv": joinEnv, + "joinUrl": joinUrl, "relPath": filepath.Rel, "merge": merge, "spew": spew.Sdump, @@ -56,11 +60,11 @@ func init() { maps.Copy(templateFuncs, taskFuncs) } -func os() string { +func goos() string { return runtime.GOOS } -func arch() string { +func goarch() string { return runtime.GOARCH } @@ -94,6 +98,14 @@ func IsSH() bool { return true } +func joinEnv(elem ...string) string { + return strings.Join(elem, string(os.PathListSeparator)) +} + +func joinUrl(elem ...string) string { + return path.Join(elem...) +} + func merge(base map[string]any, v ...map[string]any) map[string]any { cap := len(v) for _, m := range v { diff --git a/website/src/docs/reference/templating.md b/website/src/docs/reference/templating.md index ae2391048b..af09f0ac68 100644 --- a/website/src/docs/reference/templating.md +++ b/website/src/docs/reference/templating.md @@ -243,6 +243,24 @@ tasks: - echo "Working {{.USER_WORKING_DIR}}" ``` +#### `FILE_PATH_SEPARATOR` + +- **Type**: `string` +- **Description**: OS-specific path separator: Windows = `\`, others = `/` +- +> **Note**: See `joinPath` in [Path Functions](#path-functions) for joining filesystem paths for use with +> file system operations. + +### Environment Variables + +#### `PATH_LIST_SEPARATOR` + +- **Type**: `string` +- **Description**: OS-specific path separator for environment variables: Windows = `;`, others = `:` + +> **Note**: See `joinEnv` in [Environment Variable Functions](#environment-variable-functions) for joining +> paths for use in environment variables. + ### Status #### `CHECKSUM` @@ -613,6 +631,51 @@ tasks: - echo "Relative {{relPath .ROOT_DIR .TASKFILE_DIR}}" # Get relative path ``` +#### Environment Variable Functions + +```yaml +tasks: + paths: + vars: + WIN_PATH1: 'C:\Users\Person\bin' + WIN_PATH2: 'C:\Shared\bin' + cmds: + - echo "{{joinEnv .WIN_PATH1 .WIN_PATH2}}" # Join paths for windows env vars -> C:\Users\Person\bin;C:\Shared\bin +``` + +```yaml +tasks: + paths: + vars: + POSIX_PATH1: '/users/person/.local/bin' + POSIX_PATH2: '/usr/bin' + cmds: + - echo "{{joinEnv .POSIX_PATH1 .POSIX_PATH2}}" # Join paths for posix env vars -> /users/person/.local/bin:/usr/bin +``` + +#### URLs + +```yaml +tasks: + paths: + vars: + SERVER: 'http://localhost' + PATH1: 'path1' + PATH2: 'path2' + cmds: + - echo "{{joinUrl .SERVER .PATH1 .PATH2}}" # Join paths for URL -> http://localhost/path1/path2 +``` + +```yaml +tasks: + paths: + vars: + POSIX_PATH1: '/users/person/.local/bin' + POSIX_PATH2: '/usr/bin' + cmds: + - echo "{{joinEnv .POSIX_PATH1 .POSIX_PATH2}}" # Join paths for posix env vars -> /users/person/.local/bin:/usr/bin +``` + ### Data Structure Functions #### Dictionary Operations