Exemple #1
0
        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);

            string?dir = Path.GetDirectoryName(scriptPath);

            // A specific .NET Core version may be required
            return(chmodScript & withDotNet(builder, environment =>
            {
                var command = new CommandBuilder(builder.Actions, dir, environment);

                // A specific Visual Studio version may be required
                var vsTools = MsBuildRule.GetVcVarsBatFile(builder);
                if (vsTools != null)
                {
                    command.CallBatFile(vsTools.Path);
                }

                builder.MaybeIndex(command, scriptPath);
                return command.Script;
            }));
        }
Exemple #2
0
        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 is null && builder.ProjectsOrSolutionsToBuild.Any())
            {
                var firstSolution = builder.ProjectsOrSolutionsToBuild.OfType <ISolution>().FirstOrDefault();
                vsTools = firstSolution is not null
                                ? BuildTools.FindCompatibleVcVars(builder.Actions, firstSolution)
                                : BuildTools.VcVarsAllBatFiles(builder.Actions).OrderByDescending(b => b.ToolsVersion).FirstOrDefault();
            }

            if (vsTools is null && builder.Actions.IsWindows())
            {
                builder.Log(Severity.Warning, "Could not find a suitable version of VsDevCmd.bat/vcvarsall.bat");
            }

            // Use `nuget.exe` from source code repo, if present, otherwise first attempt with global
            // `nuget` command, and if that fails, attempt to download `nuget.exe` from nuget.org
            var nuget           = builder.GetFilename("nuget.exe").Select(t => t.Item1).FirstOrDefault() ?? "nuget";
            var nugetDownload   = builder.Actions.PathCombine(builder.Options.RootDirectory, ".nuget", "nuget.exe");
            var nugetDownloaded = false;

            var ret = BuildScript.Success;

            foreach (var projectOrSolution in builder.ProjectsOrSolutionsToBuild)
            {
                if (builder.Options.NugetRestore)
                {
                    BuildScript GetNugetRestoreScript() =>
                    new CommandBuilder(builder.Actions).
                    RunCommand(nuget).
                    Argument("restore").
                    QuoteArgument(projectOrSolution.FullPath).
                    Argument("-DisableParallelProcessing").
                    Script;

                    var nugetRestore          = GetNugetRestoreScript();
                    var msbuildRestoreCommand = new CommandBuilder(builder.Actions).
                                                RunCommand(msBuild).
                                                Argument("/t:restore").
                                                QuoteArgument(projectOrSolution.FullPath);

                    if (nugetDownloaded)
                    {
                        ret &= BuildScript.Try(nugetRestore | msbuildRestoreCommand.Script);
                    }
                    else
                    {
                        // If `nuget restore` fails, and we have not already attempted to download `nuget.exe`,
                        // download it and reattempt `nuget restore`.
                        var nugetDownloadAndRestore =
                            BuildScript.Bind(DownloadNugetExe(builder, nugetDownload), exitCode =>
                        {
                            nugetDownloaded = true;
                            if (exitCode != 0)
                            {
                                return(BuildScript.Failure);
                            }

                            nuget = nugetDownload;
                            return(GetNugetRestoreScript());
                        });
                        ret &= BuildScript.Try(nugetRestore | nugetDownloadAndRestore | msbuildRestoreCommand.Script);
                    }
                }

                var command = new CommandBuilder(builder.Actions);

                if (vsTools is not 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);
                }

                command.RunCommand(msBuild);
                command.QuoteArgument(projectOrSolution.FullPath);

                command.Argument("/p:UseSharedCompilation=false");

                var target        = builder.Options.MsBuildTarget ?? "rebuild";
                var platform      = builder.Options.MsBuildPlatform ?? (projectOrSolution is ISolution s1 ? s1.DefaultPlatformName : null);
                var configuration = builder.Options.MsBuildConfiguration ?? (projectOrSolution is ISolution s2 ? s2.DefaultConfigurationName : null);

                command.Argument("/t:" + target);
                if (platform is not null)
                {
                    command.Argument(string.Format("/p:Platform=\"{0}\"", platform));
                }
                if (configuration is not 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, 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);
        }