MCPcopy
hub / github.com/go-task/task / getSpecialVars

Method getSpecialVars

compiler.go:206–259  ·  view source on GitHub ↗
(t *ast.Task, call *Call)

Source from the content-addressed store, hash-verified

204}
205
206func (c *Compiler) getSpecialVars(t *ast.Task, call *Call) (map[string]string, error) {
207 // Use filepath.ToSlash for all paths to ensure consistent forward slashes
208 // across platforms. This prevents issues with backslashes being interpreted
209 // as escape sequences when paths are used in shell commands on Windows.
210 var rootTaskfile, rootDir string
211 if taskfile.IsRemoteEntrypoint(c.Entrypoint) {
212 rootTaskfile = c.Entrypoint
213 rootDir = ""
214 } else {
215 rootTaskfile = filepath.ToSlash(filepathext.SmartJoin(c.Dir, c.Entrypoint))
216 rootDir = filepath.ToSlash(c.Dir)
217 }
218
219 allVars := map[string]string{
220 "TASK_EXE": filepath.ToSlash(os.Args[0]),
221 "ROOT_TASKFILE": rootTaskfile,
222 "ROOT_DIR": rootDir,
223 "USER_WORKING_DIR": filepath.ToSlash(c.UserWorkingDir),
224 "TASK_VERSION": version.GetVersion(),
225 "PATH_LIST_SEPARATOR": string(os.PathListSeparator),
226 "FILE_PATH_SEPARATOR": string(os.PathSeparator),
227 }
228 if t != nil {
229 allVars["TASK"] = t.Task
230 if taskfile.IsRemoteEntrypoint(t.Location.Taskfile) {
231 allVars["TASKFILE"] = t.Location.Taskfile
232 allVars["TASKFILE_DIR"] = ""
233 switch {
234 case t.Dir == "":
235 allVars["TASK_DIR"] = filepath.ToSlash(c.UserWorkingDir)
236 case filepath.IsAbs(t.Dir):
237 allVars["TASK_DIR"] = filepath.ToSlash(t.Dir)
238 default:
239 allVars["TASK_DIR"] = filepath.ToSlash(filepathext.SmartJoin(c.UserWorkingDir, t.Dir))
240 }
241 } else {
242 allVars["TASK_DIR"] = filepath.ToSlash(filepathext.SmartJoin(c.Dir, t.Dir))
243 allVars["TASKFILE"] = filepath.ToSlash(t.Location.Taskfile)
244 allVars["TASKFILE_DIR"] = filepath.ToSlash(filepath.Dir(t.Location.Taskfile))
245 }
246 } else {
247 allVars["TASK"] = ""
248 allVars["TASK_DIR"] = ""
249 allVars["TASKFILE"] = ""
250 allVars["TASKFILE_DIR"] = ""
251 }
252 if call != nil {
253 allVars["ALIAS"] = call.Task
254 } else {
255 allVars["ALIAS"] = ""
256 }
257
258 return allVars, nil
259}

Callers 2

getVariablesMethod · 0.95
TestGetSpecialVarsRemoteFunction · 0.95

Calls 4

IsRemoteEntrypointFunction · 0.92
SmartJoinFunction · 0.92
GetVersionFunction · 0.92
DirMethod · 0.65

Tested by 1

TestGetSpecialVarsRemoteFunction · 0.76