Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
57599ef1
Commit
57599ef1
authored
Nov 03, 2010
by
Jon Skeet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
A few stylistic issues
parent
c58ce5dc
Show whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
241 additions
and
288 deletions
+241
-288
ProtocolBuffers.build
ProtocolBuffers.build
+5
-5
csharp_options.proto
protos/google/protobuf/csharp_options.proto
+0
-1
DependencyResolutionTest.cs
src/ProtoGen.Test/DependencyResolutionTest.cs
+6
-6
TempFile.cs
src/ProtoGen.Test/TempFile.cs
+21
-15
TestPreprocessing.cs
src/ProtoGen.Test/TestPreprocessing.cs
+41
-31
DescriptorUtil.cs
src/ProtoGen/DescriptorUtil.cs
+8
-7
Generator.cs
src/ProtoGen/Generator.cs
+12
-23
GeneratorOptions.cs
src/ProtoGen/GeneratorOptions.cs
+40
-58
Program.cs
src/ProtoGen/Program.cs
+6
-17
ProgramPreprocess.cs
src/ProtoGen/ProgramPreprocess.cs
+51
-48
ServiceGenerator.cs
src/ProtoGen/ServiceGenerator.cs
+1
-1
UmbrellaClassGenerator.cs
src/ProtoGen/UmbrellaClassGenerator.cs
+2
-2
DynamicMessageTest.cs
src/ProtocolBuffers.Test/DynamicMessageTest.cs
+0
-0
FileDescriptor.cs
src/ProtocolBuffers/Descriptors/FileDescriptor.cs
+48
-31
DynamicMessage.cs
src/ProtocolBuffers/DynamicMessage.cs
+0
-0
UnknownField.cs
src/ProtocolBuffers/UnknownField.cs
+0
-0
todo.txt
todo.txt
+0
-43
No files found.
ProtocolBuffers.build
View file @
57599ef1
...
...
@@ -266,10 +266,10 @@
<property
name=
"Configuration"
value=
"Release"
/>
<property
name=
"Platform"
value=
"Any CPU"
/>
</msbuild>
<
!--
msbuild project="${src}/ProtocolBuffers.sln">
<msbuild
project=
"${src}/ProtocolBuffers.sln"
>
<property
name=
"Configuration"
value=
"Silverlight2"
/>
<property
name=
"Platform"
value=
"Any CPU"
/>
</msbuild
--
>
</msbuild>
<!-- Note the deliberate lack of space in the platform name -->
<msbuild
project=
"${src}/ProtocolBuffers/ProtocolBuffersCF.csproj"
>
<property
name=
"Configuration"
value=
"Release"
/>
...
...
@@ -291,7 +291,7 @@
<mkdir
dir=
"${output-dir}/Protoc"
/>
<mkdir
dir=
"${output-dir}/Debug"
/>
<mkdir
dir=
"${output-dir}/Release"
/>
<
!--mkdir dir="${output-dir}/Silverlight2" /--
>
<
mkdir
dir=
"${output-dir}/Silverlight2"
/
>
<mkdir
dir=
"${output-dir}/CompactFramework35"
/>
<copy
todir=
"${output-dir}/Protoc"
>
<fileset
basedir=
"${project::get-base-directory()}/lib"
>
...
...
@@ -321,12 +321,12 @@
<exclude
name=
"**/*vshost*"
/>
</fileset>
</copy>
<
!--
copy todir="${output-dir}/Silverlight2"
<copy
todir=
"${output-dir}/Silverlight2"
flatten=
"true"
>
<fileset
basedir=
"${src}"
>
<include
name=
"ProtocolBuffers/bin/Silverlight2/Google.ProtocolBuffers.dll"
/>
</fileset>
</copy
-->
</copy
>
<copy
todir=
"${output-dir}/CompactFramework35"
flatten=
"true"
>
<fileset
basedir=
"${src}"
>
...
...
protos/google/protobuf/csharp_options.proto
View file @
57599ef1
...
...
@@ -39,7 +39,6 @@ message CSharpFileOptions {
// Generate attributes indicating non-CLS-compliance
optional
bool
cls_compliance
=
8
[
default
=
true
];
// ROK - 2010-09-03 additions to csoptions ...
// The extension that should be appended to the umbrella_classname when creating files.
optional
string
file_extension
=
221
[
default
=
".cs"
];
...
...
src/ProtoGen.Test/DependencyResolutionTest.cs
View file @
57599ef1
...
...
@@ -50,7 +50,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
FileDescriptorProto
second
=
new
FileDescriptorProto
.
Builder
{
Name
=
"Second"
}.
Build
();
FileDescriptorSet
set
=
new
FileDescriptorSet
{
FileList
=
{
first
,
second
}
};
IList
<
FileDescriptor
>
converted
=
Generator
.
ConvertDescriptors
(
set
);
IList
<
FileDescriptor
>
converted
=
Generator
.
ConvertDescriptors
(
CSharpFileOptions
.
DefaultInstance
,
set
);
Assert
.
AreEqual
(
2
,
converted
.
Count
);
Assert
.
AreEqual
(
"First"
,
converted
[
0
].
Name
);
Assert
.
AreEqual
(
0
,
converted
[
0
].
Dependencies
.
Count
);
...
...
@@ -63,7 +63,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
FileDescriptorProto
first
=
new
FileDescriptorProto
.
Builder
{
Name
=
"First"
,
DependencyList
=
{
"Second"
}
}.
Build
();
FileDescriptorProto
second
=
new
FileDescriptorProto
.
Builder
{
Name
=
"Second"
}.
Build
();
FileDescriptorSet
set
=
new
FileDescriptorSet
{
FileList
=
{
first
,
second
}
};
IList
<
FileDescriptor
>
converted
=
Generator
.
ConvertDescriptors
(
set
);
IList
<
FileDescriptor
>
converted
=
Generator
.
ConvertDescriptors
(
CSharpFileOptions
.
DefaultInstance
,
set
);
Assert
.
AreEqual
(
2
,
converted
.
Count
);
Assert
.
AreEqual
(
"First"
,
converted
[
0
].
Name
);
Assert
.
AreEqual
(
1
,
converted
[
0
].
Dependencies
.
Count
);
...
...
@@ -77,7 +77,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
FileDescriptorProto
first
=
new
FileDescriptorProto
.
Builder
{
Name
=
"First"
}.
Build
();
FileDescriptorProto
second
=
new
FileDescriptorProto
.
Builder
{
Name
=
"Second"
,
DependencyList
=
{
"First"
}
}.
Build
();
FileDescriptorSet
set
=
new
FileDescriptorSet
{
FileList
=
{
first
,
second
}
};
IList
<
FileDescriptor
>
converted
=
Generator
.
ConvertDescriptors
(
set
);
IList
<
FileDescriptor
>
converted
=
Generator
.
ConvertDescriptors
(
CSharpFileOptions
.
DefaultInstance
,
set
);
Assert
.
AreEqual
(
2
,
converted
.
Count
);
Assert
.
AreEqual
(
"First"
,
converted
[
0
].
Name
);
Assert
.
AreEqual
(
0
,
converted
[
0
].
Dependencies
.
Count
);
...
...
@@ -92,7 +92,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
FileDescriptorProto
second
=
new
FileDescriptorProto
.
Builder
{
Name
=
"Second"
,
DependencyList
=
{
"First"
}
}.
Build
();
FileDescriptorSet
set
=
new
FileDescriptorSet
{
FileList
=
{
first
,
second
}
};
try
{
Generator
.
ConvertDescriptors
(
set
);
Generator
.
ConvertDescriptors
(
CSharpFileOptions
.
DefaultInstance
,
set
);
Assert
.
Fail
(
"Expected exception"
);
}
catch
(
DependencyResolutionException
)
{
// Expected
...
...
@@ -104,7 +104,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
FileDescriptorProto
first
=
new
FileDescriptorProto
.
Builder
{
Name
=
"First"
,
DependencyList
=
{
"Second"
}
}.
Build
();
FileDescriptorSet
set
=
new
FileDescriptorSet
{
FileList
=
{
first
}
};
try
{
Generator
.
ConvertDescriptors
(
set
);
Generator
.
ConvertDescriptors
(
CSharpFileOptions
.
DefaultInstance
,
set
);
Assert
.
Fail
(
"Expected exception"
);
}
catch
(
DependencyResolutionException
)
{
// Expected
...
...
@@ -116,7 +116,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
FileDescriptorProto
first
=
new
FileDescriptorProto
.
Builder
{
Name
=
"First"
,
DependencyList
=
{
"First"
}
}.
Build
();
FileDescriptorSet
set
=
new
FileDescriptorSet
{
FileList
=
{
first
}
};
try
{
Generator
.
ConvertDescriptors
(
set
);
Generator
.
ConvertDescriptors
(
CSharpFileOptions
.
DefaultInstance
,
set
);
Assert
.
Fail
(
"Expected exception"
);
}
catch
(
DependencyResolutionException
)
{
// Expected
...
...
src/ProtoGen.Test/TempFile.cs
View file @
57599ef1
...
...
@@ -7,42 +7,48 @@ namespace Google.ProtocolBuffers.ProtoGen
{
class
ProtoFile
:
TempFile
{
public
ProtoFile
(
string
filename
,
string
contents
)
public
ProtoFile
(
string
filename
,
string
contents
)
:
base
(
filename
,
contents
)
{
_tempFile
=
filename
;
File
.
WriteAllText
(
_tempFile
,
contents
);
}
}
class
TempFile
:
IDisposable
{
protected
string
_
tempFile
;
private
string
tempFile
;
public
static
TempFile
Attach
(
string
path
)
{
TempFile
f
=
new
TempFile
();
f
.
_tempFile
=
path
;
return
f
;
return
new
TempFile
(
path
,
null
);
}
protected
TempFile
(
string
filename
,
string
contents
)
{
tempFile
=
filename
;
if
(
contents
!=
null
)
{
File
.
WriteAllText
(
tempFile
,
contents
,
new
UTF8Encoding
(
false
));
}
}
protected
TempFile
()
{
}
public
TempFile
(
string
contents
)
:
this
(
Path
.
GetTempFileName
(),
contents
)
{
File
.
WriteAllText
(
_tempFile
=
Path
.
GetTempFileName
(),
contents
,
Encoding
.
ASCII
);
}
public
string
TempPath
{
get
{
return
_
tempFile
;
}
}
public
string
TempPath
{
get
{
return
tempFile
;
}
}
public
void
ChangeExtension
(
string
ext
)
{
string
newFile
=
Path
.
ChangeExtension
(
_
tempFile
,
ext
);
File
.
Move
(
_
tempFile
,
newFile
);
_
tempFile
=
newFile
;
string
newFile
=
Path
.
ChangeExtension
(
tempFile
,
ext
);
File
.
Move
(
tempFile
,
newFile
);
tempFile
=
newFile
;
}
public
void
Dispose
()
{
if
(
File
.
Exists
(
_tempFile
))
File
.
Delete
(
_tempFile
);
if
(
File
.
Exists
(
tempFile
))
{
File
.
Delete
(
tempFile
);
}
}
}
}
src/ProtoGen.Test/TestPreprocessing.cs
View file @
57599ef1
...
...
@@ -44,8 +44,8 @@ namespace Google.ProtocolBuffers.ProtoGen
[
Category
(
"Preprocessor"
)]
public
partial
class
TestPreprocessing
{
private
static
readonly
string
TEMP
=
Path
.
Combine
(
Path
.
GetTempPath
(),
"proto-gen-test"
);
private
const
string
DEFAULT_PROTO
=
@"
private
static
readonly
string
TempPath
=
Path
.
Combine
(
Path
.
GetTempPath
(),
"proto-gen-test"
);
private
const
string
DefaultProto
=
@"
package nunit.simple;
// Test a very simple message.
message MyMessage {
...
...
@@ -53,21 +53,23 @@ message MyMessage {
}"
;
#
region
TestFixture
SetUp
/
TearDown
private
string
OriginalWorkingDirectory
=
Environment
.
CurrentDirectory
;
private
static
readonly
string
OriginalWorkingDirectory
=
Environment
.
CurrentDirectory
;
[
TestFixtureSetUp
]
public
virtual
void
Setup
()
{
Teardown
();
Directory
.
CreateDirectory
(
TEMP
);
Environment
.
CurrentDirectory
=
TEMP
;
Directory
.
CreateDirectory
(
TempPath
);
Environment
.
CurrentDirectory
=
TempPath
;
}
[
TestFixtureTearDown
]
public
virtual
void
Teardown
()
{
Environment
.
CurrentDirectory
=
OriginalWorkingDirectory
;
if
(
Directory
.
Exists
(
TEMP
))
Directory
.
Delete
(
TEMP
,
true
);
if
(
Directory
.
Exists
(
TempPath
))
{
Directory
.
Delete
(
TempPath
,
true
);
}
}
#
endregion
#
region
Helper
Methods
RunProtoGen
/
RunCsc
...
...
@@ -121,8 +123,10 @@ message MyMessage {
asm
=
Assembly
.
Load
(
allbytes
);
foreach
(
Type
t
in
asm
.
GetTypes
())
{
Debug
.
WriteLine
(
t
.
FullName
,
asm
.
FullName
);
}
}
return
asm
;
}
}
...
...
@@ -138,7 +142,7 @@ message MyMessage {
string
test
=
new
StackFrame
(
false
).
GetMethod
().
Name
;
Setup
();
using
(
TempFile
source
=
TempFile
.
Attach
(
test
+
".cs"
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DEFAULT_PROTO
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DefaultProto
))
{
RunProtoGen
(
0
,
proto
.
TempPath
);
Assembly
a
=
RunCsc
(
0
,
source
.
TempPath
);
...
...
@@ -177,7 +181,7 @@ message " + test + @" {
string
test
=
new
StackFrame
(
false
).
GetMethod
().
Name
;
Setup
();
using
(
TempFile
source
=
TempFile
.
Attach
(
test
+
".cs"
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DEFAULT_PROTO
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DefaultProto
))
{
RunProtoGen
(
0
,
proto
.
TempPath
,
"-namespace:MyNewNamespace"
);
Assembly
a
=
RunCsc
(
0
,
source
.
TempPath
);
...
...
@@ -194,7 +198,7 @@ message " + test + @" {
string
test
=
new
StackFrame
(
false
).
GetMethod
().
Name
;
Setup
();
using
(
TempFile
source
=
TempFile
.
Attach
(
"MyUmbrellaClassname.cs"
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DEFAULT_PROTO
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DefaultProto
))
{
RunProtoGen
(
0
,
proto
.
TempPath
,
"/umbrella_classname=MyUmbrellaClassname"
);
Assembly
a
=
RunCsc
(
0
,
source
.
TempPath
);
...
...
@@ -211,7 +215,7 @@ message " + test + @" {
string
test
=
new
StackFrame
(
false
).
GetMethod
().
Name
;
Setup
();
using
(
TempFile
source
=
TempFile
.
Attach
(
test
+
".cs"
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DEFAULT_PROTO
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DefaultProto
))
{
RunProtoGen
(
0
,
proto
.
TempPath
,
"-nest_classes:true"
);
Assembly
a
=
RunCsc
(
0
,
source
.
TempPath
);
...
...
@@ -228,7 +232,7 @@ message " + test + @" {
string
test
=
new
StackFrame
(
false
).
GetMethod
().
Name
;
Setup
();
using
(
TempFile
source
=
TempFile
.
Attach
(
@"nunit\simple\"
+
test
+
".cs"
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DEFAULT_PROTO
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DefaultProto
))
{
RunProtoGen
(
0
,
proto
.
TempPath
,
"-expand_namespace_directories:true"
);
Assembly
a
=
RunCsc
(
0
,
source
.
TempPath
);
...
...
@@ -271,7 +275,7 @@ message MyMessage {
string
test
=
new
StackFrame
(
false
).
GetMethod
().
Name
;
Setup
();
using
(
TempFile
source
=
TempFile
.
Attach
(
test
+
".Generated.cs"
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DEFAULT_PROTO
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DefaultProto
))
{
RunProtoGen
(
0
,
proto
.
TempPath
,
"-file_extension:.Generated.cs"
);
Assembly
a
=
RunCsc
(
0
,
source
.
TempPath
);
...
...
@@ -288,7 +292,7 @@ message MyMessage {
string
test
=
new
StackFrame
(
false
).
GetMethod
().
Name
;
Setup
();
using
(
TempFile
source
=
TempFile
.
Attach
(
test
+
".cs"
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DEFAULT_PROTO
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DefaultProto
))
{
RunProtoGen
(
0
,
proto
.
TempPath
,
"-umbrella_namespace:MyUmbrella.Namespace"
);
Assembly
a
=
RunCsc
(
0
,
source
.
TempPath
);
...
...
@@ -305,7 +309,7 @@ message MyMessage {
string
test
=
new
StackFrame
(
false
).
GetMethod
().
Name
;
Setup
();
using
(
TempFile
source
=
TempFile
.
Attach
(
test
+
".cs"
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DEFAULT_PROTO
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DefaultProto
))
{
RunProtoGen
(
0
,
proto
.
TempPath
,
"-nest_classes:true"
,
"-umbrella_namespace:MyUmbrella.Namespace"
);
Assembly
a
=
RunCsc
(
0
,
source
.
TempPath
);
...
...
@@ -341,7 +345,7 @@ message " + test + @" {
string
test
=
new
StackFrame
(
false
).
GetMethod
().
Name
;
Setup
();
using
(
TempFile
source
=
TempFile
.
Attach
(
@"generated-code\"
+
test
+
".cs"
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DEFAULT_PROTO
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DefaultProto
))
{
RunProtoGen
(
1
,
proto
.
TempPath
,
"-output_directory:generated-code"
);
Directory
.
CreateDirectory
(
"generated-code"
);
...
...
@@ -363,16 +367,18 @@ message " + test + @" {
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
@"
import ""google/protobuf/csharp_options.proto"";
option (google.protobuf.csharp_file_options).namespace = ""MyNewNamespace"";
"
+
D
EFAULT_PROTO
))
"
+
D
efaultProto
))
{
string
google
=
Path
.
Combine
(
TEMP
,
"google\\protobuf"
);
string
google
=
Path
.
Combine
(
TempPath
,
"google\\protobuf"
);
Directory
.
CreateDirectory
(
google
);
foreach
(
string
file
in
Directory
.
GetFiles
(
Path
.
Combine
(
OriginalWorkingDirectory
,
"google\\protobuf"
)))
{
File
.
Copy
(
file
,
Path
.
Combine
(
google
,
Path
.
GetFileName
(
file
)));
}
Assert
.
AreEqual
(
0
,
Directory
.
GetFiles
(
TEMP
,
"*.cs"
).
Length
);
Assert
.
AreEqual
(
0
,
Directory
.
GetFiles
(
TempPath
,
"*.cs"
).
Length
);
RunProtoGen
(
0
,
proto
.
TempPath
,
"-ignore_google_protobuf:true"
);
Assert
.
AreEqual
(
1
,
Directory
.
GetFiles
(
TEMP
,
"*.cs"
).
Length
);
Assert
.
AreEqual
(
1
,
Directory
.
GetFiles
(
TempPath
,
"*.cs"
).
Length
);
Assembly
a
=
RunCsc
(
0
,
source
.
TempPath
);
//assert that the message type is in the expected namespace
...
...
@@ -391,14 +397,16 @@ option (google.protobuf.csharp_file_options).namespace = ""MyNewNamespace"";
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
@"
import ""google/protobuf/csharp_options.proto"";
option (google.protobuf.csharp_file_options).namespace = ""MyNewNamespace"";
"
+
D
EFAULT_PROTO
))
"
+
D
efaultProto
))
{
string
google
=
Path
.
Combine
(
TEMP
,
"google\\protobuf"
);
string
google
=
Path
.
Combine
(
TempPath
,
"google\\protobuf"
);
Directory
.
CreateDirectory
(
google
);
foreach
(
string
file
in
Directory
.
GetFiles
(
Path
.
Combine
(
OriginalWorkingDirectory
,
"google\\protobuf"
)))
{
File
.
Copy
(
file
,
Path
.
Combine
(
google
,
Path
.
GetFileName
(
file
)));
}
Assert
.
AreEqual
(
0
,
Directory
.
GetFiles
(
TEMP
,
"*.cs"
).
Length
);
Assert
.
AreEqual
(
0
,
Directory
.
GetFiles
(
TempPath
,
"*.cs"
).
Length
);
//Without the option this fails due to being unable to resolve google/protobuf descriptors
RunProtoGen
(
1
,
proto
.
TempPath
,
"-ignore_google_protobuf:false"
);
}
...
...
@@ -424,15 +432,17 @@ message MyMessage {
optional string name = 1;
} "
))
{
string
google
=
Path
.
Combine
(
TEMP
,
"google\\protobuf"
);
string
google
=
Path
.
Combine
(
TempPath
,
"google\\protobuf"
);
Directory
.
CreateDirectory
(
google
);
foreach
(
string
file
in
Directory
.
GetFiles
(
Path
.
Combine
(
OriginalWorkingDirectory
,
"google\\protobuf"
)))
{
File
.
Copy
(
file
,
Path
.
Combine
(
google
,
Path
.
GetFileName
(
file
)));
}
Assert
.
AreEqual
(
0
,
Directory
.
GetFiles
(
TEMP
,
"*.cs"
).
Length
);
Assert
.
AreEqual
(
0
,
Directory
.
GetFiles
(
TempPath
,
"*.cs"
).
Length
);
//if you specify the protoc option --include_imports this should build three source files
RunProtoGen
(
0
,
proto
.
TempPath
,
"--include_imports"
);
Assert
.
AreEqual
(
3
,
Directory
.
GetFiles
(
TEMP
,
"*.cs"
).
Length
);
Assert
.
AreEqual
(
3
,
Directory
.
GetFiles
(
TempPath
,
"*.cs"
).
Length
);
//you can (and should) simply omit the inclusion of the extra source files in your project
Assembly
a
=
RunCsc
(
0
,
source
.
TempPath
);
...
...
@@ -459,15 +469,15 @@ message MyMessage {
optional string name = 1;
} "
))
{
string
google
=
Path
.
Combine
(
TEMP
,
"google\\protobuf"
);
string
google
=
Path
.
Combine
(
TempPath
,
"google\\protobuf"
);
Directory
.
CreateDirectory
(
google
);
foreach
(
string
file
in
Directory
.
GetFiles
(
Path
.
Combine
(
OriginalWorkingDirectory
,
"google\\protobuf"
)))
File
.
Copy
(
file
,
Path
.
Combine
(
google
,
Path
.
GetFileName
(
file
)));
Assert
.
AreEqual
(
0
,
Directory
.
GetFiles
(
TEMP
,
"*.cs"
).
Length
);
Assert
.
AreEqual
(
0
,
Directory
.
GetFiles
(
TempPath
,
"*.cs"
).
Length
);
//Even with --include_imports, if you provide -ignore_google_protobuf:true you only get the one source file
RunProtoGen
(
0
,
proto
.
TempPath
,
"-ignore_google_protobuf:true"
,
"--include_imports"
);
Assert
.
AreEqual
(
1
,
Directory
.
GetFiles
(
TEMP
,
"*.cs"
).
Length
);
Assert
.
AreEqual
(
1
,
Directory
.
GetFiles
(
TempPath
,
"*.cs"
).
Length
);
//you can (and should) simply omit the inclusion of the extra source files in your project
Assembly
a
=
RunCsc
(
0
,
source
.
TempPath
);
...
...
@@ -509,10 +519,10 @@ message MyMessage {
string
test
=
new
StackFrame
(
false
).
GetMethod
().
Name
;
Setup
();
using
(
TempFile
source
=
TempFile
.
Attach
(
test
+
".cs"
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DEFAULT_PROTO
))
using
(
ProtoFile
proto
=
new
ProtoFile
(
test
+
".proto"
,
DefaultProto
))
{
Environment
.
CurrentDirectory
=
OriginalWorkingDirectory
;
RunProtoGen
(
0
,
proto
.
TempPath
,
"--proto_path="
+
TEMP
);
RunProtoGen
(
0
,
proto
.
TempPath
,
"--proto_path="
+
TempPath
);
Assembly
a
=
RunCsc
(
0
,
source
.
TempPath
);
//assert that the message type is in the expected namespace
Type
t
=
a
.
GetType
(
"nunit.simple.MyMessage"
,
true
,
true
);
...
...
src/ProtoGen/DescriptorUtil.cs
View file @
57599ef1
...
...
@@ -45,22 +45,23 @@ namespace Google.ProtocolBuffers.ProtoGen {
internal
static
string
GetFullUmbrellaClassName
(
IDescriptor
descriptor
)
{
CSharpFileOptions
options
=
descriptor
.
File
.
CSharpOptions
;
string
result
=
options
.
Namespace
;
if
(
result
!=
""
)
result
+=
'.'
;
result
+=
QualifiedUmbrellaClassName
(
options
);
if
(
result
!=
""
)
{
result
+=
'.'
;
}
result
+=
GetQualifiedUmbrellaClassName
(
options
);
return
"global::"
+
result
;
}
/// <summary>
/// ROK 2010-09-03
/// Evaluates the options and returns the qualified name of the umbrella class
/// relative to the descriptor type's namespace. Basically con
tact
s the
/// relative to the descriptor type's namespace. Basically con
catenate
s the
/// UmbrellaNamespace + UmbrellaClassname fields.
/// </summary>
internal
static
string
QualifiedUmbrellaClassName
(
CSharpFileOptions
options
)
{
internal
static
string
GetQualifiedUmbrellaClassName
(
CSharpFileOptions
options
)
{
string
fullName
=
options
.
UmbrellaClassname
;
if
(!
options
.
NestClasses
&&
options
.
UmbrellaNamespace
!=
""
)
if
(!
options
.
NestClasses
&&
options
.
UmbrellaNamespace
!=
""
)
{
fullName
=
String
.
Format
(
"{0}.{1}"
,
options
.
UmbrellaNamespace
,
options
.
UmbrellaClassname
);
}
return
fullName
;
}
...
...
src/ProtoGen/Generator.cs
View file @
57599ef1
...
...
@@ -73,17 +73,17 @@ namespace Google.ProtocolBuffers.ProtoGen {
IList
<
FileDescriptor
>
descriptors
=
ConvertDescriptors
(
options
.
FileOptions
,
descriptorProtos
.
ToArray
());
//ROK Combine with Options...
foreach
(
FileDescriptor
descriptor
in
descriptors
)
// Combine with options from command line
foreach
(
FileDescriptor
descriptor
in
descriptors
)
{
descriptor
.
ConfigureWithDefaultOptions
(
options
.
FileOptions
);
}
foreach
(
FileDescriptor
descriptor
in
descriptors
)
{
//ROK 2010-09-03 Ignore google protobuf package
if
(
descriptor
.
CSharpOptions
.
IgnoreGoogleProtobuf
&&
descriptor
.
Package
==
"google.protobuf"
)
// Optionally exclude descriptors in google.protobuf
if
(
descriptor
.
CSharpOptions
.
IgnoreGoogleProtobuf
&&
descriptor
.
Package
==
"google.protobuf"
)
{
continue
;
}
Generate
(
descriptor
);
}
}
...
...
@@ -102,12 +102,8 @@ namespace Google.ProtocolBuffers.ProtoGen {
private
string
GetOutputFile
(
FileDescriptor
descriptor
)
{
CSharpFileOptions
fileOptions
=
descriptor
.
CSharpOptions
;
//ROK 2010-09-03 - added the ability to sepcify the extension used within the options
//string filename = descriptor.CSharpOptions.UmbrellaClassname + ".cs";
string
filename
=
descriptor
.
CSharpOptions
.
UmbrellaClassname
+
descriptor
.
CSharpOptions
.
FileExtension
;
//ROK 2010-09-03 - output directory can be specific to a descriptor file
//string outputDirectory = options.OutputDirectory;
string
outputDirectory
=
descriptor
.
CSharpOptions
.
OutputDirectory
;
if
(
fileOptions
.
ExpandNamespaceDirectories
)
{
string
package
=
fileOptions
.
Namespace
;
...
...
@@ -118,23 +114,19 @@ namespace Google.ProtocolBuffers.ProtoGen {
}
}
}
//ROK 2010-09-03 - Always force output directory exists since they can specify this in .proto options
// As the directory can be explicitly specified in options, we need to make sure it exists
Directory
.
CreateDirectory
(
outputDirectory
);
return
Path
.
Combine
(
outputDirectory
,
filename
);
}
// ROK 2010-09-03 - used by unit tests, we will continue to allow them to function as-is.
internal
static
IList
<
FileDescriptor
>
ConvertDescriptors
(
FileDescriptorSet
descriptorProtos
)
{
return
ConvertDescriptors
(
CSharpFileOptions
.
DefaultInstance
,
descriptorProtos
);
}
/// <summary>
/// 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.
/// </summary>
/// <exception cref="DependencyResolutionException">Not all dependencies could be resolved.</exception>
private
static
IList
<
FileDescriptor
>
ConvertDescriptors
(
CSharpFileOptions
options
,
params
FileDescriptorSet
[]
descriptorProtos
)
{
internal
static
IList
<
FileDescriptor
>
ConvertDescriptors
(
CSharpFileOptions
options
,
params
FileDescriptorSet
[]
descriptorProtos
)
{
// Simple strategy: Keep going through the list of protos to convert, only doing ones where
// we've already converted all the dependencies, until we get to a stalemate
List
<
FileDescriptorProto
>
fileList
=
new
List
<
FileDescriptorProto
>();
...
...
@@ -168,17 +160,14 @@ namespace Google.ProtocolBuffers.ProtoGen {
bool
foundAllDependencies
=
true
;
for
(
int
j
=
0
;
j
<
dependencies
.
Length
;
j
++)
{
if
(!
convertedMap
.
TryGetValue
(
candidate
.
DependencyList
[
j
],
out
dependencies
[
j
]))
{
// ROK 2010-09-03 - we can auto-magically resolve these since we already have their description
// We can auto-magically resolve these since we already have their description
// This way if the file is only referencing options it does not need to be built with the
// --include_imports definition.
if
(
localOptions
.
IgnoreGoogleProtobuf
&&
(
candidate
.
DependencyList
[
j
]
==
"google/protobuf/csharp_options.proto"
))
{
if
(
localOptions
.
IgnoreGoogleProtobuf
&&
(
candidate
.
DependencyList
[
j
]
==
"google/protobuf/csharp_options.proto"
))
{
dependencies
[
j
]
=
CSharpOptions
.
Descriptor
;
continue
;
}
if
(
localOptions
.
IgnoreGoogleProtobuf
&&
(
candidate
.
DependencyList
[
j
]
==
"google/protobuf/descriptor.proto"
))
{
if
(
localOptions
.
IgnoreGoogleProtobuf
&&
(
candidate
.
DependencyList
[
j
]
==
"google/protobuf/descriptor.proto"
))
{
dependencies
[
j
]
=
DescriptorProtoFile
.
Descriptor
;
continue
;
}
...
...
src/ProtoGen/GeneratorOptions.cs
View file @
57599ef1
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
...
...
@@ -30,6 +31,7 @@
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using
System
;
...
...
@@ -40,7 +42,6 @@ using Google.ProtocolBuffers.DescriptorProtos;
using
Google.ProtocolBuffers.Descriptors
;
namespace
Google.ProtocolBuffers.ProtoGen
{
/// <summary>
/// All the configuration required for the generator - where to generate
/// output files, the location of input files etc. While this isn't immutable
...
...
@@ -48,7 +49,6 @@ namespace Google.ProtocolBuffers.ProtoGen {
/// the generator.
/// </summary>
public
sealed
class
GeneratorOptions
{
//ROK, see below - public string OutputDirectory { get; set; }
public
IList
<
string
>
InputFiles
{
get
;
set
;
}
/// <summary>
...
...
@@ -61,13 +61,13 @@ namespace Google.ProtocolBuffers.ProtoGen {
public
bool
TryValidate
(
out
IList
<
string
>
reasons
)
{
List
<
string
>
tmpReasons
=
new
List
<
string
>();
//ROK 2010-09-03 see population of options below
ParseArguments
(
tmpReasons
);
// Output directory validation
if
(
string
.
IsNullOrEmpty
(
FileOptions
.
OutputDirectory
))
{
tmpReasons
.
Add
(
"No output directory specified"
);
}
else
{
}
else
{
if
(!
Directory
.
Exists
(
FileOptions
.
OutputDirectory
))
{
tmpReasons
.
Add
(
"Specified output directory ("
+
FileOptions
.
OutputDirectory
+
" doesn't exist."
);
}
...
...
@@ -76,7 +76,8 @@ namespace Google.ProtocolBuffers.ProtoGen {
// Input file validation (just in terms of presence)
if
(
InputFiles
==
null
||
InputFiles
.
Count
==
0
)
{
tmpReasons
.
Add
(
"No input files specified"
);
}
else
{
}
else
{
foreach
(
string
input
in
InputFiles
)
{
FileInfo
fi
=
new
FileInfo
(
input
);
if
(!
fi
.
Exists
)
{
...
...
@@ -106,17 +107,12 @@ namespace Google.ProtocolBuffers.ProtoGen {
}
}
// ROK - added to provide defaults for any of the options
//Raw arguments
// Raw arguments, used to provide defaults for proto file options
public
IList
<
string
>
Arguments
{
get
;
set
;
}
[
Obsolete
(
"Please use GeneratorOptions.FileOptions.OutputDirectory instead"
)]
public
string
OutputDirectory
{
get
{
return
FileOptions
.
OutputDirectory
;
}
public
string
OutputDirectory
{
get
{
return
FileOptions
.
OutputDirectory
;
}
set
{
CSharpFileOptions
.
Builder
bld
=
FileOptions
.
ToBuilder
();
bld
.
OutputDirectory
=
value
;
...
...
@@ -125,80 +121,74 @@ namespace Google.ProtocolBuffers.ProtoGen {
}
private
static
readonly
Regex
ArgMatch
=
new
Regex
(
@"^[-/](?<name>[\w_]+?)[:=](?<value>.*)$"
);
CSharpFileOptions
_
fileOptions
;
public
CSharpFileOptions
FileOptions
{
get
{
return
_fileOptions
??
(
_
fileOptions
=
CSharpFileOptions
.
DefaultInstance
);
}
set
{
_
fileOptions
=
value
;
}
private
CSharpFileOptions
fileOptions
;
public
CSharpFileOptions
FileOptions
{
get
{
return
fileOptions
??
(
fileOptions
=
CSharpFileOptions
.
DefaultInstance
);
}
set
{
fileOptions
=
value
;
}
}
private
void
ParseArguments
(
IList
<
string
>
tmpReasons
)
{
private
void
ParseArguments
(
IList
<
string
>
tmpReasons
)
{
bool
doHelp
=
Arguments
.
Count
==
0
;
//ROK Parse the raw arguments
InputFiles
=
new
List
<
string
>();
CSharpFileOptions
.
Builder
builder
=
FileOptions
.
ToBuilder
();
Dictionary
<
string
,
FieldDescriptor
>
fields
=
new
Dictionary
<
string
,
FieldDescriptor
>(
StringComparer
.
OrdinalIgnoreCase
);
foreach
(
FieldDescriptor
fld
in
builder
.
DescriptorForType
.
Fields
)
foreach
(
FieldDescriptor
fld
in
builder
.
DescriptorForType
.
Fields
)
{
fields
.
Add
(
fld
.
Name
,
fld
);
}
foreach
(
string
argument
in
Arguments
)
{
foreach
(
string
argument
in
Arguments
)
{
if
(
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
"-help"
,
argument
)
||
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
"/help"
,
argument
)
||
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
"-?"
,
argument
)
||
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
"/?"
,
argument
))
{
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
"/?"
,
argument
))
{
doHelp
=
true
;
break
;
}
Match
m
=
ArgMatch
.
Match
(
argument
);
if
(
m
.
Success
)
{
if
(
m
.
Success
)
{
FieldDescriptor
fld
;
string
name
=
m
.
Groups
[
"name"
].
Value
;
string
value
=
m
.
Groups
[
"value"
].
Value
;
if
(
fields
.
TryGetValue
(
name
,
out
fld
))
{
if
(
fields
.
TryGetValue
(
name
,
out
fld
))
{
object
obj
;
if
(
TryCoerceType
(
value
,
fld
,
out
obj
,
tmpReasons
))
if
(
TryCoerceType
(
value
,
fld
,
out
obj
,
tmpReasons
))
{
builder
[
fld
]
=
obj
;
}
else
if
(!
File
.
Exists
(
argument
))
{
}
else
if
(!
File
.
Exists
(
argument
))
{
doHelp
=
true
;
tmpReasons
.
Add
(
"Unknown argument '"
+
name
+
"'."
);
}
else
else
{
InputFiles
.
Add
(
argument
);
}
else
}
else
{
InputFiles
.
Add
(
argument
);
}
}
if
(
doHelp
||
InputFiles
.
Count
==
0
)
{
if
(
doHelp
||
InputFiles
.
Count
==
0
)
{
tmpReasons
.
Add
(
"Arguments:"
);
foreach
(
KeyValuePair
<
string
,
FieldDescriptor
>
field
in
fields
)
{
foreach
(
KeyValuePair
<
string
,
FieldDescriptor
>
field
in
fields
)
{
tmpReasons
.
Add
(
String
.
Format
(
"-{0}=[{1}]"
,
field
.
Key
,
field
.
Value
.
FieldType
));
}
tmpReasons
.
Add
(
"followed by one or more file paths."
);
}
else
else
{
FileOptions
=
builder
.
Build
();
}
}
private
static
bool
TryCoerceType
(
string
text
,
FieldDescriptor
field
,
out
object
value
,
IList
<
string
>
tmpReasons
)
{
private
static
bool
TryCoerceType
(
string
text
,
FieldDescriptor
field
,
out
object
value
,
IList
<
string
>
tmpReasons
)
{
value
=
null
;
switch
(
field
.
FieldType
)
{
switch
(
field
.
FieldType
)
{
case
FieldType
.
Int32
:
case
FieldType
.
SInt32
:
case
FieldType
.
SFixed32
:
...
...
@@ -237,28 +227,22 @@ namespace Google.ProtocolBuffers.ProtoGen {
value
=
text
;
break
;
case
FieldType
.
Enum
:
{
case
FieldType
.
Enum
:
{
EnumDescriptor
enumType
=
field
.
EnumType
;
int
number
;
if
(
int
.
TryParse
(
text
,
out
number
))
{
if
(
int
.
TryParse
(
text
,
out
number
))
{
value
=
enumType
.
FindValueByNumber
(
number
);
if
(
value
==
null
)
{
if
(
value
==
null
)
{
tmpReasons
.
Add
(
"Enum type \""
+
enumType
.
FullName
+
"\" has no value with number "
+
number
+
"."
);
return
false
;
}
}
else
{
else
{
value
=
enumType
.
FindValueByName
(
text
);
if
(
value
==
null
)
{
if
(
value
==
null
)
{
tmpReasons
.
Add
(
"Enum type \""
+
enumType
.
FullName
+
"\" has no value named \""
+
text
+
"\"."
);
...
...
@@ -278,6 +262,5 @@ namespace Google.ProtocolBuffers.ProtoGen {
return
true
;
}
}
}
\ No newline at end of file
src/ProtoGen/Program.cs
View file @
57599ef1
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
...
...
@@ -30,6 +31,7 @@
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using
System
;
...
...
@@ -40,12 +42,12 @@ namespace Google.ProtocolBuffers.ProtoGen {
/// <summary>
/// Entry point for the Protocol Buffers generator.
/// </summary>
class
Program
{
internal
class
Program
{
internal
static
int
Main
(
string
[]
args
)
{
try
{
// Hack to make sure everything's initialized
DescriptorProtoFile
.
Descriptor
.
ToString
();
GeneratorOptions
options
=
ParseCommandLineArguments
(
args
)
;
GeneratorOptions
options
=
new
GeneratorOptions
{
Arguments
=
args
}
;
IList
<
string
>
validationFailures
;
if
(!
options
.
TryValidate
(
out
validationFailures
))
{
...
...
@@ -58,25 +60,13 @@ namespace Google.ProtocolBuffers.ProtoGen {
Generator
generator
=
Generator
.
CreateGenerator
(
options
);
generator
.
Generate
();
return
0
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
Console
.
Error
.
WriteLine
(
"Error: {0}"
,
e
.
Message
);
Console
.
Error
.
WriteLine
();
Console
.
Error
.
WriteLine
(
"Detailed exception information: {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-solo.protobin" };
//ROK 2010-09-03 - fixes to allow parsing these options...
//options.OutputDirectory = ".";
//options.InputFiles = args;
options
.
Arguments
=
args
;
return
options
;
}
}
}
\ No newline at end of file
src/ProtoGen/ProgramPreprocess.cs
View file @
57599ef1
...
...
@@ -3,8 +3,7 @@ using System.Collections.Generic;
using
System.Diagnostics
;
using
System.IO
;
namespace
Google.ProtocolBuffers.ProtoGen
{
namespace
Google.ProtocolBuffers.ProtoGen
{
/// <summary>
/// Preprocesses any input files with an extension of '.proto' by running protoc.exe. If arguments
/// are supplied with '--' prefix they are provided to protoc.exe, otherwise they are assumed to
...
...
@@ -12,53 +11,48 @@ namespace Google.ProtocolBuffers.ProtoGen
/// --descriptor_set_out= is specified the proto buffer file is kept, otherwise it will be removed
/// after code generation.
/// </summary>
internal
class
ProgramPreprocess
{
static
int
Main
(
string
[]
args
)
{
try
{
internal
class
ProgramPreprocess
{
private
static
int
Main
(
string
[]
args
)
{
try
{
return
Environment
.
ExitCode
=
Run
(
args
);
}
catch
(
Exception
ex
)
{
catch
(
Exception
ex
)
{
Console
.
Error
.
WriteLine
(
ex
);
return
Environment
.
ExitCode
=
2
;
}
}
internal
static
int
Run
(
params
string
[]
args
)
{
internal
static
int
Run
(
params
string
[]
args
)
{
bool
deleteFile
=
false
;
string
tempFile
=
null
;
int
result
=
1
;
int
result
;
bool
doHelp
=
args
.
Length
==
0
;
try
{
try
{
List
<
string
>
protocArgs
=
new
List
<
string
>();
List
<
string
>
protoGenArgs
=
new
List
<
string
>();
foreach
(
string
arg
in
args
)
{
foreach
(
string
arg
in
args
)
{
doHelp
|=
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
arg
,
"/?"
);
doHelp
|=
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
arg
,
"/help"
);
doHelp
|=
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
arg
,
"-?"
);
doHelp
|=
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
arg
,
"-help"
);
if
(
arg
.
StartsWith
(
"--descriptor_set_out="
))
{
if
(
arg
.
StartsWith
(
"--descriptor_set_out="
))
{
tempFile
=
arg
.
Substring
(
"--descriptor_set_out="
.
Length
);
protoGenArgs
.
Add
(
tempFile
);
}
}
if
(
doHelp
)
{
if
(
doHelp
)
{
Console
.
WriteLine
();
Console
.
WriteLine
(
"PROTOC.exe: Use any of the following options that begin with '--':"
);
Console
.
WriteLine
();
try
{
RunProtoc
(
"--help"
);
}
catch
(
Exception
ex
)
{
Console
.
Error
.
WriteLine
(
ex
.
Message
);
}
try
{
RunProtoc
(
"--help"
);
}
catch
(
Exception
ex
)
{
Console
.
Error
.
WriteLine
(
ex
.
Message
);
}
Console
.
WriteLine
();
Console
.
WriteLine
();
Console
.
WriteLine
(
"PRTOGEN.exe: The following options are used to specify defaults for code generation."
);
...
...
@@ -67,14 +61,12 @@ namespace Google.ProtocolBuffers.ProtoGen
return
0
;
}
foreach
(
string
arg
in
args
)
{
if
(
arg
.
StartsWith
(
"--"
))
foreach
(
string
arg
in
args
)
{
if
(
arg
.
StartsWith
(
"--"
))
{
protocArgs
.
Add
(
arg
);
else
if
(
File
.
Exists
(
arg
)
&&
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
".proto"
,
Path
.
GetExtension
(
arg
)))
{
if
(
tempFile
==
null
)
{
}
else
if
(
File
.
Exists
(
arg
)
&&
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
".proto"
,
Path
.
GetExtension
(
arg
)))
{
if
(
tempFile
==
null
)
{
deleteFile
=
true
;
tempFile
=
Path
.
GetTempFileName
();
protocArgs
.
Add
(
String
.
Format
(
"--descriptor_set_out={0}"
,
tempFile
));
...
...
@@ -82,48 +74,53 @@ namespace Google.ProtocolBuffers.ProtoGen
}
protocArgs
.
Add
(
arg
);
}
else
else
{
protoGenArgs
.
Add
(
arg
);
}
}
if
(
tempFile
!=
null
)
{
if
(
tempFile
!=
null
)
{
result
=
RunProtoc
(
protocArgs
.
ToArray
());
if
(
result
!=
0
)
if
(
result
!=
0
)
{
return
result
;
}
}
result
=
Program
.
Main
(
protoGenArgs
.
ToArray
());
}
finally
{
if
(
deleteFile
&&
tempFile
!=
null
&&
File
.
Exists
(
tempFile
))
finally
{
if
(
deleteFile
&&
tempFile
!=
null
&&
File
.
Exists
(
tempFile
))
{
File
.
Delete
(
tempFile
);
}
}
return
result
;
}
private
static
int
RunProtoc
(
params
string
[]
args
)
{
private
static
int
RunProtoc
(
params
string
[]
args
)
{
const
string
protoc
=
"protoc.exe"
;
string
exePath
=
protoc
;
//why oh why is this not in System.IO.Path or Environment...
// Why oh why is this not in System.IO.Path or Environment...?
List
<
string
>
searchPath
=
new
List
<
string
>();
searchPath
.
Add
(
Environment
.
CurrentDirectory
);
searchPath
.
Add
(
AppDomain
.
CurrentDomain
.
BaseDirectory
);
searchPath
.
AddRange
((
Environment
.
GetEnvironmentVariable
(
"PATH"
)
??
String
.
Empty
).
Split
(
Path
.
PathSeparator
));
foreach
(
string
path
in
searchPath
)
if
(
File
.
Exists
(
exePath
=
Path
.
Combine
(
path
,
protoc
)))
foreach
(
string
path
in
searchPath
)
{
if
(
File
.
Exists
(
exePath
=
Path
.
Combine
(
path
,
protoc
)))
{
break
;
}
}
if
(!
File
.
Exists
(
exePath
))
if
(!
File
.
Exists
(
exePath
))
{
throw
new
FileNotFoundException
(
"Unable to locate "
+
protoc
+
" make sure it is in the PATH, cwd, or exe dir."
);
}
for
(
int
i
=
0
;
i
<
args
.
Length
;
i
++)
if
(
args
[
i
].
IndexOf
(
' '
)
>
0
&&
args
[
i
][
0
]
!=
'"'
)
for
(
int
i
=
0
;
i
<
args
.
Length
;
i
++)
{
if
(
args
[
i
].
IndexOf
(
' '
)
>
0
&&
args
[
i
][
0
]
!=
'"'
)
{
args
[
i
]
=
'"'
+
args
[
i
]
+
'"'
;
}
}
ProcessStartInfo
psi
=
new
ProcessStartInfo
(
exePath
);
psi
.
Arguments
=
String
.
Join
(
" "
,
args
);
...
...
@@ -136,14 +133,20 @@ namespace Google.ProtocolBuffers.ProtoGen
psi
.
WorkingDirectory
=
Environment
.
CurrentDirectory
;
Process
process
=
Process
.
Start
(
psi
);
if
(
process
==
null
)
return
1
;
if
(
process
==
null
)
{
return
1
;
}
process
.
WaitForExit
();
string
tmp
=
process
.
StandardOutput
.
ReadToEnd
();
if
(
tmp
.
Trim
().
Length
>
0
)
Console
.
Out
.
WriteLine
(
tmp
);
if
(
tmp
.
Trim
().
Length
>
0
)
{
Console
.
Out
.
WriteLine
(
tmp
);
}
tmp
=
process
.
StandardError
.
ReadToEnd
();
if
(
tmp
.
Trim
().
Length
>
0
)
Console
.
Error
.
WriteLine
(
tmp
);
if
(
tmp
.
Trim
().
Length
>
0
)
{
Console
.
Error
.
WriteLine
(
tmp
);
}
return
process
.
ExitCode
;
}
}
...
...
src/ProtoGen/ServiceGenerator.cs
View file @
57599ef1
...
...
@@ -61,7 +61,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
writer
.
WriteLine
();
writer
.
WriteLine
(
"{0} static pbd::ServiceDescriptor Descriptor {{"
,
ClassAccessLevel
);
writer
.
WriteLine
(
" get {{ return {0}.Descriptor.Services[{1}]; }}"
,
DescriptorUtil
.
QualifiedUmbrellaClassName
(
Descriptor
.
File
.
CSharpOptions
),
Descriptor
.
Index
);
DescriptorUtil
.
Get
QualifiedUmbrellaClassName
(
Descriptor
.
File
.
CSharpOptions
),
Descriptor
.
Index
);
writer
.
WriteLine
(
"}"
);
writer
.
WriteLine
(
"{0} pbd::ServiceDescriptor DescriptorForType {{"
,
ClassAccessLevel
);
writer
.
WriteLine
(
" get { return Descriptor; }"
);
...
...
src/ProtoGen/UmbrellaClassGenerator.cs
View file @
57599ef1
...
...
@@ -93,7 +93,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
writer
.
Outdent
();
writer
.
WriteLine
(
"}"
);
//ROK 2010-09-03 - c
lose the namespace around the umbrella class if defined
// C
lose the namespace around the umbrella class if defined
if
(!
Descriptor
.
CSharpOptions
.
NestClasses
&&
Descriptor
.
CSharpOptions
.
UmbrellaNamespace
!=
""
)
{
writer
.
Outdent
();
writer
.
WriteLine
(
"}"
);
...
...
@@ -122,7 +122,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
writer
.
Indent
();
writer
.
WriteLine
();
}
//ROK 2010-09-03 - a
dd the namespace around the umbrella class if defined
// A
dd the namespace around the umbrella class if defined
if
(!
Descriptor
.
CSharpOptions
.
NestClasses
&&
Descriptor
.
CSharpOptions
.
UmbrellaNamespace
!=
""
)
{
writer
.
WriteLine
(
"namespace {0} {{"
,
Descriptor
.
CSharpOptions
.
UmbrellaNamespace
);
writer
.
Indent
();
...
...
src/ProtocolBuffers.Test/DynamicMessageTest.cs
View file @
57599ef1
src/ProtocolBuffers/Descriptors/FileDescriptor.cs
View file @
57599ef1
...
...
@@ -32,17 +32,17 @@
using
System
;
using
System.Collections.Generic
;
using
System.Collections.ObjectModel
;
using
System.IO
;
using
Google.ProtocolBuffers.DescriptorProtos
;
using
FileOptions
=
Google
.
ProtocolBuffers
.
DescriptorProtos
.
FileOptions
;
namespace
Google.ProtocolBuffers.Descriptors
{
/// <summary>
/// Describes a .proto file, including everything defined within.
/// IDescriptor is implemented such that the File property returns this descriptor,
/// and the FullName is the same as the Name.
/// </summary>
public
sealed
class
FileDescriptor
:
IDescriptor
<
FileDescriptorProto
>
{
private
FileDescriptorProto
proto
;
private
readonly
IList
<
MessageDescriptor
>
messageTypes
;
private
readonly
IList
<
EnumDescriptor
>
enumTypes
;
...
...
@@ -61,44 +61,55 @@ namespace Google.ProtocolBuffers.Descriptors {
pool
.
AddPackage
(
Package
,
this
);
messageTypes
=
DescriptorUtil
.
ConvertAndMakeReadOnly
(
proto
.
MessageTypeList
,
(
message
,
index
)
=>
new
MessageDescriptor
(
message
,
this
,
null
,
index
));
(
message
,
index
)
=>
new
MessageDescriptor
(
message
,
this
,
null
,
index
));
enumTypes
=
DescriptorUtil
.
ConvertAndMakeReadOnly
(
proto
.
EnumTypeList
,
(
enumType
,
index
)
=>
new
EnumDescriptor
(
enumType
,
this
,
null
,
index
));
(
enumType
,
index
)
=>
new
EnumDescriptor
(
enumType
,
this
,
null
,
index
));
services
=
DescriptorUtil
.
ConvertAndMakeReadOnly
(
proto
.
ServiceList
,
(
service
,
index
)
=>
new
ServiceDescriptor
(
service
,
this
,
index
));
extensions
=
DescriptorUtil
.
ConvertAndMakeReadOnly
(
proto
.
ExtensionList
,
(
field
,
index
)
=>
new
FieldDescriptor
(
field
,
this
,
null
,
index
,
true
));
(
field
,
index
)
=>
new
FieldDescriptor
(
field
,
this
,
null
,
index
,
true
));
}
/// <summary>
/// ROK - Added to allow the GeneratorOptions to sepcify the default for any or all of these values
/// Allows a file descriptor to be configured with a set of external options, e.g. from the
/// command-line arguments to protogen.
/// </summary>
internal
void
ConfigureWithDefaultOptions
(
CSharpFileOptions
options
)
{
internal
void
ConfigureWithDefaultOptions
(
CSharpFileOptions
options
)
{
csharpFileOptions
=
BuildOrFakeWithDefaultOptions
(
options
);
}
private
CSharpFileOptions
BuildOrFakeWithDefaultOptions
(
CSharpFileOptions
defaultOptions
)
{
// ROK 2010-09-03 - f
ix for being able to relocate these files to any directory structure
if
(
proto
.
Package
==
"google.protobuf"
)
{
string
filename
=
System
.
IO
.
Path
.
GetFileName
(
proto
.
Name
);
private
CSharpFileOptions
BuildOrFakeWithDefaultOptions
(
CSharpFileOptions
defaultOptions
)
{
// F
ix for being able to relocate these files to any directory structure
if
(
proto
.
Package
==
"google.protobuf"
)
{
string
filename
=
Path
.
GetFileName
(
proto
.
Name
);
// TODO(jonskeet): Check if we could use FileDescriptorProto.Descriptor.Name - interesting bootstrap issues)
if
(
filename
==
"descriptor.proto"
)
{
return
new
CSharpFileOptions
.
Builder
{
Namespace
=
"Google.ProtocolBuffers.DescriptorProtos"
,
UmbrellaClassname
=
"DescriptorProtoFile"
,
NestClasses
=
false
,
MultipleFiles
=
false
,
PublicClasses
=
true
,
OutputDirectory
=
defaultOptions
.
OutputDirectory
,
IgnoreGoogleProtobuf
=
defaultOptions
.
IgnoreGoogleProtobuf
UmbrellaClassname
=
"DescriptorProtoFile"
,
NestClasses
=
false
,
MultipleFiles
=
false
,
PublicClasses
=
true
,
OutputDirectory
=
defaultOptions
.
OutputDirectory
,
IgnoreGoogleProtobuf
=
defaultOptions
.
IgnoreGoogleProtobuf
}.
Build
();
}
if
(
filename
==
"csharp_options.proto"
)
{
return
new
CSharpFileOptions
.
Builder
{
Namespace
=
"Google.ProtocolBuffers.DescriptorProtos"
,
UmbrellaClassname
=
"CSharpOptions"
,
NestClasses
=
false
,
MultipleFiles
=
false
,
PublicClasses
=
true
,
OutputDirectory
=
defaultOptions
.
OutputDirectory
,
IgnoreGoogleProtobuf
=
defaultOptions
.
IgnoreGoogleProtobuf
UmbrellaClassname
=
"CSharpOptions"
,
NestClasses
=
false
,
MultipleFiles
=
false
,
PublicClasses
=
true
,
OutputDirectory
=
defaultOptions
.
OutputDirectory
,
IgnoreGoogleProtobuf
=
defaultOptions
.
IgnoreGoogleProtobuf
}.
Build
();
}
}
...
...
@@ -115,19 +126,23 @@ namespace Google.ProtocolBuffers.Descriptors {
builder
.
UmbrellaClassname
=
NameHelpers
.
UnderscoresToPascalCase
(
NameHelpers
.
StripProto
(
baseName
));
}
// ROK 2010-09-03 - auto
fix for name collision by placing umbrella class into a new namespace. This
// still won't fix the collisions with nesting enabled; however, you have to turn that on so whatever
.
if
(!
builder
.
NestClasses
&&
!
builder
.
HasUmbrellaNamespace
)
{
// Auto-
fix for name collision by placing umbrella class into a new namespace. This
// still won't fix the collisions with nesting enabled; however, you have to turn that on explicitly anyway
.
if
(!
builder
.
NestClasses
&&
!
builder
.
HasUmbrellaNamespace
)
{
bool
collision
=
false
;
foreach
(
IDescriptor
d
in
MessageTypes
)
foreach
(
IDescriptor
d
in
MessageTypes
)
{
collision
|=
d
.
Name
==
builder
.
UmbrellaClassname
;
foreach
(
IDescriptor
d
in
Services
)
}
foreach
(
IDescriptor
d
in
Services
)
{
collision
|=
d
.
Name
==
builder
.
UmbrellaClassname
;
foreach
(
IDescriptor
d
in
EnumTypes
)
}
foreach
(
IDescriptor
d
in
EnumTypes
)
{
collision
|=
d
.
Name
==
builder
.
UmbrellaClassname
;
if
(
collision
)
}
if
(
collision
)
{
builder
.
UmbrellaNamespace
=
"Proto"
;
}
}
return
builder
.
Build
();
}
...
...
@@ -140,7 +155,7 @@ namespace Google.ProtocolBuffers.Descriptors {
}
/// <value>
/// The <see cref="FileOptions" /> defined in <c>descriptor.proto</c>.
/// The <see cref="
DescriptorProtos.
FileOptions" /> defined in <c>descriptor.proto</c>.
/// </value>
public
FileOptions
Options
{
get
{
return
proto
.
Options
;
}
...
...
@@ -350,19 +365,21 @@ namespace Google.ProtocolBuffers.Descriptors {
public
delegate
ExtensionRegistry
InternalDescriptorAssigner
(
FileDescriptor
descriptor
);
public
static
FileDescriptor
InternalBuildGeneratedFileFrom
(
byte
[]
descriptorData
,
FileDescriptor
[]
dependencies
,
InternalDescriptorAssigner
descriptorAssigner
)
{
FileDescriptor
[]
dependencies
,
InternalDescriptorAssigner
descriptorAssigner
)
{
FileDescriptorProto
proto
;
try
{
proto
=
FileDescriptorProto
.
ParseFrom
(
descriptorData
);
}
catch
(
InvalidProtocolBufferException
e
)
{
}
catch
(
InvalidProtocolBufferException
e
)
{
throw
new
ArgumentException
(
"Failed to parse protocol buffer descriptor for generated code."
,
e
);
}
FileDescriptor
result
;
try
{
result
=
BuildFrom
(
proto
,
dependencies
);
}
catch
(
DescriptorValidationException
e
)
{
}
catch
(
DescriptorValidationException
e
)
{
throw
new
ArgumentException
(
"Invalid embedded descriptor for \""
+
proto
.
Name
+
"\"."
,
e
);
}
...
...
@@ -372,7 +389,8 @@ namespace Google.ProtocolBuffers.Descriptors {
// We must re-parse the proto using the registry.
try
{
proto
=
FileDescriptorProto
.
ParseFrom
(
descriptorData
,
registry
);
}
catch
(
InvalidProtocolBufferException
e
)
{
}
catch
(
InvalidProtocolBufferException
e
)
{
throw
new
ArgumentException
(
"Failed to parse protocol buffer descriptor for generated code."
,
e
);
}
...
...
@@ -381,7 +399,6 @@ namespace Google.ProtocolBuffers.Descriptors {
return
result
;
}
/// <summary>
/// Replace our FileDescriptorProto with the given one, which is
/// identical except that it might contain extensions that weren't present
...
...
src/ProtocolBuffers/DynamicMessage.cs
View file @
57599ef1
src/ProtocolBuffers/UnknownField.cs
View file @
57599ef1
todo.txt
View file @
57599ef1
Work complete on csharptest/branch
New Options (in csharp_options.proto):
1 - Add a way to specify the output directory
Added an option to csharp_options.proto called "output_directory", defaults to "."
2 - Added an option "file_extension" to control the suffix for cs files generated, defaults to ".cs"
This enables using ".Generated.cs" to provide easy filtering and cleanup of generated code.
3 - Added the option for "umbrella_namespace" used when nest_classes=false and having name conflicts
4 - Optionally remove dependencies to csharp options
provided option "ignore_google_protobuf" to prevent generation of code for csharp_options.proto and descriptor.proto
option also relaxes the contraint of having either of these compiled into the proto buffer input
5 - Investigate command line parsing library
All proto options for csharp_options.proto can now be provided via the command-line by using the following format:
/option=value or -option:value
i.e. use /namespace=My.Name.Space
6 - Investigate calling protoc directly
ProgramPreprocess.cs - input files with an extension of '.proto' by running protoc.exe. If arguments
are supplied with '--' prefix they are provided to protoc.exe, otherwise they are assumed to
be used for ProtoGen.exe which is run on the resulting output proto buffer. If the option
--descriptor_set_out= is specified the proto buffer file is kept, otherwise it will be removed
after code generation.
7 - Unable to resolve dependencies correctly
Fixed an issue where two or more proto-buffers are provided to protogen.exe that depend on types
defined in one-another. The dependency walker of the generator was not taking into account all
inputs when evaluating the descriptor's existence.
8 - Added several (20) nunits to automate the command-line invocation of each option and ensured the
generated code compiles into a working assembly and contains some of the expected types. Not sure
how to disable this test for mono, I'm certain it will not work; however, I still believe it's worth
having. As a side-benefit from testing the command-line options, it verifies behavior of each
setting in csharp_options (save for multi-file).
Current task list (not in order)
? Optionally remove dependencies to csharp options
- Remove multifile support
- Docs
- Clean up protogen code
X Add flags to protogen
- Avoid using reflection for messages which don't need it (is this
possible?)
- Bring service generation into line with Java
...
...
@@ -53,7 +12,5 @@ X Add flags to protogen
- Reformat code
- Change generated format
- Add regions to copyright
X Investigate command line parsing library
X Investigate calling protoc directly
- Build and publish binaries
- Work out why the Compact Framework 3.5 build fails under VS2010
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment