From 7fd62ffd77ccedfc6dbe2384901b375859b726b6 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 4 Sep 2008 15:13:21 +0100 Subject: Initial setup of generator code. Little real functionality so far. --- csharp/ProtoGen.Test/DependencyResolutionTest.cs | 17 + csharp/ProtoGen.Test/GeneratorTest.cs | 7 + csharp/ProtoGen.Test/Properties/AssemblyInfo.cs | 36 + .../Google.ProtocolBuffers.ProtoGen.Test.snk | Bin 0 -> 596 bytes csharp/ProtoGen.Test/ProtoGen.Test.csproj | 72 + csharp/ProtoGen/DependencyResolutionException.cs | 17 + csharp/ProtoGen/Generator.cs | 47 + csharp/ProtoGen/GeneratorOptions.cs | 69 + csharp/ProtoGen/InvalidOptionsException.cs | 36 + csharp/ProtoGen/Program.cs | 41 + csharp/ProtoGen/Properties/AssemblyInfo.cs | 42 + .../Properties/Google.ProtocolBuffers.ProtoGen.snk | Bin 0 -> 596 bytes csharp/ProtoGen/ProtoGen.csproj | 66 + csharp/ProtoGen/app.config | 3 + .../Properties/Google.ProtocolBuffers.Test.snk | Bin 0 -> 596 bytes .../Properties/ProtocolBuffers.Test.snk | Bin 596 -> 0 bytes .../ProtocolBuffers.Test.csproj | 8 +- csharp/ProtocolBuffers.Test/lib/Rhino.Mocks.dll | Bin 287744 -> 0 bytes csharp/ProtocolBuffers.Test/lib/Rhino.Mocks.xml | 4849 -------------------- csharp/ProtocolBuffers.Test/lib/nunit-license.txt | 15 - .../ProtocolBuffers.Test/lib/nunit.framework.dll | Bin 45056 -> 0 bytes csharp/ProtocolBuffers.Test/lib/rhino-license.txt | 25 - csharp/lib/Rhino.Mocks.dll | Bin 0 -> 287744 bytes csharp/lib/Rhino.Mocks.xml | 4849 ++++++++++++++++++++ csharp/lib/nunit-license.txt | 15 + csharp/lib/nunit.framework.dll | Bin 0 -> 45056 bytes csharp/lib/rhino-license.txt | 25 + 27 files changed, 5346 insertions(+), 4893 deletions(-) create mode 100644 csharp/ProtoGen.Test/DependencyResolutionTest.cs create mode 100644 csharp/ProtoGen.Test/GeneratorTest.cs create mode 100644 csharp/ProtoGen.Test/Properties/AssemblyInfo.cs create mode 100644 csharp/ProtoGen.Test/Properties/Google.ProtocolBuffers.ProtoGen.Test.snk create mode 100644 csharp/ProtoGen.Test/ProtoGen.Test.csproj create mode 100644 csharp/ProtoGen/DependencyResolutionException.cs create mode 100644 csharp/ProtoGen/Generator.cs create mode 100644 csharp/ProtoGen/GeneratorOptions.cs create mode 100644 csharp/ProtoGen/InvalidOptionsException.cs create mode 100644 csharp/ProtoGen/Program.cs create mode 100644 csharp/ProtoGen/Properties/AssemblyInfo.cs create mode 100644 csharp/ProtoGen/Properties/Google.ProtocolBuffers.ProtoGen.snk create mode 100644 csharp/ProtoGen/ProtoGen.csproj create mode 100644 csharp/ProtoGen/app.config create mode 100644 csharp/ProtocolBuffers.Test/Properties/Google.ProtocolBuffers.Test.snk delete mode 100644 csharp/ProtocolBuffers.Test/Properties/ProtocolBuffers.Test.snk delete mode 100644 csharp/ProtocolBuffers.Test/lib/Rhino.Mocks.dll delete mode 100644 csharp/ProtocolBuffers.Test/lib/Rhino.Mocks.xml delete mode 100644 csharp/ProtocolBuffers.Test/lib/nunit-license.txt delete mode 100644 csharp/ProtocolBuffers.Test/lib/nunit.framework.dll delete mode 100644 csharp/ProtocolBuffers.Test/lib/rhino-license.txt create mode 100644 csharp/lib/Rhino.Mocks.dll create mode 100644 csharp/lib/Rhino.Mocks.xml create mode 100644 csharp/lib/nunit-license.txt create mode 100644 csharp/lib/nunit.framework.dll create mode 100644 csharp/lib/rhino-license.txt (limited to 'csharp') diff --git a/csharp/ProtoGen.Test/DependencyResolutionTest.cs b/csharp/ProtoGen.Test/DependencyResolutionTest.cs new file mode 100644 index 00000000..1906c44d --- /dev/null +++ b/csharp/ProtoGen.Test/DependencyResolutionTest.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; +using NUnit.Framework; + +namespace ProtoGen { + /// + /// Tests for the dependency resolution in Generator. + /// + [TestFixture] + public class DependencyResolutionTest { + + [Test] + public void TwoDistinctFiles() { + } + } +} diff --git a/csharp/ProtoGen.Test/GeneratorTest.cs b/csharp/ProtoGen.Test/GeneratorTest.cs new file mode 100644 index 00000000..137d9348 --- /dev/null +++ b/csharp/ProtoGen.Test/GeneratorTest.cs @@ -0,0 +1,7 @@ +using NUnit.Framework; + +namespace ProtoGen { + [TestFixture] + public class GeneratorTest { + } +} diff --git a/csharp/ProtoGen.Test/Properties/AssemblyInfo.cs b/csharp/ProtoGen.Test/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..98a0a384 --- /dev/null +++ b/csharp/ProtoGen.Test/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ProtoGen.Test")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ProtoGen.Test")] +[assembly: AssemblyCopyright("Copyright © 2008")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("40720ee3-2d15-4271-8c42-8f9cfd01b52f")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/csharp/ProtoGen.Test/Properties/Google.ProtocolBuffers.ProtoGen.Test.snk b/csharp/ProtoGen.Test/Properties/Google.ProtocolBuffers.ProtoGen.Test.snk new file mode 100644 index 00000000..3f53cbd4 Binary files /dev/null and b/csharp/ProtoGen.Test/Properties/Google.ProtocolBuffers.ProtoGen.Test.snk differ diff --git a/csharp/ProtoGen.Test/ProtoGen.Test.csproj b/csharp/ProtoGen.Test/ProtoGen.Test.csproj new file mode 100644 index 00000000..c1c05243 --- /dev/null +++ b/csharp/ProtoGen.Test/ProtoGen.Test.csproj @@ -0,0 +1,72 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {C268DA4C-4004-47DA-AF23-44C983281A68} + Library + Properties + ProtoGen + Google.ProtocolBuffers.ProtoGen.Test + v2.0 + 512 + true + Properties\Google.ProtocolBuffers.ProtoGen.Test.snk + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + False + ..\lib\nunit.framework.dll + + + False + ..\lib\Rhino.Mocks.dll + + + + + + + + + + + {6908BDCE-D925-43F3-94AC-A531E6DF2591} + ProtocolBuffers + + + {250ADE34-82FD-4BAE-86D5-985FBE589C4A} + ProtoGen + + + + + + + + \ No newline at end of file diff --git a/csharp/ProtoGen/DependencyResolutionException.cs b/csharp/ProtoGen/DependencyResolutionException.cs new file mode 100644 index 00000000..4e483fc1 --- /dev/null +++ b/csharp/ProtoGen/DependencyResolutionException.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Google.ProtocolBuffers.ProtoGen { + /// + /// Exception thrown when dependencies within a descriptor set can't be resolved. + /// + public sealed class DependencyResolutionException : Exception { + public DependencyResolutionException(string message) : base(message) { + } + + public DependencyResolutionException(string format, params object[] args) + : base(string.Format(format, args)) { + } + } +} diff --git a/csharp/ProtoGen/Generator.cs b/csharp/ProtoGen/Generator.cs new file mode 100644 index 00000000..d1ff76b2 --- /dev/null +++ b/csharp/ProtoGen/Generator.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using Google.ProtocolBuffers.DescriptorProtos; +using System.IO; +using Google.ProtocolBuffers.Descriptors; + +namespace Google.ProtocolBuffers.ProtoGen { + /// + /// Code generator for protocol buffers. Only C# is supported at the moment. + /// + public sealed class Generator { + + readonly GeneratorOptions options; + + private Generator(GeneratorOptions options) { + options.Validate(); + this.options = options; + } + + /// + /// Returns a generator configured with the specified options. + /// + public static Generator CreateGenerator(GeneratorOptions options) { + return new Generator(options); + } + + public void Generate() { + foreach (string inputFile in options.InputFiles) { + FileDescriptorSet descriptorProtos; + using (Stream inputStream = File.OpenRead(inputFile)) { + descriptorProtos = FileDescriptorSet.ParseFrom(inputStream); + } + List descriptors = ConvertDescriptors(descriptorProtos); + } + } + + /// + /// Resolves any dependencies and converts FileDescriptorProtos into FileDescriptors. + /// The list returned is in the same order as the protos are listed in the descriptor set. + /// Note: this method is internal rather than private to allow testing. + /// + /// Not all dependencies could be resolved. + internal static List ConvertDescriptors(FileDescriptorSet descriptorProtos) { + return null; + } + } +} diff --git a/csharp/ProtoGen/GeneratorOptions.cs b/csharp/ProtoGen/GeneratorOptions.cs new file mode 100644 index 00000000..c37352ed --- /dev/null +++ b/csharp/ProtoGen/GeneratorOptions.cs @@ -0,0 +1,69 @@ +using System.Collections.Generic; +using System.IO; + +namespace Google.ProtocolBuffers.ProtoGen { + + /// + /// All the configuration required for the generator - where to generate + /// output files, the location of input files etc. While this isn't immutable + /// in practice, the contents shouldn't be changed after being passed to + /// the generator. + /// + public sealed class GeneratorOptions { + + public string OutputDirectory { get; set; } + public IList InputFiles { get; set; } + + /// + /// Attempts to validate the options, but doesn't throw an exception if they're invalid. + /// Instead, when this method returns false, the output variable will contain a collection + /// of reasons for the validation failure. + /// + /// Variable to receive a list of reasons in case of validation failure. + /// true if the options are valid; false otherwise + public bool TryValidate(out IList reasons) { + List tmpReasons = new List(); + + // Output directory validation + if (string.IsNullOrEmpty(OutputDirectory)) { + tmpReasons.Add("No output directory specified"); + } else { + if (!Directory.Exists(OutputDirectory)) { + tmpReasons.Add("Specified output directory (" + OutputDirectory + " doesn't exist."); + } + } + + // Input file validation (just in terms of presence) + if (InputFiles == null || InputFiles.Count == 0) { + tmpReasons.Add("No input files specified"); + } else { + foreach (string input in InputFiles) { + FileInfo fi = new FileInfo(input); + if (!fi.Exists) { + tmpReasons.Add("Input file " + input + " doesn't exist."); + } + } + } + + if (tmpReasons.Count != 0) { + reasons = tmpReasons; + return false; + } + + reasons = null; + return true; + } + + /// + /// Validates that all the options have been set and are valid, + /// throwing an exception if they haven't. + /// + /// The options are invalid. + public void Validate() { + IList reasons; + if (!TryValidate(out reasons)) { + throw new InvalidOptionsException(reasons); + } + } + } +} diff --git a/csharp/ProtoGen/InvalidOptionsException.cs b/csharp/ProtoGen/InvalidOptionsException.cs new file mode 100644 index 00000000..aee03228 --- /dev/null +++ b/csharp/ProtoGen/InvalidOptionsException.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Google.ProtocolBuffers.Collections; + +namespace Google.ProtocolBuffers.ProtoGen { + /// + /// Exception thrown to indicate that the options passed were invalid. + /// + public sealed class InvalidOptionsException : Exception { + + private readonly IList reasons; + + /// + /// An immutable list of reasons why the options were invalid. + /// + public IList Reasons { + get { return reasons; } + } + + public InvalidOptionsException(IList reasons) + : base(BuildMessage(reasons)) { + this.reasons = Lists.AsReadOnly(reasons); + } + + private static string BuildMessage(IEnumerable reasons) { + StringBuilder builder = new StringBuilder("Invalid options:"); + builder.AppendLine(); + foreach (string reason in reasons) { + builder.Append(" "); + builder.AppendLine(reason); + } + return builder.ToString(); + } + } +} diff --git a/csharp/ProtoGen/Program.cs b/csharp/ProtoGen/Program.cs new file mode 100644 index 00000000..22245ab8 --- /dev/null +++ b/csharp/ProtoGen/Program.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; + +namespace Google.ProtocolBuffers.ProtoGen { + /// + /// Entry point for the Protocol Buffers generator. + /// + class Program { + static int Main(string[] args) { + try { + + GeneratorOptions options = ParseCommandLineArguments(args); + + IList validationFailures; + if (!options.TryValidate(out validationFailures)) { + // We've already got the message-building logic in the exception... + InvalidOptionsException exception = new InvalidOptionsException(validationFailures); + Console.WriteLine(exception.Message); + return 1; + } + + Generator generator = Generator.CreateGenerator(options); + generator.Generate(); + + + return 0; + } catch (Exception e) { + Console.Error.WriteLine("Caught unhandled exception: {0}", e); + return 1; + } + } + + private static GeneratorOptions ParseCommandLineArguments(string[] args) { + GeneratorOptions options = new GeneratorOptions(); + string baseDir = "c:\\Users\\Jon\\Documents\\Visual Studio 2008\\Projects\\ProtocolBuffers"; + options.OutputDirectory = baseDir + "\\tmp"; + options.InputFiles = new[] { baseDir + "\\protos\\nwind.protobin" }; + return options; + } + } +} \ No newline at end of file diff --git a/csharp/ProtoGen/Properties/AssemblyInfo.cs b/csharp/ProtoGen/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..24f5d781 --- /dev/null +++ b/csharp/ProtoGen/Properties/AssemblyInfo.cs @@ -0,0 +1,42 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ProtoGen")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ProtoGen")] +[assembly: AssemblyCopyright("Copyright © 2008")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("7101763b-7a38-41be-87f5-7ede4c554509")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: InternalsVisibleTo("Google.ProtocolBuffers.ProtoGen.Test,PublicKey=" + +"0024000004800000940000000602000000240000525341310004000001000100cf43741ffc3e65" + +"c85707245e144e90f1bb82f20d1b1555846008d4d5d5c9270a980350dcb1ddd40fcdde13c2780c" + +"75c9057123daa5613cb6551e2b8bd2254e8f84b3893369869e5119b752442aef7156c4defc489b" + +"96c44ff801fe8d94199e048f8ff414813c9c811a029bcd697040700dc66982539e9b368cb5e725" + +"feed60f2")] diff --git a/csharp/ProtoGen/Properties/Google.ProtocolBuffers.ProtoGen.snk b/csharp/ProtoGen/Properties/Google.ProtocolBuffers.ProtoGen.snk new file mode 100644 index 00000000..8e4d98a5 Binary files /dev/null and b/csharp/ProtoGen/Properties/Google.ProtocolBuffers.ProtoGen.snk differ diff --git a/csharp/ProtoGen/ProtoGen.csproj b/csharp/ProtoGen/ProtoGen.csproj new file mode 100644 index 00000000..b4d06c50 --- /dev/null +++ b/csharp/ProtoGen/ProtoGen.csproj @@ -0,0 +1,66 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {250ADE34-82FD-4BAE-86D5-985FBE589C4A} + Exe + Properties + Google.ProtocolBuffers.ProtoGen + ProtoGen + v2.0 + 512 + + + true + Properties\Google.ProtocolBuffers.ProtoGen.snk + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + {6908BDCE-D925-43F3-94AC-A531E6DF2591} + ProtocolBuffers + + + + + \ No newline at end of file diff --git a/csharp/ProtoGen/app.config b/csharp/ProtoGen/app.config new file mode 100644 index 00000000..df20690a --- /dev/null +++ b/csharp/ProtoGen/app.config @@ -0,0 +1,3 @@ + + + diff --git a/csharp/ProtocolBuffers.Test/Properties/Google.ProtocolBuffers.Test.snk b/csharp/ProtocolBuffers.Test/Properties/Google.ProtocolBuffers.Test.snk new file mode 100644 index 00000000..20046fb2 Binary files /dev/null and b/csharp/ProtocolBuffers.Test/Properties/Google.ProtocolBuffers.Test.snk differ diff --git a/csharp/ProtocolBuffers.Test/Properties/ProtocolBuffers.Test.snk b/csharp/ProtocolBuffers.Test/Properties/ProtocolBuffers.Test.snk deleted file mode 100644 index 20046fb2..00000000 Binary files a/csharp/ProtocolBuffers.Test/Properties/ProtocolBuffers.Test.snk and /dev/null differ diff --git a/csharp/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj b/csharp/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj index 19cacd01..6335c614 100644 --- a/csharp/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj +++ b/csharp/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj @@ -13,7 +13,7 @@ v2.0 512 true - Properties\ProtocolBuffers.Test.snk + Properties\Google.ProtocolBuffers.Test.snk true @@ -35,11 +35,11 @@ False - lib\nunit.framework.dll + ..\lib\nunit.framework.dll False - lib\Rhino.Mocks.dll + ..\lib\Rhino.Mocks.dll @@ -79,7 +79,7 @@ - +