Rebooting Force File Before Build

We have a tool that generates some code files (.cs) that are used to build the project.

EDIT: These files are the other half of some partial classes, so the build process requires access to the source. It is impossible, for example, to compile it into a DLL, and then set the build order.

The tool starts at the pre-assembly stage, but the files are updated in the solution only after the assembly, which means that the assembly must be performed twice to clear errors after changing the input.

Example:

  • Edit Instrument Input File
  • Run assembly
    • Tool Runs and Modifies the Source File
  • Build failed
  • Run assembly
    • The tool Launches and modifies the source file (but it does not actually change since the input remains the same)
  • Build succeeds

Any ideas on how we can end the double build and still let our tool run from VS?

Thanks guys!

+4
source share
3 answers

The answer was "Use Cake " and Powershell

0
source

It would be trivial to write a VS macro that would execute your tool and then run the build so that you can complete the whole process with a single keystroke, the faster you do the double build.

Alternatively (or in combination with the above), you can add a custom tool to the VS Tool menu that your tool executes (see Tools-> External Tools). Then just run this custom tool before you build - it's still a tedious double step, but much faster and easier than building twice. (and you can probably leave your tool at the pre-build stage, so the old double-assembly method will work).

Another option may be to edit the MSBuild script for your project, so that Exec is your tool earlier in the build process, so that changes to the file are selected during dependency scanning (so only one pass is needed to build).

None of them is a great solution, but I hope they can give you an advantage that will provide an acceptable improvement over your current situation.

+3
source

reorganize your soln into 2 projects: the first gen cs file, the second uses it (like a dll).

The first project (name it Gen) has 2 events after the build: 1 to run the tool and recreate the source file, and 2) compile the src Gen'ed file for use in the second project:

Gen.exe csc.exe /target:library Gened.cs 

The second project (call Use) refers to the DLL and calls it.

== Gen.cs

 using System.IO; namespace sm3 {class Gen {static string bod = "public static int var = 46;"; static string clas = "public class Gened {" + bod + "}"; static string ns = "namespace sm3 {" + clas + "}"; static void Main(string[] args) {StreamWriter SW; SW = File.CreateText("Gened.cs"); SW.WriteLine(ns); SW.Close(); }}} 

== Use.cs

 using System; namespace sm3 {class Use {static void Main(string[] args) {Gened g = new Gened(); Console.Write(Gened.var.ToString()); Console.ReadLine(); }}} 
+1
source

Source: https://habr.com/ru/post/1305508/


All Articles