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

Unified Diff: mojom/mojom_parser/generators/common/util.go

Issue 1737143003: A mojom .d generator. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Update sha1. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojom/mojom_parser/generators/common/common.go ('k') | mojom/mojom_parser/generators/common/util_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojom/mojom_parser/generators/common/util.go
diff --git a/mojom/mojom_parser/generators/common/util.go b/mojom/mojom_parser/generators/common/util.go
new file mode 100644
index 0000000000000000000000000000000000000000..05444d3f5a6d46a2bd1bde5caf7712770cfd3c7d
--- /dev/null
+++ b/mojom/mojom_parser/generators/common/util.go
@@ -0,0 +1,73 @@
+// 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.
+
+package common
+
+import (
+ "log"
+ "os"
+ "path"
+ "path/filepath"
+)
+
+// OutputWriterByFilePath returns |Writer| that writes to a file whose relative
+// path from config.OutputDir() is equivalent to the relative path from
+// config.SrcRootPath() to fileName. Also, the extension of the file is changed
+// from ".mojom" to |ext|.
+//
+// e.g. If
+// ext = ".d"
+// fileName = /alpha/beta/gamma/file.mojom
+// SrcRootPath = /alpha/
+// OutputDir = /some/output/path/
+//
+// The writer writes to /some/output/path/beta/gamma/file.d
+func OutputWriterByFilePath(fileName string, config GeneratorConfig, ext string) Writer {
+ fileName = changeExt(fileName, ext)
+ outPath := outputFileByFilePath(fileName, config)
+
+ return createAndOpen(outPath)
+}
+
+// outputFileByFilePath computes a file path such that the relative path
+// from config.SrcRootPath() to the returned path is equivalent to the relative
+// path from config.SrcRootPath() to fileName.
+//
+// e.g. If
+// fileName = /alpha/beta/gamma/file.mojom
+// SrcRootPath = /alpha/
+// OutputDir = /some/output/path/
+//
+// The returned path is /some/output/path/beta/gamma/file.mojom
+func outputFileByFilePath(fileName string, config GeneratorConfig) string {
+ var err error
+ relFileName, err := filepath.Rel(config.SrcRootPath(), fileName)
+ if err != nil {
+ log.Fatalln(err.Error())
+ }
+ return filepath.Join(config.OutputDir(), relFileName)
+}
+
+// createAndOpen opens for writing the specified file. If the file or any part
+// of the directory structure in its path does not exist, they are created.
+func createAndOpen(outPath string) (file Writer) {
+ // Create the directory that will contain the output.
+ outDir := path.Dir(outPath)
+ if err := os.MkdirAll(outDir, os.ModeDir|0770); err != nil && !os.IsExist(err) {
+ log.Fatalln(err.Error())
+ }
+
+ var err error
+ file, err = os.OpenFile(outPath, os.O_WRONLY, 0660)
+ if err != nil {
+ log.Fatalln(err.Error())
+ }
+
+ return
+}
+
+// changeExt changes the extension of a file to the specified extension.
+func changeExt(fileName string, ext string) string {
+ return fileName[:len(fileName)-len(filepath.Ext(fileName))] + ext
+}
« no previous file with comments | « mojom/mojom_parser/generators/common/common.go ('k') | mojom/mojom_parser/generators/common/util_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698