Allow creation of namespace directories

pull/288/head
Jon Skeet 15 years ago
parent 55a212d384
commit d3f2d098fa
  1. 24
      protos/google/protobuf/csharp_options.proto
  2. 19
      src/ProtoGen/Generator.cs
  3. 59
      src/ProtocolBuffers/DescriptorProtos/CSharpOptions.cs

@ -5,12 +5,36 @@ import "google/protobuf/descriptor.proto";
package google.protobuf; package google.protobuf;
message CSharpFileOptions { message CSharpFileOptions {
// Namespace for generated classes; defaults to the package.
optional string namespace = 1; optional string namespace = 1;
// Name of the "umbrella" class used for metadata about all
// the messages within this file. Default is based on the name
// of the file.
optional string umbrella_classname = 2; optional string umbrella_classname = 2;
// Whether classes should be public (true) or internal (false)
optional bool public_classes = 3 [default = true]; optional bool public_classes = 3 [default = true];
// Whether to generate a single file for everything within the
// .proto file (false), or one file per message (true).
// This option is not currently honored; please log a feature
// request if you really want it.
optional bool multiple_files = 4; optional bool multiple_files = 4;
// Whether to nest messages within a single umbrella class (true)
// or create the umbrella class as a peer, with messages as
// top-level classes in the namespace (false)
optional bool nest_classes = 5; optional bool nest_classes = 5;
// Generate appropriate support for Code Contracts
// (Ongoing; support should improve over time)
optional bool code_contracts = 6; optional bool code_contracts = 6;
// Create subdirectories for namespaces, e.g. namespace "Foo.Bar"
// would generate files within [output directory]/Foo/Bar
optional bool expand_namespace_directories = 7;
} }
extend FileOptions { extend FileOptions {

@ -82,12 +82,29 @@ namespace Google.ProtocolBuffers.ProtoGen {
/// </summary> /// </summary>
private void Generate(FileDescriptor descriptor) { private void Generate(FileDescriptor descriptor) {
UmbrellaClassGenerator ucg = new UmbrellaClassGenerator(descriptor); UmbrellaClassGenerator ucg = new UmbrellaClassGenerator(descriptor);
using (TextWriter textWriter = File.CreateText(Path.Combine(options.OutputDirectory, descriptor.CSharpOptions.UmbrellaClassname + ".cs"))) { using (TextWriter textWriter = File.CreateText(GetOutputFile(descriptor))) {
TextGenerator writer = new TextGenerator(textWriter); TextGenerator writer = new TextGenerator(textWriter);
ucg.Generate(writer); ucg.Generate(writer);
} }
} }
private string GetOutputFile(FileDescriptor descriptor) {
CSharpFileOptions fileOptions = descriptor.CSharpOptions;
string filename = descriptor.CSharpOptions.UmbrellaClassname + ".cs";
string outputDirectory = options.OutputDirectory;
if (fileOptions.ExpandNamespaceDirectories) {
string package = fileOptions.Namespace;
if (!string.IsNullOrEmpty(package)) {
string[] bits = package.Split('.');
foreach (string bit in bits) {
outputDirectory = Path.Combine(outputDirectory, bit);
}
Directory.CreateDirectory(outputDirectory);
}
}
return Path.Combine(outputDirectory, filename);
}
/// <summary> /// <summary>
/// Resolves any dependencies and converts FileDescriptorProtos into FileDescriptors. /// 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. /// The list returned is in the same order as the protos are listed in the descriptor set.

@ -37,22 +37,22 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
byte[] descriptorData = global::System.Convert.FromBase64String( byte[] descriptorData = global::System.Convert.FromBase64String(
"CiRnb29nbGUvcHJvdG9idWYvY3NoYXJwX29wdGlvbnMucHJvdG8SD2dvb2ds" + "CiRnb29nbGUvcHJvdG9idWYvY3NoYXJwX29wdGlvbnMucHJvdG8SD2dvb2ds" +
"ZS5wcm90b2J1ZhogZ29vZ2xlL3Byb3RvYnVmL2Rlc2NyaXB0b3IucHJvdG8i" + "ZS5wcm90b2J1ZhogZ29vZ2xlL3Byb3RvYnVmL2Rlc2NyaXB0b3IucHJvdG8i" +
"pgEKEUNTaGFycEZpbGVPcHRpb25zEhEKCW5hbWVzcGFjZRgBIAEoCRIaChJ1" + "zAEKEUNTaGFycEZpbGVPcHRpb25zEhEKCW5hbWVzcGFjZRgBIAEoCRIaChJ1" +
"bWJyZWxsYV9jbGFzc25hbWUYAiABKAkSHAoOcHVibGljX2NsYXNzZXMYAyAB" + "bWJyZWxsYV9jbGFzc25hbWUYAiABKAkSHAoOcHVibGljX2NsYXNzZXMYAyAB" +
"KAg6BHRydWUSFgoObXVsdGlwbGVfZmlsZXMYBCABKAgSFAoMbmVzdF9jbGFz" + "KAg6BHRydWUSFgoObXVsdGlwbGVfZmlsZXMYBCABKAgSFAoMbmVzdF9jbGFz" +
"c2VzGAUgASgIEhYKDmNvZGVfY29udHJhY3RzGAYgASgIIisKEkNTaGFycEZp" + "c2VzGAUgASgIEhYKDmNvZGVfY29udHJhY3RzGAYgASgIEiQKHGV4cGFuZF9u" +
"ZWxkT3B0aW9ucxIVCg1wcm9wZXJ0eV9uYW1lGAEgASgJOl4KE2NzaGFycF9m" + "YW1lc3BhY2VfZGlyZWN0b3JpZXMYByABKAgiKwoSQ1NoYXJwRmllbGRPcHRp" +
"aWxlX29wdGlvbnMSHC5nb29nbGUucHJvdG9idWYuRmlsZU9wdGlvbnMY6Acg" + "b25zEhUKDXByb3BlcnR5X25hbWUYASABKAk6XgoTY3NoYXJwX2ZpbGVfb3B0" +
"ASgLMiIuZ29vZ2xlLnByb3RvYnVmLkNTaGFycEZpbGVPcHRpb25zOmEKFGNz" + "aW9ucxIcLmdvb2dsZS5wcm90b2J1Zi5GaWxlT3B0aW9ucxjoByABKAsyIi5n" +
"aGFycF9maWVsZF9vcHRpb25zEh0uZ29vZ2xlLnByb3RvYnVmLkZpZWxkT3B0" + "b29nbGUucHJvdG9idWYuQ1NoYXJwRmlsZU9wdGlvbnM6YQoUY3NoYXJwX2Zp" +
"aW9ucxjoByABKAsyIy5nb29nbGUucHJvdG9idWYuQ1NoYXJwRmllbGRPcHRp" + "ZWxkX29wdGlvbnMSHS5nb29nbGUucHJvdG9idWYuRmllbGRPcHRpb25zGOgH" +
"b25z"); "IAEoCzIjLmdvb2dsZS5wcm90b2J1Zi5DU2hhcnBGaWVsZE9wdGlvbnM=");
pbd::FileDescriptor.InternalDescriptorAssigner assigner = delegate(pbd::FileDescriptor root) { pbd::FileDescriptor.InternalDescriptorAssigner assigner = delegate(pbd::FileDescriptor root) {
descriptor = root; descriptor = root;
internal__static_google_protobuf_CSharpFileOptions__Descriptor = Descriptor.MessageTypes[0]; internal__static_google_protobuf_CSharpFileOptions__Descriptor = Descriptor.MessageTypes[0];
internal__static_google_protobuf_CSharpFileOptions__FieldAccessorTable = internal__static_google_protobuf_CSharpFileOptions__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable<global::Google.ProtocolBuffers.DescriptorProtos.CSharpFileOptions, global::Google.ProtocolBuffers.DescriptorProtos.CSharpFileOptions.Builder>(internal__static_google_protobuf_CSharpFileOptions__Descriptor, new pb::FieldAccess.FieldAccessorTable<global::Google.ProtocolBuffers.DescriptorProtos.CSharpFileOptions, global::Google.ProtocolBuffers.DescriptorProtos.CSharpFileOptions.Builder>(internal__static_google_protobuf_CSharpFileOptions__Descriptor,
new string[] { "Namespace", "UmbrellaClassname", "PublicClasses", "MultipleFiles", "NestClasses", "CodeContracts", }); new string[] { "Namespace", "UmbrellaClassname", "PublicClasses", "MultipleFiles", "NestClasses", "CodeContracts", "ExpandNamespaceDirectories", });
internal__static_google_protobuf_CSharpFieldOptions__Descriptor = Descriptor.MessageTypes[1]; internal__static_google_protobuf_CSharpFieldOptions__Descriptor = Descriptor.MessageTypes[1];
internal__static_google_protobuf_CSharpFieldOptions__FieldAccessorTable = internal__static_google_protobuf_CSharpFieldOptions__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable<global::Google.ProtocolBuffers.DescriptorProtos.CSharpFieldOptions, global::Google.ProtocolBuffers.DescriptorProtos.CSharpFieldOptions.Builder>(internal__static_google_protobuf_CSharpFieldOptions__Descriptor, new pb::FieldAccess.FieldAccessorTable<global::Google.ProtocolBuffers.DescriptorProtos.CSharpFieldOptions, global::Google.ProtocolBuffers.DescriptorProtos.CSharpFieldOptions.Builder>(internal__static_google_protobuf_CSharpFieldOptions__Descriptor,
@ -152,6 +152,16 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
get { return codeContracts_; } get { return codeContracts_; }
} }
public const int ExpandNamespaceDirectoriesFieldNumber = 7;
private bool hasExpandNamespaceDirectories;
private bool expandNamespaceDirectories_ = false;
public bool HasExpandNamespaceDirectories {
get { return hasExpandNamespaceDirectories; }
}
public bool ExpandNamespaceDirectories {
get { return expandNamespaceDirectories_; }
}
public override bool IsInitialized { public override bool IsInitialized {
get { get {
return true; return true;
@ -177,6 +187,9 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
if (HasCodeContracts) { if (HasCodeContracts) {
output.WriteBool(6, CodeContracts); output.WriteBool(6, CodeContracts);
} }
if (HasExpandNamespaceDirectories) {
output.WriteBool(7, ExpandNamespaceDirectories);
}
UnknownFields.WriteTo(output); UnknownFields.WriteTo(output);
} }
@ -205,6 +218,9 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
if (HasCodeContracts) { if (HasCodeContracts) {
size += pb::CodedOutputStream.ComputeBoolSize(6, CodeContracts); size += pb::CodedOutputStream.ComputeBoolSize(6, CodeContracts);
} }
if (HasExpandNamespaceDirectories) {
size += pb::CodedOutputStream.ComputeBoolSize(7, ExpandNamespaceDirectories);
}
size += UnknownFields.SerializedSize; size += UnknownFields.SerializedSize;
memoizedSerializedSize = size; memoizedSerializedSize = size;
return size; return size;
@ -315,6 +331,9 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
if (other.HasCodeContracts) { if (other.HasCodeContracts) {
CodeContracts = other.CodeContracts; CodeContracts = other.CodeContracts;
} }
if (other.HasExpandNamespaceDirectories) {
ExpandNamespaceDirectories = other.ExpandNamespaceDirectories;
}
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
@ -371,6 +390,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
CodeContracts = input.ReadBool(); CodeContracts = input.ReadBool();
break; break;
} }
case 56: {
ExpandNamespaceDirectories = input.ReadBool();
break;
}
} }
} }
} }
@ -485,6 +508,24 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
result.codeContracts_ = false; result.codeContracts_ = false;
return this; return this;
} }
public bool HasExpandNamespaceDirectories {
get { return result.HasExpandNamespaceDirectories; }
}
public bool ExpandNamespaceDirectories {
get { return result.ExpandNamespaceDirectories; }
set { SetExpandNamespaceDirectories(value); }
}
public Builder SetExpandNamespaceDirectories(bool value) {
result.hasExpandNamespaceDirectories = true;
result.expandNamespaceDirectories_ = value;
return this;
}
public Builder ClearExpandNamespaceDirectories() {
result.hasExpandNamespaceDirectories = false;
result.expandNamespaceDirectories_ = false;
return this;
}
} }
static CSharpFileOptions() { static CSharpFileOptions() {
object.ReferenceEquals(global::Google.ProtocolBuffers.DescriptorProtos.CSharpOptions.Descriptor, null); object.ReferenceEquals(global::Google.ProtocolBuffers.DescriptorProtos.CSharpOptions.Descriptor, null);

Loading…
Cancel
Save