Add basic HCL Taskfile parsing #3
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Testing
go test ./...Propt
Task 3: Implement Basic HCL Parsing Logic
Purpose: Develop the core logic to parse an HCL Taskfile and produce the internal Taskfile structure. In
this step, handle the basic elements (Taskfile version and simple tasks definitions) to establish the
parsing workflow. More complex substructures (commands, deps, vars) will be handled in the next task.
• Implementation (Parsing Approach):
• Use HashiCorp’s HCL library (hcl/v2) for parsing. For example, utilize hclparse.NewParser() to
read the file and produce an HCL AST (abstract syntax tree). Then traverse or decode this AST into Go
structures.
• Define HCL Structure: Decide how Taskfiles are represented in HCL syntax and map that to Go
structs. A recommended approach is to use HCL blocks for tasks. For instance:
version = "3"
task "build" {
desc = "Build the project"
cmds = ["go build ./..."]
}
Here, each task is an HCL block named “task” with the task name as a label. At the top level, version (and
other global keys like vars, env, etc.) are HCL attributes.
the HCL file:
type HCLTaskfile struct {
Version string
hcl:"version"Tasks []HCLTaskBlock
hcl:"task,block"// (Later, we'll add vars, env, includes, etc.)
}
type HCLTaskBlock struct {
Name string
hcl:"name,label"Desc *string
hcl:"desc,attr"// Other simple task fields (we will expand later)
Cmds []string
hcl:"cmds,attr"}
The HCLTaskBlock struct represents the content of each task "" { ... } block, capturing the task
name via a label. Initially, include a few basic fields like description and cmds as attributes.
decode the HCL body into your HCLTaskfile struct. Check for errors/diagnostics from the HCL library and
handle them (return a user-friendly error if the file is invalid).
• Convert to Internal Taskfile: Map the decoded HCLTaskfile into the existing Taskfile structure.
For example, set Taskfile.Version and create entries in Taskfile.Tasks map for each HCLTaskBlock (using
the block’s Name as the map key, and populating a new Task struct with the parsed fields). At this stage,
you may populate only the fields we decoded (e.g. Desc, and a preliminary handling of cmds – perhaps just
store them as Shell commands in Task.Cmds).
• Leave placeholders or TODO comments for fields not yet handled (vars, env, deps, etc.), which
will be done in the next task.
task with a description and one command. Write a unit test that uses the HCL loader to parse this file and
returns a Taskfile struct. Verify that the Version matches and the Tasks map contains the task with
correct name, description, and command.
• Test error handling: feed invalid HCL content (e.g. malformed syntax) and ensure the HCL parser
returns an error that our code surfaces properly (e.g. error message contains the file name and position
of the parse error).
• Confirm that YAML functionality is still unaffected: run the YAML parsing tests again to ensure
no regression.
https://chatgpt.com/codex/tasks/task_e_6892047fd3d88330bd246acda2d2c536