Closed GoogleCodeExporter closed 9 years ago
Here's a naff workaround I concocted (I have this in the .csproj file for my
project):
<ItemGroup>
<ProtoFiles Include="Protos\*.proto" />
</ItemGroup>
<UsingTask TaskName="FixupGeneratedFiles" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO"/>
<Using Namespace="System.Text"/>
<Using Namespace="System.Text.RegularExpressions"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
var compilerGeneratedText = "[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute]";
foreach (var file in Files)
{
var text = File.ReadAllText(file.ItemSpec);
var sb = new StringBuilder(text);
var offset = 0;
var matches = Regex.Matches(text, @"(public|internal).*class");
foreach (Match match in matches)
{
sb.Insert(match.Index + offset, compilerGeneratedText);
offset += compilerGeneratedText.Length;
}
sb.Insert(0, "#pragma warning disable");
sb.AppendLine();
sb.AppendLine("#pragma warning restore");
sb.Insert(0, "// <auto-generated />" + Environment.NewLine);
File.WriteAllText(file.ItemSpec, sb.ToString());
}
]]>
</Code>
</Task>
</UsingTask>
<Target Name="BeforeBuild">
<CallTarget Targets="ProtoCompile;ProtoGenerate" />
</Target>
<Target Name="ProtoCompile" Inputs="@(ProtoFiles)" Outputs="Protos\PF.protobin">
<Message Text="Compiling .proto files" Importance="Low" />
<Exec Command=""..\..\Lib\protobuf-csharp\protoc.exe" -o Protos\PF.protobin --proto_path=Protos @(ProtoFiles -> 'Protos\%(Filename)%(Extension)', ' ')" />
</Target>
<Target Name="ProtoGenerate">
<Message Text="Generating C# code from compiled .protos" Importance="Low" />
<Exec Command=""..\..\Lib\protobuf-csharp\protogen.exe" Protos\PF.protobin -namespace=Xxx.Protos -output_directory=Protos" />
<ItemGroup>
<GeneratedFiles Include="Protos\*.cs" />
</ItemGroup>
<FixupGeneratedFiles Files="@(GeneratedFiles)"/>
</Target>
Original comment by kent.boo...@gmail.com
on 1 Aug 2011 at 10:17
1. Can you give details of the warnings generated? I'd rather not suppress
warnings, as it may indicate a problem with the generated code. We could make
it an option if really necessary though.
2. I'm not sure about [CompilerGenerated] - are there any other attributes
which would make sense? I can't remember offhand what the *designer* generated
code ends up like...
3. Sounds good to me. Right at the very top of the file?
Original comment by jonathan.skeet
on 4 Aug 2011 at 7:58
Thanks for the reply.
1. If you have XML documentation enabled for your project, you get warnings
about missing API doc comments. That's the only one I've found thus far, so
maybe you could just disable those specific warnings (#1591).
2. Good point - I hadn't considered the fact that it's a partial class so not
all of it may be generated. CompilerGeneratedAttribute can target anything, so
you could generate this for every generated *member* instead.
The Winforms designer just wraps generated code in a region, but this doesn't
(by default anyway) help code analysis to detect that it's generated code. If I
stick a [CompilerGenerated] attribute on the InitializeComponent method, CA
ignores it as expected. Sounds like people generally modify the template to
include this attribute (see
http://stackoverflow.com/questions/2768764/attribute-missing-from-generated-winf
orm-code).
3. Yes, the very top.
On a side note, I really wish all relevant tooling would respect the one way of
marking code as generated!
Original comment by kent.boo...@gmail.com
on 5 Aug 2011 at 8:21
Implemented some of the suggestions:
1. Added #pragma warning disable 1591
2. The GeneratedCodeAttribute, CompilerGeneratedAttribute, and
NonUserCodeAttribute have already been added.
3. Added code region for "Designer generated code". Resharper respects this
and disables warnings. I did not add the suggested comment "// <auto-generated
/>" as I can find no reference to it's use online. If the code region is not
working in a specific tool, and you can provide the product documentation link
about this comment we will get it integrated.
Pending review:
http://code.google.com/p/protobuf-csharp-port/source/list?name=issue-16
Original comment by Grig...@gmail.com
on 13 Aug 2011 at 1:20
[deleted comment]
This mentioned <auto-generated>:
http://shishkin.wordpress.com/2008/07/08/stylecop-how-to-ignore-generated-code/
It says that the region change should fix it too though, so let's see whether
that works and revisit it if necessary.
Original comment by jonathan.skeet
on 13 Aug 2011 at 6:28
The requested changes have been completed.
Original comment by Grig...@gmail.com
on 13 Aug 2011 at 11:30
Original issue reported on code.google.com by
kent.boo...@gmail.com
on 1 Aug 2011 at 9:52