Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(436)

Side by Side Diff: mojom/mojom_parser/generators/deps/deps_generator.go

Issue 1737143003: A mojom .d generator. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 main
6
7 // deps_generator.go implements the main function of the deps generator.
8 // The deps generator generates .d files. The syntax is that which is used to
9 // specify dependencies in makefiles.
rudominer 2016/02/27 01:28:10 Can you give a link to something that defines a .d
azani 2016/02/29 21:28:32 Sorry, not really. The format seems to be indirect
10
11 import (
12 "log"
13 "os"
14 "path"
15 "path/filepath"
16
17 "mojom/mojom_parser/generators/common"
18 )
19
20 func main() {
21 config := common.GetCliConfig(os.Args)
22 common.GenerateOutput(WriteDepsFile, GetOutputFile, config)
23 }
24
25 // WriteDepsFile writes a .d file for the specified file to the specified writer .
26 func WriteDepsFile(fileName string, config common.GeneratorConfig, writer common .Writer) {
27 dFileName := fileName[:len(fileName)-len(filepath.Ext(fileName))] + ".d"
28 writer.WriteString(path.Base(dFileName))
29 writer.WriteString(" : ")
30
31 imports := GetTransitiveClosure(fileName, config)
32 for _, imported := range imports {
33 writer.WriteString(imported)
34 writer.WriteString(" ")
35 }
36 writer.WriteString("\n")
37 }
38
39 // GetOutputFile returns a writer to which the generator output should be
40 // written for that specified file.
41 // In this case, the writer writes to a file whose relative path from
42 // config.OutputDir() if equivalent to the relative path from config.SrcRootPath ()
43 // to fileName.
44 //
45 // e.g. If
46 // fileName = /alpha/beta/gamma/file.mojom
47 // SrcRootPath = /alpha/
48 // OutputDir = /some/output/path/
49 //
50 // The writer writes to /some/output/path/beta/gamma/file.d
51 //
52 // TODO(azani): This is likely to be a common pattern to for the output path of
53 // the generated code. Refactor into something re-usable before writing the next
54 // generator.
55 func GetOutputFile(fileName string, config common.GeneratorConfig) (file common. Writer) {
rudominer 2016/02/27 01:28:10 Seems like this or something like it should go in
azani 2016/02/29 21:28:32 Done.
56 // Switch the file's extension to .d
57 fileName = fileName[:len(fileName)-len(filepath.Ext(fileName))] + ".d"
58
59 var err error
60 relFileName, err := filepath.Rel(config.SrcRootPath(), fileName)
61 if err != nil {
62 log.Fatalln(err.Error())
63 }
64 outPath := filepath.Join(config.OutputDir(), relFileName)
65
66 // Create the directory that will contain the output.
67 outDir := path.Dir(outPath)
68 if err := os.MkdirAll(outDir, os.ModeDir|0770); err != nil && !os.IsExis t(err) {
69 log.Fatalln(err.Error())
70 }
71
72 file, err = os.OpenFile(outPath, os.O_WRONLY, 0660)
73 if err != nil {
74 log.Fatalln(err.Error())
75 }
76
77 return
78 }
79
80 // GetTransitiveClosure gets the list of transitive imports starting with
81 // rootFile. rootFile itself is not included.
82 // The imports are specified as paths relative to the directory in which
83 // rootFile is found.
84 func GetTransitiveClosure(rootFile string, config common.GeneratorConfig) (resul t []string) {
rudominer 2016/02/27 01:28:10 Should this also be a common utility?
azani 2016/02/29 21:28:32 Looking at the other current generators, I don't t
85 fileGraph := config.FileGraph()
86 toVisit := []string{rootFile}
87 rootFileDir := path.Dir(rootFile)
88
89 for len(toVisit) > 0 {
90 curFileName := toVisit[len(toVisit)-1]
91 toVisit = toVisit[1:len(toVisit)]
92
93 curFile := fileGraph.Files[curFileName]
94 rel, err := filepath.Rel(rootFileDir, curFileName)
95 if err != nil {
96 log.Fatalln(err.Error())
97 }
98 result = append(result, rel)
99
100 if curFile.Imports != nil {
101 for _, importFileName := range *curFile.Imports {
102 toVisit = append(toVisit, importFileName)
103 }
104 }
105 }
106 return
107 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698