| // 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. |
| |
| // embed is a tool to embed text files as resource. |
| package main |
| |
| import ( |
| "bytes" |
| "fmt" |
| "io/ioutil" |
| "os" |
| "path/filepath" |
| "strings" |
| |
| "golang.org/x/tools/imports" |
| ) |
| |
| const header = ` |
| // 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. |
| |
| package %s |
| |
| ` |
| |
| type embed struct { |
| filename string |
| name string |
| contents []byte |
| } |
| |
| func run() error { |
| pwd, err := filepath.Abs(".") |
| if err != nil { |
| return err |
| } |
| files, err := ioutil.ReadDir(".") |
| if err != nil { |
| return err |
| } |
| entries := []embed{} |
| for _, info := range files { |
| if info.IsDir() { |
| continue |
| } |
| extension := filepath.Ext(info.Name()) |
| if extension == ".go" { |
| continue |
| } |
| name := strings.Replace(info.Name(), ".", "_", -1) |
| data, err := ioutil.ReadFile(info.Name()) |
| if err != nil { |
| return err |
| } |
| entries = append(entries, embed{info.Name(), name, data}) |
| } |
| // write the header |
| _, pkg := filepath.Split(pwd) |
| 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") |
| } |
| // reformat the output |
| result, err := imports.Process("", b.Bytes(), nil) |
| if err != nil { |
| result = b.Bytes() |
| } |
| if err := ioutil.WriteFile("embed.go", 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) |
| } |
| } |