aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2013-05-07 15:38:21 -0500
committerrogerk <devnull@localhost>2013-05-07 15:38:21 -0500
commit71e22f8b7f08116d7ed021b59fe1f154110a2a6a (patch)
tree11f12a2633f19a26c25d0c676a0787f8800df1ee
parentef7091c9eda6d4e02860c879d5b1cf7044fd144c (diff)
downloadprotobuf-71e22f8b7f08116d7ed021b59fe1f154110a2a6a.tar.gz
protobuf-71e22f8b7f08116d7ed021b59fe1f154110a2a6a.tar.bz2
protobuf-71e22f8b7f08116d7ed021b59fe1f154110a2a6a.zip
Issue #43: Fix to correct identical 'umbrella_classname' options from trying to write to the same filename.
-rw-r--r--src/ProtoGen/Generator.cs30
1 files changed, 26 insertions, 4 deletions
diff --git a/src/ProtoGen/Generator.cs b/src/ProtoGen/Generator.cs
index 516018ef..e37d655e 100644
--- a/src/ProtoGen/Generator.cs
+++ b/src/ProtoGen/Generator.cs
@@ -34,6 +34,7 @@
#endregion
+using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
@@ -85,6 +86,19 @@ namespace Google.ProtocolBuffers.ProtoGen
descriptor.ConfigureWithDefaultOptions(options.FileOptions);
}
+ bool duplicates = false;
+ Dictionary<string, bool> names = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
+ foreach (FileDescriptor descriptor in descriptors)
+ {
+ string file = GetOutputFile(descriptor, false);
+ if (names.ContainsKey(file))
+ {
+ duplicates = true;
+ break;
+ }
+ names.Add(file, true);
+ }
+
foreach (FileDescriptor descriptor in descriptors)
{
// Optionally exclude descriptors in google.protobuf
@@ -92,7 +106,7 @@ namespace Google.ProtocolBuffers.ProtoGen
{
continue;
}
- Generate(descriptor);
+ Generate(descriptor, duplicates);
}
}
@@ -100,21 +114,29 @@ namespace Google.ProtocolBuffers.ProtoGen
/// Generates code for a particular file. All dependencies must
/// already have been resolved.
/// </summary>
- private void Generate(FileDescriptor descriptor)
+ private void Generate(FileDescriptor descriptor, bool duplicates)
{
UmbrellaClassGenerator ucg = new UmbrellaClassGenerator(descriptor);
- using (TextWriter textWriter = File.CreateText(GetOutputFile(descriptor)))
+ using (TextWriter textWriter = File.CreateText(GetOutputFile(descriptor, duplicates)))
{
TextGenerator writer = new TextGenerator(textWriter, options.LineBreak);
ucg.Generate(writer);
}
}
- private string GetOutputFile(FileDescriptor descriptor)
+ private string GetOutputFile(FileDescriptor descriptor, bool duplicates)
{
CSharpFileOptions fileOptions = descriptor.CSharpOptions;
string filename = descriptor.CSharpOptions.UmbrellaClassname + descriptor.CSharpOptions.FileExtension;
+ if (duplicates)
+ {
+ string namepart;
+ if (String.IsNullOrEmpty(descriptor.Name) || String.IsNullOrEmpty(namepart = Path.GetFileNameWithoutExtension(descriptor.Name)))
+ throw new ApplicationException("Duplicate UmbrellaClassname options created a file name collision.");
+
+ filename = namepart + descriptor.CSharpOptions.FileExtension;
+ }
string outputDirectory = descriptor.CSharpOptions.OutputDirectory;
if (fileOptions.ExpandNamespaceDirectories)