assets fix
This commit is contained in:
50
main.go
50
main.go
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/aymerick/raymond"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/op/go-logging"
|
||||
cpy "github.com/otiai10/copy"
|
||||
"gopkg.in/russross/blackfriday.v2"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
@@ -27,13 +28,17 @@ var templateCache = make(map[string]*raymond.Template)
|
||||
// GlobalConfig is config which is used only once in root dir
|
||||
type GlobalConfig struct {
|
||||
Webserver struct {
|
||||
Type *string `yaml:"Type"`
|
||||
Type string `yaml:"Type"`
|
||||
} `yaml:"Webserver"`
|
||||
|
||||
Assets struct {
|
||||
Path *string `yaml:"Path"`
|
||||
DeployType *string `yaml:"DeployType"`
|
||||
FixTemplate *bool `yaml:"FixTemplate"`
|
||||
FromPath string `yaml:"FromPath"`
|
||||
ToPath string `yaml:"ToPath"`
|
||||
Action string `yaml:"Action"`
|
||||
FixTemplate struct {
|
||||
Find string `yaml:"Find"`
|
||||
Replace string `yaml:"Replace"`
|
||||
} `yaml:"FixTemplate"`
|
||||
} `yaml:"Assets"`
|
||||
}
|
||||
|
||||
@@ -402,6 +407,22 @@ func processContent(conf *PathConfigTree) {
|
||||
log.Panicf("could not execute template '%s' for input file '%s': %s", templateFile, inFile, err)
|
||||
}
|
||||
|
||||
if find := globalConfig.Assets.FixTemplate.Find; find != "" {
|
||||
log.Debugf("fixing assets paths in '%s' for '%s'", templateFile, inFile)
|
||||
tmpPath := ""
|
||||
for i := len(strings.Split(curNavPath, "/")); i > 0; i-- {
|
||||
tmpPath += "../"
|
||||
}
|
||||
regex, err := regexp.Compile(find)
|
||||
if err != nil {
|
||||
log.Panicf("could not compile regexp '%s' for assets path: %s", find, err)
|
||||
}
|
||||
repl := globalConfig.Assets.FixTemplate.Replace
|
||||
repl = tmpPath + "/" + globalConfig.Assets.ToPath + "/" + repl
|
||||
repl = path.Clean(repl) + "/"
|
||||
result = regex.ReplaceAllString(result, repl)
|
||||
}
|
||||
|
||||
log.Noticef("writing to output file: %s", outFile)
|
||||
err = ioutil.WriteFile(outFile, []byte(result), 0644)
|
||||
if err != nil {
|
||||
@@ -417,6 +438,25 @@ func processContent(conf *PathConfigTree) {
|
||||
}
|
||||
}
|
||||
|
||||
func processAssets() {
|
||||
switch globalConfig.Assets.Action {
|
||||
case "copy":
|
||||
from := globalConfig.Assets.FromPath
|
||||
to := globalConfig.Assets.ToPath
|
||||
if !strings.HasPrefix(from, "/") {
|
||||
from = *inDir + "/" + from
|
||||
}
|
||||
if !strings.HasPrefix(to, "/") {
|
||||
to = *outDir + "/" + to
|
||||
}
|
||||
log.Noticef("copying assets from '%s' to '%s'", from, to)
|
||||
err := cpy.Copy(from, to)
|
||||
if err != nil {
|
||||
log.Panicf("could not copy assets from '%s' to '%s': %s", from, to, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
spew.Config.DisablePointerAddresses = true
|
||||
spew.Config.DisableCapacities = true
|
||||
@@ -535,4 +575,6 @@ func main() {
|
||||
|
||||
processContent(contentConfig)
|
||||
|
||||
processAssets()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user