func main() { var buf bytes.Buffer for ii := 1; ii < len(os.Args); ii++ { filename := os.Args[ii] + ".go" data, err := ioutil.ReadFile(filename) if err != nil { panic(fmt.Errorf("error reading %s: %s", filename, err)) } lines := strings.Split(string(data), "\n") var file bytes.Buffer for _, v := range lines { if v == "" || strings.HasPrefix(v, "// +build") { continue } if strings.HasPrefix(v, "package") { if buf.Len() == 0 { buf.WriteString(v) buf.WriteByte('\n') } continue } file.WriteString(v) file.WriteByte('\n') } name := strings.Replace(filepath.Base(filename), ".", "_", -1) buf.WriteString(fmt.Sprintf("const %s = `%s`\n", name, file.String())) } if err := genutil.WriteAutogen("-", buf.Bytes()); err != nil { panic(err) } }
func Compile(filename string, translations []*po.Po, opts *CompileOptions) error { var buf bytes.Buffer dir := filepath.Dir(filename) p, err := build.ImportDir(dir, 0) if err == nil { fmt.Fprintf(&buf, "package %s\n", p.Name) } buf.WriteString("import \"gnd.la/i18n/table\"\n") buf.WriteString(genutil.AutogenString()) buf.WriteString("func init() {\n") var defaultContext string if opts != nil { defaultContext = opts.DefaultContext } for _, v := range translations { table := poToTable(v, defaultContext) form, err := funcFromFormula(v.Attrs["Plural-Forms"]) if err != nil { return err } data, err := table.Encode() if err != nil { return err } fmt.Fprintf(&buf, "table.Register(%q, func (n int) int {\n%s\n}, %q)\n", v.Attrs["Language"], form, data) } buf.WriteString("\n}\n") return genutil.WriteAutogen(filename, buf.Bytes()) }
func bakeCommand(_ *command.Args, opts *bakeOptions) error { extensions := []string{".html", ".css", ".js"} if opts.Dir == "" { return errors.New("dir can't be empty") } if opts.Name == "" { base := filepath.Base(opts.Dir) if opts.VFS { opts.Name = base + "FS" } else { opts.Name = base + "Data" } } if opts.Out == "" { opts.Out = filepath.Base(opts.Dir) + "_baked.go" } // go ignores files starting with _ opts.Out = strings.TrimLeft(opts.Out, "_") extensions = append(extensions, strings.Split(opts.Extensions, ",")...) var buf bytes.Buffer odir := filepath.Dir(opts.Out) p, err := build.ImportDir(odir, 0) if err == nil { buf.WriteString(fmt.Sprintf("package %s\n", p.Name)) } buf.WriteString(genutil.AutogenString()) if err := writeBakedFSCode(&buf, opts, extensions); err != nil { return err } if err := genutil.WriteAutogen(opts.Out, buf.Bytes()); err != nil { return err } log.Debugf("Assets written to %s (%d bytes)", opts.Out, buf.Len()) return nil }