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
64580d09
Commit
64580d09
authored
Nov 22, 2010
by
ArnoldZokas
Browse files
Options
Browse Files
Download
Plain Diff
Fixed merge conflicts
parents
a695dfaa
57599ef1
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
21 changed files
with
520 additions
and
53 deletions
+520
-53
.gitignore
.gitignore
+1
-0
csharp_options.proto
protos/google/protobuf/csharp_options.proto
+16
-0
DependencyResolutionTest.cs
src/ProtoGen.Test/DependencyResolutionTest.cs
+6
-6
ProtoGen.Test.csproj
src/ProtoGen.Test/ProtoGen.Test.csproj
+16
-0
TempFile.cs
src/ProtoGen.Test/TempFile.cs
+54
-0
TestPreprocessing.cs
src/ProtoGen.Test/TestPreprocessing.cs
+0
-0
DescriptorUtil.cs
src/ProtoGen/DescriptorUtil.cs
+17
-2
Generator.cs
src/ProtoGen/Generator.cs
+47
-10
GeneratorOptions.cs
src/ProtoGen/GeneratorOptions.cs
+173
-9
Program.cs
src/ProtoGen/Program.cs
+7
-15
ProgramPreprocess.cs
src/ProtoGen/ProgramPreprocess.cs
+153
-0
ProtoGen.csproj
src/ProtoGen/ProtoGen.csproj
+2
-0
ServiceGenerator.cs
src/ProtoGen/ServiceGenerator.cs
+1
-1
UmbrellaClassGenerator.cs
src/ProtoGen/UmbrellaClassGenerator.cs
+14
-1
DynamicMessageTest.cs
src/ProtocolBuffers.Test/DynamicMessageTest.cs
+2
-2
ProtocolBuffers.sln
src/ProtocolBuffers.sln
+8
-0
CSharpOptions.cs
src/ProtocolBuffers/DescriptorProtos/CSharpOptions.cs
+0
-0
FileDescriptor.cs
src/ProtocolBuffers/Descriptors/FileDescriptor.cs
+0
-0
DynamicMessage.cs
src/ProtocolBuffers/DynamicMessage.cs
+1
-1
UnknownField.cs
src/ProtocolBuffers/UnknownField.cs
+2
-2
todo.txt
todo.txt
+0
-4
No files found.
.gitignore
View file @
64580d09
...
...
@@ -5,6 +5,7 @@ src/AddressBook/bin
src/AddressBook/obj
src/ProtocolBuffers/bin/
src/ProtocolBuffers/obj/
src/ProtocolBuffers/objCF
src/ProtocolBuffers.Test/bin/
src/ProtocolBuffers.Test/obj/
src/ProtoBench/bin/
...
...
protos/google/protobuf/csharp_options.proto
View file @
64580d09
...
...
@@ -38,6 +38,22 @@ message CSharpFileOptions {
// Generate attributes indicating non-CLS-compliance
optional
bool
cls_compliance
=
8
[
default
=
true
];
// The extension that should be appended to the umbrella_classname when creating files.
optional
string
file_extension
=
221
[
default
=
".cs"
];
// A nested namespace for the umbrella class. Helpful for name collisions caused by
// umbrella_classname conflicting with an existing type. This will be automatically
// set to 'Proto' if a collision is detected with types being generated. This value
// is ignored when nest_classes == true
optional
string
umbrella_namespace
=
222
;
// The output path for the source file(s) generated
optional
string
output_directory
=
223
[
default
=
"."
];
// Will ignore the type generations and remove dependencies for the descriptor proto
// files that declare their package to be "google.protobuf"
optional
bool
ignore_google_protobuf
=
224
[
default
=
false
];
}
extend
FileOptions
{
...
...
src/ProtoGen.Test/DependencyResolutionTest.cs
View file @
64580d09
...
...
@@ -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/ProtoGen.Test.csproj
View file @
64580d09
...
...
@@ -73,10 +73,14 @@
<HintPath>
..\..\lib\Rhino.Mocks.dll
</HintPath>
</Reference>
<Reference
Include=
"System"
/>
<Reference
Include=
"System.Data"
/>
<Reference
Include=
"System.Xml"
/>
</ItemGroup>
<ItemGroup>
<Compile
Include=
"DependencyResolutionTest.cs"
/>
<Compile
Include=
"Properties\AssemblyInfo.cs"
/>
<Compile
Include=
"TempFile.cs"
/>
<Compile
Include=
"TestPreprocessing.cs"
/>
</ItemGroup>
<ItemGroup>
<ProjectReference
Include=
"..\ProtocolBuffers\ProtocolBuffers.csproj"
>
...
...
@@ -107,6 +111,18 @@
<ProductName>
Windows Installer 3.1
</ProductName>
<Install>
true
</Install>
</BootstrapperPackage>
<None
Include=
"..\..\lib\protoc.exe"
>
<Link>
protoc.exe
</Link>
<CopyToOutputDirectory>
Always
</CopyToOutputDirectory>
</None>
<None
Include=
"..\..\protos\google\protobuf\csharp_options.proto"
>
<Link>
google\protobuf\csharp_options.proto
</Link>
<CopyToOutputDirectory>
Always
</CopyToOutputDirectory>
</None>
<None
Include=
"..\..\protos\google\protobuf\descriptor.proto"
>
<Link>
google\protobuf\descriptor.proto
</Link>
<CopyToOutputDirectory>
Always
</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import
Project=
"$(MSBuildToolsPath)\Microsoft.CSharp.targets"
/>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
...
...
src/ProtoGen.Test/TempFile.cs
0 → 100644
View file @
64580d09
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Text
;
namespace
Google.ProtocolBuffers.ProtoGen
{
class
ProtoFile
:
TempFile
{
public
ProtoFile
(
string
filename
,
string
contents
)
:
base
(
filename
,
contents
)
{
}
}
class
TempFile
:
IDisposable
{
private
string
tempFile
;
public
static
TempFile
Attach
(
string
path
)
{
return
new
TempFile
(
path
,
null
);
}
protected
TempFile
(
string
filename
,
string
contents
)
{
tempFile
=
filename
;
if
(
contents
!=
null
)
{
File
.
WriteAllText
(
tempFile
,
contents
,
new
UTF8Encoding
(
false
));
}
}
public
TempFile
(
string
contents
)
:
this
(
Path
.
GetTempFileName
(),
contents
)
{
}
public
string
TempPath
{
get
{
return
tempFile
;
}
}
public
void
ChangeExtension
(
string
ext
)
{
string
newFile
=
Path
.
ChangeExtension
(
tempFile
,
ext
);
File
.
Move
(
tempFile
,
newFile
);
tempFile
=
newFile
;
}
public
void
Dispose
()
{
if
(
File
.
Exists
(
tempFile
))
{
File
.
Delete
(
tempFile
);
}
}
}
}
src/ProtoGen.Test/TestPreprocessing.cs
0 → 100644
View file @
64580d09
This diff is collapsed.
Click to expand it.
src/ProtoGen/DescriptorUtil.cs
View file @
64580d09
...
...
@@ -45,11 +45,26 @@ namespace Google.ProtocolBuffers.ProtoGen {
internal
static
string
GetFullUmbrellaClassName
(
IDescriptor
descriptor
)
{
CSharpFileOptions
options
=
descriptor
.
File
.
CSharpOptions
;
string
result
=
options
.
Namespace
;
if
(
result
!=
""
)
result
+=
'.'
;
result
+=
options
.
UmbrellaClassname
;
if
(
result
!=
""
)
{
result
+=
'.'
;
}
result
+=
GetQualifiedUmbrellaClassName
(
options
);
return
"global::"
+
result
;
}
/// <summary>
/// Evaluates the options and returns the qualified name of the umbrella class
/// relative to the descriptor type's namespace. Basically concatenates the
/// UmbrellaNamespace + UmbrellaClassname fields.
/// </summary>
internal
static
string
GetQualifiedUmbrellaClassName
(
CSharpFileOptions
options
)
{
string
fullName
=
options
.
UmbrellaClassname
;
if
(!
options
.
NestClasses
&&
options
.
UmbrellaNamespace
!=
""
)
{
fullName
=
String
.
Format
(
"{0}.{1}"
,
options
.
UmbrellaNamespace
,
options
.
UmbrellaClassname
);
}
return
fullName
;
}
internal
static
string
GetMappedTypeName
(
MappedType
type
)
{
switch
(
type
)
{
case
MappedType
.
Int32
:
return
"int"
;
...
...
src/ProtoGen/Generator.cs
View file @
64580d09
...
...
@@ -60,19 +60,30 @@ namespace Google.ProtocolBuffers.ProtoGen {
}
public
void
Generate
()
{
List
<
FileDescriptorSet
>
descriptorProtos
=
new
List
<
FileDescriptorSet
>();
foreach
(
string
inputFile
in
options
.
InputFiles
)
{
FileDescriptorSet
descriptorProtos
;
ExtensionRegistry
extensionRegistry
=
ExtensionRegistry
.
CreateInstance
();
extensionRegistry
.
Add
(
CSharpOptions
.
CSharpFileOptions
);
extensionRegistry
.
Add
(
CSharpOptions
.
CSharpFieldOptions
);
using
(
Stream
inputStream
=
File
.
OpenRead
(
inputFile
))
{
descriptorProtos
=
FileDescriptorSet
.
ParseFrom
(
inputStream
,
extensionRegistry
);
descriptorProtos
.
Add
(
FileDescriptorSet
.
ParseFrom
(
inputStream
,
extensionRegistry
)
);
}
IList
<
FileDescriptor
>
descriptors
=
ConvertDescriptors
(
descriptorProtos
);
}
IList
<
FileDescriptor
>
descriptors
=
ConvertDescriptors
(
options
.
FileOptions
,
descriptorProtos
.
ToArray
());
// Combine with options from command line
foreach
(
FileDescriptor
descriptor
in
descriptors
)
{
descriptor
.
ConfigureWithDefaultOptions
(
options
.
FileOptions
);
}
foreach
(
FileDescriptor
descriptor
in
descriptors
)
{
Generate
(
descriptor
);
foreach
(
FileDescriptor
descriptor
in
descriptors
)
{
// Optionally exclude descriptors in google.protobuf
if
(
descriptor
.
CSharpOptions
.
IgnoreGoogleProtobuf
&&
descriptor
.
Package
==
"google.protobuf"
)
{
continue
;
}
Generate
(
descriptor
);
}
}
...
...
@@ -90,8 +101,10 @@ namespace Google.ProtocolBuffers.ProtoGen {
private
string
GetOutputFile
(
FileDescriptor
descriptor
)
{
CSharpFileOptions
fileOptions
=
descriptor
.
CSharpOptions
;
string
filename
=
descriptor
.
CSharpOptions
.
UmbrellaClassname
+
".cs"
;
string
outputDirectory
=
options
.
OutputDirectory
;
string
filename
=
descriptor
.
CSharpOptions
.
UmbrellaClassname
+
descriptor
.
CSharpOptions
.
FileExtension
;
string
outputDirectory
=
descriptor
.
CSharpOptions
.
OutputDirectory
;
if
(
fileOptions
.
ExpandNamespaceDirectories
)
{
string
package
=
fileOptions
.
Namespace
;
if
(!
string
.
IsNullOrEmpty
(
package
))
{
...
...
@@ -99,9 +112,11 @@ namespace Google.ProtocolBuffers.ProtoGen {
foreach
(
string
bit
in
bits
)
{
outputDirectory
=
Path
.
Combine
(
outputDirectory
,
bit
);
}
Directory
.
CreateDirectory
(
outputDirectory
);
}
}
// As the directory can be explicitly specified in options, we need to make sure it exists
Directory
.
CreateDirectory
(
outputDirectory
);
return
Path
.
Combine
(
outputDirectory
,
filename
);
}
...
...
@@ -111,10 +126,13 @@ namespace Google.ProtocolBuffers.ProtoGen {
/// Note: this method is internal rather than private to allow testing.
/// </summary>
/// <exception cref="DependencyResolutionException">Not all dependencies could be resolved.</exception>
internal
static
IList
<
FileDescriptor
>
ConvertDescriptors
(
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
IList
<
FileDescriptorProto
>
fileList
=
descriptorProtos
.
FileList
;
List
<
FileDescriptorProto
>
fileList
=
new
List
<
FileDescriptorProto
>();
foreach
(
FileDescriptorSet
set
in
descriptorProtos
)
fileList
.
AddRange
(
set
.
FileList
);
FileDescriptor
[]
converted
=
new
FileDescriptor
[
fileList
.
Count
];
Dictionary
<
string
,
FileDescriptor
>
convertedMap
=
new
Dictionary
<
string
,
FileDescriptor
>();
...
...
@@ -131,9 +149,28 @@ namespace Google.ProtocolBuffers.ProtoGen {
}
FileDescriptorProto
candidate
=
fileList
[
i
];
FileDescriptor
[]
dependencies
=
new
FileDescriptor
[
candidate
.
DependencyList
.
Count
];
CSharpFileOptions
.
Builder
builder
=
options
.
ToBuilder
();
if
(
candidate
.
Options
.
HasExtension
(
DescriptorProtos
.
CSharpOptions
.
CSharpFileOptions
))
{
builder
.
MergeFrom
(
candidate
.
Options
.
GetExtension
(
DescriptorProtos
.
CSharpOptions
.
CSharpFileOptions
));
}
CSharpFileOptions
localOptions
=
builder
.
Build
();
bool
foundAllDependencies
=
true
;
for
(
int
j
=
0
;
j
<
dependencies
.
Length
;
j
++)
{
if
(!
convertedMap
.
TryGetValue
(
candidate
.
DependencyList
[
j
],
out
dependencies
[
j
]))
{
// 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"
))
{
dependencies
[
j
]
=
CSharpOptions
.
Descriptor
;
continue
;
}
if
(
localOptions
.
IgnoreGoogleProtobuf
&&
(
candidate
.
DependencyList
[
j
]
==
"google/protobuf/descriptor.proto"
))
{
dependencies
[
j
]
=
DescriptorProtoFile
.
Descriptor
;
continue
;
}
foundAllDependencies
=
false
;
break
;
}
...
...
src/ProtoGen/GeneratorOptions.cs
View file @
64580d09
#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,13 +31,17 @@
// 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
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Text.RegularExpressions
;
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
...
...
@@ -44,8 +49,6 @@ namespace Google.ProtocolBuffers.ProtoGen {
/// the generator.
/// </summary>
public
sealed
class
GeneratorOptions
{
public
string
OutputDirectory
{
get
;
set
;
}
public
IList
<
string
>
InputFiles
{
get
;
set
;
}
/// <summary>
...
...
@@ -58,19 +61,23 @@ namespace Google.ProtocolBuffers.ProtoGen {
public
bool
TryValidate
(
out
IList
<
string
>
reasons
)
{
List
<
string
>
tmpReasons
=
new
List
<
string
>();
ParseArguments
(
tmpReasons
);
// Output directory validation
if
(
string
.
IsNullOrEmpty
(
OutputDirectory
))
{
if
(
string
.
IsNullOrEmpty
(
FileOptions
.
OutputDirectory
))
{
tmpReasons
.
Add
(
"No output directory specified"
);
}
else
{
if
(!
Directory
.
Exists
(
OutputDirectory
))
{
tmpReasons
.
Add
(
"Specified output directory ("
+
OutputDirectory
+
" doesn't exist."
);
}
else
{
if
(!
Directory
.
Exists
(
FileOptions
.
OutputDirectory
))
{
tmpReasons
.
Add
(
"Specified output directory ("
+
FileOptions
.
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
{
}
else
{
foreach
(
string
input
in
InputFiles
)
{
FileInfo
fi
=
new
FileInfo
(
input
);
if
(!
fi
.
Exists
)
{
...
...
@@ -99,5 +106,161 @@ namespace Google.ProtocolBuffers.ProtoGen {
throw
new
InvalidOptionsException
(
reasons
);
}
}
// 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
;
}
set
{
CSharpFileOptions
.
Builder
bld
=
FileOptions
.
ToBuilder
();
bld
.
OutputDirectory
=
value
;
FileOptions
=
bld
.
Build
();
}
}
private
static
readonly
Regex
ArgMatch
=
new
Regex
(
@"^[-/](?<name>[\w_]+?)[:=](?<value>.*)$"
);
private
CSharpFileOptions
fileOptions
;
public
CSharpFileOptions
FileOptions
{
get
{
return
fileOptions
??
(
fileOptions
=
CSharpFileOptions
.
DefaultInstance
);
}
set
{
fileOptions
=
value
;
}
}
private
void
ParseArguments
(
IList
<
string
>
tmpReasons
)
{
bool
doHelp
=
Arguments
.
Count
==
0
;
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
)
{
fields
.
Add
(
fld
.
Name
,
fld
);
}
foreach
(
string
argument
in
Arguments
)
{
if
(
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
"-help"
,
argument
)
||
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
"/help"
,
argument
)
||
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
"-?"
,
argument
)
||
StringComparer
.
OrdinalIgnoreCase
.
Equals
(
"/?"
,
argument
))
{
doHelp
=
true
;
break
;
}
Match
m
=
ArgMatch
.
Match
(
argument
);
if
(
m
.
Success
)
{
FieldDescriptor
fld
;
string
name
=
m
.
Groups
[
"name"
].
Value
;
string
value
=
m
.
Groups
[
"value"
].
Value
;
if
(
fields
.
TryGetValue
(
name
,
out
fld
))
{
object
obj
;
if
(
TryCoerceType
(
value
,
fld
,
out
obj
,
tmpReasons
))
{
builder
[
fld
]
=
obj
;
}
}
else
if
(!
File
.
Exists
(
argument
))
{
doHelp
=
true
;
tmpReasons
.
Add
(
"Unknown argument '"
+
name
+
"'."
);
}
else
{
InputFiles
.
Add
(
argument
);
}
}
else
{
InputFiles
.
Add
(
argument
);
}
}
if
(
doHelp
||
InputFiles
.
Count
==
0
)
{
tmpReasons
.
Add
(
"Arguments:"
);
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
{
FileOptions
=
builder
.
Build
();
}
}
private
static
bool
TryCoerceType
(
string
text
,
FieldDescriptor
field
,
out
object
value
,
IList
<
string
>
tmpReasons
)
{
value
=
null
;
switch
(
field
.
FieldType
)
{
case
FieldType
.
Int32
:
case
FieldType
.
SInt32
:
case
FieldType
.
SFixed32
:
value
=
Int32
.
Parse
(
text
);
break
;
case
FieldType
.
Int64
:
case
FieldType
.
SInt64
:
case
FieldType
.
SFixed64
:
value
=
Int64
.
Parse
(
text
);
break
;
case
FieldType
.
UInt32
:
case
FieldType
.
Fixed32
:
value
=
UInt32
.
Parse
(
text
);
break
;
case
FieldType
.
UInt64
:
case
FieldType
.
Fixed64
:
value
=
UInt64
.
Parse
(
text
);
break
;
case
FieldType
.
Float
:
value
=
float
.
Parse
(
text
);
break
;
case
FieldType
.
Double
:
value
=
Double
.
Parse
(
text
);
break
;
case
FieldType
.
Bool
:
value
=
Boolean
.
Parse
(
text
);
break
;
case
FieldType
.
String
:
value
=
text
;
break
;
case
FieldType
.
Enum
:
{
EnumDescriptor
enumType
=
field
.
EnumType
;
int
number
;
if
(
int
.
TryParse
(
text
,
out
number
))
{
value
=
enumType
.
FindValueByNumber
(
number
);
if
(
value
==
null
)
{
tmpReasons
.
Add
(
"Enum type \""
+
enumType
.
FullName
+
"\" has no value with number "
+
number
+
"."
);
return
false
;
}
}
else
{
value
=
enumType
.
FindValueByName
(
text
);
if
(
value
==
null
)
{
tmpReasons
.
Add
(
"Enum type \""
+
enumType
.
FullName
+
"\" has no value named \""
+
text
+
"\"."
);
return
false
;
}
}
break
;
}
case
FieldType
.
Bytes
:
case
FieldType
.
Message
:
case
FieldType
.
Group
:
tmpReasons
.
Add
(
"Unhandled field type "
+
field
.
FieldType
.
ToString
()
+
"."
);
return
false
;
}
return
true
;
}
}
}
}
\ No newline at end of file
src/ProtoGen/Program.cs
View file @
64580d09
#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
{
static
int
Main
(
string
[]
args
)
{
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,22 +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" };
options
.
OutputDirectory
=
"."
;
options
.
InputFiles
=
args
;
return
options
;
}
}
}
\ No newline at end of file
src/ProtoGen/ProgramPreprocess.cs
0 → 100644
View file @
64580d09
using
System
;
using
System.Collections.Generic
;
using
System.Diagnostics
;
using
System.IO
;
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
/// 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.
/// </summary>
internal
class
ProgramPreprocess
{
private
static
int
Main
(
string
[]
args
)
{
try
{
return
Environment
.
ExitCode
=
Run
(
args
);
}
catch
(
Exception
ex
)
{
Console
.
Error
.
WriteLine
(
ex
);
return
Environment
.
ExitCode
=
2
;
}
}
internal
static
int
Run
(
params
string
[]
args
)
{
bool
deleteFile
=
false
;
string
tempFile
=
null
;
int
result
;
bool
doHelp
=
args
.
Length
==
0
;
try
{
List
<
string
>
protocArgs
=
new
List
<
string
>();
List
<
string
>
protoGenArgs
=
new
List
<
string
>();
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="
))
{
tempFile
=
arg
.
Substring
(
"--descriptor_set_out="
.
Length
);
protoGenArgs
.
Add
(
tempFile
);
}
}
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
);
}
Console
.
WriteLine
();
Console
.
WriteLine
();
Console
.
WriteLine
(
"PRTOGEN.exe: The following options are used to specify defaults for code generation."
);
Console
.
WriteLine
();
Program
.
Main
(
new
string
[
0
]);
return
0
;
}
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
)
{
deleteFile
=
true
;
tempFile
=
Path
.
GetTempFileName
();
protocArgs
.
Add
(
String
.
Format
(
"--descriptor_set_out={0}"
,
tempFile
));
protoGenArgs
.
Add
(
tempFile
);
}
protocArgs
.
Add
(
arg
);
}
else
{
protoGenArgs
.
Add
(
arg
);
}
}
if
(
tempFile
!=
null
)
{
result
=
RunProtoc
(
protocArgs
.
ToArray
());
if
(
result
!=
0
)
{
return
result
;
}
}
result
=
Program
.
Main
(
protoGenArgs
.
ToArray
());
}
finally
{
if
(
deleteFile
&&
tempFile
!=
null
&&
File
.
Exists
(
tempFile
))
{
File
.
Delete
(
tempFile
);
}
}
return
result
;
}
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...?
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
)))
{
break
;
}
}
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
]
!=
'"'
)
{
args
[
i
]
=
'"'
+
args
[
i
]
+
'"'
;
}
}
ProcessStartInfo
psi
=
new
ProcessStartInfo
(
exePath
);
psi
.
Arguments
=
String
.
Join
(
" "
,
args
);
psi
.
RedirectStandardError
=
true
;
psi
.
RedirectStandardInput
=
false
;
psi
.
RedirectStandardOutput
=
true
;
psi
.
ErrorDialog
=
false
;
psi
.
CreateNoWindow
=
true
;
psi
.
UseShellExecute
=
false
;
psi
.
WorkingDirectory
=
Environment
.
CurrentDirectory
;
Process
process
=
Process
.
Start
(
psi
);
if
(
process
==
null
)
{
return
1
;
}
process
.
WaitForExit
();
string
tmp
=
process
.
StandardOutput
.
ReadToEnd
();
if
(
tmp
.
Trim
().
Length
>
0
)
{
Console
.
Out
.
WriteLine
(
tmp
);
}
tmp
=
process
.
StandardError
.
ReadToEnd
();
if
(
tmp
.
Trim
().
Length
>
0
)
{
Console
.
Error
.
WriteLine
(
tmp
);
}
return
process
.
ExitCode
;
}
}
}
src/ProtoGen/ProtoGen.csproj
View file @
64580d09
...
...
@@ -34,6 +34,7 @@
<IsWebBootstrapper>
false
</IsWebBootstrapper>
<UseApplicationTrust>
false
</UseApplicationTrust>
<BootstrapperEnabled>
true
</BootstrapperEnabled>
<StartupObject>
Google.ProtocolBuffers.ProtoGen.ProgramPreprocess
</StartupObject>
</PropertyGroup>
<PropertyGroup
Condition=
" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "
>
<DebugSymbols>
true
</DebugSymbols>
...
...
@@ -79,6 +80,7 @@
<Compile
Include=
"MessageFieldGenerator.cs"
/>
<Compile
Include=
"MessageGenerator.cs"
/>
<Compile
Include=
"PrimitiveFieldGenerator.cs"
/>
<Compile
Include=
"ProgramPreprocess.cs"
/>
<Compile
Include=
"RepeatedEnumFieldGenerator.cs"
/>
<Compile
Include=
"RepeatedMessageFieldGenerator.cs"
/>
<Compile
Include=
"RepeatedPrimitiveFieldGenerator.cs"
/>
...
...
src/ProtoGen/ServiceGenerator.cs
View file @
64580d09
...
...
@@ -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}]; }}"
,
Descriptor
.
File
.
CSharpOptions
.
UmbrellaClassname
,
Descriptor
.
Index
);
Descriptor
Util
.
GetQualifiedUmbrellaClassName
(
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 @
64580d09
...
...
@@ -92,6 +92,12 @@ namespace Google.ProtocolBuffers.ProtoGen {
if
(!
Descriptor
.
CSharpOptions
.
NestClasses
)
{
writer
.
Outdent
();
writer
.
WriteLine
(
"}"
);
// Close the namespace around the umbrella class if defined
if
(!
Descriptor
.
CSharpOptions
.
NestClasses
&&
Descriptor
.
CSharpOptions
.
UmbrellaNamespace
!=
""
)
{
writer
.
Outdent
();
writer
.
WriteLine
(
"}"
);
}
}
WriteChildren
(
writer
,
"Enums"
,
Descriptor
.
EnumTypes
);
WriteChildren
(
writer
,
"Messages"
,
Descriptor
.
MessageTypes
);
...
...
@@ -115,7 +121,14 @@ namespace Google.ProtocolBuffers.ProtoGen {
writer
.
WriteLine
(
"namespace {0} {{"
,
Descriptor
.
CSharpOptions
.
Namespace
);
writer
.
Indent
();
writer
.
WriteLine
();
}
}
// Add the namespace around the umbrella class if defined
if
(!
Descriptor
.
CSharpOptions
.
NestClasses
&&
Descriptor
.
CSharpOptions
.
UmbrellaNamespace
!=
""
)
{
writer
.
WriteLine
(
"namespace {0} {{"
,
Descriptor
.
CSharpOptions
.
UmbrellaNamespace
);
writer
.
Indent
();
writer
.
WriteLine
();
}
if
(
Descriptor
.
CSharpOptions
.
CodeContracts
)
{
writer
.
WriteLine
(
"[global::System.Diagnostics.Contracts.ContractVerificationAttribute(false)]"
);
}
...
...
src/ProtocolBuffers.Test/DynamicMessageTest.cs
View file @
64580d09
...
...
@@ -76,7 +76,7 @@ namespace Google.ProtocolBuffers {
public
void
DynamicMessageSettersRejectNull
()
{
IBuilder
builder
=
DynamicMessage
.
CreateBuilder
(
TestAllTypes
.
Descriptor
);
reflectionTester
.
AssertReflectionSettersRejectNull
(
builder
);
}
}
[
Test
]
public
void
DynamicMessageExtensionAccessors
()
{
...
...
@@ -93,7 +93,7 @@ namespace Google.ProtocolBuffers {
public
void
DynamicMessageExtensionSettersRejectNull
()
{
IBuilder
builder
=
DynamicMessage
.
CreateBuilder
(
TestAllExtensions
.
Descriptor
);
extensionsReflectionTester
.
AssertReflectionSettersRejectNull
(
builder
);
}
}
[
Test
]
public
void
DynamicMessageRepeatedSetters
()
{
...
...
src/ProtocolBuffers.sln
View file @
64580d09
...
...
@@ -17,6 +17,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtoBench", "ProtoBench\Pr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtoDump", "ProtoDump\ProtoDump.csproj", "{D7282E99-2DC3-405B-946F-177DB2FD2AE2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "proto", "proto", "{1F896D5C-5FC2-4671-9216-781CB8187EC7}"
ProjectSection(SolutionItems) = preProject
..\protos\tutorial\addressbook.proto = ..\protos\tutorial\addressbook.proto
..\protos\google\protobuf\csharp_options.proto = ..\protos\google\protobuf\csharp_options.proto
..\protos\google\protobuf\descriptor.proto = ..\protos\google\protobuf\descriptor.proto
..\todo.txt = ..\todo.txt
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
...
...
src/ProtocolBuffers/DescriptorProtos/CSharpOptions.cs
View file @
64580d09
This diff is collapsed.
Click to expand it.
src/ProtocolBuffers/Descriptors/FileDescriptor.cs
View file @
64580d09
This diff is collapsed.
Click to expand it.
src/ProtocolBuffers/DynamicMessage.cs
View file @
64580d09
...
...
@@ -335,7 +335,7 @@ namespace Google.ProtocolBuffers {
}
public
override
bool
IsInitialized
{
get
{
return
fields
.
IsInitializedWithRespectTo
(
type
);
}
get
{
return
fields
.
IsInitializedWithRespectTo
(
type
);
}
}
public
override
Builder
MergeFrom
(
CodedInputStream
input
,
ExtensionRegistry
extensionRegistry
)
{
...
...
src/ProtocolBuffers/UnknownField.cs
View file @
64580d09
...
...
@@ -273,7 +273,7 @@ namespace Google.ProtocolBuffers {
if
(
extras
.
Count
==
0
)
{
return
current
;
}
if
(
current
==
null
)
{
if
(
current
==
null
)
{
current
=
new
List
<
T
>(
extras
);
}
else
{
current
.
AddRange
(
extras
);
...
...
@@ -359,7 +359,7 @@ namespace Google.ProtocolBuffers {
/// <returns></returns>
private
static
ReadOnlyCollection
<
T
>
MakeReadOnly
<
T
>(
ref
List
<
T
>
list
)
{
ReadOnlyCollection
<
T
>
ret
=
list
==
null
?
Lists
<
T
>.
Empty
:
new
ReadOnlyCollection
<
T
>(
list
);
list
=
null
;
list
=
null
;
return
ret
;
}
}
...
...
todo.txt
View file @
64580d09
Current task list (not in order)
- Optionally remove dependencies to csharp options
- Remove multifile support
- Docs
- Clean up protogen code
- Add flags to protogen
- Avoid using reflection for messages which don't need it (is this
possible?)
- Bring service generation into line with Java
...
...
@@ -14,7 +12,5 @@ Current task list (not in order)
- Reformat code
- Change generated format
- Add regions to copyright
- Investigate command line parsing library
- 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