Search Unity

Manually generate .sln and .csproj files

Discussion in 'Editor & General Support' started by liortal, Dec 11, 2016.

  1. liortal


    Oct 17, 2012

    I'd like to know if there's any way I could manually generate the .sln file and matching .csproj projects for our project ?

    I'd like to set up a continuous integration process that will run static code analysis for our code. This does not necessarily have to run on a machine with Unity installed.

    Since we are not committing our solution to source control (it is automatically generated and updated), there's no easy way to execute analysis, which requires the path to the .sln file.

    I was wondering if there is a simple way to achieve this.
  2. tylerw-savatronix


    Nov 10, 2013
    I know this is old, but I think warrants some necromancy since I need the same thing (but for DocFX reference documentation, which requires csproj files. Build server doesn't have those as they aren't checked in and it doesn't generate them since the editor is never opened on the server).

    Googling has turned up nothing for having them generated from the command line.
  3. nickgravelyn


    Jan 22, 2009
    I also wanted this for using Sublime text as an editor with Omnisharp and found a way to do it with Reflection: SyncSolution.cs.

    It's possible it'll break between releases of Unity but it works as far as I've tested so far. You can use the -executeMethod command line argument to run the static method from a commandline, but it does require the Unity editor.
    KapiteinPannekoek likes this.
  4. tylerw-savatronix


    Nov 10, 2013
    It looks like using executeMethod I can just call

    EditorApplication.ExecuteMenuItem("Assets/Open C# Project");

    from within the method created for executeMethod and it should work (if there's no VS/MonoDevelop installed on the server I think it should just generate the solution files. I'm curious to see if it'll ExecuteMenuItem will return false under these conditions).


    No dice. Time to try it with with reflection (thanks in advance nick)

    *Edit Again*

    My TeamCity blade was configuring the command arguments incorrectly so -executeMethod was never being called. Both versions work though!
    Last edited: May 12, 2017
    mkusan likes this.
  5. nickgravelyn


    Jan 22, 2009
    ExecuteMenuItem should work, too. I did reflection specifically because I didn't want it trying to open up Visual Studio after generating the solution.
  6. Anoril


    Sep 24, 2013

    I want to do the Sln/CSProj files generation from command line and I made up a script that use both reflection and generic ExecuteMenuItem way with no success.

    The call returns "true" each time, but no SLN nor CSPROJ files are produced.

    I note that when I open the project first with Unity, then call my script, it works well. When the script is called from a GIT pull, it does not work (but still replies "true").

    Anyone can help?

    My command line:
    "d:\Program Files\Unity\2018.4.9f1\Editor\Unity.exe" -batchmode -quit -nographics -logFile "runner.log" -executeMethod QualityPrepareCommand.PrepareSonarFiles

    The script code:
    Code (CSharp):
    1. public static class QualityPrepareCommand
    2.     {
    3.         public static void PrepareSonarFiles ()
    4.         {
    5.             Debug.Log("### QualityPrepareCommand:PrepareSonarFiles - Started...");
    6.             // We actually ask Unity to create the CSPROJ and SLN files.
    7.             bool success = EditorApplication.ExecuteMenuItem("Assets/Open C# Project");
    8.             Debug.Log("### QualityPrepareCommand:PrepareSonarFiles - " + (success ? "Done" : "FAILED") + ".");
    10.             // Unsupported Version
    11.             Debug.Log("### QualityPrepareCommand:PrepareSonarFiles - Started V2...");
    12.             System.Type T = System.Type.GetType("UnityEditor.SyncVS,UnityEditor");
    13.             System.Reflection.MethodInfo SyncSolution = T.GetMethod("SyncSolution", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
    14.             SyncSolution.Invoke(null, null);
    15.             Debug.Log("### QualityPrepareCommand:PrepareSonarFiles - Ended V2...");
    16.             // ---
    17.         }
    18.     }
    Note 2: The GIT repository only contains Assets, Logs, Packages and ProjectSettings folders. Maybe I should store more thatn that on GIT? Like Library or other floders?
    Last edited: Dec 2, 2019