| // Copyright (C) 2016 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" |
| "path/filepath" |
| "strings" |
| |
| "android.googlesource.com/platform/tools/gpu/framework/app" |
| "android.googlesource.com/platform/tools/gpu/framework/log" |
| |
| "golang.org/x/tools/imports" |
| ) |
| |
| var ( |
| pkg string |
| output string |
| ) |
| |
| const header = ` |
| //////////////////////////////////////////////////////////////////////////////// |
| // Do not modify! |
| // Generated by embed |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| package %s |
| |
| ` |
| |
| func main() { |
| flag.StringVar(&pkg, "package", "", "the package to use, defaults to the dir name") |
| flag.StringVar(&output, "out", "embed.go", "the file to generate") |
| app.ShortHelp = "embed: A tool to embed files into go source." |
| app.Run(run) |
| } |
| |
| type embed struct { |
| path string |
| filename string |
| name string |
| contents []byte |
| } |
| |
| func run(ctx log.Context) error { |
| 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 |
| } |
| if pkg == "" { |
| 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") |
| ctx.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 |
| } |