Manually generate .sln and .csproj files

Hey,

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.

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.

1 Like

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.

1 Like

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).

Edit

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!

1 Like

ExecuteMenuItem should work, too. I did reflection specifically because I didn’t want it trying to open up Visual Studio after generating the solution.

Hi!

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:

public static class QualityPrepareCommand
    {
        public static void PrepareSonarFiles ()
        {
            Debug.Log("### QualityPrepareCommand:PrepareSonarFiles - Started...");
            // We actually ask Unity to create the CSPROJ and SLN files.
            bool success = EditorApplication.ExecuteMenuItem("Assets/Open C# Project");
            Debug.Log("### QualityPrepareCommand:PrepareSonarFiles - " + (success ? "Done" : "FAILED") + ".");

            // Unsupported Version
            Debug.Log("### QualityPrepareCommand:PrepareSonarFiles - Started V2...");
            System.Type T = System.Type.GetType("UnityEditor.SyncVS,UnityEditor");
            System.Reflection.MethodInfo SyncSolution = T.GetMethod("SyncSolution", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
            SyncSolution.Invoke(null, null);
            Debug.Log("### QualityPrepareCommand:PrepareSonarFiles - Ended V2...");
            // ---
        }
    }

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?