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 package org.chromium.testing.robolectric.template; | 5 package org.chromium.testing.robolectric.template; |
6 | 6 |
7 import java.nio.file.Path; | 7 import java.nio.file.Path; |
8 import java.nio.file.Paths; | 8 import java.nio.file.Paths; |
9 | 9 import java.util.LinkedList; |
| 10 import java.util.List; |
10 /** | 11 /** |
11 * Parses command line arguments for ProcessRobolectricTemplate. | 12 * Parses command line arguments for ProcessRobolectricTemplate. |
12 */ | 13 */ |
13 public class ProcessTemplateArgParser { | 14 public class ProcessTemplateArgParser { |
14 | 15 |
| 16 private List<TemplateFileInfo> mTemplateFileInfoList; |
15 private Path mBaseTemplateDir; | 17 private Path mBaseTemplateDir; |
16 private Path mOutputDir; | 18 private Path mOutputDir; |
17 private Integer mApiLevel; | 19 private Integer mApiLevel; |
18 | 20 |
19 public static ProcessTemplateArgParser parse(String[] args) { | 21 public static ProcessTemplateArgParser parse(String[] args) { |
20 | 22 |
21 ProcessTemplateArgParser parsed = new ProcessTemplateArgParser(); | 23 ProcessTemplateArgParser parsed = new ProcessTemplateArgParser(); |
22 | 24 |
23 for (int i = 0; i < args.length; ++i) { | 25 for (int i = 0; i < args.length; ++i) { |
24 if (args[i].startsWith("-")) { | 26 if (args[i].startsWith("-")) { |
25 String argName; | 27 String argName; |
26 if (args[i].startsWith("-", 1)) { | 28 if (args[i].startsWith("-", 1)) { |
27 argName = args[i].substring(2, args[i].length()); | 29 argName = args[i].substring(2, args[i].length()); |
28 } else { | 30 } else { |
29 argName = args[i].substring(1, args[i].length()); | 31 argName = args[i].substring(1, args[i].length()); |
30 } | 32 } |
31 try { | 33 try { |
32 if ("output-dir".equals(argName)) { | 34 if ("process-file".equals(argName)) { |
| 35 // Read the two command line arguments after the flag. |
| 36 // Format is --process-file <template file> <output file
>. |
| 37 // Argument can be passed multiple times. |
| 38 Path templatePath = Paths.get(args[++i]); |
| 39 Path outputPath = Paths.get(args[++i]); |
| 40 parsed.addTemplateFileInfo( |
| 41 new TemplateFileInfo(templatePath, outputPath)); |
| 42 } else if ("api-level".equals(argName)) { |
33 // Read the command line argument after the flag. | 43 // Read the command line argument after the flag. |
34 parsed.setOutputDir(args[++i]); | 44 parsed.setApiLevel(args[++i]); |
35 } else if ("base-template-dir".equals(argName)) { | 45 } else if ("base-template-dir".equals(argName)) { |
36 // Read the command line argument after the flag. | 46 // Read the command line argument after the flag. |
37 parsed.setBaseTemplateDir(args[++i]); | 47 parsed.setBaseTemplateDir(args[++i]); |
38 } else if ("api-level".equals(argName)) { | |
39 // Read the command line argument after the flag. | |
40 parsed.setApiLevel(args[++i]); | |
41 } else { | 48 } else { |
42 System.out.println("Ignoring flag: \"" + argName + "\"")
; | 49 System.out.println("Ignoring flag: \"" + argName + "\"")
; |
43 } | 50 } |
44 } catch (ArrayIndexOutOfBoundsException e) { | 51 } catch (ArrayIndexOutOfBoundsException e) { |
45 System.err.println("No value specified for argument \"" + ar
gName + "\""); | 52 System.err.println("No value specified for argument \"" + ar
gName + "\""); |
46 System.exit(1); | 53 System.exit(1); |
47 } | 54 } |
48 } else { | 55 } else { |
49 System.out.println("Ignoring argument: \"" + args[i] + "\""); | 56 System.out.println("Ignoring argument: \"" + args[i] + "\""); |
50 } | 57 } |
51 } | 58 } |
52 | 59 |
53 if (parsed.getOutputDir() == null) { | |
54 System.err.println("--output-dir argument required."); | |
55 System.exit(1); | |
56 } | |
57 | |
58 if (parsed.getBaseTemplateDir() == null) { | 60 if (parsed.getBaseTemplateDir() == null) { |
59 System.err.println("--base-template-dir argument required."); | 61 System.err.println("--base-template-dir argument required."); |
60 System.exit(1); | 62 System.exit(1); |
61 } | 63 } |
62 | 64 |
63 if (parsed.getApiLevel() == null) { | 65 if (parsed.getApiLevel() == null) { |
64 System.err.println("--api-level argument required."); | 66 System.err.println("--api-level argument required."); |
65 System.exit(1); | 67 System.exit(1); |
66 } | 68 } |
67 return parsed; | 69 return parsed; |
68 } | 70 } |
69 | 71 |
70 private ProcessTemplateArgParser() { | 72 private ProcessTemplateArgParser() { |
| 73 mApiLevel = null; |
71 mBaseTemplateDir = null; | 74 mBaseTemplateDir = null; |
72 mOutputDir = null; | 75 mOutputDir = null; |
73 mApiLevel = null; | 76 mTemplateFileInfoList = new LinkedList<TemplateFileInfo>(); |
| 77 } |
| 78 |
| 79 public Integer getApiLevel() { |
| 80 return mApiLevel; |
74 } | 81 } |
75 | 82 |
76 public Path getBaseTemplateDir() { | 83 public Path getBaseTemplateDir() { |
77 return mBaseTemplateDir; | 84 return mBaseTemplateDir; |
78 } | 85 } |
79 | 86 |
80 public Path getOutputDir() { | 87 public List<TemplateFileInfo> getTemplateFileInfoList() { |
81 return mOutputDir; | 88 return mTemplateFileInfoList; |
82 } | 89 } |
83 | 90 |
84 public Integer getApiLevel() { | 91 private void setApiLevel(String integer) { |
85 return mApiLevel; | 92 mApiLevel = Integer.parseInt(integer); |
86 } | 93 } |
87 | 94 |
88 private void setBaseTemplateDir(String path) { | 95 private void setBaseTemplateDir(String path) { |
89 mBaseTemplateDir = Paths.get(path); | 96 mBaseTemplateDir = Paths.get(path); |
90 } | 97 } |
91 | 98 |
92 private void setOutputDir(String path) { | 99 private void addTemplateFileInfo(TemplateFileInfo templateFileInfo) { |
93 mOutputDir = Paths.get(path); | 100 mTemplateFileInfoList.add(templateFileInfo); |
94 } | |
95 | |
96 private void setApiLevel(String integer) { | |
97 mApiLevel = Integer.parseInt(integer); | |
98 } | 101 } |
99 } | 102 } |
OLD | NEW |