| 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..fdfee9763614edd29177ad2717f5820b79f37fe0
|
| --- /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.
|
| +
|
| +// cli.go implements logic to create a generator config object from command
|
| +// line arguments.
|
| +
|
| +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
|
| +}
|
|
|