OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package common | |
6 | |
7 import ( | |
8 "mojom/mojom_parser/generated/mojom_files" | |
9 ) | |
10 | |
11 // common groups together functions which make it easier for generators | |
12 // implemented in go to implement the same interface. | |
13 | |
14 // GeneratorConfig is used to specify configuration for a generator. | |
15 type GeneratorConfig interface { | |
16 // FileGraph returns a parsed MojomFileGraph. | |
17 FileGraph() *mojom_files.MojomFileGraph | |
18 | |
19 // OutputDir returns an absolute path in which generator output must be | |
20 // written. | |
21 OutputDir() string | |
22 | |
23 // SrcRootPath returns an absolute path to the root of the source tree. This | |
24 // is used to compute the relative path from the root of the source tree to | |
25 // mojom files for which output is to be generated. | |
26 SrcRootPath() string | |
27 | |
28 // GenImports returns true if all files in the provided file graph shoul d | |
29 // be generated and and false if only the files for which a SpecifiedFil eName | |
30 // is specified should be generated. | |
31 GenImports() bool | |
rudominer
2016/02/27 01:28:10
My suggestion for a more complete description. Do
azani
2016/02/29 21:28:32
Done.
| |
32 } | |
33 | |
34 // Writer is the interface used by the generators to write their output. | |
35 // It is also made to faciliate testing by allowing generator code to write | |
36 // to a provided buffer such as a bytes.Buffer. | |
37 type Writer interface { | |
38 WriteString(s string) (n int, err error) | |
39 } | |
40 | |
41 // writeOutput is the type of the generator function which writes the specified | |
42 // file's generated code to the writer. | |
43 type writeOutput func(fileName string, config GeneratorConfig, writer Writer) | |
44 | |
45 // getOutputWriter is the type of the generator function which decides where | |
46 // the generated code for a particular file is to be written. | |
47 // This allows generators to decide where they should write their output to. | |
48 type getOutputWriter func(fileName string, config GeneratorConfig) Writer | |
49 | |
50 // GenerateOutput iterates through the files in the file graph in config and | |
rudominer
2016/02/27 01:28:10
config -> |config|
azani
2016/02/29 21:28:32
Done.
| |
51 // for all the files for which an output should be generated, it uses | |
52 // writeOutput to write the output to the location specified by getOutputWriter. | |
53 func GenerateOutput(writeOutput writeOutput, getOutputWriter getOutputWriter, co nfig GeneratorConfig) { | |
54 for fileName, file := range config.FileGraph().Files { | |
55 if config.GenImports() || *file.SpecifiedFileName != "" { | |
56 writer := getOutputWriter(fileName, config) | |
57 writeOutput(fileName, config, writer) | |
rudominer
2016/02/27 01:28:10
getOutputWriter isn't really helping here and inst
azani
2016/02/29 21:28:32
I prefer this decoupling because it means that it'
| |
58 } | |
59 } | |
60 } | |
OLD | NEW |