Skip to content

Commit

Permalink
Add origin refers to workflow original path.
Browse files Browse the repository at this point in the history
  • Loading branch information
jellyterra committed Sep 26, 2024
1 parent ea879cb commit 187d1a2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
22 changes: 13 additions & 9 deletions cmd/pagine/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func CollectAndRunWorkflows(root, dest *vfs.DirFS, jobBuilderRoot fs.StatFS) error {
bases, err := CollectWorkflows(root, dest)
bases, err := CollectWorkflows(root)
if err != nil {
return err
}
Expand All @@ -27,7 +27,16 @@ func CollectAndRunWorkflows(root, dest *vfs.DirFS, jobBuilderRoot fs.StatFS) err
var workflows []*workflow.Workflow

for _, base := range bases {
wf, err := config.LoadWorkflow(base, jobBuilderRoot)
origin, err := root.Chroot(base)
if err != nil {
return err
}
wfRoot, err := dest.Chroot(base)
if err != nil {
return err
}

wf, err := config.LoadWorkflow(origin, wfRoot, jobBuilderRoot)
if err != nil {
return err
}
Expand Down Expand Up @@ -56,7 +65,7 @@ func CollectAndRunWorkflows(root, dest *vfs.DirFS, jobBuilderRoot fs.StatFS) err
return nil
}

func CollectWorkflows(root, dest *vfs.DirFS) (dirs []*vfs.DirFS, _ error) {
func CollectWorkflows(root *vfs.DirFS) (dirs []string, _ error) {
return dirs, fs.WalkDir(root, "/", func(path string, _ fs.DirEntry, err error) error {
if err != nil {
return err
Expand All @@ -67,12 +76,7 @@ func CollectWorkflows(root, dest *vfs.DirFS) (dirs []*vfs.DirFS, _ error) {
return nil // Skip.
}

sub, err := dest.Chroot(path)
if err != nil {
return err
}

dirs = append(dirs, sub)
dirs = append(dirs, path)

return nil
})
Expand Down
12 changes: 6 additions & 6 deletions config/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@ type Workflow struct {
} `yaml:"stage"`
}

func LoadJob(root *vfs.DirFS, m map[string]any, jobBuilderRoot fs.StatFS) (*workflow.Job, error) {
func LoadJob(origin, root *vfs.DirFS, m map[string]any, jobBuilderRoot fs.StatFS) (*workflow.Job, error) {
if m["type"] == nil {
return nil, fmt.Errorf("job type is not specified")
}
jobBuilderFile := m["type"].(string) + ".tmpl"

if jobBuilderRoot == nil {
return workflow.BuildJob(builtin.Builders, jobBuilderFile, root, m)
return workflow.BuildJob(builtin.Builders, jobBuilderFile, origin, root, m)
}

_, err := jobBuilderRoot.Stat(jobBuilderFile)
switch {
case err == nil:
case os.IsNotExist(err):
return workflow.BuildJob(builtin.Builders, jobBuilderFile, root, m)
return workflow.BuildJob(builtin.Builders, jobBuilderFile, origin, root, m)
default:
return nil, err
}

return workflow.BuildJob(jobBuilderRoot, jobBuilderFile, root, m)
return workflow.BuildJob(jobBuilderRoot, jobBuilderFile, origin, root, m)
}

func LoadWorkflow(root *vfs.DirFS, jobBuilderRoot fs.StatFS) (*workflow.Workflow, error) {
func LoadWorkflow(origin, root *vfs.DirFS, jobBuilderRoot fs.StatFS) (*workflow.Workflow, error) {

var (
rawWorkflow Workflow
Expand All @@ -64,7 +64,7 @@ func LoadWorkflow(root *vfs.DirFS, jobBuilderRoot fs.StatFS) (*workflow.Workflow
var jobs collection.Vector[*workflow.Job]

for _, job := range stage.Job {
job, err := LoadJob(root, job, jobBuilderRoot)
job, err := LoadJob(origin, root, job, jobBuilderRoot)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion workflow/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"text/template"
)

func BuildJob(builderTmplRoot fs.FS, builderTmpl string, root *vfs.DirFS, config map[string]any) (*Job, error) {
func BuildJob(builderTmplRoot fs.FS, builderTmpl string, origin, root *vfs.DirFS, config map[string]any) (*Job, error) {
var args common.Slice
var export = common.Map{
Raw: map[string]any{
Expand All @@ -26,6 +26,7 @@ func BuildJob(builderTmplRoot fs.FS, builderTmpl string, root *vfs.DirFS, config
}

funcMap := map[string]any{
"origin": common.WrapObject(origin.Path),
"root": common.WrapObject(root.Path),
"job": common.WrapObject(&export),
"path": common.WrapObject(common.V1Path{}),
Expand Down
8 changes: 5 additions & 3 deletions workflow/builtin/tailwindcss/v1.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{{ if eq .title nil }}{{ panic "missing property: title" }}{{ end }}
{{ if eq .config nil }}{{ panic "missing property: config" }}{{ end }}
{{ if eq .input nil }}{{ panic "missing property: input" }}{{ end }}
{{ if eq .output nil }}{{ panic "missing property: output" }}{{ end }}
{{ job.Set "title" .title }}
{{ if strings.Contains .config ".." }}{{ panic (printf "%s is illegal" .config) }}{{ end }}
{{ if strings.Contains .input ".." }}{{ panic (printf "%s is illegal" .input) }}{{ end }}
{{ if strings.Contains .output ".." }}{{ panic (printf "%s is illegal" .output) }}{{ end }}
{{ job.Set "title" "Build Tailwind CSS" }}
{{ job.Set "executable" "tailwindcss" }}
{{ (job.Get "args").Push "-c" (path.Join root .config) "--input" (path.Join root .input) "--output" (path.Join root .output) }}
{{ (job.Get "args").Push "-c" (path.Join origin .config) "--input" (path.Join origin .input) "--output" (path.Join root .output) }}
1 change: 0 additions & 1 deletion workflow/builtin/tsc/v1.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{{ if eq .path nil }}{{ panic "missing property: path" }}{{ end }}
{{ if eq .title nil }}{{ panic "missing property: title" }}{{ end }}
{{ job.Set "title" "Build TypeScript" }}
{{ job.Set "executable" "tsc" }}
{{ if strings.Contains .path ".." }}{{ panic (printf "%s is illegal" .path) }}{{ end }}
Expand Down

0 comments on commit 187d1a2

Please sign in to comment.