1
0
mirror of https://github.com/beego/bee.git synced 2024-11-04 20:30:55 +00:00
bee/cmd/commands/bale/bale.go

251 lines
5.2 KiB
Go
Raw Normal View History

2013-09-03 17:23:58 +00:00
// Copyright 2013 bee authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package bale
2013-09-03 17:23:58 +00:00
import (
2013-09-04 00:04:32 +00:00
"bytes"
"compress/gzip"
"fmt"
"io"
2013-09-03 17:23:58 +00:00
"os"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/beego/bee/cmd/commands"
"github.com/beego/bee/cmd/commands/version"
"github.com/beego/bee/config"
beeLogger "github.com/beego/bee/logger"
"github.com/beego/bee/utils"
2013-09-03 17:23:58 +00:00
)
var CmdBale = &commands.Command{
2013-09-03 17:23:58 +00:00
UsageLine: "bale",
Short: "Transforms non-Go files to Go source files",
Long: `Bale command compress all the static files in to a single binary file.
2013-09-03 17:23:58 +00:00
This is useful to not have to carry static files including js, css, images and
views when deploying a Web application.
2013-09-03 17:23:58 +00:00
It will auto-generate an unpack function to the main package then run it during the runtime.
This is mainly used for zealots who are requiring 100% Go code.
`,
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
Run: runBale,
2013-09-03 17:23:58 +00:00
}
func init() {
commands.AvailableCommands = append(commands.AvailableCommands, CmdBale)
}
2013-09-03 17:23:58 +00:00
func runBale(cmd *commands.Command, args []string) int {
2013-09-04 00:04:32 +00:00
os.RemoveAll("bale")
2013-09-03 17:23:58 +00:00
os.Mkdir("bale", os.ModePerm)
// Pack and compress data
for _, p := range config.Conf.Bale.Dirs {
if !utils.IsExist(p) {
beeLogger.Log.Warnf("Skipped directory: %s", p)
2013-09-05 09:05:18 +00:00
continue
}
beeLogger.Log.Infof("Packaging directory: %s", p)
2013-09-03 17:23:58 +00:00
filepath.Walk(p, walkFn)
}
2013-09-04 00:04:32 +00:00
// Generate auto-uncompress function.
buf := new(bytes.Buffer)
buf.WriteString(fmt.Sprintf(BaleHeader, config.Conf.Bale.Import,
2013-09-04 00:04:32 +00:00
strings.Join(resFiles, "\",\n\t\t\""),
strings.Join(resFiles, ",\n\t\tbale.R")))
fw, err := os.Create("bale.go")
if err != nil {
beeLogger.Log.Fatalf("Failed to create file: %s", err)
2013-09-04 00:04:32 +00:00
}
defer fw.Close()
_, err = fw.Write(buf.Bytes())
if err != nil {
beeLogger.Log.Fatalf("Failed to write data: %s", err)
2013-09-04 00:04:32 +00:00
}
beeLogger.Log.Success("Baled resources successfully!")
2014-08-15 09:38:51 +00:00
return 0
2013-09-04 00:04:32 +00:00
}
const (
// BaleHeader ...
2016-07-22 23:05:01 +00:00
BaleHeader = `package main
2013-09-04 00:04:32 +00:00
import(
"os"
"strings"
"path"
"%s"
)
func isExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
2013-09-03 17:23:58 +00:00
}
2013-09-04 00:04:32 +00:00
func init() {
files := []string{
"%s",
}
funcs := []func() []byte{
bale.R%s,
}
for i, f := range funcs {
fp := getFilePath(files[i])
if !isExist(fp) {
saveFile(fp, f())
}
}
}
func getFilePath(name string) string {
name = strings.Replace(name, "_4_", "/", -1)
name = strings.Replace(name, "_3_", " ", -1)
name = strings.Replace(name, "_2_", "-", -1)
name = strings.Replace(name, "_1_", ".", -1)
name = strings.Replace(name, "_0_", "_", -1)
return name
}
func saveFile(filePath string, b []byte) (int, error) {
os.MkdirAll(path.Dir(filePath), os.ModePerm)
fw, err := os.Create(filePath)
if err != nil {
return 0, err
}
defer fw.Close()
return fw.Write(b)
}
`
)
var resFiles = make([]string, 0, 10)
func walkFn(resPath string, info os.FileInfo, _ error) error {
2013-09-03 17:23:58 +00:00
if info.IsDir() || filterSuffix(resPath) {
return nil
}
// Open resource files
2013-09-04 00:04:32 +00:00
fr, err := os.Open(resPath)
if err != nil {
beeLogger.Log.Fatalf("Failed to read file: %s", err)
2013-09-04 00:04:32 +00:00
}
// Convert path
2013-09-04 00:04:32 +00:00
resPath = strings.Replace(resPath, "_", "_0_", -1)
resPath = strings.Replace(resPath, ".", "_1_", -1)
resPath = strings.Replace(resPath, "-", "_2_", -1)
resPath = strings.Replace(resPath, " ", "_3_", -1)
2013-09-03 17:23:58 +00:00
sep := "/"
if runtime.GOOS == "windows" {
sep = "\\"
}
2013-09-04 00:04:32 +00:00
resPath = strings.Replace(resPath, sep, "_4_", -1)
// Create corresponding Go source files
2013-09-03 17:23:58 +00:00
os.MkdirAll(path.Dir(resPath), os.ModePerm)
2013-09-04 00:04:32 +00:00
fw, err := os.Create("bale/" + resPath + ".go")
if err != nil {
beeLogger.Log.Fatalf("Failed to create file: %s", err)
2013-09-04 00:04:32 +00:00
}
defer fw.Close()
// Write header
2016-07-22 23:05:01 +00:00
fmt.Fprintf(fw, Header, resPath)
2013-09-04 00:04:32 +00:00
// Copy and compress data
2013-09-04 00:04:32 +00:00
gz := gzip.NewWriter(&ByteWriter{Writer: fw})
io.Copy(gz, fr)
gz.Close()
// Write footer.
2016-07-22 23:05:01 +00:00
fmt.Fprint(fw, Footer)
2013-09-04 00:04:32 +00:00
resFiles = append(resFiles, resPath)
2013-09-03 17:23:58 +00:00
return nil
}
func filterSuffix(name string) bool {
for _, s := range config.Conf.Bale.IngExt {
2013-09-03 17:23:58 +00:00
if strings.HasSuffix(name, s) {
return true
}
}
return false
}
2013-09-04 00:04:32 +00:00
const (
// Header ...
2016-07-22 23:05:01 +00:00
Header = `package bale
2013-09-04 00:04:32 +00:00
import(
"bytes"
"compress/gzip"
"io"
)
func R%s() []byte {
gz, err := gzip.NewReader(bytes.NewBuffer([]byte{`
// Footer ...
2016-07-22 23:05:01 +00:00
Footer = `
2013-09-04 00:04:32 +00:00
}))
if err != nil {
panic("Unpack resources failed: " + err.Error())
}
var b bytes.Buffer
io.Copy(&b, gz)
gz.Close()
return b.Bytes()
}`
)
var newline = []byte{'\n'}
// ByteWriter ...
2013-09-04 00:04:32 +00:00
type ByteWriter struct {
io.Writer
c int
}
func (w *ByteWriter) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return
}
for n = range p {
if w.c%12 == 0 {
w.Writer.Write(newline)
w.c = 0
}
fmt.Fprintf(w.Writer, "0x%02x,", p[n])
w.c++
}
n++
return
}