blob: 16b4207f349aef4169d41b861ea5555d967e7674 [file] [log] [blame]
// Copyright (C) 2015 The Android Open Source Project
//
// 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.
// The embed command is used to embed text files into Go executables as strings.
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/imports"
)
var (
verbose = flag.Bool("v", false, "verbose messages")
output = flag.String("out", "embed.go", "the file to generate")
)
const usage = `embed: A tool to embed files into go source.
Usage: embed [--out=file] <inputs>...
-help: show this help message
`
const header = `
////////////////////////////////////////////////////////////////////////////////
// Do not modify!
// Generated by embed
////////////////////////////////////////////////////////////////////////////////
package %s
`
type embed struct {
path string
filename string
name string
contents []byte
}
func run() error {
flag.Usage = func() {
fmt.Printf(usage)
flag.PrintDefaults()
}
flag.Parse()
args := flag.Args()
entries := []*embed{}
if len(args) == 0 {
pwd, err := filepath.Abs(".")
if err != nil {
return err
}
files, err := ioutil.ReadDir(pwd)
if err != nil {
return err
}
for _, info := range files {
if info.IsDir() {
continue
}
extension := filepath.Ext(info.Name())
if extension == ".go" {
continue
}
path := filepath.Join(pwd, info.Name())
entries = append(entries, &embed{path: path})
}
} else {
for _, arg := range args {
path, err := filepath.Abs(arg)
if err != nil {
return err
}
entries = append(entries, &embed{path: path})
}
}
var err error
for _, entry := range entries {
entry.filename = filepath.Base(entry.path)
entry.name = strings.Replace(entry.filename, ".", "_", -1)
entry.contents, err = ioutil.ReadFile(entry.path)
if err != nil {
return err
}
}
// write the header
out, err := filepath.Abs(*output)
if err != nil {
return err
}
pkg := filepath.Base(filepath.Dir(out))
b := &bytes.Buffer{}
fmt.Fprintf(b, header, pkg)
// write the map
fmt.Fprint(b, "var embedded = map[string]string{\n")
for _, entry := range entries {
fmt.Fprintf(b, "%s_file:\n%s,\n", entry.name, entry.name)
}
fmt.Fprint(b, "}\n")
// write the data lumps
for _, entry := range entries {
fmt.Fprintf(b, "const %s_file = `%s`\n", entry.name, entry.filename)
fmt.Fprintf(b, "const %s = `", entry.name)
fmt.Fprint(b, strings.Replace(string(entry.contents), "`", "` + \"`\" + `", -1))
fmt.Fprint(b, "`\n")
if *verbose {
fmt.Printf("Embed %s from %s\n", entry.name, entry.path)
}
}
// reformat the output
result, err := imports.Process("", b.Bytes(), nil)
if err != nil {
result = b.Bytes()
}
if err := ioutil.WriteFile(out, result, 0666); err != nil {
return err
}
return nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "embed failed: %v\n", err)
os.Exit(1)
}
}