Exemplo n.º 1
0
 /// <summary>
 ///     Create an MSBuild project collection.
 /// </summary>
 /// <param name="solutionDirectory">
 ///     The base (i.e. solution) directory.
 /// </param>
 /// <param name="globalPropertyOverrides">
 ///     An optional dictionary containing property values to override.
 /// </param>
 /// <returns>
 ///     The project collection.
 /// </returns>
 public static ProjectCollection CreateProjectCollection(string solutionDirectory, Dictionary <string, string> globalPropertyOverrides = null)
 {
     return(CreateProjectCollection(solutionDirectory,
                                    DotNetRuntimeInfo.GetCurrent(solutionDirectory),
                                    globalPropertyOverrides
                                    ));
 }
Exemplo n.º 2
0
        /// <summary>
        ///     Create an MSBuild project collection.
        /// </summary>
        /// <param name="solutionDirectory">
        ///     The base (i.e. solution) directory.
        /// </param>
        /// <param name="globalPropertyOverrides">
        ///     An optional dictionary containing property values to override.
        /// </param>
        /// <param name="logger">
        ///     An optional <see cref="ILogger"/> to use for diagnostic purposes (if not specified, the static <see cref="Log.Logger"/> will be used).
        /// </param>
        /// <returns>
        ///     The project collection.
        /// </returns>
        public static ProjectCollection CreateProjectCollection(string solutionDirectory, Dictionary <string, string> globalPropertyOverrides = null, ILogger logger = null)
        {
            if (logger == null)
            {
                logger = Log.Logger;
            }

            return(CreateProjectCollection(solutionDirectory,
                                           DotNetRuntimeInfo.GetCurrent(solutionDirectory, logger),
                                           globalPropertyOverrides
                                           ));
        }
Exemplo n.º 3
0
 /// <summary>
 ///     Create an MSBuild project collection.
 /// </summary>
 /// <param name="solutionDirectory">
 ///     The base (i.e. solution) directory.
 /// </param>
 /// <returns>
 ///     The project collection.
 /// </returns>
 public static ProjectCollection CreateProjectCollection(string solutionDirectory)
 {
     return(CreateProjectCollection(solutionDirectory,
                                    DotNetRuntimeInfo.GetCurrent(solutionDirectory)
                                    ));
 }
        /// <summary>
        ///     Find and use the latest version of the MSBuild engine compatible with the current SDK.
        /// </summary>
        /// <param name="baseDirectory">
        ///     An optional base directory where dotnet.exe should be run (this may affect the version it reports due to global.json).
        /// </param>
        /// <param name="logger">
        ///     An optional <see cref="ILogger"/> to use for diagnostic purposes (if not specified, the static <see cref="Log.Logger"/> will be used).
        /// </param>
        public static void DiscoverMSBuildEngine(string baseDirectory = null, ILogger logger = null)
        {
            if (MSBuildLocator.IsRegistered)
            {
                MSBuildLocator.Unregister();
            }

            _registeredMSBuildInstance = null;

            // Assume working directory is VS code's current working directory (i.e. the workspace root).
            //
            // Really, until we figure out a way to change the version of MSBuild we're using after the server has started,
            // we're still going to have problems here.
            //
            // In the end we will probably wind up having to move all the MSBuild stuff out to a separate process, and use something like GRPC (or even Akka.NET's remoting) to communicate with it.
            // It can be stopped and restarted by the language server (even having different instances for different SDK / MSBuild versions).
            //
            // This will also ensure that the language server's model doesn't expose any MSBuild objects anywhere.
            //
            // For now, though, let's choose the dumb option.
            DotNetRuntimeInfo runtimeInfo = DotNetRuntimeInfo.GetCurrent(baseDirectory, logger);

            // SDK versions are in SemVer format...
            SemanticVersion targetSdkSemanticVersion;

            if (!SemanticVersion.TryParse(runtimeInfo.SdkVersion, out targetSdkSemanticVersion))
            {
                throw new Exception($"Cannot determine SDK version information for current .NET SDK (located at '{runtimeInfo.BaseDirectory}').");
            }

            // ...which MSBuildLocator does not understand.
            Version targetSdkVersion = new Version(
                major: targetSdkSemanticVersion.Major,
                minor: targetSdkSemanticVersion.Minor,
                build: targetSdkSemanticVersion.Patch
                );

            var queryOptions = new VisualStudioInstanceQueryOptions
            {
                // We can only load the .NET Core MSBuild engine
                DiscoveryTypes = DiscoveryType.DotNetSdk
            };

            VisualStudioInstance[] allInstances = MSBuildLocator
                                                  .QueryVisualStudioInstances(queryOptions)
                                                  .ToArray();

            VisualStudioInstance latestInstance = allInstances
                                                  .OrderByDescending(instance => instance.Version)
                                                  .FirstOrDefault(instance =>
                                                                  // We need a version of MSBuild for the currently-supported SDK
                                                                  instance.Version == targetSdkVersion
                                                                  );

            if (latestInstance == null)
            {
                string foundVersions = String.Join(", ", allInstances.Select(instance => instance.Version));

                throw new Exception($"Cannot locate MSBuild engine for .NET SDK v{targetSdkVersion}. This probably means that MSBuild Project Tools cannot find the MSBuild for the current project instance. It did find the following version(s), though: [{foundVersions}].");
            }

            MSBuildLocator.RegisterInstance(latestInstance);

            _registeredMSBuildInstance = latestInstance;
        }