Chromium Code Reviews| Index: mojom/mojom_parser/generators/common/cli.go |
| diff --git a/mojom/mojom_parser/generators/common/cli.go b/mojom/mojom_parser/generators/common/cli.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..56552f1c469f7e8efd23fb4b6359591a6cdb17c4 |
| --- /dev/null |
| +++ b/mojom/mojom_parser/generators/common/cli.go |
| @@ -0,0 +1,111 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// common groups together functions which make it easier for generators |
|
rudominer
2016/02/27 01:28:10
This is the same comment from common.go
azani
2016/02/29 21:28:32
Done.
|
| +// implemented in go to implement the same interface. |
| + |
| +package common |
| + |
| +import ( |
| + "flag" |
| + "io" |
| + "io/ioutil" |
| + "log" |
| + "os" |
| + "path/filepath" |
| + |
| + "mojo/public/go/bindings" |
| + "mojom/mojom_parser/generated/mojom_files" |
| +) |
| + |
| +// GetConfig provides the primary interface for generators. |
| +// By calling GetConfig, a generator implements the command line interface |
| +// that is used by all generators. |
| +func GetCliConfig(args []string) GeneratorConfig { |
| + config := new(generatorCliConfig) |
| + flagSet := flag.NewFlagSet("Generator Common Flag Set", flag.ExitOnError) |
| + |
| + var fileGraphFile string |
| + flagSet.StringVar(&fileGraphFile, "file-graph", "-", |
| + "Location of the parser output. \"-\" for stdin. (default \"-\")") |
| + flagSet.StringVar(&config.outputDir, "output-dir", ".", |
| + "output directory for generated files.") |
| + flagSet.StringVar(&config.srcRootPath, "src-root-path", ".", |
| + "relative path to the root of the source tree.") |
| + flagSet.BoolVar(&config.noGenImports, "no-gen-imports", false, |
| + "Generate code only for the files that are specified on the command line. "+ |
| + "By default, code is generated for all specified files and their transitive imports.") |
| + |
| + flagSet.Parse(args[1:]) |
| + |
| + // Compute the absolute path for the source root and the output directory. |
| + var err error |
| + config.srcRootPath, err = filepath.Abs(config.srcRootPath) |
| + if err != nil { |
| + log.Fatalln(err.Error()) |
| + } |
| + |
| + config.outputDir, err = filepath.Abs(config.outputDir) |
| + if err != nil { |
| + log.Fatalln(err.Error()) |
| + } |
| + |
| + // Read the file graph either from standard in or the specified file. |
| + var fileGraphReader io.Reader |
| + if fileGraphFile == "-" { |
| + fileGraphReader = os.Stdin |
| + } else { |
| + var err error |
| + if fileGraphReader, err = os.Open(fileGraphFile); err != nil { |
| + log.Fatalln(err.Error()) |
| + } |
| + } |
| + |
| + config.fileGraph = decodeFileGraph(fileGraphReader) |
| + return config |
| +} |
| + |
| +// decodeFileGraph decodes a serialized MojomFileGraph. |
| +func decodeFileGraph(in io.Reader) (fileGraph *mojom_files.MojomFileGraph) { |
| + inputBytes, err := ioutil.ReadAll(in) |
| + if err != nil { |
| + log.Fatalf("Failed to read: %v\n", err) |
| + } |
| + |
| + decoder := bindings.NewDecoder(inputBytes, nil) |
| + fileGraph = new(mojom_files.MojomFileGraph) |
| + if err := fileGraph.Decode(decoder); err != nil { |
| + log.Fatalf("Failed to decode file graph: %v\n", err) |
| + } |
| + |
| + return |
| +} |
| + |
| +// generatorCliConfig implements GeneratorConfig. |
| +type generatorCliConfig struct { |
| + fileGraph *mojom_files.MojomFileGraph |
| + outputDir string |
| + srcRootPath string |
| + noGenImports bool |
| +} |
| + |
| +// See GeneratorConfig. |
| +func (c *generatorCliConfig) FileGraph() *mojom_files.MojomFileGraph { |
| + return c.fileGraph |
| +} |
| + |
| +// See GeneratorConfig. |
| +func (c *generatorCliConfig) OutputDir() string { |
| + return c.outputDir |
| +} |
| + |
| +// See GeneratorConfig. |
| +func (c *generatorCliConfig) SrcRootPath() string { |
| + return c.srcRootPath |
| +} |
| + |
| +// See GeneratorConfig. |
| +func (c *generatorCliConfig) GenImports() bool { |
| + return !c.noGenImports |
| +} |