| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file contains fmtCmd which formats a .mojom file and outputs the | 5 // This file contains fmtCmd which formats a .mojom file and outputs the |
| 6 // formatted mojom file to stdout. | 6 // formatted mojom file to stdout. |
| 7 // The fmt command is invoked as follows: | 7 // The fmt command is invoked as follows: |
| 8 // | 8 // |
| 9 // mojom fmt [-w] <mojom_file> | 9 // mojom fmt [-w] <mojom_file> |
| 10 // | 10 // |
| 11 // If -w is specified and the formatted file is different from the original
, | 11 // If -w is specified and the formatted file is different from the original
, |
| 12 // the file is overwritten with the formatted version. | 12 // the file is overwritten with the formatted version. |
| 13 package main | 13 package main |
| 14 | 14 |
| 15 import ( | 15 import ( |
| 16 "flag" | 16 "flag" |
| 17 "fmt" | 17 "fmt" |
| 18 "io/ioutil" | 18 "io/ioutil" |
| 19 "log" |
| 19 "mojom/mojom_parser/formatter" | 20 "mojom/mojom_parser/formatter" |
| 20 "os" | 21 "os" |
| 21 "path/filepath" | 22 "path/filepath" |
| 22 ) | 23 ) |
| 23 | 24 |
| 24 // fmtCmd implements the fmt command for the mojom tool. | 25 // 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 // 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 // line starting with the program name and followed by the invoked command. |
| 27 func fmtCmd(args []string) { | 28 func fmtCmd(args []string) { |
| 28 flagSet := flag.NewFlagSet("fmt", flag.ContinueOnError) | 29 flagSet := flag.NewFlagSet("fmt", flag.ContinueOnError) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 45 | 46 |
| 46 inputFileName := flagSet.Arg(0) | 47 inputFileName := flagSet.Arg(0) |
| 47 if inputFileName == "" { | 48 if inputFileName == "" { |
| 48 fmt.Fprintln(os.Stderr, "No .mojom file given.") | 49 fmt.Fprintln(os.Stderr, "No .mojom file given.") |
| 49 printUsage() | 50 printUsage() |
| 50 os.Exit(1) | 51 os.Exit(1) |
| 51 } | 52 } |
| 52 | 53 |
| 53 originalBytes, err := ioutil.ReadFile(inputFileName) | 54 originalBytes, err := ioutil.ReadFile(inputFileName) |
| 54 if err != nil { | 55 if err != nil { |
| 55 » » ErrorExit(err.Error()) | 56 » » log.Fatalln(err.Error()) |
| 56 } | 57 } |
| 57 | 58 |
| 58 original := string(originalBytes[:]) | 59 original := string(originalBytes[:]) |
| 59 | 60 |
| 60 var formatted string | 61 var formatted string |
| 61 formatted, err = formatter.FormatMojom(inputFileName, original) | 62 formatted, err = formatter.FormatMojom(inputFileName, original) |
| 62 if err != nil { | 63 if err != nil { |
| 63 » » ErrorExit(err.Error()) | 64 » » log.Fatalln(err.Error()) |
| 64 } | 65 } |
| 65 | 66 |
| 66 if overwrite { | 67 if overwrite { |
| 67 if formatted != original { | 68 if formatted != original { |
| 68 if err := ioutil.WriteFile(inputFileName, []byte(formatt
ed), 0); err != nil { | 69 if err := ioutil.WriteFile(inputFileName, []byte(formatt
ed), 0); err != nil { |
| 69 » » » » ErrorExit(err.Error()) | 70 » » » » log.Fatalln(err.Error()) |
| 70 } | 71 } |
| 71 } | 72 } |
| 72 } else { | 73 } else { |
| 73 fmt.Print(formatted) | 74 fmt.Print(formatted) |
| 74 } | 75 } |
| 75 } | 76 } |
| OLD | NEW |