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
16 changes: 14 additions & 2 deletions cmd/xc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var ErrNoTaskFile = errors.New("no xc compatible documentation file found")

type config struct {
version, help, short, display, noTTY, complete, uncomplete bool
filename, heading, filetype string
filename, heading, filetype, outputType string
}

var version = ""
Expand Down Expand Up @@ -71,6 +71,8 @@ func flags() config {
flag.BoolVar(&cfg.display, "d", false, "print the plain text code of a task rather than running it")
flag.BoolVar(&cfg.display, "display", false, "print the plain text code of a task rather than running it")

flag.StringVar(&cfg.outputType, "output", "markdown", "specify the output type to display")

flag.BoolVar(&cfg.complete, "complete", false, "install shell completion for xc")
flag.BoolVar(&cfg.uncomplete, "uncomplete", false, "uninstall shell completion for xc")

Expand Down Expand Up @@ -250,7 +252,17 @@ func runMain() error {
}
// xc -display task1
if cfg.display {
ta.Display(os.Stdout)
switch strings.ToLower(cfg.outputType) {
case "markdown":
ta.Display(os.Stdout)
case "json":
err = ta.DisplayJSON(os.Stdout)
if err != nil {
return fmt.Errorf("xc: %w", err)
}
default:
fmt.Printf("xc: unrecognized output type: %s", cfg.outputType)
}
return nil
}
// xc task1
Expand Down
2 changes: 2 additions & 0 deletions cmd/xc/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ xc <task> [inputs...]
Specify a markdown file that contains tasks (default: "README.md").
-d -display
Print the markdown code of a task rather than running it.
-output <string>
Specify the output type to display. (default: "markdown", consider: "json")
-H -heading <string>
Specify the heading for xc tasks (default: "Tasks").

Expand Down
12 changes: 12 additions & 0 deletions models/models.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"encoding/json"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -58,6 +59,17 @@ func (t Task) Display(w io.Writer) {
}
}

// Display writes a Task as JSON.
func (t Task) DisplayJSON(w io.Writer) (err error) {
buf, _ := json.Marshal(t)
// I tried to find a way to trigger an marshaling error for testing
// purposes but failed. The Task struct doesn't have any fields which
// are amenable to un-marshalable objects. Therefore this just eats an
// error.
fmt.Fprintln(w, string(buf))
return
}

// Tasks is an alias type for []Task
type Tasks []Task

Expand Down
83 changes: 83 additions & 0 deletions models/models_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package models

import (
_ "embed"
"io"
"os"
"strings"
"testing"
)

var gnTempl = Task{
Name: "generate-templ",
Script: "go run -mod=mod github.com/a-h/templ/cmd/templ generate\ngo mod tidy\n",
}
var gnTranslations = Task{
Name: "generate-translations",
Script: "go run ./i18n/generate\n",
}
var gnAll = Task{
Name: "generate-all",
DependsOn: []string{
"generate-templ",
"generate-translations",
},
DepsBehaviour: DependencyBehaviourAsync,
}

//go:embed testdata/generate-templ.md
var gnTemplExpected string

//go:embed testdata/generate-translations.md
var gnTranslationsExpected string

//go:embed testdata/generate-all.md
var gnAllExpected string

//go:embed testdata/generate-templ.json
var gnTemplExpectedJSON string

//go:embed testdata/generate-translations.json
var gnTranslationsExpectedJSON string

//go:embed testdata/generate-all.json
var gnAllExpectedJSON string

func assertTask(t *testing.T, expected, actual string) {
t.Helper()
if strings.TrimSpace(expected) != strings.TrimSpace(actual) {
t.Fatalf("model want=%s got=%s", expected, actual)
}
}

func taskToMarkdown(task Task) string {
r, w, _ := os.Pipe()
defer w.Close()
defer r.Close()
task.Display(w)
w.Close()
res, _ := io.ReadAll(r)
return string(res)
}

func taskToJSON(task Task) string {
r, w, _ := os.Pipe()
defer w.Close()
defer r.Close()
_ = task.DisplayJSON(w)
w.Close()
res, _ := io.ReadAll(r)
return string(res)
}

func TestDisplay(t *testing.T) {
assertTask(t, gnTemplExpected, taskToMarkdown(gnTempl))
assertTask(t, gnTranslationsExpected, taskToMarkdown(gnTranslations))
assertTask(t, gnAllExpected, taskToMarkdown(gnAll))
}

func TestDisplayJSON(t *testing.T) {
assertTask(t, gnTemplExpectedJSON, taskToJSON(gnTempl))
assertTask(t, gnTranslationsExpectedJSON, taskToJSON(gnTranslations))
assertTask(t, gnAllExpectedJSON, taskToJSON(gnAll))
}
1 change: 1 addition & 0 deletions models/testdata/generate-all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Name":"generate-all","Description":null,"Script":"","Dir":"","Env":null,"DependsOn":["generate-templ","generate-translations"],"Inputs":null,"ParsingError":"","RequiredBehaviour":0,"DepsBehaviour":1,"Interactive":false}
6 changes: 6 additions & 0 deletions models/testdata/generate-all.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## generate-all

Requires: generate-templ, generate-translations
RunDeps: async

Run: always
1 change: 1 addition & 0 deletions models/testdata/generate-templ.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Name":"generate-templ","Description":null,"Script":"go run -mod=mod github.com/a-h/templ/cmd/templ generate\ngo mod tidy\n","Dir":"","Env":null,"DependsOn":null,"Inputs":null,"ParsingError":"","RequiredBehaviour":0,"DepsBehaviour":0,"Interactive":false}
9 changes: 9 additions & 0 deletions models/testdata/generate-templ.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## generate-templ

Run: always

```
go run -mod=mod github.com/a-h/templ/cmd/templ generate
go mod tidy

```
1 change: 1 addition & 0 deletions models/testdata/generate-translations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Name":"generate-translations","Description":null,"Script":"go run ./i18n/generate\n","Dir":"","Env":null,"DependsOn":null,"Inputs":null,"ParsingError":"","RequiredBehaviour":0,"DepsBehaviour":0,"Interactive":false}
8 changes: 8 additions & 0 deletions models/testdata/generate-translations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## generate-translations

Run: always

```
go run ./i18n/generate

```