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

Side by Side Diff: mojom/mojom_parser/fmt_cmd.go

Issue 1702873002: Mojom formatter incorporated in the mojom binary. (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 // This file contains fmtCmd which formats a .mojom file and outputs the
6 // formatted mojom file to stdout.
7 // The fmt command is invoked as follows:
8 //
9 // mojom fmt [-w] <mojom_file>
10 //
11 // If -w is specified and the formatted file is different from the original ,
12 // the file is overwritten with the formatted version.
13 package main
14
15 import (
16 "flag"
17 "fmt"
18 "io/ioutil"
19 "mojom/mojom_parser/formatter"
20 "os"
21 "path/filepath"
22 )
23
24 // fmtCmd implements the fmt command for the mojom tool.
25 // The slice of strings |args| is the list of arguments passed on the command
26 // line starting with the program name and followed by the invoked command.
27 func fmtCmd(args []string) {
28 flagSet := flag.NewFlagSet("fmt", flag.ContinueOnError)
29 var overwrite bool
30 flagSet.BoolVar(&overwrite, "w", false,
31 "Overwrite the specified file with the formatted version if it i s different from the original.")
32
33 printUsage := func() {
34 fmt.Fprintf(os.Stderr, "Usage: %s fmt [-w] <mojom_file>\n\n", fi lepath.Base(args[0]))
35 fmt.Fprintf(os.Stderr, UsageString(flagSet))
36 }
37
38 if err := flagSet.Parse(args[2:]); err != nil {
39 if err != flag.ErrHelp {
40 fmt.Fprintln(os.Stderr, err.Error())
41 }
42 printUsage()
43 os.Exit(1)
44 }
45
46 inputFileName := flagSet.Arg(0)
47 if inputFileName == "" {
48 fmt.Fprintln(os.Stderr, "No .mojom file given.")
49 printUsage()
50 os.Exit(1)
51 }
52
53 originalBytes, err := ioutil.ReadFile(inputFileName)
54 if err != nil {
55 ErrorExit(err.Error())
56 }
57
58 original := string(originalBytes[:])
59
60 var formatted string
61 formatted, err = formatter.FormatMojom(inputFileName, original)
62 if err != nil {
63 ErrorExit(err.Error())
64 }
65
66 if overwrite {
67 if formatted != original {
68 if err := ioutil.WriteFile(inputFileName, []byte(formatt ed), 0); err != nil {
69 ErrorExit(err.Error())
70 }
71 }
72 } else {
73 fmt.Print(formatted)
74 }
75 }
OLDNEW
« no previous file with comments | « mojo/public/tools/bindings/pylib/mojom/parse/parser_runner.py ('k') | mojom/mojom_parser/mojom_main.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698