示例#1
0
        private static ProjectInformation GetSdkProject(string projectPath)
        {
            var legacyToolsPath = GetToolsPath();
            var sdkToolsPath    = GetSdkBasePath(projectPath);

            var legacyProperties = GetLegacyGlobalProperties(projectPath, legacyToolsPath);
            var globalProperties = GetSdkGlobalProperties(projectPath, sdkToolsPath);

            globalProperties.Add("MSBuildExtensionsPath32", legacyProperties["MSBuildExtensionsPath32"]);

            Environment.SetEnvironmentVariable(
                "MSBuildExtensionsPath",
                globalProperties["MSBuildExtensionsPath"]);
            Environment.SetEnvironmentVariable(
                "MSBuildSDKsPath",
                globalProperties["MSBuildSDKsPath"]);

            var projectCollection = new ProjectCollection(globalProperties);

            projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, legacyToolsPath, projectCollection, string.Empty));
            projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, sdkToolsPath, projectCollection, string.Empty));

            var project = projectCollection.LoadProject(projectPath);

            return(new ProjectInformation(projectCollection, project, false));
        }
示例#2
0
        static void Main(string[] args)
        {
            var path = Path.Combine(WORKSPACE_ROOT, "Tests", "Blazor", "AotSample", "AotSample.csproj");

            var toolsPath        = "/usr/local/share/dotnet/sdk/3.0.100";
            var globalProperties = GetGlobalProperties(path, toolsPath);

            Environment.SetEnvironmentVariable("MSBuildExtensionsPath", globalProperties["MSBuildExtensionsPath"]);
            Environment.SetEnvironmentVariable("MSBuildSDKsPath", globalProperties["MSBuildSDKsPath"]);

            var collection = new ProjectCollection(globalProperties);

            collection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, toolsPath, collection, string.Empty));

            StringBuilder logBuilder = new StringBuilder();
            ConsoleLogger logger     = new ConsoleLogger(LoggerVerbosity.Normal, x => logBuilder.Append(x), null, null);

            collection.RegisterLogger(logger);

            var project  = collection.LoadProject(path);
            var instance = project.CreateProjectInstance();

            Console.Error.WriteLine("LOADED PROJECT.");

            if (!instance.Build("Build", new ILogger[] { logger }))
            {
                Console.WriteLine("BUILD FAILED!");
            }
            else
            {
                Console.WriteLine("BUILD SUCCESS!");
            }

            Console.WriteLine(logBuilder);
        }
示例#3
0
        private void evaluateProjects()
        {
            globalProperties = new Dictionary <String, String>();
            String vsDirectory = Context.Options.VSDirectory;

            if (String.IsNullOrEmpty(vsDirectory))
            {
                vsDirectory = Environment.GetEnvironmentVariable("VSINSTALLDIR");
            }
            if (String.IsNullOrEmpty(vsDirectory))
            {
                throw new ArgumentException("Visual Studio directory is not specified");
            }
            String msBuildDirectory = Context.Options.MSBuildDirectory;

            if (String.IsNullOrEmpty(msBuildDirectory))
            {
                msBuildDirectory = Path.Combine(vsDirectory, "MSBuild");
            }

            globalProperties.Add("VCTargetsPath", Path.Combine(vsDirectory, "Common7", "IDE", "VC", "VCTargets"));
            globalProperties.Add("MSBuildExtensionsPath", msBuildDirectory);

            var toolsVersion = Context.Options.ToolsVersion;

            projectCollection = new ProjectCollection();
            var toolset = new Toolset(toolsVersion, Path.Combine(msBuildDirectory, toolsVersion, "Bin"), projectCollection, string.Empty);

            projectCollection.AddToolset(toolset);

            foreach (var projectEntity in this.Entities <VcProjectEntity>().Where(x => x.Valid))
            {
                projectEntity.EvaluatedProject = evaluateProject(projectEntity.Root);
            }
        }
示例#4
0
        public void SetDefaultToolsVersion()
        {
            string oldValue = Environment.GetEnvironmentVariable("MSBUILDLEGACYDEFAULTTOOLSVERSION");

            try
            {
                // In the new world of figuring out the ToolsVersion to use, we completely ignore the default
                // ToolsVersion in the ProjectCollection.  However, this test explicitly depends on modifying
                // that, so we need to turn the new defaulting behavior off in order to verify that this still works.
                Environment.SetEnvironmentVariable("MSBUILDLEGACYDEFAULTTOOLSVERSION", "1");
                InternalUtilities.RefreshInternalEnvironmentValues();

                ProjectCollection collection = new ProjectCollection();
                collection.AddToolset(new Toolset("x", @"c:\y", collection, null));

                collection.DefaultToolsVersion = "x";

                Assert.Equal("x", collection.DefaultToolsVersion);

                string content = @"
                    <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
                        <Target Name='t'/>
                    </Project>
                ";

                Project project = new Project(XmlReader.Create(new StringReader(content)), null, null, collection);

                Assert.Equal("x", project.ToolsVersion);
            }
            finally
            {
                Environment.SetEnvironmentVariable("MSBUILDLEGACYDEFAULTTOOLSVERSION", oldValue);
                InternalUtilities.RefreshInternalEnvironmentValues();
            }
        }
示例#5
0
        private static Project LoadProject(string projectFile)
        {
            var toolsPath         = EnvironmentUtils.NetCoreRuntimePath;
            var globalProperties  = EnvironmentUtils.GetCoreGlobalProperties(projectFile, toolsPath);
            var projectCollection = new ProjectCollection(globalProperties);

            projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, toolsPath, projectCollection, string.Empty));

            Environment.SetEnvironmentVariable("MSBuildExtensionsPath", EnvironmentUtils.NetCoreRuntimePath);
            Environment.SetEnvironmentVariable("MSBuildSDKsPath", EnvironmentUtils.MSBuildSDKsPath);
            Environment.SetEnvironmentVariable("MSBuildEnableWorkloadResolver", bool.FalseString); // NET 6.0 +

            // TODO: Make properly async
            var fileContents = new StringReader(File.ReadAllText(projectFile)); // read {projectFile} separately in order to avoid locking it on FS
            var xmlReader    = XmlReader.Create(fileContents, XmlSettings);
            var projectRoot  = ProjectRootElement.Create(xmlReader, projectCollection);

            // In order to have it accessible from MSBuild
            projectRoot.FullPath = projectFile;

            var properties = new Dictionary <string, string>()
            {
                // In order not to build the dependent projects
                { "DesignTimeBuild", "true" },
            };

            //Project project = projectCollection.LoadProject(projectPath);
            return(new Project(projectRoot, properties, toolsVersion: null, projectCollection: projectCollection));
        }
示例#6
0
        private static ProjectCollection CreateProjectCollection()
        {
            ProjectCollection projectCollection = new ProjectCollection(globalProperties: null, loggers: null, toolsetDefinitionLocations: ToolsetDefinitionLocations.None);

            projectCollection.AddToolset(new Toolset(MockToolsVersion, Path.GetTempPath(), projectCollection, msbuildOverrideTasksPath: null));

            return(projectCollection);
        }
示例#7
0
        private ProjectCollection CreateProjectCollection()
        {
            ProjectCollection projectCollection = new ProjectCollection(_globalProperties);

            projectCollection.RemoveAllToolsets();  // Make sure we're only using the latest tools
            projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, _buildEnvironment.GetToolsPath(), projectCollection, string.Empty));
            projectCollection.DefaultToolsVersion = ToolLocationHelper.CurrentToolsVersion;
            return(projectCollection);
        }
        internal static ProjectCollection CreateProjectCollection()
        {
            string toolsPath         = VisualStudioSolutionUtility.GetMSBuildPath();
            var    globalProperties  = GetGlobalProperties(VisualStudioSolutionUtility.GetMSBuildPath());
            var    projectCollection = new ProjectCollection(globalProperties);

            // change toolset to internal.
            projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, toolsPath, projectCollection, string.Empty));
            return(projectCollection);
        }
示例#9
0
        private ProjectCollection CreateProjectCollection()
        {
            var projectCollection = new ProjectCollection(_globalProperties);

            projectCollection.RemoveAllToolsets();  // Make sure we're only using the latest tools
            projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, _dotnetSdkPaths.ToolsPath, projectCollection, String.Empty));
            projectCollection.DefaultToolsVersion = ToolLocationHelper.CurrentToolsVersion;

            return(projectCollection);
        }
        public static Project GetProject(string projectPath, ConsoleLogger logger)
        {
            string toolsPath = GetToolsPath();
            Dictionary <string, string> globalProperties  = GetGlobalProperties(projectPath, toolsPath);
            ProjectCollection           projectCollection = new ProjectCollection(globalProperties);

            projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, toolsPath, projectCollection, string.Empty));
            projectCollection.RegisterLogger(logger);
            return(projectCollection.LoadProject(projectPath));
        }
示例#11
0
        /// <summary>
        ///     Create an MSBuild project collection.
        /// </summary>
        /// <param name="solutionDirectory">
        ///     The base (i.e. solution) directory.
        /// </param>
        /// <param name="runtimeInfo">
        ///     Information about the current .NET Core runtime.
        /// </param>
        /// <param name="globalPropertyOverrides">
        ///     An optional dictionary containing property values to override.
        /// </param>
        /// <returns>
        ///     The project collection.
        /// </returns>
        public static ProjectCollection CreateProjectCollection(string solutionDirectory, DotNetRuntimeInfo runtimeInfo, Dictionary <string, string> globalPropertyOverrides = null)
        {
            if (String.IsNullOrWhiteSpace(solutionDirectory))
            {
                throw new ArgumentException("Argument cannot be null, empty, or entirely composed of whitespace: 'baseDir'.", nameof(solutionDirectory));
            }

            if (runtimeInfo == null)
            {
                throw new ArgumentNullException(nameof(runtimeInfo));
            }

            if (String.IsNullOrWhiteSpace(runtimeInfo.BaseDirectory))
            {
                throw new InvalidOperationException("Cannot determine base directory for .NET Core (check the output of 'dotnet --info').");
            }

            Dictionary <string, string> globalProperties = CreateGlobalMSBuildProperties(runtimeInfo, solutionDirectory, globalPropertyOverrides);

            EnsureMSBuildEnvironment(globalProperties);

            ProjectCollection projectCollection = new ProjectCollection(globalProperties)
            {
                IsBuildEnabled = false
            };

            SemanticVersion netcoreVersion;

            if (!SemanticVersion.TryParse(runtimeInfo.Version, out netcoreVersion))
            {
                throw new FormatException($"Cannot parse .NET Core version '{runtimeInfo.Version}' (does not appear to be a valid semantic version).");
            }

            // For .NET Core 3.0 and newer, toolset version is simply "Current" instead of "15.0" (tintoy/msbuild-project-tools-vscode#46).
            string toolsVersion = netcoreVersion.Major < 3 ? "15.0" : "Current";

            // Override toolset paths (for some reason these point to the main directory where the dotnet executable lives).
            Toolset toolset = projectCollection.GetToolset(toolsVersion);

            toolset = new Toolset(toolsVersion,
                                  toolsPath: runtimeInfo.BaseDirectory,
                                  projectCollection: projectCollection,
                                  msbuildOverrideTasksPath: ""
                                  );

            // Other toolset versions won't be supported by the .NET Core SDK
            projectCollection.RemoveAllToolsets();

            // TODO: Add configuration setting that enables user to configure custom toolsets.

            projectCollection.AddToolset(toolset);
            projectCollection.DefaultToolsVersion = toolsVersion;

            return(projectCollection);
        }
示例#12
0
        private static ProjectInformation GetLegacyProject(string projectPath)
        {
            var legacyToolsPath = GetToolsPath();

            var globalProperties  = GetLegacyGlobalProperties(projectPath, legacyToolsPath);
            var projectCollection = new ProjectCollection(globalProperties);

            projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, legacyToolsPath, projectCollection, string.Empty));

            var project = projectCollection.LoadProject(projectPath);

            return(new ProjectInformation(projectCollection, project, true));
        }
        private ProjectCollection CreateProjectCollection()
        {
            ProjectCollection projectCollection = new ProjectCollection(_globalProperties);

            projectCollection.RemoveAllToolsets();  // Make sure we're only using the latest tools
            projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, _pathHelper.ToolsPath, projectCollection, string.Empty));
            projectCollection.DefaultToolsVersion = ToolLocationHelper.CurrentToolsVersion;
            if (_logger != null)
            {
                projectCollection.RegisterLogger(_logger);
            }
            return(projectCollection);
        }
        public static Project GetCoreProject(string projectPath)
        {
            string toolsPath = GetCoreBasePath(projectPath);
            Dictionary <string, string> globalProperties  = GetCoreGlobalProperties(projectPath, toolsPath);
            ProjectCollection           projectCollection = new ProjectCollection(globalProperties);

            projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, toolsPath, projectCollection, string.Empty));

            Environment.SetEnvironmentVariable("MSBuildExtensionsPath", globalProperties["MSBuildExtensionsPath"]);
            Environment.SetEnvironmentVariable("MSBuildSDKsPath", globalProperties["MSBuildSDKsPath"]);

            Project project = projectCollection.LoadProject(projectPath);

            return(project);
        }
示例#15
0
        /// <summary>
        /// Creates a standard ProjectCollection and adds a fake toolset with the following contents to it:
        ///
        /// ToolsVersion = Fake
        /// Base Properties:
        /// a = a1
        /// b = b1
        ///
        /// SubToolset "12.0":
        /// d = d4
        /// e = e5
        ///
        /// SubToolset "v11.0":
        /// b = b2
        /// c = c2
        ///
        /// SubToolset "FakeSubToolset":
        /// a = a3
        /// c = c3
        ///
        /// SubToolset "v13.0":
        /// f = f6
        /// g = g7
        /// </summary>
        private Toolset GetFakeToolset(IDictionary <string, string> globalPropertiesForProjectCollection)
        {
            ProjectCollection projectCollection = new ProjectCollection(globalPropertiesForProjectCollection);

            IDictionary <string, string> properties = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            properties.Add("a", "a1");
            properties.Add("b", "b1");

            Dictionary <string, SubToolset> subToolsets = new Dictionary <string, SubToolset>(StringComparer.OrdinalIgnoreCase);

            // SubToolset 12.0 properties
            PropertyDictionary <ProjectPropertyInstance> subToolset12Properties = new PropertyDictionary <ProjectPropertyInstance>();

            subToolset12Properties.Set(ProjectPropertyInstance.Create("d", "d4"));
            subToolset12Properties.Set(ProjectPropertyInstance.Create("e", "e5"));

            // SubToolset v11.0 properties
            PropertyDictionary <ProjectPropertyInstance> subToolset11Properties = new PropertyDictionary <ProjectPropertyInstance>();

            subToolset11Properties.Set(ProjectPropertyInstance.Create("b", "b2"));
            subToolset11Properties.Set(ProjectPropertyInstance.Create("c", "c2"));

            // FakeSubToolset properties
            PropertyDictionary <ProjectPropertyInstance> fakeSubToolsetProperties = new PropertyDictionary <ProjectPropertyInstance>();

            fakeSubToolsetProperties.Set(ProjectPropertyInstance.Create("a", "a3"));
            fakeSubToolsetProperties.Set(ProjectPropertyInstance.Create("c", "c3"));

            // SubToolset v13.0 properties
            PropertyDictionary <ProjectPropertyInstance> subToolset13Properties = new PropertyDictionary <ProjectPropertyInstance>();

            subToolset13Properties.Set(ProjectPropertyInstance.Create("f", "f6"));
            subToolset13Properties.Set(ProjectPropertyInstance.Create("g", "g7"));

            subToolsets.Add("12.0", new SubToolset("12.0", subToolset12Properties));
            subToolsets.Add("v11.0", new SubToolset("v11.0", subToolset11Properties));
            subToolsets.Add("FakeSubToolset", new SubToolset("FakeSubToolset", fakeSubToolsetProperties));
            subToolsets.Add("v13.0", new SubToolset("v13.0", subToolset13Properties));

            Toolset parentToolset = projectCollection.GetToolset("4.0");

            Toolset fakeToolset = new Toolset("Fake", parentToolset.ToolsPath, properties, projectCollection, subToolsets, parentToolset.OverrideTasksPath);

            projectCollection.AddToolset(fakeToolset);

            return(fakeToolset);
        }
示例#16
0
        /// <summary>
        /// Creates a toolset with the given tools version if one does not already exist.
        /// </summary>
        private static void CreateMockToolsetIfNotExists(string toolsVersion, ProjectCollection projectCollection)
        {
            ProjectCollection pc = projectCollection;

            if (!pc.Toolsets.Any(t => String.Equals(t.ToolsVersion, toolsVersion, StringComparison.OrdinalIgnoreCase)))
            {
                Toolset template = pc.Toolsets.First(t => String.Equals(t.ToolsVersion, pc.DefaultToolsVersion, StringComparison.OrdinalIgnoreCase));
                var     toolset  = new Toolset(
                    toolsVersion,
                    template.ToolsPath,
                    template.Properties.ToDictionary(p => p.Key, p => p.Value.EvaluatedValue),
                    pc,
                    null);
                pc.AddToolset(toolset);
            }
        }
示例#17
0
        internal static ProjectCollection CreateProjectCollection()
        {
            //!!!!
            //var ss = ToolLocationHelper.GetPathToBuildToolsFile( "msbuild.exe", ToolLocationHelper.CurrentToolsVersion );
            //Log.Info( ss );

            string toolsPath         = VisualStudioSolutionUtility.GetMSBuildFolderPath();
            var    globalProperties  = GetGlobalProperties(VisualStudioSolutionUtility.GetMSBuildFolderPath());
            var    projectCollection = new ProjectCollection(globalProperties);

            // change toolset to internal.

            //var toolsPath2 = ToolLocationHelper.GetPathToBuildTools( ToolLocationHelper.CurrentToolsVersion );
            //Log.Info( toolsPath2 );

            //!!!!
            ////projectCollection.AddToolset( new Toolset( "3.1.302", toolsPath, projectCollection, string.Empty ) );
            projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, toolsPath, projectCollection, string.Empty));
            return(projectCollection);
        }
示例#18
0
        public void ProjectInstance_ToolsPath_Restore_Build_Target(string projectName)
        {
            var toolsPath        = GetToolsPath();
            var projectPath      = GetProjectFullPath(projectName);
            var globalProperties = GetGlobalProperties(projectPath, toolsPath);

            globalProperties.Add("Configuration", "Release");
            globalProperties.Add("Platform", "AnyCPU");

            var projectCollection = new ProjectCollection(globalProperties);

            projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, toolsPath, projectCollection, string.Empty));

            _ = new Copy();

            var projectInstance = projectCollection.LoadProject(GetProjectFullPath(projectName)).CreateProjectInstance();
            var result          = projectInstance.Build(new[] { "Restore", "Build" }, new ILogger[] { CreateLogger() });

            Assert.True(result);
            Debug.WriteLine(_logBuilder);
        }
示例#19
0
        /// <summary>
        ///     Create an MSBuild project collection.
        /// </summary>
        /// <param name="solutionDirectory">
        ///     The base (i.e. solution) directory.
        /// </param>
        /// <param name="runtimeInfo">
        ///     Information about the current .NET Core runtime.
        /// </param>
        /// <returns>
        ///     The project collection.
        /// </returns>
        public static ProjectCollection CreateProjectCollection(string solutionDirectory, DotNetRuntimeInfo runtimeInfo)
        {
            if (String.IsNullOrWhiteSpace(solutionDirectory))
            {
                throw new ArgumentException("Argument cannot be null, empty, or entirely composed of whitespace: 'baseDir'.", nameof(solutionDirectory));
            }

            if (runtimeInfo == null)
            {
                throw new ArgumentNullException(nameof(runtimeInfo));
            }

            if (String.IsNullOrWhiteSpace(runtimeInfo.BaseDirectory))
            {
                throw new InvalidOperationException("Cannot determine base directory for .NET Core.");
            }

            Dictionary <string, string> globalProperties = CreateGlobalMSBuildProperties(runtimeInfo, solutionDirectory);

            EnsureMSBuildEnvironment(globalProperties);

            ProjectCollection projectCollection = new ProjectCollection(globalProperties)
            {
                IsBuildEnabled = false
            };

            // Override toolset paths (for some reason these point to the main directory where the dotnet executable lives).
            Toolset toolset = projectCollection.GetToolset("15.0");

            toolset = new Toolset(
                toolsVersion: "15.0",
                toolsPath: globalProperties["MSBuildExtensionsPath"],
                projectCollection: projectCollection,
                msbuildOverrideTasksPath: ""
                );
            projectCollection.AddToolset(toolset);

            return(projectCollection);
        }