Skip to content

Commit

Permalink
wip: flatten .variant files
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Sep 26, 2020
1 parent 604396c commit 01c55e4
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
26 changes: 26 additions & 0 deletions export_embedded.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package variant

import (
"github.com/spf13/cobra"
)

func newExportFlattened(r *Runner) *cobra.Command {
return &cobra.Command{
Use: "flattened SRC_DIR DST_DIR",
Short: "Process all the imports and export the \"flattened\" variant command to DST_DIR",
Example: `$ variant export embedded examples/simple flattened
$ cd flattened
$ variant run -h
`,
Args: cobra.ExactArgs(2),
RunE: func(c *cobra.Command, args []string) error {
err := r.ap.ExportFlattened(args[0], args[1])
if err != nil {
c.SilenceUsage = true
}
return err
},
}
}
76 changes: 76 additions & 0 deletions pkg/app/export_flattened.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package app

import (
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/hashicorp/hcl/v2/ext/typeexpr"
"github.com/mumoshu/variant2/pkg/conf"
"io/ioutil"
"os"
"path/filepath"
)

func (app *App) ExportFlattened(srcDir, dstDir string) error {
if err := os.MkdirAll(dstDir, 0755); err != nil {
return err
}

a, err := New(FromDir(srcDir))
if err != nil {
return err
}

f := hclwrite.NewEmptyFile()

{
rootBody := f.Body()

for _, j := range a.JobByName {
if j.Name == "" {
continue
}
rootBody.AppendNewline()
jobBlock := rootBody.AppendNewBlock("job", []string{j.Name})
jobBody := jobBlock.Body()

for i, o := range j.Options {
if i != 0 {
jobBody.AppendNewline()
}
optBlock := jobBody.AppendNewBlock("option", []string{o.Name})
tpe, diagnostics := typeexpr.TypeConstraint(o.Type)
if diagnostics.HasErrors() {
return diagnostics
}
v := typeexpr.TypeConstraintVal(tpe)
optBlock.Body().SetAttributeValue("type", v)
}

for i, p := range j.Parameters {
if i != 0 {
jobBody.AppendNewline()
}
paramBlock := jobBody.AppendNewBlock("parameter", []string{p.Name})
tpe, diagnostics := typeexpr.TypeConstraint(p.Type)
if diagnostics.HasErrors() {
return diagnostics
}
v := typeexpr.TypeConstraintVal(tpe)
paramBlock.Body().SetAttributeValue("type", v)
}
}
}

tokens := f.BuildTokens(nil)
raw := tokens.Bytes()
formatted := hclwrite.Format(raw)

binName := filepath.Base(dstDir)

cfgPath := filepath.Join(dstDir, binName+conf.VariantFileExt)

if err := ioutil.WriteFile(cfgPath, formatted, 0644); err != nil {
return err
}

return nil
}
19 changes: 14 additions & 5 deletions pkg/app/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func newConfigFromSources(srcs map[string][]byte) (map[string]*hcl.File, *HCL2Co
return nameToFiles, cc, err
}

func newApp(app *App, cc *HCL2Config, importDir func(string) (*App, error)) (*App, error) {
func processImports(cc *HCL2Config, importDir func(string) (*App, error)) (*HCL2Config, map[string]JobSpec, error) {
jobs := append([]JobSpec{cc.JobSpec}, cc.Jobs...)

var conf *HCL2Config
Expand All @@ -280,7 +280,7 @@ func newApp(app *App, cc *HCL2Config, importDir func(string) (*App, error)) (*Ap
a, err := importDir(src)

if err != nil {
return nil, err
return nil, nil, err
}

importedJobs := append([]JobSpec{a.Config.JobSpec}, a.Config.Jobs...)
Expand All @@ -294,7 +294,7 @@ func newApp(app *App, cc *HCL2Config, importDir func(string) (*App, error)) (*Ap
// their types MUST match.
merged, err := mergeParamsAndOpts(importedJob, j)
if err != nil {
return nil, fmt.Errorf("merging globals: %w", err)
return nil, nil, fmt.Errorf("merging globals: %w", err)
}

merged.Name = ""
Expand Down Expand Up @@ -331,7 +331,7 @@ func newApp(app *App, cc *HCL2Config, importDir func(string) (*App, error)) (*Ap
for _, g := range globals {
merged, err := mergeParamsAndOpts(g, root)
if err != nil {
return nil, fmt.Errorf("merging globals: %w", err)
return nil, nil, fmt.Errorf("merging globals: %w", err)
}

root = *merged
Expand All @@ -343,9 +343,18 @@ func newApp(app *App, cc *HCL2Config, importDir func(string) (*App, error)) (*Ap
conf = cc
}

return conf, jobByName, nil
}

func newApp(app *App, cc *HCL2Config, importDir func(string) (*App, error)) (*App, error) {
conf, jobByName, err := processImports(cc, importDir)
if err != nil {
return nil, fmt.Errorf("processing imports: %w", err)
}

app.Config = conf

app.Config.JobSpec = root
app.Config.JobSpec = jobByName[""]

app.JobByName = jobByName

Expand Down
1 change: 1 addition & 0 deletions variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ func (r *Runner) createVariantRootCommand() *cobra.Command {
exportCmd.AddCommand(shimCmd)
exportCmd.AddCommand(newExportGo(r))
exportCmd.AddCommand(newExportBinary(r))
exportCmd.AddCommand(newExportFlattened(r))
}

generateCmd := &cobra.Command{
Expand Down

0 comments on commit 01c55e4

Please sign in to comment.