Skip to content

Commit d86eb62

Browse files
authored
feat: support custom output filenames (#19)
1 parent 22c440d commit d86eb62

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

Diff for: README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,35 @@ Choose one of the following:
2424

2525
## Usage
2626

27-
```bash
27+
```shell
2828
# Renders `./my.schema.json` to `./out/SchemaTitle.md`.
2929
schemadoc gen --in ./my.schema.json
3030

3131
# Renders all json schema files in `./schemas` to `./docs`.
3232
schemadoc gen --in ./schemas --out ./docs
3333
```
3434

35+
To see schemadoc in action, check out
36+
[Generator.md](https://github.com/twelvelabs/stamp/blob/main/docs/Generator.md)
37+
which is rendered from
38+
[stamp.schema.json](https://github.com/twelvelabs/stamp/blob/main/docs/stamp.schema.json)
39+
at build time.
40+
41+
## Customizing
42+
43+
Schemadoc ships with a built in [template](./internal/jsonschema/templates/markdown.tpl.md) for rendering markdown.
44+
To customize (or render something other than markdown)
45+
you can supply your own Go [text/template](https://pkg.go.dev/text/template) file:
46+
47+
```shell
48+
schemadoc gen --in ./schemas --out ./dest --template path/to/my-xml-template.tpl --outfile "{{ .EntityName }}.xml"
49+
```
50+
51+
Each top-level JSON schema in `./schemas` will be parsed into a
52+
[Schema](./internal/jsonschema/schema.go) struct and passed into
53+
`my-xml-template.tpl`.
54+
The rendered files will be written to `./dest/$SchemaName.xml`.
55+
3556
## Development
3657

3758
```shell

Diff for: internal/cmd/gen.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func NewGenCmd(app *core.App) *cobra.Command {
2929
a := &GenAction{
3030
App: app,
3131
}
32+
if err := defaults.Set(a); err != nil {
33+
panic(err)
34+
}
3235

3336
cmd := &cobra.Command{
3437
Use: "gen",
@@ -42,7 +45,8 @@ func NewGenCmd(app *core.App) *cobra.Command {
4245
flags := cmd.Flags()
4346
flags.StringVarP(&a.InPath, "in", "i", a.InPath, "file path or dir to one or more JSON schema files")
4447
flags.StringVarP(&a.OutDir, "out", "o", a.OutDir, "output dir to generate files to")
45-
flags.StringVarP(&a.TemplatePath, "template", "t", a.TemplatePath, "optional template path")
48+
flags.StringVarP(&a.OutFile, "outfile", "f", a.OutFile, "custom filename pattern for generated files")
49+
flags.StringVarP(&a.TemplatePath, "template", "t", a.TemplatePath, "custom template path")
4650

4751
return cmd
4852
}
@@ -52,6 +56,7 @@ type GenAction struct {
5256

5357
InPath string `validate:"required"`
5458
OutDir string `validate:"required" default:"out"`
59+
OutFile string `validate:"required" default:"{{ .EntityName }}.md"`
5560
SchemaPaths []string
5661
TemplatePath string
5762
}
@@ -93,8 +98,12 @@ func (a *GenAction) Run(_ context.Context, _ []string) error {
9398
return err
9499
}
95100

96-
renderedPath := filepath.Join(a.OutDir, fmt.Sprintf("%s.md", scm.EntityName()))
97-
if err := os.WriteFile(renderedPath, []byte(rendered), fsutil.DefaultFileMode); err != nil {
101+
outFile, err := render.String(a.OutFile, scm)
102+
if err != nil {
103+
return err
104+
}
105+
outPath := filepath.Join(a.OutDir, outFile)
106+
if err := os.WriteFile(outPath, []byte(rendered), fsutil.DefaultFileMode); err != nil {
98107
return err
99108
}
100109
}
@@ -105,13 +114,11 @@ func (a *GenAction) Run(_ context.Context, _ []string) error {
105114
func (a *GenAction) setup() error {
106115
start := time.Now()
107116

108-
if err := defaults.Set(a); err != nil {
109-
return err
110-
}
111117
if err := validate.Struct(a); err != nil {
112118
msg := err.Error()
113119
msg = strings.ReplaceAll(msg, "InPath", `'--in'`)
114120
msg = strings.ReplaceAll(msg, "OutDir", `'--out'`)
121+
msg = strings.ReplaceAll(msg, "OutFile", `'--outfile'`)
115122
msg = strings.ReplaceAll(msg, "field", "flag")
116123
return fmt.Errorf(msg)
117124
}
@@ -150,6 +157,7 @@ func (a *GenAction) setup() error {
150157
"duration", time.Since(start),
151158
"in", a.SchemaPaths,
152159
"out", a.OutDir,
160+
"outfile", a.OutFile,
153161
"template", a.TemplatePath,
154162
)
155163
return nil

0 commit comments

Comments
 (0)