Update github.com/mjibson/esc (#515)

* Update github.com/mjibson/esc

* Update github.com/mjibson/esc
* Fix generate.go to new signature for esc.Run

* Internal: Upgrade to lastest "esc"
This commit is contained in:
Tom Limoncelli 2020-01-29 13:43:25 -05:00 committed by GitHub
parent 75756136cb
commit 92b51dbc65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 1419 additions and 126 deletions

View file

@ -2,6 +2,7 @@ package main
import (
"log"
"os"
"github.com/mjibson/esc/embed"
)
@ -15,7 +16,17 @@ func main() {
Private: true,
Files: []string{`pkg/js/helpers.js`},
}
embed.Run(conf)
var err error
out := os.Stdout
if conf.OutputFile != "" {
if out, err = os.Create(conf.OutputFile); err != nil {
log.Fatal(err)
}
defer out.Close()
}
embed.Run(conf, out)
if err := generateFeatureMatrix(); err != nil {
log.Fatal(err)

5
go.mod
View file

@ -31,11 +31,11 @@ require (
github.com/kr/pretty v0.1.0 // indirect
github.com/malexdev/utfutil v0.0.0-20180510171754-00c8d4a8e7a8 // indirect
github.com/miekg/dns v1.0.14
github.com/mjibson/esc v0.1.0
github.com/mjibson/esc v0.2.0
github.com/namedotcom/go v0.0.0-20180403034216-08470befbe04
github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014
github.com/philhug/opensrs-go v0.0.0-20171126225031-9dfa7433020d
github.com/pkg/errors v0.8.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prasmussen/gandi-api v0.0.0-20180224132202-58d3d4205661
github.com/renier/xmlrpc v0.0.0-20170708154548-ce4a1a486c03 // indirect
github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d
@ -53,6 +53,7 @@ require (
golang.org/x/net v0.0.0-20190628185345-da137c7871d7
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect
golang.org/x/tools v0.0.0-20200129045341-207d3de1faaf // indirect
google.golang.org/api v0.7.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/ini.v1 v1.42.0 // indirect

10
go.sum
View file

@ -171,6 +171,8 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mjibson/esc v0.1.0 h1:5ch+murgrcwDFLOE2hwj0f7kE4xJfJhkSCAjSLY182o=
github.com/mjibson/esc v0.1.0/go.mod h1:9Hw9gxxfHulMF5OJKCyhYD7PzlSdhzXyaGEBRPH1OPs=
github.com/mjibson/esc v0.2.0 h1:k96hdaR9Z+nMcnDwNrOvhdBqtjyMrbVyxLpsRCdP2mA=
github.com/mjibson/esc v0.2.0/go.mod h1:9Hw9gxxfHulMF5OJKCyhYD7PzlSdhzXyaGEBRPH1OPs=
github.com/namedotcom/go v0.0.0-20180403034216-08470befbe04 h1:o6uBwrhM5C8Ll3MAAxrQxRHEu7FkapwTuI2WmL1rw4g=
github.com/namedotcom/go v0.0.0-20180403034216-08470befbe04/go.mod h1:5sN+Lt1CaY4wsPvgQH/jsuJi4XO2ssZbdsIizr4CVC8=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
@ -183,6 +185,8 @@ github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
@ -228,6 +232,7 @@ go.opencensus.io v0.22.0 h1:C9hSCOW830chIVkdja34wa6Ky+IzWllkUinR+BtRZd4=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 h1:ULYEB3JvPRE/IfO+9uO7vKV/xzVTO7XPAwm8xbf4w2g=
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@ -239,6 +244,7 @@ golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTk
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@ -294,7 +300,11 @@ golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBn
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190624190245-7f2218787638 h1:uIfBkD8gLczr4XDgYpt/qJYds2YJwZRNw4zs7wSnNhk=
golang.org/x/tools v0.0.0-20190624190245-7f2218787638/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20200129045341-207d3de1faaf h1:mFgR10kFfr83r2+nXf0GZC2FKrFhMSs9NdJ0YdEaGiY=
golang.org/x/tools v0.0.0-20200129045341-207d3de1faaf/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0 h1:9sdfJOzWlkqPltHAuzT2Cp+yrBeY1KRVYgms8soxMwM=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=

View file

@ -1,4 +1,4 @@
// Code generated by "esc "; DO NOT EDIT.
// Code generated by "esc"; DO NOT EDIT.
package js
@ -6,6 +6,8 @@ import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
@ -100,7 +102,24 @@ func (f *_escFile) Close() error {
}
func (f *_escFile) Readdir(count int) ([]os.FileInfo, error) {
return nil, nil
if !f.isDir {
return nil, fmt.Errorf(" escFile.Readdir: '%s' is not directory", f.name)
}
fis, ok := _escDirs[f.local]
if !ok {
return nil, fmt.Errorf(" escFile.Readdir: '%s' is directory, but we have no info about content of this dir, local=%s", f.name, f.local)
}
limit := count
if count <= 0 || limit > len(fis) {
limit = len(fis)
}
if len(fis) == 0 && count > 0 {
return nil, io.EOF
}
return fis[0:limit], nil
}
func (f *_escFile) Stat() (os.FileInfo, error) {
@ -191,6 +210,7 @@ func _escFSMustString(useLocal bool, name string) string {
var _escData = map[string]*_escFile{
"/helpers.js": {
name: "helpers.js",
local: "pkg/js/helpers.js",
size: 21786,
modtime: 0,
@ -291,9 +311,6 @@ cWUv15QfCPzz0btv4f5RYO9rb3+7uOohVn7eYr4q6Kcx+aec+Efv3lXfWRp1Fn1b8RFjLSLD60GFtJJ+
ZI8PWcxTMsc9EklYB9Tf8R1JEf8vAAD//1RgxiUaVQAA
`,
},
"/": {
isDir: true,
local: "pkg/js",
},
}
var _escDirs = map[string][]os.FileInfo{}

View file

@ -6,6 +6,7 @@ import (
"compress/gzip"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"os"
"path"
@ -15,6 +16,9 @@ import (
"strconv"
"strings"
"text/template"
"github.com/pkg/errors"
"golang.org/x/tools/imports"
)
// Config contains all information needed to run esc.
@ -36,6 +40,8 @@ type Config struct {
Private bool
// NoCompression, if true, stores the files without compression.
NoCompression bool
// Invocation, if set, is added to the invocation string in the generated template.
Invocation string
// Files is the list of files or directories to embed.
Files []string
@ -43,20 +49,36 @@ type Config struct {
var modTime *int64
type headerTemplateParams struct {
var tmpl = template.Must(template.New("").Parse(fileTemplate))
type templateParams struct {
Invocation string
PackageName string
FunctionPrefix string
Files []*_escFile
Dirs []*_escDir
}
type _escFile struct {
data []byte
local string
Name string
BaseName string
Data []byte
Local string
ModTime int64
Compressed string
fileinfo os.FileInfo
}
type _escDir struct {
Name string
BaseName string
Local string
ChildFileNames []string
}
// Run executes a Config.
func Run(conf *Config) error {
func Run(conf *Config, out io.Writer) error {
var err error
if conf.ModTime != "" {
i, err := strconv.ParseInt(conf.ModTime, 10, 64)
@ -65,8 +87,9 @@ func Run(conf *Config) error {
}
modTime = &i
}
var fnames, dirnames []string
content := make(map[string]_escFile)
alreadyPrepared := make(map[string]bool, 10)
escFiles := make([]*_escFile, 0, 10)
prefix := filepath.ToSlash(conf.Prefix)
var ignoreRegexp *regexp.Regexp
if conf.Ignore != "" {
@ -82,6 +105,11 @@ func Run(conf *Config) error {
return err
}
}
gzipLevel := gzip.BestCompression
if conf.NoCompression {
gzipLevel = gzip.NoCompression
}
directories := make([]*_escDir, 0, 10)
for _, base := range conf.Files {
files := []string{base}
for len(files) > 0 {
@ -98,146 +126,126 @@ func Run(conf *Config) error {
if err != nil {
return err
}
fpath := filepath.ToSlash(fname)
n := canonicFileName(fname, prefix)
if fi.IsDir() {
fis, err := f.Readdir(0)
if err != nil {
return err
}
for _, fi := range fis {
files = append(files, filepath.Join(fname, fi.Name()))
dir := &_escDir{
Name: n,
BaseName: path.Base(n),
Local: fpath,
ChildFileNames: make([]string, 0, len(fis)),
}
for _, fi := range fis {
childFName := filepath.Join(fname, fi.Name())
files = append(files, childFName)
if ignoreRegexp != nil && ignoreRegexp.MatchString(childFName) {
continue
}
if includeRegexp == nil || includeRegexp.MatchString(childFName) {
dir.ChildFileNames = append(dir.ChildFileNames, canonicFileName(filepath.Join(fname, fi.Name()), prefix))
}
}
sort.Strings(dir.ChildFileNames)
directories = append(directories, dir)
} else if includeRegexp == nil || includeRegexp.MatchString(fname) {
b, err := ioutil.ReadAll(f)
if err != nil {
return errors.Wrap(err, "readAll return err")
}
if alreadyPrepared[n] {
return fmt.Errorf("%s, %s: duplicate Name after prefix removal", n, fpath)
}
escFile := &_escFile{
Name: n,
BaseName: path.Base(n),
Data: b,
Local: fpath,
fileinfo: fi,
ModTime: fi.ModTime().Unix(),
}
if modTime != nil {
escFile.ModTime = *modTime
}
if err := escFile.fillCompressed(gzipLevel); err != nil {
return err
}
fpath := filepath.ToSlash(fname)
n := strings.TrimPrefix(fpath, prefix)
n = path.Join("/", n)
if _, ok := content[n]; ok {
return fmt.Errorf("%s, %s: duplicate name after prefix removal", n, fpath)
}
content[n] = _escFile{data: b, local: fpath, fileinfo: fi}
fnames = append(fnames, n)
escFiles = append(escFiles, escFile)
alreadyPrepared[n] = true
}
f.Close()
}
}
sort.Strings(fnames)
w := new(bytes.Buffer)
headerText, err := header(conf.Package, !(conf.Private))
if nil != err {
return fmt.Errorf("failed to expand autogenerated code: %s", err)
sort.Slice(escFiles, func(i, j int) bool { return strings.Compare(escFiles[i].Name, escFiles[j].Name) == -1 })
sort.Slice(directories, func(i, j int) bool { return strings.Compare(directories[i].Name, directories[j].Name) == -1 })
functionPrefix := ""
if conf.Private {
functionPrefix = "_esc"
}
if _, err := w.Write(headerText); err != nil {
return fmt.Errorf("failed to write output: %s", err)
}
dirs := map[string]bool{"/": true}
gzipLevel := gzip.BestCompression
if conf.NoCompression {
gzipLevel = gzip.NoCompression
}
for _, fname := range fnames {
f := content[fname]
for b := path.Dir(fname); b != "/"; b = path.Dir(b) {
dirs[b] = true
}
var buf bytes.Buffer
gw, err := gzip.NewWriterLevel(&buf, gzipLevel)
if err != nil {
return err
}
if _, err := gw.Write(f.data); err != nil {
return err
}
if err := gw.Close(); err != nil {
return err
}
t := f.fileinfo.ModTime().Unix()
if modTime != nil {
t = *modTime
}
fmt.Fprintf(w, `
%q: {
local: %q,
size: %v,
modtime: %v,
compressed: %s,
},%s`, fname, f.local, len(f.data), t, segment(&buf), "\n")
}
for d := range dirs {
dirnames = append(dirnames, d)
}
sort.Strings(dirnames)
for _, dir := range dirnames {
local := path.Join(prefix, dir)
if len(local) == 0 {
local = "."
}
if local[0] == '/' {
// Read dirs relative to the go proc's cwd vs system's
// fs root.
local = local[1:]
}
fmt.Fprintf(w, `
%q: {
isDir: true,
local: %q,
},%s`, dir, local, "\n")
}
w.WriteString(footer)
out := os.Stdout
buf := bytes.NewBuffer(nil)
tmpl.Execute(buf, templateParams{
Invocation: conf.Invocation,
PackageName: conf.Package,
FunctionPrefix: functionPrefix,
Files: escFiles,
Dirs: directories,
})
fakeOutFileName := "static.go"
if conf.OutputFile != "" {
if out, err = os.Create(conf.OutputFile); err != nil {
return err
}
fakeOutFileName = conf.OutputFile
}
if _, err := w.WriteTo(out); err != nil {
return err
}
if conf.OutputFile != "" {
return out.Close()
data, err := imports.Process(fakeOutFileName, buf.Bytes(), nil)
if err != nil {
return errors.Wrap(err, "imports.Process return error")
}
fmt.Fprint(out, string(data))
return nil
}
func segment(s *bytes.Buffer) string {
func canonicFileName(fname, prefix string) string {
fpath := filepath.ToSlash(fname)
return path.Join("/", strings.TrimPrefix(fpath, prefix))
}
func (f *_escFile) fillCompressed(gzipLevel int) error {
var buf bytes.Buffer
gw, err := gzip.NewWriterLevel(&buf, gzipLevel)
if err != nil {
return err
}
if _, err := gw.Write(f.Data); err != nil {
return err
}
if err := gw.Close(); err != nil {
return err
}
var b bytes.Buffer
b64 := base64.NewEncoder(base64.StdEncoding, &b)
b64.Write(s.Bytes())
b64.Write(buf.Bytes())
b64.Close()
res := "`\n"
res := "\n"
chunk := make([]byte, 80)
for n, _ := b.Read(chunk); n > 0; n, _ = b.Read(chunk) {
res += string(chunk[0:n]) + "\n"
}
return res + "`"
}
func header(packageName string, enableExports bool) ([]byte, error) {
functionPrefix := ""
if !enableExports {
functionPrefix = "_esc"
}
headerParams := headerTemplateParams{
Invocation: strings.Join(os.Args[1:], " "),
PackageName: packageName,
FunctionPrefix: functionPrefix,
}
tmpl, err := template.New("").Parse(headerTemplate)
if nil != err {
return nil, err
}
var b bytes.Buffer
err = tmpl.Execute(&b, headerParams)
if nil != err {
return nil, err
}
return b.Bytes(), nil
f.Compressed = res
return nil
}
const (
headerTemplate = `// Code generated by "esc {{.Invocation}}"; DO NOT EDIT.
fileTemplate = `// Code generated by "esc{{with .Invocation}} {{.}}{{end}}"; DO NOT EDIT.
package {{.PackageName}}
@ -339,9 +347,27 @@ func (f *_escFile) Close() error {
}
func (f *_escFile) Readdir(count int) ([]os.FileInfo, error) {
return nil, nil
if !f.isDir {
return nil, fmt.Errorf(" escFile.Readdir: '%s' is not directory", f.name)
}
fis, ok := _escDirs[f.local]
if !ok {
return nil, fmt.Errorf(" escFile.Readdir: '%s' is directory, but we have no info about content of this dir, local=%s", f.name, f.local)
}
limit := count
if count <= 0 || limit > len(fis) {
limit = len(fis)
}
if len(fis) == 0 && count > 0 {
return nil, io.EOF
}
return fis[0:limit], nil
}
func (f *_escFile) Stat() (os.FileInfo, error) {
return f, nil
}
@ -428,7 +454,33 @@ func {{.FunctionPrefix}}FSMustString(useLocal bool, name string) string {
}
var _escData = map[string]*_escFile{
`
footer = `}
{{ range .Files }}
"{{ .Name }}": {
name: "{{ .BaseName }}",
local: "{{ .Local }}",
size: {{ .Data | len }},
modtime: {{ .ModTime }},
compressed: ` + "`" + `{{ .Compressed }}` + "`" + `,
},
{{ end -}}
{{ range .Dirs }}
"{{ .Name }}": {
name: "{{ .BaseName }}",
local: ` + "`" + `{{ .Local }}` + "`" + `,
isDir: true,
},
{{ end }}
}
var _escDirs = map[string][]os.FileInfo{
{{ range .Dirs }}
"{{ .Local }}": {
{{ range .ChildFileNames -}}
_escData["{{.}}"],
{{ end }}
},
{{ end }}
}
`
)

1202
vendor/vendor.json vendored Normal file

File diff suppressed because it is too large Load diff