Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 45 additions & 23 deletions internal/templater/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package templater
import (
"maps"
"math/rand/v2"
"os"
"path"
"path/filepath"
"runtime"
"strings"
Expand All @@ -21,27 +23,31 @@ var templateFuncs template.FuncMap

func init() {
taskFuncs := template.FuncMap{
"OS": os,
"ARCH": arch,
"numCPU": runtime.NumCPU,
"catLines": catLines,
"splitLines": splitLines,
"fromSlash": filepath.FromSlash,
"toSlash": filepath.ToSlash,
"exeExt": exeExt,
"shellQuote": shellQuote,
"splitArgs": splitArgs,
"IsSH": IsSH, // Deprecated
"joinPath": filepath.Join,
"relPath": filepath.Rel,
"merge": merge,
"spew": spew.Sdump,
"fromYaml": fromYaml,
"mustFromYaml": mustFromYaml,
"toYaml": toYaml,
"mustToYaml": mustToYaml,
"uuid": uuid.New,
"randIntN": rand.IntN,
"OS": goos,
"ARCH": goarch,
"numCPU": runtime.NumCPU,
"catLines": catLines,
"splitLines": splitLines,
"fromSlash": filepath.FromSlash,
"toSlash": filepath.ToSlash,
"exeExt": exeExt,
"shellQuote": shellQuote,
"splitArgs": splitArgs,
"IsSH": IsSH, // Deprecated
"joinPath": filepath.Join,
"joinEnv": joinEnv,
"joinUrl": joinUrl,
"relPath": filepath.Rel,
"merge": merge,
"spew": spew.Sdump,
"fromYaml": fromYaml,
"mustFromYaml": mustFromYaml,
"toYaml": toYaml,
"mustToYaml": mustToYaml,
"uuid": uuid.New,
"randIntN": rand.IntN,
"PATH_LIST_SEPARATOR": pathListSeparator,
"FILE_PATH_SEPARATOR": filePathSeparator,
}

// aliases
Expand All @@ -56,11 +62,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
}

Expand Down Expand Up @@ -94,6 +100,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 {
Expand Down Expand Up @@ -130,3 +144,11 @@ func mustToYaml(v any) (string, error) {
}
return string(output), nil
}

func pathListSeparator() string {
return string(os.PathListSeparator)
}

func filePathSeparator() string {
return string(os.PathSeparator)
}
63 changes: 63 additions & 0 deletions website/src/docs/reference/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down