Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mechanism to produce an import path for a go file. #38

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,17 @@ The following fragments are predefined:
* `{{outName}}` is the base name of the output file being written, including any extensions
* `{{outRoot}}` is the base name of the output file being written without any extensions
* `{{outParent}}` is the directory name of the output file being written, without the preceeding path
* `{{importPath}}` produces the import path of the output file
* `{{importParent}}` produces the parent of the import path of the output file

The template* fragments refer to the top level file, the one sent to the got command to process,
while the include* fragments refer to the file being processed currently. For example, if the
"a.tmpl" file included the "b.tmpl" file, then {{includeName}} in the b.tmpl file would
produce "b.tmpl", while {{templateName}} in the b.tmpl file would produce "a.tmpl".

importPath and importParent are used to generate import paths to files that are relative
to the output file, since go does not support relative import paths.

#### Example
```

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/goradd/got

require (
github.com/goradd/gofile v1.1.1
github.com/goradd/gofile v1.2.0
github.com/stretchr/testify v1.8.4
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/goradd/gofile v1.1.1 h1:Qi7L4WvIK+LjTujpZRRux4BZ8/OFhnQzMRASM/akFGo=
github.com/goradd/gofile v1.1.1/go.mod h1:ZjSvnGak2csGsJgEu8AgQc06eaoonhg2MzbqXON9o1M=
github.com/goradd/gofile v1.2.0 h1:sCHG3icAFGO8gwl5Yy8llvhlIl8k5uaYa6uE23Z56MA=
github.com/goradd/gofile v1.2.0/go.mod h1:ZjSvnGak2csGsJgEu8AgQc06eaoonhg2MzbqXON9o1M=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
24 changes: 15 additions & 9 deletions internal/got/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ func lexFile(fileName string,
incRoot := namedBlocks[blockIncludeRoot].text
incParent := namedBlocks[blockIncludeParent].text

namedBlocks[blockIncludePath] = namedBlockEntry{fp, 0, locationRef{}}
namedBlocks[blockIncludeName] = namedBlockEntry{filepath.Base(fp), 0, locationRef{}}
namedBlocks[blockIncludeRoot] = namedBlockEntry{root, 0, locationRef{}}
namedBlocks[blockIncludeParent] = namedBlockEntry{filepath.Base(filepath.Dir(fp)), 0, locationRef{}}
namedBlocks[blockIncludePath] = namedBlockEntry{fp, 0, locationRef{}, nil}
namedBlocks[blockIncludeName] = namedBlockEntry{filepath.Base(fp), 0, locationRef{}, nil}
namedBlocks[blockIncludeRoot] = namedBlockEntry{root, 0, locationRef{}, nil}
namedBlocks[blockIncludeParent] = namedBlockEntry{filepath.Base(filepath.Dir(fp)), 0, locationRef{}, nil}

l.run()

// restore
namedBlocks[blockIncludePath] = namedBlockEntry{incPath, 0, locationRef{}}
namedBlocks[blockIncludeName] = namedBlockEntry{incName, 0, locationRef{}}
namedBlocks[blockIncludeRoot] = namedBlockEntry{incRoot, 0, locationRef{}}
namedBlocks[blockIncludeParent] = namedBlockEntry{incParent, 0, locationRef{}}
namedBlocks[blockIncludePath] = namedBlockEntry{incPath, 0, locationRef{}, nil}
namedBlocks[blockIncludeName] = namedBlockEntry{incName, 0, locationRef{}, nil}
namedBlocks[blockIncludeRoot] = namedBlockEntry{incRoot, 0, locationRef{}, nil}
namedBlocks[blockIncludeParent] = namedBlockEntry{incParent, 0, locationRef{}, nil}
}()
return l
}
Expand Down Expand Up @@ -483,6 +483,12 @@ func processParams(name string, in namedBlockEntry, params []string) (out string
var i int
var s string

if in.f != nil {
// A processing function is being used
out, err = in.f(in.text, params)
return
}

if len(params) == 1 &&
params[0] == "" &&
in.paramCount == 0 {
Expand Down Expand Up @@ -895,7 +901,7 @@ func (l *lexer) addNamedBlock(name string, text string, paramCount int) error {
blockName: l.blockName,
lineNum: l.lineNum,
offset: l.lineRuneNum,
}}
}, nil}
return nil
}

Expand Down
48 changes: 36 additions & 12 deletions internal/got/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"

Expand All @@ -28,12 +29,16 @@ const (
blockIncludeName = "includeName"
blockIncludeRoot = "includeRoot"
blockIncludeParent = "includeParent"

blockImportPath = "importPath"
blockImportParent = "importParent"
)

type namedBlockEntry struct {
text string
paramCount int
ref locationRef
f func(string, []string) (string, error) // process text at the time the fragment is used
}

var modules map[string]string
Expand Down Expand Up @@ -153,10 +158,10 @@ func processFile(file, outDir string, asts []astType, runImports bool) error {
}

// Default named block values
namedBlocks[blockIncludePath] = namedBlockEntry{"", 0, locationRef{}}
namedBlocks[blockIncludeName] = namedBlockEntry{"", 0, locationRef{}}
namedBlocks[blockIncludeRoot] = namedBlockEntry{"", 0, locationRef{}}
namedBlocks[blockIncludeParent] = namedBlockEntry{"", 0, locationRef{}}
namedBlocks[blockIncludePath] = namedBlockEntry{"", 0, locationRef{}, nil}
namedBlocks[blockIncludeName] = namedBlockEntry{"", 0, locationRef{}, nil}
namedBlocks[blockIncludeRoot] = namedBlockEntry{"", 0, locationRef{}, nil}
namedBlocks[blockIncludeParent] = namedBlockEntry{"", 0, locationRef{}, nil}

file, _ = filepath.Abs(file)
root := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
Expand All @@ -168,10 +173,10 @@ func processFile(file, outDir string, asts []astType, runImports bool) error {
root = strings.TrimSuffix(root, ext)
}

namedBlocks[blockTemplatePath] = namedBlockEntry{file, 0, locationRef{}}
namedBlocks[blockTemplateName] = namedBlockEntry{filepath.Base(file), 0, locationRef{}}
namedBlocks[blockTemplateRoot] = namedBlockEntry{root, 0, locationRef{}}
namedBlocks[blockTemplateParent] = namedBlockEntry{filepath.Base(filepath.Dir(file)), 0, locationRef{}}
namedBlocks[blockTemplatePath] = namedBlockEntry{file, 0, locationRef{}, nil}
namedBlocks[blockTemplateName] = namedBlockEntry{filepath.Base(file), 0, locationRef{}, nil}
namedBlocks[blockTemplateRoot] = namedBlockEntry{root, 0, locationRef{}, nil}
namedBlocks[blockTemplateParent] = namedBlockEntry{filepath.Base(filepath.Dir(file)), 0, locationRef{}, nil}

newPath, _ = filepath.Abs(newPath)
root = strings.TrimSuffix(filepath.Base(newPath), filepath.Ext(newPath))
Expand All @@ -183,10 +188,16 @@ func processFile(file, outDir string, asts []astType, runImports bool) error {
root = strings.TrimSuffix(root, ext)
}

namedBlocks[blockOutPath] = namedBlockEntry{newPath, 0, locationRef{}}
namedBlocks[blockOutName] = namedBlockEntry{filepath.Base(newPath), 0, locationRef{}}
namedBlocks[blockOutRoot] = namedBlockEntry{root, 0, locationRef{}}
namedBlocks[blockOutParent] = namedBlockEntry{filepath.Base(filepath.Dir(newPath)), 0, locationRef{}}
namedBlocks[blockOutPath] = namedBlockEntry{newPath, 0, locationRef{}, nil}
namedBlocks[blockOutName] = namedBlockEntry{filepath.Base(newPath), 0, locationRef{}, nil}
namedBlocks[blockOutRoot] = namedBlockEntry{root, 0, locationRef{}, nil}
namedBlocks[blockOutParent] = namedBlockEntry{filepath.Base(filepath.Dir(newPath)), 0, locationRef{}, nil}

// These two are slow functions, so we cannot call them on every file,
// but rather just when they are needed, so we use a processing function
// that gets called at the time the nameBlock is invoked.
namedBlocks[blockImportPath] = namedBlockEntry{newPath, 0, locationRef{}, importPath}
namedBlocks[blockImportParent] = namedBlockEntry{newPath, 0, locationRef{}, importParent}

a, err := buildAst(file, namedBlocks)
if err != nil {
Expand Down Expand Up @@ -373,3 +384,16 @@ func fileIsNewer(path1, path2 string) bool {
modTime2 := file2.ModTime()
return modTime1.After(modTime2)
}

func importPath(outPath string, params []string) (string, error) {
return sys.ImportPath(outPath)
}

func importParent(outPath string, params []string) (string, error) {
out, err := sys.ImportPath(outPath)
if err != nil {
return "", err
}
dir := path.Dir(out)
return dir, err
}
2 changes: 2 additions & 0 deletions internal/testdata/expected/TestPredefines.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ template
runner.inc
runner
inc
github.com/goradd/got/internal/testdata/template
github.com/goradd/got/internal/testdata


testPredefines.tpl.got
Expand Down
3 changes: 3 additions & 0 deletions internal/testdata/src/testPredefines.tpl.got
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
{{includeName}}
{{includeRoot}}
{{includeParent}}
{{# test processing functions }}
{{importPath}}
{{importParent}}

{{: "predef.inc" }}

Expand Down
Loading