如何在目录下构建所有解决方案(.SLN)文件

时间:2012-12-03 02:41:20

标签: build msbuild subdirectory

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

这样做非常简单:

  1. 将样本安装在干净的VM上(我们将恢复为每次测试运行的快照)。
  2. 创建一个BuildAll.proj(MSBuild)文件,该文件调用所有已安装的.sln文件。
  3. 使用MSBuild运行生成的BuildAll.proj文件
  4. 步骤2需要一种方法来生成BuildAll.proj文件。有没有办法告诉MSBuild在子目录下运行所有​​.sln文件或创建一个调用所有底层.slnl文件的BuildAll.proj?

3 个答案:

答案 0 :(得分:2)

此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
}

答案 1 :(得分: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); 
        } 
    } 
}

答案 2 :(得分:0)

您可以将以下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
}