Commit 97a1c1e0 authored by Jan Tattermusch's avatar Jan Tattermusch

Merge pull request #785 from jskeet/csharp-directories

Generate C# directory hierarchy with new option
parents c9418912 9489817d
...@@ -35,11 +35,10 @@ if [ -z "$PROTOC" ]; then ...@@ -35,11 +35,10 @@ if [ -z "$PROTOC" ]; then
fi fi
fi fi
# Descriptor proto # descriptor.proto and well-known types
$PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf/Reflection \ $PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf \
src/google/protobuf/descriptor.proto --csharp_opt=base_namespace=Google.Protobuf \
src/google/protobuf/descriptor.proto \
$PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf/WellKnownTypes \
src/google/protobuf/any.proto \ src/google/protobuf/any.proto \
src/google/protobuf/api.proto \ src/google/protobuf/api.proto \
src/google/protobuf/duration.proto \ src/google/protobuf/duration.proto \
...@@ -51,15 +50,18 @@ $PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf/WellKnownTypes \ ...@@ -51,15 +50,18 @@ $PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf/WellKnownTypes \
src/google/protobuf/type.proto \ src/google/protobuf/type.proto \
src/google/protobuf/wrappers.proto src/google/protobuf/wrappers.proto
$PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf.Test/TestProtos \ # Test protos where the namespace matches the target location
$PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf.Test \
--csharp_opt=base_namespace=Google.Protobuf \
src/google/protobuf/map_unittest_proto3.proto \ src/google/protobuf/map_unittest_proto3.proto \
src/google/protobuf/unittest_proto3.proto \ src/google/protobuf/unittest_proto3.proto \
src/google/protobuf/unittest_import_proto3.proto \ src/google/protobuf/unittest_import_proto3.proto \
src/google/protobuf/unittest_import_public_proto3.proto \ src/google/protobuf/unittest_import_public_proto3.proto \
src/google/protobuf/unittest_well_known_types.proto src/google/protobuf/unittest_well_known_types.proto
# Different base namespace to the protos above
$PROTOC -Icsharp/protos --csharp_out=csharp/src/Google.Protobuf.Test/TestProtos \ $PROTOC -Icsharp/protos --csharp_out=csharp/src/Google.Protobuf.Test \
--csharp_opt=base_namespace=UnitTest.Issues \
csharp/protos/unittest_issues.proto csharp/protos/unittest_issues.proto
# AddressBook sample protos # AddressBook sample protos
......
...@@ -129,9 +129,9 @@ namespace Google.Protobuf.Reflection { ...@@ -129,9 +129,9 @@ namespace Google.Protobuf.Reflection {
"Z29vZ2xlLnByb3RvYnVmLlNvdXJjZUNvZGVJbmZvLkxvY2F0aW9uGoYBCghM", "Z29vZ2xlLnByb3RvYnVmLlNvdXJjZUNvZGVJbmZvLkxvY2F0aW9uGoYBCghM",
"b2NhdGlvbhIQCgRwYXRoGAEgAygFQgIQARIQCgRzcGFuGAIgAygFQgIQARIY", "b2NhdGlvbhIQCgRwYXRoGAEgAygFQgIQARIQCgRzcGFuGAIgAygFQgIQARIY",
"ChBsZWFkaW5nX2NvbW1lbnRzGAMgASgJEhkKEXRyYWlsaW5nX2NvbW1lbnRz", "ChBsZWFkaW5nX2NvbW1lbnRzGAMgASgJEhkKEXRyYWlsaW5nX2NvbW1lbnRz",
"GAQgASgJEiEKGWxlYWRpbmdfZGV0YWNoZWRfY29tbWVudHMYBiADKAlCWwoT", "GAQgASgJEiEKGWxlYWRpbmdfZGV0YWNoZWRfY29tbWVudHMYBiADKAlCWAoT",
"Y29tLmdvb2dsZS5wcm90b2J1ZkIQRGVzY3JpcHRvclByb3Rvc0gBWgpkZXNj", "Y29tLmdvb2dsZS5wcm90b2J1ZkIQRGVzY3JpcHRvclByb3Rvc0gBWgpkZXNj",
"cmlwdG9yogIDR1BCqgIaR29vZ2xlLlByb3RvYnVmLlJlZmxlY3Rpb26wAgE=")); "cmlwdG9yogIDR1BCqgIaR29vZ2xlLlByb3RvYnVmLlJlZmxlY3Rpb24="));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData, descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] { }, new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] { new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
......
...@@ -24,9 +24,9 @@ namespace Google.Protobuf.WellKnownTypes { ...@@ -24,9 +24,9 @@ namespace Google.Protobuf.WellKnownTypes {
byte[] descriptorData = global::System.Convert.FromBase64String( byte[] descriptorData = global::System.Convert.FromBase64String(
string.Concat( string.Concat(
"Chtnb29nbGUvcHJvdG9idWYvZW1wdHkucHJvdG8SD2dvb2dsZS5wcm90b2J1", "Chtnb29nbGUvcHJvdG9idWYvZW1wdHkucHJvdG8SD2dvb2dsZS5wcm90b2J1",
"ZiIHCgVFbXB0eUJKChNjb20uZ29vZ2xlLnByb3RvYnVmQgpFbXB0eVByb3Rv", "ZiIHCgVFbXB0eUJNChNjb20uZ29vZ2xlLnByb3RvYnVmQgpFbXB0eVByb3Rv",
"UAGiAgNHUEKqAh5Hb29nbGUuUHJvdG9idWYuV2VsbEtub3duVHlwZXNiBnBy", "UAGgAQGiAgNHUEKqAh5Hb29nbGUuUHJvdG9idWYuV2VsbEtub3duVHlwZXNi",
"b3RvMw==")); "BnByb3RvMw=="));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData, descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] { }, new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] { new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
......
...@@ -24,9 +24,9 @@ namespace Google.Protobuf.WellKnownTypes { ...@@ -24,9 +24,9 @@ namespace Google.Protobuf.WellKnownTypes {
byte[] descriptorData = global::System.Convert.FromBase64String( byte[] descriptorData = global::System.Convert.FromBase64String(
string.Concat( string.Concat(
"CiBnb29nbGUvcHJvdG9idWYvZmllbGRfbWFzay5wcm90bxIPZ29vZ2xlLnBy", "CiBnb29nbGUvcHJvdG9idWYvZmllbGRfbWFzay5wcm90bxIPZ29vZ2xlLnBy",
"b3RvYnVmIhoKCUZpZWxkTWFzaxINCgVwYXRocxgBIAMoCUJOChNjb20uZ29v", "b3RvYnVmIhoKCUZpZWxkTWFzaxINCgVwYXRocxgBIAMoCUJRChNjb20uZ29v",
"Z2xlLnByb3RvYnVmQg5GaWVsZE1hc2tQcm90b1ABogIDR1BCqgIeR29vZ2xl", "Z2xlLnByb3RvYnVmQg5GaWVsZE1hc2tQcm90b1ABoAEBogIDR1BCqgIeR29v",
"LlByb3RvYnVmLldlbGxLbm93blR5cGVzYgZwcm90bzM=")); "Z2xlLlByb3RvYnVmLldlbGxLbm93blR5cGVzYgZwcm90bzM="));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData, descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] { }, new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] { new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
......
...@@ -25,9 +25,9 @@ namespace Google.Protobuf.WellKnownTypes { ...@@ -25,9 +25,9 @@ namespace Google.Protobuf.WellKnownTypes {
string.Concat( string.Concat(
"CiRnb29nbGUvcHJvdG9idWYvc291cmNlX2NvbnRleHQucHJvdG8SD2dvb2ds", "CiRnb29nbGUvcHJvdG9idWYvc291cmNlX2NvbnRleHQucHJvdG8SD2dvb2ds",
"ZS5wcm90b2J1ZiIiCg1Tb3VyY2VDb250ZXh0EhEKCWZpbGVfbmFtZRgBIAEo", "ZS5wcm90b2J1ZiIiCg1Tb3VyY2VDb250ZXh0EhEKCWZpbGVfbmFtZRgBIAEo",
"CUJSChNjb20uZ29vZ2xlLnByb3RvYnVmQhJTb3VyY2VDb250ZXh0UHJvdG9Q", "CUJVChNjb20uZ29vZ2xlLnByb3RvYnVmQhJTb3VyY2VDb250ZXh0UHJvdG9Q",
"AaICA0dQQqoCHkdvb2dsZS5Qcm90b2J1Zi5XZWxsS25vd25UeXBlc2IGcHJv", "AaABAaICA0dQQqoCHkdvb2dsZS5Qcm90b2J1Zi5XZWxsS25vd25UeXBlc2IG",
"dG8z")); "cHJvdG8z"));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData, descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] { }, new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] { new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
......
...@@ -27,9 +27,9 @@ namespace Google.Protobuf.WellKnownTypes { ...@@ -27,9 +27,9 @@ namespace Google.Protobuf.WellKnownTypes {
"KAMiHAoLVUludDY0VmFsdWUSDQoFdmFsdWUYASABKAQiGwoKSW50MzJWYWx1", "KAMiHAoLVUludDY0VmFsdWUSDQoFdmFsdWUYASABKAQiGwoKSW50MzJWYWx1",
"ZRINCgV2YWx1ZRgBIAEoBSIcCgtVSW50MzJWYWx1ZRINCgV2YWx1ZRgBIAEo", "ZRINCgV2YWx1ZRgBIAEoBSIcCgtVSW50MzJWYWx1ZRINCgV2YWx1ZRgBIAEo",
"DSIaCglCb29sVmFsdWUSDQoFdmFsdWUYASABKAgiHAoLU3RyaW5nVmFsdWUS", "DSIaCglCb29sVmFsdWUSDQoFdmFsdWUYASABKAgiHAoLU3RyaW5nVmFsdWUS",
"DQoFdmFsdWUYASABKAkiGwoKQnl0ZXNWYWx1ZRINCgV2YWx1ZRgBIAEoDEJN", "DQoFdmFsdWUYASABKAkiGwoKQnl0ZXNWYWx1ZRINCgV2YWx1ZRgBIAEoDEJQ",
"ChNjb20uZ29vZ2xlLnByb3RvYnVmQg1XcmFwcGVyc1Byb3RvUAGiAgNHUEKq", "ChNjb20uZ29vZ2xlLnByb3RvYnVmQg1XcmFwcGVyc1Byb3RvUAGgAQGiAgNH",
"Ah5Hb29nbGUuUHJvdG9idWYuV2VsbEtub3duVHlwZXNiBnByb3RvMw==")); "UEKqAh5Hb29nbGUuUHJvdG9idWYuV2VsbEtub3duVHlwZXNiBnByb3RvMw=="));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData, descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] { }, new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] { new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
......
...@@ -36,10 +36,12 @@ ...@@ -36,10 +36,12 @@
#include <google/protobuf/descriptor.pb.h> #include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/printer.h> #include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/compiler/csharp/csharp_generator.h> #include <google/protobuf/compiler/csharp/csharp_generator.h>
#include <google/protobuf/compiler/csharp/csharp_umbrella_class.h>
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_names.h>
#include <google/protobuf/compiler/csharp/csharp_umbrella_class.h>
using google::protobuf::internal::scoped_ptr; using google::protobuf::internal::scoped_ptr;
...@@ -48,9 +50,39 @@ namespace protobuf { ...@@ -48,9 +50,39 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
std::string GetOutputFile(const google::protobuf::FileDescriptor* file, const std::string file_extension) std::string GetOutputFile(
{ const google::protobuf::FileDescriptor* file,
return GetUmbrellaClassUnqualifiedName(file) + file_extension; const std::string file_extension,
const bool generate_directories,
const std::string base_namespace,
string* error) {
string relative_filename = GetUmbrellaClassUnqualifiedName(file) + file_extension;
if (!generate_directories) {
return relative_filename;
}
string ns = GetFileNamespace(file);
string namespace_suffix = ns;
if (!base_namespace.empty()) {
// Check that the base_namespace is either equal to or a leading part of
// the file namespace. This isn't just a simple prefix; "Foo.B" shouldn't
// be regarded as a prefix of "Foo.Bar". The simplest option is to add "."
// to both.
string extended_ns = ns + ".";
if (extended_ns.find(base_namespace + ".") != 0) {
*error = "Namespace " + ns + " is not a prefix namespace of base namespace " + base_namespace;
return ""; // This will be ignored, because we've set an error.
}
namespace_suffix = ns.substr(base_namespace.length());
if (namespace_suffix.find(".") == 0) {
namespace_suffix = namespace_suffix.substr(1);
}
}
string namespace_dir = StringReplace(namespace_suffix, ".", "/", true);
if (!namespace_dir.empty()) {
namespace_dir += "/";
}
return namespace_dir + relative_filename;
} }
void GenerateFile(const google::protobuf::FileDescriptor* file, void GenerateFile(const google::protobuf::FileDescriptor* file,
...@@ -75,16 +107,26 @@ bool Generator::Generate( ...@@ -75,16 +107,26 @@ bool Generator::Generate(
} }
std::string file_extension = ".cs"; std::string file_extension = ".cs";
std::string base_namespace = "";
bool generate_directories = false;
for (int i = 0; i < options.size(); i++) { for (int i = 0; i < options.size(); i++) {
if (options[i].first == "file_extension") { if (options[i].first == "file_extension") {
file_extension = options[i].second; file_extension = options[i].second;
} else if (options[i].first == "base_namespace") {
base_namespace = options[i].second;
generate_directories = true;
} else { } else {
*error = "Unknown generator option: " + options[i].first; *error = "Unknown generator option: " + options[i].first;
return false; return false;
} }
} }
std::string filename = GetOutputFile(file, file_extension); string filename_error = "";
std::string filename = GetOutputFile(file, file_extension, generate_directories, base_namespace, &filename_error);
if (!filename_error.empty()) {
*error = filename_error;
return false;
}
scoped_ptr<io::ZeroCopyOutputStream> output( scoped_ptr<io::ZeroCopyOutputStream> output(
generator_context->Open(filename)); generator_context->Open(filename));
io::Printer printer(output.get(), '$'); io::Printer printer(output.get(), '$');
......
...@@ -101,8 +101,6 @@ std::string StringToBase64(const std::string& input); ...@@ -101,8 +101,6 @@ std::string StringToBase64(const std::string& input);
std::string FileDescriptorToBase64(const FileDescriptor* descriptor); std::string FileDescriptorToBase64(const FileDescriptor* descriptor);
uint FixedMakeTag(const FieldDescriptor* descriptor);
FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
// Determines whether the given message is a map entry message, i.e. one implicitly created // Determines whether the given message is a map entry message, i.e. one implicitly created
......
...@@ -166,7 +166,7 @@ void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) { ...@@ -166,7 +166,7 @@ void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) {
// TODO(jonskeet): Consider a C#-escaping format here instead of just Base64. // TODO(jonskeet): Consider a C#-escaping format here instead of just Base64.
std::string base64 = FileDescriptorToBase64(file_); std::string base64 = FileDescriptorToBase64(file_);
while (base64.size() > 60) { while (base64.size() > 60) {
printer->Print("\"$base64$\", \n", "base64", base64.substr(0, 60)); printer->Print("\"$base64$\",\n", "base64", base64.substr(0, 60));
base64 = base64.substr(60); base64 = base64.substr(60);
} }
printer->Print("\"$base64$\"));\n", "base64", base64); printer->Print("\"$base64$\"));\n", "base64", base64);
......
...@@ -72,7 +72,7 @@ int main(int argc, char* argv[]) { ...@@ -72,7 +72,7 @@ int main(int argc, char* argv[]) {
// CSharp // CSharp
google::protobuf::compiler::csharp::Generator csharp_generator; google::protobuf::compiler::csharp::Generator csharp_generator;
cli.RegisterGenerator("--csharp_out", &csharp_generator, cli.RegisterGenerator("--csharp_out", "--csharp_opt", &csharp_generator,
"Generate C# source file."); "Generate C# source file.");
// Objective C // Objective C
......
...@@ -755,9 +755,9 @@ void protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto() { ...@@ -755,9 +755,9 @@ void protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto() {
"tion\032\206\001\n\010Location\022\020\n\004path\030\001 \003(\005B\002\020\001\022\020\n\004s" "tion\032\206\001\n\010Location\022\020\n\004path\030\001 \003(\005B\002\020\001\022\020\n\004s"
"pan\030\002 \003(\005B\002\020\001\022\030\n\020leading_comments\030\003 \001(\t\022" "pan\030\002 \003(\005B\002\020\001\022\030\n\020leading_comments\030\003 \001(\t\022"
"\031\n\021trailing_comments\030\004 \001(\t\022!\n\031leading_de" "\031\n\021trailing_comments\030\004 \001(\t\022!\n\031leading_de"
"tached_comments\030\006 \003(\tB;\n\023com.google.prot" "tached_comments\030\006 \003(\tBX\n\023com.google.prot"
"obufB\020DescriptorProtosH\001Z\ndescriptor\242\002\003G" "obufB\020DescriptorProtosH\001Z\ndescriptor\242\002\003G"
"PB", 4962); "PB\252\002\032Google.Protobuf.Reflection", 4991);
::google::protobuf::MessageFactory::InternalRegisterGeneratedFile( ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile(
"google/protobuf/descriptor.proto", &protobuf_RegisterTypes); "google/protobuf/descriptor.proto", &protobuf_RegisterTypes);
FileDescriptorSet::default_instance_ = new FileDescriptorSet(); FileDescriptorSet::default_instance_ = new FileDescriptorSet();
......
...@@ -43,8 +43,7 @@ package google.protobuf; ...@@ -43,8 +43,7 @@ package google.protobuf;
option go_package = "descriptor"; option go_package = "descriptor";
option java_package = "com.google.protobuf"; option java_package = "com.google.protobuf";
option java_outer_classname = "DescriptorProtos"; option java_outer_classname = "DescriptorProtos";
// Re-enable this once the tools have picked up the csharp_namespace option. option csharp_namespace = "Google.Protobuf.Reflection";
// option csharp_namespace = "Google.ProtocolBuffers.DescriptorProtos";
option objc_class_prefix = "GPB"; option objc_class_prefix = "GPB";
// descriptor.proto must be optimized for speed because reflection-based // descriptor.proto must be optimized for speed because reflection-based
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment