Skip to content

Commit

Permalink
Templates can access modules in top order
Browse files Browse the repository at this point in the history
Use `.OrderedModules` property to access modules
in their topological order.
  • Loading branch information
buddhike committed Apr 19, 2020
1 parent 286a70c commit 42a13c3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/apply.go
Expand Up @@ -31,11 +31,12 @@ import (

// TemplateData is the data passed into template.
type TemplateData struct {
Args map[string]interface{}
Sha string
Env map[string]string
Modules map[string]*Module
ModulesList []*Module
Args map[string]interface{}
Sha string
Env map[string]string
Modules map[string]*Module
ModulesList []*Module
OrderedModules []*Module
}

// KVP is a key value pair.
Expand Down Expand Up @@ -288,10 +289,11 @@ func processTemplate(buffer []byte, m *Manifest, output io.Writer) error {
}

data := &TemplateData{
Sha: m.Sha,
Env: getEnvMap(),
Modules: m.Modules.indexByName(),
ModulesList: sortedModules,
Sha: m.Sha,
Env: getEnvMap(),
Modules: m.Modules.indexByName(),
ModulesList: sortedModules,
OrderedModules: m.Modules,
}

return temp.Execute(output, data)
Expand Down
22 changes: 22 additions & 0 deletions lib/apply_test.go
Expand Up @@ -388,3 +388,25 @@ func TestTheOrderOfModulesList(t *testing.T) {

assert.Equal(t, "app-a,app-b,app-c,\n", output.String())
}

func TestTheOrderOfOrderedModulesList(t *testing.T) {
clean()
repo := NewTestRepo(t, ".tmp/repo")

check(t, repo.InitModuleWithOptions("app-a", &Spec{Name: "app-a", Dependencies: []string{"app-b"}}))
check(t, repo.InitModuleWithOptions("app-b", &Spec{Name: "app-b", Dependencies: []string{"app-c"}}))
check(t, repo.InitModule("app-c"))

check(t, repo.WriteContent("template.tmpl", `
{{- range $i, $mod := .OrderedModules}}
{{- $mod.Name }},
{{- end}}
`))

check(t, repo.Commit("first"))

output := new(bytes.Buffer)
check(t, NewWorld(t, ".tmp/repo").System.ApplyHead("template.tmpl", output))

assert.Equal(t, "app-c,app-b,app-a,\n", output.String())
}

0 comments on commit 42a13c3

Please sign in to comment.