Skip to content

Commit

Permalink
hof/gen+fmt: fix various bugs, set formatting to false when docker no…
Browse files Browse the repository at this point in the history
…t present, start wiring up formatter config from gens down
  • Loading branch information
verdverm committed Aug 2, 2022
1 parent 8ec3b93 commit eef2383
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 19 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ release:
make -C formatters images
cd cmd/hof && goreleaser --rm-dist -p 1
make -C formatters push

snapshot:
cd cmd/hof && goreleaser --rm-dist -p 1 --snapshot
9 changes: 6 additions & 3 deletions lib/fmt/fmtrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ func init() {
}
}

func GracefulInit() {
func GracefulInit() error {
err := initDockerCli()
if err != nil {
return
return err
}

err = updateFormatterStatus()
if err != nil {
return
return err
}

return nil
}

type Formatter struct {
Expand Down Expand Up @@ -141,6 +143,7 @@ var fmtrDefaultConfigs = map[string]interface{}{
func FormatSource(filename string, content []byte, fmtrName string, config interface{}, formatData bool) ([]byte, error) {
// extract filename & extension
// TODO, better extract multipart extensions (as supported by prettier)
// want to prefer the longest
_, fn := filepath.Split(filename)
ext := filepath.Ext(fn)

Expand Down
4 changes: 4 additions & 0 deletions lib/gen/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type File struct {
DatafileFormat string // Data format file
StaticFile bool

// Formatting
FormattingDisabled bool
FormattingConfig *FmtConfig

// Template delimiters
TemplateDelims *templates.Delims

Expand Down
42 changes: 33 additions & 9 deletions lib/gen/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,49 @@ func init() {
if val == "true" || val == "1" {
FORMAT_DISABLED=true
}

// gracefully init images / containers
hfmt.GracefulInit()
err := hfmt.GracefulInit()
if err != nil {
FORMAT_DISABLED=true
}
}

func (F *File) FormatRendered() error {
type FmtConfig struct {
Formatter string
Config interface{}
}

// via container / system
if !FORMAT_DISABLED {
func (F *File) FormatRendered() (err error) {
// should we format? (has it been disabled anywhere)
if !(FORMAT_DISABLED || F.FormattingDisabled) {
// inspect file settings to see if there is fmtr config...
// try using hof/fmt containers, this is auto inference
fmtd, err := hfmt.FormatSource(F.Filepath, F.RenderContent, "", nil, false)
if err != nil {
return err

// current content
fmtd := F.RenderContent

// check for custom config
if F.FormattingConfig != nil && F.FormattingConfig.Config != nil {
fmtd, err = hfmt.FormatSource(
F.Filepath, fmtd,
F.FormattingConfig.Formatter,
F.FormattingConfig.Config,
!F.FormattingDisabled,
)
if err != nil {
return err
}
} else {
fmtd, err = hfmt.FormatSource(F.Filepath, fmtd, "", nil, !F.FormattingDisabled)
if err != nil {
return err
}
}

// update content
F.RenderContent = fmtd
}

return nil
}

Expand Down
16 changes: 9 additions & 7 deletions lib/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ func runGen(args []string, rootflags flags.RootPflagpole, cmdflags flags.GenFlag
}
// TODO: we could make this configurable
R.WorkingDir, _ = os.Getwd()
R.cwdToRoot, err = filepath.Rel(R.WorkingDir, R.CueModuleRoot)
if err != nil {
return err
}
R.rootToCwd, err = filepath.Rel(R.CueModuleRoot, R.WorkingDir)
if err != nil {
return err
if R.CueModuleRoot != "" {
R.cwdToRoot, err = filepath.Rel(R.WorkingDir, R.CueModuleRoot)
if err != nil {
return err
}
R.rootToCwd, err = filepath.Rel(R.CueModuleRoot, R.WorkingDir)
if err != nil {
return err
}
}

// log cue dirs
Expand Down
5 changes: 5 additions & 0 deletions lib/gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ type Generator struct {
WatchFull []string
WatchFast []string

// Formatting
FormattingDisabled bool
FormatData bool
FormattingConfigs map[string]FmtConfig

// The list fo files for hof to generate, in cue values
Out []*File

Expand Down
84 changes: 84 additions & 0 deletions lib/gen/loadcue.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
)

func (G *Generator) DecodeFromCUE() (errs []error) {
// TODO, what if a user's generator doesn't use the schema?
// happens when to unspecified fields?
// should we just unify with the schema here too?
// what about versions?

// fmt.Println("Gen Load:", G.Name)
start := time.Now()

Expand All @@ -31,6 +36,14 @@ func (G *Generator) DecodeFromCUE() (errs []error) {
errs = append(errs, err)
}

if err := G.loadFormattingBools(); err != nil {
errs = append(errs, err)
}

if err := G.loadFormattingConfigs(); err != nil {
errs = append(errs, err)
}

if err := G.loadTemplates(); err != nil {
errs = append(errs, err)
}
Expand Down Expand Up @@ -146,6 +159,40 @@ func (G *Generator) loadWatchFast() error {
return val.Decode(&G.WatchFast)
}

func (G *Generator) loadFormattingBools() (err error) {
val := G.CueValue.LookupPath(cue.ParsePath("Formatting.Disabled"))
if val.Err() != nil {
return nil
// return val.Err()
}
G.FormattingDisabled, err = val.Bool()
if err != nil {
return err
}

val = G.CueValue.LookupPath(cue.ParsePath("Formatting.FormatData"))
if val.Err() != nil {
return nil
// return val.Err()
}
G.FormatData, err = val.Bool()
if err != nil {
return err
}
return nil
}

func (G *Generator) loadFormattingConfigs() error {
val := G.CueValue.LookupPath(cue.ParsePath("Formatting.Formatters"))
if val.Err() != nil {
return nil
return val.Err()
}

G.FormattingConfigs = make(map[string]FmtConfig)
return val.Decode(&G.FormattingConfigs)
}

func (G *Generator) loadTemplates() error {
val := G.CueValue.LookupPath(cue.ParsePath("Templates"))
if val.Err() != nil {
Expand Down Expand Up @@ -316,6 +363,43 @@ func (G *Generator) loadFile(file *File, val cue.Value) error {

}

// Formatting
var err error
fval := val.LookupPath(cue.ParsePath("Formatting"))
if fval.Err() == nil && fval.Exists() {
fdval := fval.LookupPath(cue.ParsePath("Disabled"))
if fdval.Err() == nil && fdval.Exists() {
file.FormattingDisabled, err = fdval.Bool()
if err != nil {
return err
}
} else {
// use default from Generator, depending on file type (tmpl|data)
if file.Value.Exists() {
file.FormattingDisabled = !G.FormatData
} else {
file.FormattingDisabled = G.FormattingDisabled
}
}

ffval := fval.LookupPath(cue.ParsePath("Foramtter"))
if ffval.Err() == nil && ffval.Exists() {
cfg := new(FmtConfig)
file.FormattingConfig = cfg
cfg.Formatter, err = ffval.String()
if err != nil {
return err
}

fcval := fval.LookupPath(cue.ParsePath("Config"))
err = fcval.Decode(&cfg.Config)
if err != nil {
return err
}
}
}


G.Out = append(G.Out, file)
}

Expand Down
9 changes: 9 additions & 0 deletions schema/gen/file.cue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ package gen
// '.' will bre replaced by generator defaults
TemplateDelims?: #TemplateDelims

// Formatting Control
Formatting?: {
Disabled?: bool
// Name of the formatter, like 'prettier' or 'black'
Formatter: string
// formatter specific configuration
Config: _
}

// Note, intentionally closed to prevent user error when creating GenFiles
}

Expand Down
23 changes: 23 additions & 0 deletions schema/gen/generator.cue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ package gen
// Enable Diff3
Diff3: bool | *true

// Formatting Control
Formatting: {
// default for all files, unless overriden in a file
Disabled: bool | *false

// Should data files also be formatted?
// (cue,yaml,json,toml,xml)
FormatData: bool | *true

// Map of names to formatter config values.
// Supports multiple configurations for a formatter,
// particularly useful for prettier.
// Hof has defaults it will use if none are specified

// map from file extensions to formatters
Formatters: [Extenstion=string]: {
// Name of the formatter, like 'prettier' or 'black'
Formatter: string
// formatter specific configuration
Config: _
}
}

// The final list of files for hof to generate
Out: [...#File]

Expand Down

0 comments on commit eef2383

Please sign in to comment.