public BuildScript Analyse(Autobuilder builder) { builder.Log(Severity.Info, "Attempting to build using .NET Core"); var projects = builder.SolutionsToBuild.Any() ? builder.SolutionsToBuild.SelectMany(s => s.Projects).ToArray() : builder.GetExtensions(Language.CSharp.ProjectExtension).Select(p => new Project(builder, p)).ToArray(); var notDotNetProject = projects.FirstOrDefault(p => !p.DotNetProject); if (notDotNetProject != null) { builder.Log(Severity.Info, "Not using .NET Core because of incompatible project {0}", notDotNetProject); return(BuildScript.Failure); } if (!builder.SolutionsToBuild.Any()) { // Attempt dotnet build in root folder return(WithDotNet(builder, dotNet => { var info = GetInfoCommand(builder.Actions, dotNet); var clean = GetCleanCommand(builder.Actions, dotNet).Script; var restore = GetRestoreCommand(builder.Actions, dotNet).Script; var build = GetBuildCommand(builder, dotNet).Script; return info & clean & BuildScript.Try(restore) & build; })); } // Attempt dotnet build on each solution return(WithDotNet(builder, dotNet => { var ret = GetInfoCommand(builder.Actions, dotNet); foreach (var solution in builder.SolutionsToBuild) { var cleanCommand = GetCleanCommand(builder.Actions, dotNet); cleanCommand.QuoteArgument(solution.Path); var clean = cleanCommand.Script; var restoreCommand = GetRestoreCommand(builder.Actions, dotNet); restoreCommand.QuoteArgument(solution.Path); var restore = restoreCommand.Script; var buildCommand = GetBuildCommand(builder, dotNet); buildCommand.QuoteArgument(solution.Path); var build = buildCommand.Script; ret &= clean & BuildScript.Try(restore) & build; } return ret; })); }
public BuildScript Analyse(Autobuilder builder, bool auto) { if (!builder.ProjectsOrSolutionsToBuild.Any()) { return(BuildScript.Failure); } if (auto) { var notDotNetProject = builder.ProjectsOrSolutionsToBuild. SelectMany(p => Enumerators.Singleton(p).Concat(p.IncludedProjects)). OfType <Project>(). FirstOrDefault(p => !p.DotNetProject); if (notDotNetProject != null) { builder.Log(Severity.Info, "Not using .NET Core because of incompatible project {0}", notDotNetProject); return(BuildScript.Failure); } builder.Log(Severity.Info, "Attempting to build using .NET Core"); } return(WithDotNet(builder, dotNet => { var ret = GetInfoCommand(builder.Actions, dotNet); foreach (var projectOrSolution in builder.ProjectsOrSolutionsToBuild) { var cleanCommand = GetCleanCommand(builder.Actions, dotNet); cleanCommand.QuoteArgument(projectOrSolution.FullPath); var clean = cleanCommand.Script; var restoreCommand = GetRestoreCommand(builder.Actions, dotNet); restoreCommand.QuoteArgument(projectOrSolution.FullPath); var restore = restoreCommand.Script; var buildCommand = GetBuildCommand(builder, dotNet); buildCommand.QuoteArgument(projectOrSolution.FullPath); var build = buildCommand.Script; ret &= clean & BuildScript.Try(restore) & build; } return ret; })); }
public BuildScript Analyse(Autobuilder builder, bool auto) { builder.Log(Severity.Info, "Attempting to locate build script"); var extensions = builder.Actions.IsWindows() ? winExtensions : linuxExtensions; var scripts = buildScripts.SelectMany(s => extensions.Select(e => s + e)); var scriptPath = builder.Paths.Where(p => scripts.Any(p.Item1.ToLower().EndsWith)).OrderBy(p => p.Item2).Select(p => p.Item1).FirstOrDefault(); if (scriptPath == null) { return(BuildScript.Failure); } var chmod = new CommandBuilder(builder.Actions); chmod.RunCommand("/bin/chmod", $"u+x {scriptPath}"); var chmodScript = builder.Actions.IsWindows() ? BuildScript.Success : BuildScript.Try(chmod.Script); var dir = Path.GetDirectoryName(scriptPath); // A specific .NET Core version may be required return(chmodScript & DotNetRule.WithDotNet(builder, dotNet => { var command = new CommandBuilder(builder.Actions, dir, dotNet?.Environment); // A specific Visual Studio version may be required var vsTools = MsBuildRule.GetVcVarsBatFile(builder); if (vsTools != null) { command.CallBatFile(vsTools.Path); } command.IndexCommand(builder.Odasa, scriptPath); return command.Script; })); }
public BuildScript Analyse(Autobuilder builder, bool auto) { if (!builder.ProjectsOrSolutionsToBuild.Any()) { return(BuildScript.Failure); } if (auto) { builder.Log(Severity.Info, "Attempting to build using MSBuild"); } var vsTools = GetVcVarsBatFile(builder); if (vsTools == null && builder.ProjectsOrSolutionsToBuild.Any()) { var firstSolution = builder.ProjectsOrSolutionsToBuild.OfType <ISolution>().FirstOrDefault(); vsTools = firstSolution != null ? BuildTools.FindCompatibleVcVars(builder.Actions, firstSolution) : BuildTools.VcVarsAllBatFiles(builder.Actions).OrderByDescending(b => b.ToolsVersion).FirstOrDefault(); } if (vsTools == null && builder.Actions.IsWindows()) { builder.Log(Severity.Warning, "Could not find a suitable version of vcvarsall.bat"); } var nuget = builder.SemmlePlatformTools != null? builder.Actions.PathCombine(builder.SemmlePlatformTools, "csharp", "nuget", "nuget.exe") : "nuget"; var ret = BuildScript.Success; foreach (var projectOrSolution in builder.ProjectsOrSolutionsToBuild) { if (builder.Options.NugetRestore) { var nugetCommand = new CommandBuilder(builder.Actions). RunCommand(nuget). Argument("restore"). QuoteArgument(projectOrSolution.FullPath); ret &= BuildScript.Try(nugetCommand.Script); } var command = new CommandBuilder(builder.Actions); if (vsTools != null) { command.CallBatFile(vsTools.Path); // `vcvarsall.bat` sets a default Platform environment variable, // which may not be compatible with the supported platforms of the // given project/solution. Unsetting it means that the default platform // of the project/solution is used instead. command.RunCommand("set Platform=&& type NUL", quoteExe: false); } builder.MaybeIndex(command, MsBuild); command.QuoteArgument(projectOrSolution.FullPath); command.Argument("/p:UseSharedCompilation=false"); string target = builder.Options.MsBuildTarget != null ? builder.Options.MsBuildTarget : "rebuild"; string?platform = builder.Options.MsBuildPlatform != null ? builder.Options.MsBuildPlatform : projectOrSolution is ISolution s1 ? s1.DefaultPlatformName : null; string?configuration = builder.Options.MsBuildConfiguration != null ? builder.Options.MsBuildConfiguration : projectOrSolution is ISolution s2 ? s2.DefaultConfigurationName : null; command.Argument("/t:" + target); if (platform != null) { command.Argument(string.Format("/p:Platform=\"{0}\"", platform)); } if (configuration != null) { command.Argument(string.Format("/p:Configuration=\"{0}\"", configuration)); } command.Argument("/p:MvcBuildViews=true"); command.Argument(builder.Options.MsBuildArguments); ret &= command.Script; } return(ret); }
public BuildScript Analyse(Autobuilder builder) { if (!builder.ProjectsOrSolutionsToBuild.Any()) { return(BuildScript.Failure); } builder.Log(Severity.Info, "Attempting to build using MSBuild"); var vsTools = GetVcVarsBatFile(builder); if (vsTools == null && builder.ProjectsOrSolutionsToBuild.Any()) { var firstSolution = builder.ProjectsOrSolutionsToBuild.OfType <ISolution>().FirstOrDefault(); vsTools = firstSolution != null ? BuildTools.FindCompatibleVcVars(builder.Actions, firstSolution) : BuildTools.VcVarsAllBatFiles(builder.Actions).OrderByDescending(b => b.ToolsVersion).FirstOrDefault(); } if (vsTools == null && builder.Actions.IsWindows()) { builder.Log(Severity.Warning, "Could not find a suitable version of vcvarsall.bat"); } var nuget = builder.Actions.PathCombine(builder.SemmlePlatformTools, "csharp", "nuget", "nuget.exe"); var ret = BuildScript.Success; foreach (var projectOrSolution in builder.ProjectsOrSolutionsToBuild) { if (builder.Options.NugetRestore) { var nugetCommand = new CommandBuilder(builder.Actions). RunCommand(nuget). Argument("restore"). QuoteArgument(projectOrSolution.FullPath); ret &= BuildScript.Try(nugetCommand.Script); } var command = new CommandBuilder(builder.Actions); if (vsTools != null) { command.CallBatFile(vsTools.Path); } command.IndexCommand(builder.Odasa, MsBuild); command.QuoteArgument(projectOrSolution.FullPath); command.Argument("/p:UseSharedCompilation=false"); string target = builder.Options.MsBuildTarget != null ? builder.Options.MsBuildTarget : "rebuild"; string platform = builder.Options.MsBuildPlatform != null ? builder.Options.MsBuildPlatform : projectOrSolution is ISolution s1 ? s1.DefaultPlatformName : null; string configuration = builder.Options.MsBuildConfiguration != null ? builder.Options.MsBuildConfiguration : projectOrSolution is ISolution s2 ? s2.DefaultConfigurationName : null; command.Argument("/t:" + target); if (platform != null) { command.Argument(string.Format("/p:Platform=\"{0}\"", platform)); } if (configuration != null) { command.Argument(string.Format("/p:Configuration=\"{0}\"", configuration)); } command.Argument("/p:MvcBuildViews=true"); command.Argument(builder.Options.MsBuildArguments); ret &= command.Script; } return(ret); }
/// <summary> /// Returns a script for downloading a specific .NET Core SDK version, if the /// version is not already installed. /// /// See https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script. /// </summary> static BuildScript DownloadDotNetVersion(Autobuilder builder, string path, string version) { return(BuildScript.Bind(GetInstalledSdksScript(builder.Actions), (sdks, sdksRet) => { if (sdksRet == 0 && sdks.Count() == 1 && sdks[0].StartsWith(version + " ", StringComparison.Ordinal)) { // The requested SDK is already installed (and no other SDKs are installed), so // no need to reinstall return BuildScript.Failure; } builder.Log(Severity.Info, "Attempting to download .NET Core {0}", version); if (builder.Actions.IsWindows()) { var psScript = @"param([string]$Version, [string]$InstallDir) add-type @"" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } ""@ $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12' [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy $Script = Invoke-WebRequest -useb 'https://dot.net/v1/dotnet-install.ps1' $arguments = @{ Channel = 'release' Version = $Version InstallDir = $InstallDir } $ScriptBlock = [scriptblock]::create("".{$($Script)} $(&{$args} @arguments)"") Invoke-Command -ScriptBlock $ScriptBlock"; var psScriptFile = builder.Actions.PathCombine(builder.Options.RootDirectory, "install-dotnet.ps1"); builder.Actions.WriteAllText(psScriptFile, psScript); var install = new CommandBuilder(builder.Actions). RunCommand("powershell"). Argument("-NoProfile"). Argument("-ExecutionPolicy"). Argument("unrestricted"). Argument("-file"). Argument(psScriptFile). Argument("-Version"). Argument(version). Argument("-InstallDir"). Argument(path); var removeScript = new CommandBuilder(builder.Actions). RunCommand("del"). Argument(psScriptFile); return install.Script & BuildScript.Try(removeScript.Script); } else { var curl = new CommandBuilder(builder.Actions). RunCommand("curl"). Argument("-L"). Argument("-sO"). Argument("https://dot.net/v1/dotnet-install.sh"); var chmod = new CommandBuilder(builder.Actions). RunCommand("chmod"). Argument("u+x"). Argument("dotnet-install.sh"); var install = new CommandBuilder(builder.Actions). RunCommand("./dotnet-install.sh"). Argument("--channel"). Argument("release"). Argument("--version"). Argument(version). Argument("--install-dir"). Argument(path); var removeScript = new CommandBuilder(builder.Actions). RunCommand("rm"). Argument("dotnet-install.sh"); return curl.Script & chmod.Script & install.Script & BuildScript.Try(removeScript.Script); } })); }
BuildScript GetCSharpBuildScript() { /// <summary> /// A script that checks that the C# extractor has been executed. /// </summary> BuildScript CheckExtractorRun(bool warnOnFailure) => BuildScript.Create(actions => { if (actions.FileExists(Extractor.GetCSharpLogPath())) { return(0); } if (warnOnFailure) { Log(Severity.Error, "No C# code detected during build."); } return(1); }); var attempt = BuildScript.Failure; switch (GetCSharpBuildStrategy()) { case CSharpBuildStrategy.CustomBuildCommand: attempt = new BuildCommandRule().Analyse(this) & CheckExtractorRun(true); break; case CSharpBuildStrategy.Buildless: // No need to check that the extractor has been executed in buildless mode attempt = new StandaloneBuildRule().Analyse(this); break; case CSharpBuildStrategy.MSBuild: attempt = new MsBuildRule().Analyse(this) & CheckExtractorRun(true); break; case CSharpBuildStrategy.DotNet: attempt = new DotNetRule().Analyse(this) & CheckExtractorRun(true); break; case CSharpBuildStrategy.Auto: var cleanTrapFolder = BuildScript.DeleteDirectory(Actions.GetEnvironmentVariable("TRAP_FOLDER")); var cleanSourceArchive = BuildScript.DeleteDirectory(Actions.GetEnvironmentVariable("SOURCE_ARCHIVE")); var cleanExtractorLog = BuildScript.DeleteFile(Extractor.GetCSharpLogPath()); var attemptExtractorCleanup = BuildScript.Try(cleanTrapFolder) & BuildScript.Try(cleanSourceArchive) & BuildScript.Try(cleanExtractorLog); /// <summary> /// Execute script `s` and check that the C# extractor has been executed. /// If either fails, attempt to cleanup any artifacts produced by the extractor, /// and exit with code 1, in order to proceed to the next attempt. /// </summary> BuildScript IntermediateAttempt(BuildScript s) => (s & CheckExtractorRun(false)) | (attemptExtractorCleanup & BuildScript.Failure); attempt = // First try .NET Core IntermediateAttempt(new DotNetRule().Analyse(this)) | // Then MSBuild (() => IntermediateAttempt(new MsBuildRule().Analyse(this))) | // And finally look for a script that might be a build script (() => new BuildCommandAutoRule().Analyse(this) & CheckExtractorRun(true)) | // All attempts failed: print message AutobuildFailure(); break; } return (attempt & (() => new AspBuildRule().Analyse(this)) & (() => new XmlBuildRule().Analyse(this))); }