Git Product home page Git Product logo

Comments (7)

GoogleCodeExporter avatar GoogleCodeExporter commented on August 19, 2024
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 [email protected] on 1 Aug 2011 at 10:17

from protobuf-csharp-port.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 19, 2024
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

from protobuf-csharp-port.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 19, 2024
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 [email protected] on 5 Aug 2011 at 8:21

from protobuf-csharp-port.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 19, 2024
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 [email protected] on 13 Aug 2011 at 1:20

  • Changed state: Started

from protobuf-csharp-port.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 19, 2024
[deleted comment]

from protobuf-csharp-port.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 19, 2024
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

from protobuf-csharp-port.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 19, 2024
The requested changes have been completed.

Original comment by [email protected] on 13 Aug 2011 at 11:30

  • Changed state: Fixed

from protobuf-csharp-port.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.