Skip to content

Commit

Permalink
feat: Add support for trim input prefix and set the prefix to the ass…
Browse files Browse the repository at this point in the history
…et name.

Detail:
the tree (~/go/src/github.com/user/project):

    ./
    ./assets/
        templates/admin/template.tmpl
        static/js/vendors/jquery.min.js
    ./.xbinputs.yml

contents of ./.xbinputs.yml:
sources:
    - path: assets
      prefix: _
      recursive: true
      ns: "(2)/$PKG/.."

The new translate process "[C=(2)]/[B=$PKG]/[A=..]":
1. the Variables:
    - BASE_DIR: `assets`
    - BASE_DIR ABS PATH: `~/go/src/github.com/user/project/assets`
    - ASSETS_PACKAGE (inside `$GOPATH/src`): `github.com/user/project/assets`
2. translate the namespace:
    1. initial value: `(2)/$PKG/..`
    2. replaces `$PKG` to ASSETS_PACKAGE: `(2)/github.com/user/project/assets/..`
    3. clean it: `(2)/github.com/user/project`
    4. detect namespace DIR_REPLACES_COUNT: `(2)` -> set DIR_REPLACES_COUNT to `2`
3. walk assets:
    1. `templates/admin/template.tmpl`:
        1. detect DIR_REPLACES_COUNT (as `2`) values (from root path): two values:
            - first value `templates`
            - second value `admin`
            - result: `templates/admin`
        2. remove DIR_REPLACES_COUNT match in template path:
            - current template path is `templates/admin/template.tmpl`
            - now template path is: `template.tmpl`
        3. replaces DIR_REPLACES_COUNT match in namespace:
            - current namespace is `(2)/github.com/user/project`
            - replaces `(2)` to `templates/admin`
            - result: `templates/admin/github.com/user/project`
        3. make ASSET_NAME: join namespace and template path:
            - namespace is `templates/admin/github.com/user/project`
            - template path is `template.tmpl`
            - ASSET_NAME is `templates/admin/github.com/user/project/template.tmpl`
    2. `static/js/vendors/jquery.min.js`:
        1. detect DIR_REPLACES_COUNT (as `2`) values (from root path): two values:
            - first value `static`
            - second value `js`
            - result: `static/js`
        2. remove DIR_REPLACES_COUNT match in template path:
            - current template path is `static/js/vendors/jquery.min.js`
            - now template path is: `vendors/jquery.min.js`
        3. replaces DIR_REPLACES_COUNT match in namespace:
            - current namespace is `(2)/github.com/user/project`
            - replaces `(2)` to `static/js`
            - result: `static/js/github.com/user/project`
        3. make ASSET_NAME: join namespace and template path:
            - namespace is `static/js/github.com/user/project`
            - template path is `vendors/jquery.min.js`
            - ASSET_NAME is `static/js/github.com/user/project/jquery.min.js`
4. the assets names:
    - `templates/admin/template.tmpl` -> `templates/admin/github.com/user/project/template.tmpl`
    - `static/js/vendors/jquery.min.js` -> `static/js/github.com/user/project/jquery.min.js`
  • Loading branch information
moisespsena committed May 16, 2020
1 parent c2b5656 commit 0ca7ad4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type InputConfig struct {

NameSpace string

DirReplacesCount int

WalkFunc func(visited *map[string]bool, recursive bool, cb func(info walker.FileInfo) error) error
}

Expand Down
39 changes: 26 additions & 13 deletions config_many.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"strings"

"github.com/go-errors/errors"
"github.com/pkg/errors"

"github.com/apex/log"

Expand Down Expand Up @@ -48,13 +48,14 @@ func (s ManyConfigInputSlice) Items(ctx context.Context) (r []InputConfig, err e
}

type ManyConfigInput struct {
Path string
Prefix string
NameSpace string `mapstructure:"ns" yaml:"ns"`
Recursive bool
Ignore IgnoreSlice
IgnoreGlob IgnoreGlobSlice `mapstructure:"ignore_glob" yaml:"ignore_glob"`
Pkg string
Path string
Prefix string
NameSpace string `mapstructure:"ns" yaml:"ns"`
Recursive bool
Ignore IgnoreSlice
DirReplacesCount int
IgnoreGlob IgnoreGlobSlice `mapstructure:"ignore_glob" yaml:"ignore_glob"`
Pkg string
}

func (i *ManyConfigInput) UnmarshalMap(value interface{}) (err error) {
Expand Down Expand Up @@ -119,13 +120,24 @@ func (i ManyConfigInput) Config(ctx context.Context) (configs []*InputConfig, er
if i.NameSpace == "_" {
i.NameSpace = i.Pkg
} else if strings.Contains(i.NameSpace, "$PKG") {
i.NameSpace = strings.Replace(i.NameSpace, "$PKG", i.Pkg, 1)
i.NameSpace = strings.ReplaceAll(i.NameSpace, "$PKG", i.Pkg)
}

if i.NameSpace, err = i.format(ctx, "name_space", i.NameSpace); err != nil {
return
}

if startPos := strings.IndexByte(i.NameSpace, '('); startPos != -1 {
if endPos := strings.IndexByte(i.NameSpace[startPos:], ')'); endPos > 0 {
endPos += startPos
if i.DirReplacesCount, err = strconv.Atoi(i.NameSpace[1:endPos]); err != nil {
err = errors.Wrapf(err, "parse prefix trimer count")
return
}
i.NameSpace = i.NameSpace[0:startPos] + "?" + i.NameSpace[endPos+1:]
}
}

i.NameSpace = path.Clean(i.NameSpace)
}

Expand Down Expand Up @@ -199,10 +211,11 @@ func (i ManyConfigInput) Config(ctx context.Context) (configs []*InputConfig, er
}

c := &InputConfig{
Path: i.Path,
Recursive: i.Recursive,
Prefix: i.Prefix,
NameSpace: i.NameSpace,
Path: i.Path,
Recursive: i.Recursive,
Prefix: i.Prefix,
NameSpace: i.NameSpace,
DirReplacesCount: i.DirReplacesCount,
}

if i.Prefix == "_" {
Expand Down
10 changes: 10 additions & 0 deletions convert_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,17 @@ func (this Finder) find(input *InputConfig, prefix string) (err error) {
}

if len(info.NamePrefix) > 0 {
var namePrefix []string
for i := 0; i < input.DirReplacesCount; i++ {
if pos := strings.IndexByte(asset.Name, '/'); pos > 0 {
namePrefix = append(namePrefix, asset.Name[0:pos])
asset.Name = strings.TrimPrefix(asset.Name[pos+1:], "/")
}
}
asset.Name = path.Join(append(info.NamePrefix, asset.Name)...)
if len(namePrefix) > 0 {
asset.Name = strings.ReplaceAll(asset.Name, "?", path.Join(namePrefix...))
}
}

// This shouldn't happen.
Expand Down

0 comments on commit 0ca7ad4

Please sign in to comment.