1 | #region Copyright notice and license
|
---|
2 | // Protocol Buffers - Google's data interchange format
|
---|
3 | // Copyright 2008 Google Inc. All rights reserved.
|
---|
4 | // http://github.com/jskeet/dotnet-protobufs/
|
---|
5 | // Original C++/Java/Python code:
|
---|
6 | // http://code.google.com/p/protobuf/
|
---|
7 | //
|
---|
8 | // Redistribution and use in source and binary forms, with or without
|
---|
9 | // modification, are permitted provided that the following conditions are
|
---|
10 | // met:
|
---|
11 | //
|
---|
12 | // * Redistributions of source code must retain the above copyright
|
---|
13 | // notice, this list of conditions and the following disclaimer.
|
---|
14 | // * Redistributions in binary form must reproduce the above
|
---|
15 | // copyright notice, this list of conditions and the following disclaimer
|
---|
16 | // in the documentation and/or other materials provided with the
|
---|
17 | // distribution.
|
---|
18 | // * Neither the name of Google Inc. nor the names of its
|
---|
19 | // contributors may be used to endorse or promote products derived from
|
---|
20 | // this software without specific prior written permission.
|
---|
21 | //
|
---|
22 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
23 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
24 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
25 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
26 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
27 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
28 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
29 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
30 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
31 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
32 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
33 | #endregion
|
---|
34 |
|
---|
35 | using System.Collections.Generic;
|
---|
36 | using System.IO;
|
---|
37 |
|
---|
38 | namespace Google.ProtocolBuffers.ProtoGen {
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | /// All the configuration required for the generator - where to generate
|
---|
42 | /// output files, the location of input files etc. While this isn't immutable
|
---|
43 | /// in practice, the contents shouldn't be changed after being passed to
|
---|
44 | /// the generator.
|
---|
45 | /// </summary>
|
---|
46 | public sealed class GeneratorOptions {
|
---|
47 |
|
---|
48 | public string OutputDirectory { get; set; }
|
---|
49 | public IList<string> InputFiles { get; set; }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// Attempts to validate the options, but doesn't throw an exception if they're invalid.
|
---|
53 | /// Instead, when this method returns false, the output variable will contain a collection
|
---|
54 | /// of reasons for the validation failure.
|
---|
55 | /// </summary>
|
---|
56 | /// <param name="reasons">Variable to receive a list of reasons in case of validation failure.</param>
|
---|
57 | /// <returns>true if the options are valid; false otherwise</returns>
|
---|
58 | public bool TryValidate(out IList<string> reasons) {
|
---|
59 | List<string> tmpReasons = new List<string>();
|
---|
60 |
|
---|
61 | // Output directory validation
|
---|
62 | if (string.IsNullOrEmpty(OutputDirectory)) {
|
---|
63 | tmpReasons.Add("No output directory specified");
|
---|
64 | } else {
|
---|
65 | if (!Directory.Exists(OutputDirectory)) {
|
---|
66 | tmpReasons.Add("Specified output directory (" + OutputDirectory + " doesn't exist.");
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | // Input file validation (just in terms of presence)
|
---|
71 | if (InputFiles == null || InputFiles.Count == 0) {
|
---|
72 | tmpReasons.Add("No input files specified");
|
---|
73 | } else {
|
---|
74 | foreach (string input in InputFiles) {
|
---|
75 | FileInfo fi = new FileInfo(input);
|
---|
76 | if (!fi.Exists) {
|
---|
77 | tmpReasons.Add("Input file " + input + " doesn't exist.");
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (tmpReasons.Count != 0) {
|
---|
83 | reasons = tmpReasons;
|
---|
84 | return false;
|
---|
85 | }
|
---|
86 |
|
---|
87 | reasons = null;
|
---|
88 | return true;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /// <summary>
|
---|
92 | /// Validates that all the options have been set and are valid,
|
---|
93 | /// throwing an exception if they haven't.
|
---|
94 | /// </summary>
|
---|
95 | /// <exception cref="InvalidOptionsException">The options are invalid.</exception>
|
---|
96 | public void Validate() {
|
---|
97 | IList<string> reasons;
|
---|
98 | if (!TryValidate(out reasons)) {
|
---|
99 | throw new InvalidOptionsException(reasons);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|