Search code examples
c#.net.net-coremsbuildmsbuild-task

Command line tool or dotnet tool to remove unused using in compile time


Is there any way to remove unused using statement in compile time? it can be any cli tool or dotnet tool.

The reason is follwing: We have code generator, and this code generator generates a lot of using, but in some cases the usings are need, and some not. But at generation time there is no way to decide which using is used and which is not used in later code.

So it wolud be great when we have tool to remove the unused using statements. The editor command are not working of course, beacuse when we rebuild the project then the generated files will be newly generated. Therefore the mechasnim must be run at compile time, after the generator has done.

I checked the dotnet-format tool, but there is no option for this.

EDIT 1: There is a simple sample for imput file, which is the input of the generator.

import valuedto.dml
import valuemodel.dml

service: SampleService
{
    controller: ValueController
    {
        public:
        {
            PublicValueDTO GetValue( string id );
        }
        public:
        {
            PrivateValueModel GetValuePrivate( string id );
        }
    }
}

As you can see the service file imports two different model files. The one dml-file contains the public dto-s the other contains the private models.

The generator uses this as imput, and generates a lots of code:

  • clinet interface
  • httmp clinet implenetation
  • controller implementation
  • client dll
  • etc...

The one of the generators are just collect the public functions, and create a assembly for 3rd-party clients, to use the public interface. But in this casae very hard to decide which imports must be use. In real world, there is mor import, the import is imported are other import recursively.

If the the public interface contains using-s from private models, the generated code is not compilable



Please concentrate the main question in your answer, not the actual implementations. Beacuse it just a very very small sample, and not a real one (missing versions, permissions, route, verb, multipart, etc...)




Solution

  • The free jetbrains cli tool can do code cleanups. Removing unused using directives is one of the things it can do.

    https://www.jetbrains.com/help/resharper/ReSharper_Command_Line_Tools.html#install-and-use-resharper-command-line-tools-as-net-core-tools