Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Oct 21, 2024
1 parent d6e0557 commit 871bad8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 54 deletions.
7 changes: 4 additions & 3 deletions resources/resource_transformers/js/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,11 @@ func (c *Client) build(opts Options, transformCtx *resources.ResourceTransformat
}

errorMessage = msg.Text
errorMessage = strings.ReplaceAll(errorMessage, NsImportHugo+":", "")
// TODO1 handle all namespaces, make more general
errorMessage = strings.ReplaceAll(errorMessage, NsHugoImport+":", "")

if strings.HasPrefix(errorPath, NsImportHugo) {
errorPath = strings.TrimPrefix(errorPath, NsImportHugo+":")
if strings.HasPrefix(errorPath, NsHugoImport) {
errorPath = strings.TrimPrefix(errorPath, NsHugoImport+":")
contentr, err = hugofs.Os.Open(errorPath)
} else {
var fi os.FileInfo
Expand Down
45 changes: 25 additions & 20 deletions resources/resource_transformers/js/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ import (
)

const (
NsImportHugo = "ns-hugo"
nsParams = "ns-params"
NsHugoImport = "ns-hugo-import"
NsHugoImportResolveFunc = "ns-hugo-import-resolvefunc"
nsHugoParams = "ns-hugo-params"

stdinImporter = "<stdin>"
)
Expand Down Expand Up @@ -270,7 +271,7 @@ func createBuildPlugins(c *Client, depsManager identity.Manager, opts Options) (

if opts.ImportOnResolveFunc != nil {
if s := opts.ImportOnResolveFunc(impPath, args); s != "" {
return api.OnResolveResult{Path: s, Namespace: NsImportHugo}, nil
return api.OnResolveResult{Path: s, Namespace: NsHugoImportResolveFunc}, nil
}
}

Expand Down Expand Up @@ -318,7 +319,7 @@ func createBuildPlugins(c *Client, depsManager identity.Manager, opts Options) (
// in server mode, we may get stale entries on renames etc.,
// but that shouldn't matter too much.
c.rs.JSConfigBuilder.AddSourceRoot(m.SourceRoot)
return api.OnResolveResult{Path: m.Filename, Namespace: NsImportHugo}, nil
return api.OnResolveResult{Path: m.Filename, Namespace: NsHugoImport}, nil
}

// Fall back to ESBuild's resolve.
Expand All @@ -332,22 +333,13 @@ func createBuildPlugins(c *Client, depsManager identity.Manager, opts Options) (
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
return resolveImport(args)
})
build.OnLoad(api.OnLoadOptions{Filter: `.*`, Namespace: NsImportHugo},
build.OnLoad(api.OnLoadOptions{Filter: `.*`, Namespace: NsHugoImport},
func(args api.OnLoadArgs) (api.OnLoadResult, error) {
var c string
if opts.ImportOnLoadFunc != nil {
if s := opts.ImportOnLoadFunc(args); s != "" {
c = s
}
}

if c == "" {
b, err := os.ReadFile(args.Path)
if err != nil {
return api.OnLoadResult{}, fmt.Errorf("failed to read %q: %w", args.Path, err)
}
c = string(b)
b, err := os.ReadFile(args.Path)
if err != nil {
return api.OnLoadResult{}, fmt.Errorf("failed to read %q: %w", args.Path, err)
}
c := string(b)

return api.OnLoadResult{
// See https://github.com/evanw/esbuild/issues/502
Expand All @@ -358,6 +350,19 @@ func createBuildPlugins(c *Client, depsManager identity.Manager, opts Options) (
Loader: loaderFromFilename(args.Path),
}, nil
})
build.OnLoad(api.OnLoadOptions{Filter: `.*`, Namespace: NsHugoImportResolveFunc},
func(args api.OnLoadArgs) (api.OnLoadResult, error) {
c := opts.ImportOnLoadFunc(args)
if c == "" {
return api.OnLoadResult{}, fmt.Errorf("ImportOnLoadFunc failed to resolve %q", args.Path)
}

return api.OnLoadResult{
ResolveDir: opts.ResolveDir,
Contents: &c,
Loader: loaderFromFilename(args.Path),
}, nil
})
},
}

Expand All @@ -378,10 +383,10 @@ func createBuildPlugins(c *Client, depsManager identity.Manager, opts Options) (
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
return api.OnResolveResult{
Path: args.Importer,
Namespace: nsParams,
Namespace: nsHugoParams,
}, nil
})
build.OnLoad(api.OnLoadOptions{Filter: `.*`, Namespace: nsParams},
build.OnLoad(api.OnLoadOptions{Filter: `.*`, Namespace: nsHugoParams},
func(args api.OnLoadArgs) (api.OnLoadResult, error) {
bb := b

Expand Down
5 changes: 1 addition & 4 deletions tpl/internal/go_templates/texttemplate/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,7 @@ func (s *state) walkIfOrWith(typ parse.NodeType, dot reflect.Value, pipe *parse.
}
if truth {
if typ == parse.NodeWith {
func() {
defer s.pushWithValue(val)()
s.walk(val, list)
}()
s.walk(val, list)
} else {
s.walk(dot, list)
}
Expand Down
34 changes: 8 additions & 26 deletions tpl/internal/go_templates/texttemplate/hugo_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"reflect"

"github.com/gohugoio/hugo/common/hreflect"
"github.com/gohugoio/hugo/common/types"

"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
)
Expand Down Expand Up @@ -111,31 +110,14 @@ func (t *Template) executeWithState(state *state, value reflect.Value) (err erro
// template so that multiple executions of the same template
// can execute in parallel.
type state struct {
tmpl *Template
ctx context.Context // Added for Hugo. The original data context.
prep Preparer // Added for Hugo.
helper ExecHelper // Added for Hugo.
withValues []reflect.Value // Added for Hugo. Push-down stack of values.

wr io.Writer
node parse.Node // current node, for errors
vars []variable // push-down stack of variable values.
depth int // the height of the stack of executing templates.
}

func (s *state) pushWithValue(value reflect.Value) func() {
s.withValues = append(s.withValues, value)
return func() {
// TODO1 remove all this.
v, _ := indirect(s.withValues[len(s.withValues)-1])
if hreflect.IsValid(v) {
if closer, ok := v.Interface().(types.Closer); ok {
closer.Close()
}
}

s.withValues = s.withValues[:len(s.withValues)-1]
}
tmpl *Template
ctx context.Context // Added for Hugo. The original data context.
prep Preparer // Added for Hugo.
helper ExecHelper // Added for Hugo.
wr io.Writer
node parse.Node // current node, for errors
vars []variable // push-down stack of variable values.
depth int // the height of the stack of executing templates.
}

func (s *state) evalFunction(dot reflect.Value, node *parse.IdentifierNode, cmd parse.Node, args []parse.Node, final reflect.Value) reflect.Value {
Expand Down
2 changes: 1 addition & 1 deletion tpl/js/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ func (b *batcher) doBuild() (*Package, error) {
},
ErrorMessageResolveFunc: func(args api.Message) *js.ErrorMessageResolved {
if loc := args.Location; loc != nil {
path := strings.TrimPrefix(loc.File, js.NsImportHugo+":")
path := strings.TrimPrefix(loc.File, js.NsHugoImportResolveFunc+":")
if r, found := state.importResource.Get(path); found {
path = strings.TrimPrefix(path, js.PrefixHugoVirtual)
var contentr hugio.ReadSeekCloser
Expand Down

0 comments on commit 871bad8

Please sign in to comment.