2

我们需要自动测试我们发布的所有 C#、C++ 和 VB.NET 示例是否正确编译。我们需要它来构建所有文件而不列出每个文件。列出每一个意味着如果有人忘记添加一个新的(这将在某天发生),显式调用将错过它。通过遍历所有 .sln 文件,我们总能得到一切。

这样做很容易:

  1. 在干净的 VM 上安装示例(我们在每次测试运行时恢复为快照)。
  2. 创建一个调用所有已安装 .sln 文件的 BuildAll.proj (MSBuild) 文件。
  3. 使用 MSBuild 运行生成的 BuildAll.proj 文件

第 2 步需要一种方法来生成 BuildAll.proj 文件。有没有办法告诉 MSBuild 运行子目录下的所有 .sln 文件或创建调用所有底层 .slnl 文件的 BuildAll.proj ?

4

3 回答 3

4

此 PowerShell 脚本将还原所有 NuGet 包并在当前目录中递归构建所有解决方案。确保 nuget.exe 和 msbuild 在您的 PATH 中。

$baseDir = (Get-Item -Path ".\" -Verbose).FullName
$items = Get-ChildItem -Path $baseDir -Include *.sln -Recurse
foreach ($item in $items){
    nuget restore $item
    msbuild $item
}
于 2018-01-16T14:13:14.073 回答
2

您可以将以下 PowerShell 用于 .NET Core 项目

$baseDir = (Get-Item -Path ".\" -Verbose).FullName
Write-Host ("Scanning *.sln files in " + $baseDir)
$solutionPaths = Get-ChildItem -Path $baseDir -Include *.sln -Recurse
Write-Host ("Total found: " + $solutionPaths.Count)
foreach ($solutionPath in $solutionPaths) {  
    Write-Host ("Building => " + $solutionPath)
    dotnet build $solutionPath
}
于 2020-09-11T17:39:46.753 回答
0

我们找不到任何东西,所以我们编写了一个程序来创建一个 BuildAll.proj,它调用一个目录下的所有 .sln 文件。完整的解决方案在Windward Wrocks(我的博客)。

代码是:

using System; 
using System.IO; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 


namespace BuildDotNetTestScript 
{ 
    /// <summary> 
    /// Builds a test script to compile all .sln files under the directory in. 
    /// </summary> 
    public class Program 
    { 
        private static readonly XNamespace xmlns = "http://schemas.microsoft.com/developer/msbuild/2003"; 


        private enum VS_VER 
        { 
            VS_2005, 
            VS_2008, 
            VS_2010, 
            NONE 
        } 


        private static VS_VER vsVersion = VS_VER.NONE; 


        /// <summary> 
        /// Build TestAll.proj for all .sln files in this directory and sub-directories. 
        /// </summary> 
        /// <param name="args">Optional: [-VS2005 | -VS2008 | -VS2010] TestAll.proj root_folder</param> 
        public static void Main(string[] args) 
        { 


            int indexArgs = 0; 
            if (args.Length >= 1 && args[0][0] == '-') 
            { 
                indexArgs = 1; 
                switch (args[0].ToUpper().Trim()) 
                { 
                    case "-VS2005": 
                        vsVersion = VS_VER.VS_2005; 
                        break; 
                    case "-VS2008": 
                        vsVersion = VS_VER.VS_2008; 
                        break; 
                    case "-VS2010": 
                        vsVersion = VS_VER.VS_2010; 
                        break; 
                    default: 
                        Console.Error.WriteLine("Only options are -VS2005, -VS2008, or -VS2010"); 
                        Environment.Exit(1); 
                        return; 
                } 
            } 


            string projFile = Path.GetFullPath(args.Length > indexArgs ? args[indexArgs] : "TestAll.proj"); 
            string rootDirectory = 
                Path.GetFullPath(args.Length > indexArgs + 1 ? args[indexArgs + 1] : Directory.GetCurrentDirectory()); 
            Console.Out.WriteLine(string.Format("Creating project file {0}", projFile)); 
            Console.Out.WriteLine(string.Format("Root directory {0}", rootDirectory)); 


            XDocument xdoc = new XDocument(); 
            XElement elementProject = new XElement(xmlns + "Project"); 
            xdoc.Add(elementProject); 
            elementProject.Add(new XAttribute("DefaultTargets", "compile")); 
            elementProject.Add(new XAttribute("ToolsVersion", "3.5")); 


            XElement elementPropertyGroup = new XElement(xmlns + "PropertyGroup"); 
            elementProject.Add(elementPropertyGroup); 
            XElement elementDevEnv = new XElement(xmlns + "devenv"); 
            elementPropertyGroup.Add(elementDevEnv); 
            elementDevEnv.Value = "devenv.exe"; 


            XElement elementTarget = new XElement(xmlns + "Target"); 
            elementProject.Add(elementTarget); 
            elementTarget.Add(new XAttribute("Name", "compile")); 


            // add .sln files - recursively 
            AddSlnFiles(elementTarget, rootDirectory, rootDirectory); 


            Console.Out.WriteLine("writing project file to disk"); 
            // no BOM 
            using (var writer = new XmlTextWriter(projFile, new UTF8Encoding(false))) 
            { 
                writer.Formatting = Formatting.Indented; 
                xdoc.Save(writer); 
            } 


            Console.Out.WriteLine("all done"); 
        } 


        private static void AddSlnFiles(XElement elementTarget, string rootDirectory, string folder) 
        { 


            // add .sln files 
            foreach (string fileOn in Directory.GetFiles(folder, "*.sln")) 
            { 
                // .../JS/... is VS2005 
                bool isJSharp = fileOn.ToUpper().Replace('\\', '/').Contains("/JS/"); 
                bool versionMatch = true; 
                switch (vsVersion) 
                { 
                    case VS_VER.VS_2005: 
                        if ((!fileOn.ToUpper().Contains("VS2005")) && (! isJSharp)) 
                            versionMatch = false; 
                        break; 
                    case VS_VER.VS_2008: 
                        if (isJSharp || !fileOn.ToUpper().Contains("VS2008")) 
                            versionMatch = false; 
                        break; 
                    case VS_VER.VS_2010: 
                        if (isJSharp || !fileOn.ToUpper().Contains("VS2010")) 
                            versionMatch = false; 
                        break; 
                    default: 
                        if (isJSharp || fileOn.ToUpper().Contains("VS2005") || fileOn.ToUpper().Contains("VS2008") || fileOn.ToUpper().Contains("VS2010")) 
                            versionMatch = false; 
                        break; 
                } 
                if (!versionMatch) 
                    continue; 


                string command = string.Format("\"$(devenv)\" \"{0}\" /Rebuild", Path.GetFileName(fileOn)); 
                XElement elementExec = new XElement(xmlns + "Exec"); 
                elementExec.Add(new XAttribute("Command", command)); 


                string workingFolder; 
                if (folder.StartsWith(rootDirectory)) 
                { 
                    workingFolder = folder.Substring(rootDirectory.Length).Trim(); 
                    if ((workingFolder.Length > 0) && (workingFolder[0] == Path.DirectorySeparatorChar || workingFolder[0] == Path.AltDirectorySeparatorChar)) 
                        workingFolder = workingFolder.Substring(1); 
                } 
                else 
                    workingFolder = folder; 
                if (workingFolder.Length > 0) 
                    elementExec.Add(new XAttribute("WorkingDirectory", workingFolder)); 
                elementTarget.Add(elementExec); 
            } 


            // look in sub-directories 
            foreach (string subDirectory in Directory.GetDirectories(folder)) 
                AddSlnFiles(elementTarget, rootDirectory, subDirectory); 
        } 
    } 
}
于 2012-12-03T02:41:19.647 回答