Inheritance: MonoBehaviour
        public Solution(Autobuilder builder, string path, bool allowProject) : base(builder, path)
        {
            try
            {
                solution = SolutionFile.Parse(FullPath);
            }
            catch (Exception ex) when (ex is InvalidProjectFileException || ex is FileNotFoundException)
            {
                // We allow specifying projects as solutions in lgtm.yml, so model
                // that scenario as a solution with just that one project
                if (allowProject)
                {
                    includedProjects = new[] { new Project(builder, path) };
                    return;
                }

                builder.Log(Severity.Info, $"Unable to read solution file {path}.");
                includedProjects = new Project[0];
                return;
            }

            includedProjects =
                solution.ProjectsInOrder.
                Where(p => p.ProjectType == SolutionProjectType.KnownToBeMSBuildFormat).
                Select(p => builder.Actions.PathCombine(DirectoryName, builder.Actions.PathCombine(p.RelativePath.Split('\\', StringSplitOptions.RemoveEmptyEntries)))).
                Select(p => new Project(builder, p)).
                ToArray();
        }
Exemple #2
0
        private static BuildScript WithDotNet(Autobuilder builder, Func <string?, IDictionary <string, string>?, BuildScript> f)
        {
            var installDir    = builder.Actions.PathCombine(builder.Options.RootDirectory, ".dotnet");
            var installScript = DownloadDotNet(builder, installDir);

            return(BuildScript.Bind(installScript, installed =>
            {
                Dictionary <string, string>?env;
                if (installed == 0)
                {
                    // The installation succeeded, so use the newly installed .NET Core
                    var path = builder.Actions.GetEnvironmentVariable("PATH");
                    var delim = builder.Actions.IsWindows() ? ";" : ":";
                    env = new Dictionary <string, string> {
                        { "DOTNET_MULTILEVEL_LOOKUP", "false" },     // prevent look up of other .NET Core SDKs
                        { "DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "true" },
                        { "PATH", installDir + delim + path }
                    };
                }
                else
                {
                    installDir = null;
                    env = null;
                }

                if (env is null)
                {
                    env = new Dictionary <string, string>();
                }
                env.Add("UseSharedCompilation", "false");
                return f(installDir, env);
            }));
        }
Exemple #3
0
        private static BuildScript WithDotNet(Autobuilder builder, Func <string?, IDictionary <string, string>?, bool, BuildScript> f)
        {
            var installDir    = builder.Actions.PathCombine(builder.Options.RootDirectory, ".dotnet");
            var installScript = DownloadDotNet(builder, installDir);

            return(BuildScript.Bind(installScript, installed =>
            {
                Dictionary <string, string>?env;
                if (installed == 0)
                {
                    // The installation succeeded, so use the newly installed .NET Core
                    var path = builder.Actions.GetEnvironmentVariable("PATH");
                    var delim = builder.Actions.IsWindows() ? ";" : ":";
                    env = new Dictionary <string, string> {
                        { "DOTNET_MULTILEVEL_LOOKUP", "false" },     // prevent look up of other .NET Core SDKs
                        { "DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "true" },
                        { "PATH", installDir + delim + path }
                    };
                }
                else
                {
                    installDir = null;
                    env = null;
                }

                // The CLR tracer is always compatible on Windows
                if (builder.Actions.IsWindows())
                {
                    return f(installDir, env, true);
                }

                // The CLR tracer is only compatible on .NET Core >= 3 on Linux and macOS (see
                // https://github.com/dotnet/coreclr/issues/19622)
                return BuildScript.Bind(GetInstalledRuntimesScript(builder.Actions, installDir, env), (runtimes, runtimesRet) =>
                {
                    var compatibleClr = false;
                    if (runtimesRet == 0)
                    {
                        var minimumVersion = new Version(3, 0);
                        var regex = new Regex(@"Microsoft\.NETCore\.App (\d\.\d\.\d)");
                        compatibleClr = runtimes
                                        .Select(runtime => regex.Match(runtime))
                                        .Where(m => m.Success)
                                        .Select(m => m.Groups[1].Value)
                                        .Any(m => Version.TryParse(m, out var v) && v >= minimumVersion);
                    }

                    if (!compatibleClr)
                    {
                        if (env is null)
                        {
                            env = new Dictionary <string, string>();
                        }
                        env.Add("UseSharedCompilation", "false");
                    }

                    return f(installDir, env, compatibleClr);
                });
            }));
        }
Exemple #4
0
        /// <summary>
        /// Gets the BAT file used to initialize the appropriate Visual Studio
        /// version/platform, as specified by the `vstools_version` property in
        /// lgtm.yml.
        ///
        /// Returns <code>null</code> when no version is specified.
        /// </summary>
        public static VcVarsBatFile?GetVcVarsBatFile(Autobuilder builder)
        {
            VcVarsBatFile?vsTools = null;

            if (builder.Options.VsToolsVersion is not null)
            {
                if (int.TryParse(builder.Options.VsToolsVersion, out var msToolsVersion))
                {
                    foreach (var b in BuildTools.VcVarsAllBatFiles(builder.Actions))
                    {
                        builder.Log(Severity.Info, "Found {0} version {1}", b.Path, b.ToolsVersion);
                    }

                    vsTools = BuildTools.FindCompatibleVcVars(builder.Actions, msToolsVersion);
                    if (vsTools is null)
                    {
                        builder.Log(Severity.Warning, "Could not find build tools matching version {0}", msToolsVersion);
                    }
                    else
                    {
                        builder.Log(Severity.Info, "Setting Visual Studio tools to {0}", vsTools.Path);
                    }
                }
                else
                {
                    builder.Log(Severity.Error, "The format of vstools_version is incorrect. Please specify an integer.");
                }
            }

            return(vsTools);
        }
Exemple #5
0
        /// <summary>
        /// Returns a script for downloading relevant versions of the
        /// .NET Core SDK. The SDK(s) will be installed at <code>installDir</code>
        /// (provided that the script succeeds).
        /// </summary>
        private static BuildScript DownloadDotNet(Autobuilder builder, string installDir)
        {
            if (!string.IsNullOrEmpty(builder.Options.DotNetVersion))
            {
                // Specific version supplied in configuration: always use that
                return(DownloadDotNetVersion(builder, installDir, builder.Options.DotNetVersion));
            }

            // Download versions mentioned in `global.json` files
            // See https://docs.microsoft.com/en-us/dotnet/core/tools/global-json
            var installScript   = BuildScript.Success;
            var validGlobalJson = false;

            foreach (var path in builder.Paths.Select(p => p.Item1).Where(p => p.EndsWith("global.json", StringComparison.Ordinal)))
            {
                string version;
                try
                {
                    var o = JObject.Parse(File.ReadAllText(path));
                    version = (string)(o?["sdk"]?["version"] !);
                }
                catch  // lgtm[cs/catch-of-all-exceptions]
                {
                    // not a valid global.json file
                    continue;
                }

                installScript  &= DownloadDotNetVersion(builder, installDir, version);
                validGlobalJson = true;
            }

            return(validGlobalJson ? installScript : BuildScript.Failure);
        }
Exemple #6
0
        /// <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>
        private 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 psCommand = $"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; &([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) -Version {version} -InstallDir {path}";

                    BuildScript GetInstall(string pwsh) =>
                    new CommandBuilder(builder.Actions).
                    RunCommand(pwsh).
                    Argument("-NoProfile").
                    Argument("-ExecutionPolicy").
                    Argument("unrestricted").
                    Argument("-Command").
                    Argument("\"" + psCommand + "\"").
                    Script;

                    return GetInstall("pwsh") | GetInstall("powershell");
                }
                else
                {
                    var downloadDotNetInstallSh = BuildScript.DownloadFile(
                        "https://dot.net/v1/dotnet-install.sh",
                        "dotnet-install.sh",
                        e => builder.Log(Severity.Warning, $"Failed to download 'dotnet-install.sh': {e.Message}"));

                    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 downloadDotNetInstallSh & chmod.Script & install.Script & BuildScript.Try(removeScript.Script);
                }
            }));
        }
Exemple #7
0
        public BuildScript Analyse(Autobuilder builder, bool auto)
        {
            BuildScript GetCommand(string?solution)
            {
                string standalone;

                if (builder.CodeQLExtractorLangRoot is object && builder.CodeQlPlatform is object)
                {
                    standalone = builder.Actions.PathCombine(builder.CodeQLExtractorLangRoot, "tools", builder.CodeQlPlatform, "Semmle.Extraction.CSharp.Standalone");
                }
                else if (builder.SemmlePlatformTools is object)
                {
                    standalone = builder.Actions.PathCombine(builder.SemmlePlatformTools, "csharp", "Semmle.Extraction.CSharp.Standalone");
                }
                else
                {
                    return(BuildScript.Failure);
                }

                var cmd = new CommandBuilder(builder.Actions);

                cmd.RunCommand(standalone);

                if (solution != null)
                {
                    cmd.QuoteArgument(solution);
                }

                cmd.Argument("--references:.");

                if (!builder.Options.NugetRestore)
                {
                    cmd.Argument("--skip-nuget");
                }

                return(cmd.Script);
            }

            if (!builder.Options.Buildless)
            {
                return(BuildScript.Failure);
            }

            if (!builder.Options.Solution.Any())
            {
                return(GetCommand(null));
            }

            var script = BuildScript.Success;

            foreach (var solution in builder.Options.Solution)
            {
                script &= GetCommand(solution);
            }

            return(script);
        }
        public BuildScript Analyse(Autobuilder builder, bool auto)
        {
            BuildScript GetCommand(string?solution)
            {
                string standalone;

                if (builder.CodeQLExtractorLangRoot is not null && builder.CodeQlPlatform is not null)
                {
                    standalone = builder.Actions.PathCombine(builder.CodeQLExtractorLangRoot, "tools", builder.CodeQlPlatform, "Semmle.Extraction.CSharp.Standalone");
                }
        public BuildScript Analyse(Autobuilder builder, bool auto)
        {
            var javaHome = builder.JavaHome;
            var dist     = builder.Distribution;

            var command = new CommandBuilder(builder.Actions).
                          RunCommand(builder.Actions.PathCombine(javaHome, "bin", "java")).
                          Argument("-jar").
                          QuoteArgument(builder.Actions.PathCombine(dist, "tools", "extractor-asp.jar")).
                          Argument(".");

            return(command.Script);
        }
Exemple #10
0
        public BuildScript Analyse(Autobuilder builder, bool auto)
        {
            if (!builder.Options.Indexing || builder.Odasa is null)
            {
                return(BuildScript.Success);
            }

            var command = new CommandBuilder(builder.Actions).
                          RunCommand(builder.Odasa).
                          Argument("index --xml --extensions config csproj props xml");

            return(command.Script);
        }
Exemple #11
0
        public BuildScript Analyse(Autobuilder builder, bool auto)
        {
            (var javaHome, var dist) =
                builder.CodeQLJavaHome != null ?
                (builder.CodeQLJavaHome, builder.CodeQLExtractorCSharpRoot) :
                (builder.SemmleJavaHome, builder.SemmleDist);
            var command = new CommandBuilder(builder.Actions).
                          RunCommand(builder.Actions.PathCombine(javaHome, "bin", "java")).
                          Argument("-jar").
                          QuoteArgument(builder.Actions.PathCombine(dist, "tools", "extractor-asp.jar")).
                          Argument(".");

            return(command.Script);
        }
        public BuildScript Analyse(Autobuilder builder, bool auto)
        {
            BuildScript GetCommand(string?solution)
            {
                if (builder.SemmlePlatformTools is null)
                {
                    return(BuildScript.Failure);
                }

                var standalone = builder.Actions.PathCombine(builder.SemmlePlatformTools, "csharp", "Semmle.Extraction.CSharp.Standalone");
                var cmd        = new CommandBuilder(builder.Actions);

                cmd.RunCommand(standalone);

                if (solution != null)
                {
                    cmd.QuoteArgument(solution);
                }

                cmd.Argument("--references:.");

                if (!builder.Options.NugetRestore)
                {
                    cmd.Argument("--skip-nuget");
                }

                return(cmd.Script);
            }

            if (!builder.Options.Buildless)
            {
                return(BuildScript.Failure);
            }

            var solutions = builder.Options.Solution.Length;

            if (solutions == 0)
            {
                return(GetCommand(null));
            }

            var script = BuildScript.Success;

            foreach (var solution in builder.Options.Solution)
            {
                script &= GetCommand(solution);
            }

            return(script);
        }
Exemple #13
0
        /// <summary>
        /// Gets the `dotnet build` script.
        ///
        /// The CLR tracer only works on .NET Core >= 3 on Linux and macOS (see
        /// https://github.com/dotnet/coreclr/issues/19622), so in case we are
        /// running on an older .NET Core, we disable shared compilation (and
        /// hence the need for CLR tracing), by adding a
        /// `/p:UseSharedCompilation=false` argument.
        /// </summary>
        private static BuildScript GetBuildScript(Autobuilder builder, string?dotNetPath, IDictionary <string, string>?environment, bool compatibleClr, string projOrSln)
        {
            var build  = new CommandBuilder(builder.Actions, null, environment);
            var script = builder.MaybeIndex(build, DotNetCommand(builder.Actions, dotNetPath)).
                         Argument("build").
                         Argument("--no-incremental");

            return(compatibleClr ?
                   script.Argument(builder.Options.DotNetArguments).
                   QuoteArgument(projOrSln).
                   Script :
                   script.Argument("/p:UseSharedCompilation=false").
                   Argument(builder.Options.DotNetArguments).
                   QuoteArgument(projOrSln).
                   Script);
        }
Exemple #14
0
 /// <summary>
 /// Returns a script for downloading `nuget.exe` from nuget.org.
 /// </summary>
 private static BuildScript DownloadNugetExe(Autobuilder builder, string path) =>
 BuildScript.Create(_ =>
 {
     builder.Log(Severity.Info, "Attempting to download nuget.exe");
     return(0);
 })
 &
 BuildScript.DownloadFile(
     "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe",
     path,
     e => builder.Log(Severity.Warning, $"Failed to download 'nuget.exe': {e.Message}"))
 &
 BuildScript.Create(_ =>
 {
     builder.Log(Severity.Info, $"Successfully downloaded {path}");
     return(0);
 });
Exemple #15
0
        public BuildScript Analyse(Autobuilder builder, bool auto)
        {
            if (builder.Options.BuildCommand == null)
                return BuildScript.Failure;

            // Custom build commands may require a specific .NET Core version
            return withDotNet(builder, environment =>
                {
                    var command = new CommandBuilder(builder.Actions, null, environment);

                    // Custom build commands may require a specific Visual Studio version
                    var vsTools = MsBuildRule.GetVcVarsBatFile(builder);
                    if (vsTools != null)
                        command.CallBatFile(vsTools.Path);
                    builder.MaybeIndex(command, builder.Options.BuildCommand);

                    return command.Script;
                });
        }
        void TestAutobuilderScript(Autobuilder autobuilder, int expectedOutput, int commandsRun)
        {
            Assert.Equal(expectedOutput, autobuilder.GetBuildScript().Run(Actions, StartCallback, EndCallback));

            // Check expected commands actually ran
            Assert.Equal(commandsRun, StartCallbackIn.Count);
            Assert.Equal(commandsRun, EndCallbackIn.Count);
            Assert.Equal(commandsRun, EndCallbackReturn.Count);

            var action = Actions.RunProcess.GetEnumerator();

            for (int cmd = 0; cmd < commandsRun; ++cmd)
            {
                Assert.True(action.MoveNext());

                Assert.Equal(action.Current.Key, StartCallbackIn[cmd]);
                Assert.Equal(action.Current.Value, EndCallbackReturn[cmd]);
            }
        }
Exemple #17
0
        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 is not 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, (dotNetPath, environment) =>
            {
                var ret = GetInfoCommand(builder.Actions, dotNetPath, environment);
                foreach (var projectOrSolution in builder.ProjectsOrSolutionsToBuild)
                {
                    var cleanCommand = GetCleanCommand(builder.Actions, dotNetPath, environment);
                    cleanCommand.QuoteArgument(projectOrSolution.FullPath);
                    var clean = cleanCommand.Script;

                    var restoreCommand = GetRestoreCommand(builder.Actions, dotNetPath, environment);
                    restoreCommand.QuoteArgument(projectOrSolution.FullPath);
                    var restore = restoreCommand.Script;

                    var build = GetBuildScript(builder, dotNetPath, environment, projectOrSolution.FullPath);

                    ret &= BuildScript.Try(clean) & BuildScript.Try(restore) & build;
                }
                return ret;
            }));
        }
Exemple #18
0
        public BuildScript Analyse(Autobuilder builder)
        {
            if (builder.Options.BuildCommand == null)
            {
                return(BuildScript.Failure);
            }

            // Custom build commands may require a specific .NET Core version
            return(DotNetRule.WithDotNet(builder, dotNet =>
            {
                var command = new CommandBuilder(builder.Actions, null, dotNet?.Environment);

                // Custom build commands may require a specific Visual Studio version
                var vsTools = MsBuildRule.GetVcVarsBatFile(builder);
                if (vsTools != null)
                {
                    command.CallBatFile(vsTools.Path);
                }
                command.IndexCommand(builder.Odasa, builder.Options.BuildCommand);

                return command.Script;
            }));
        }
Exemple #19
0
        public BuildScript Analyse(Autobuilder builder, bool auto)
        {
            if (builder.Options.BuildCommand is null)
            {
                return(BuildScript.Failure);
            }

            // Custom build commands may require a specific .NET Core version
            return(withDotNet(builder, environment =>
            {
                var command = new CommandBuilder(builder.Actions, null, environment);

                // Custom build commands may require a specific Visual Studio version
                var vsTools = MsBuildRule.GetVcVarsBatFile(builder);
                if (vsTools is not null)
                {
                    command.CallBatFile(vsTools.Path);
                }
                command.RunCommand(builder.Options.BuildCommand);

                return command.Script;
            }));
        }
Exemple #20
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 #21
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);
        }
 protected ProjectOrSolution(Autobuilder builder, string path)
 {
     FullPath      = builder.Actions.GetFullPath(path);
     DirectoryName = builder.Actions.GetDirectoryName(path) ?? "";
 }
 protected ProjectOrSolution(Autobuilder builder, string path)
 {
     FullPath = builder.Actions.GetFullPath(path);
 }
Exemple #24
0
        /// <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>
        private 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 downloadDotNetInstallSh = BuildScript.DownloadFile(
                        "https://dot.net/v1/dotnet-install.sh",
                        "dotnet-install.sh",
                        e => builder.Log(Severity.Warning, $"Failed to download 'dotnet-install.sh': {e.Message}"));

                    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 downloadDotNetInstallSh & chmod.Script & install.Script & BuildScript.Try(removeScript.Script);
                }
            }));
        }
Exemple #25
0
 private static void NewMenuOption()
 {
     Autobuilder.BuildGame();
 }
Exemple #26
0
        public Project(Autobuilder builder, string path) : base(builder, path)
        {
            ToolsVersion         = new Version();
            includedProjectsLazy = new Lazy <List <Project> >(() => new List <Project>());

            if (!builder.Actions.FileExists(FullPath))
            {
                return;
            }

            XmlDocument projFile;

            try
            {
                projFile = builder.Actions.LoadXml(FullPath);
            }
            catch (Exception ex) when(ex is XmlException || ex is FileNotFoundException)
            {
                builder.Log(Severity.Info, $"Unable to read project file {path}.");
                return;
            }

            var root = projFile.DocumentElement;

            if (root.Name == "Project")
            {
                if (root.HasAttribute("Sdk"))
                {
                    DotNetProject = true;
                    return;
                }

                var toolsVersion = root.GetAttribute("ToolsVersion");
                if (!string.IsNullOrEmpty(toolsVersion))
                {
                    try
                    {
                        ToolsVersion      = new Version(toolsVersion);
                        ValidToolsVersion = true;
                    }
                    catch  // lgtm[cs/catch-of-all-exceptions]
                           // Generic catch clause - Version constructor throws about 5 different exceptions.
                    {
                        builder.Log(Severity.Warning, "Project {0} has invalid tools version {1}", path, toolsVersion);
                    }
                }

                includedProjectsLazy = new Lazy <List <Project> >(() =>
                {
                    var ret = new List <Project>();
                    // The documentation on `.proj` files is very limited, but it appears that both
                    // `<ProjectFile Include="X"/>` and `<ProjectFiles Include="X"/>` is valid
                    var mgr = new XmlNamespaceManager(projFile.NameTable);
                    mgr.AddNamespace("msbuild", "http://schemas.microsoft.com/developer/msbuild/2003");
                    var projectFileIncludes  = root.SelectNodes("//msbuild:Project/msbuild:ItemGroup/msbuild:ProjectFile/@Include", mgr).OfType <XmlNode>();
                    var projectFilesIncludes = root.SelectNodes("//msbuild:Project/msbuild:ItemGroup/msbuild:ProjectFiles/@Include", mgr).OfType <XmlNode>();
                    foreach (var include in projectFileIncludes.Concat(projectFilesIncludes))
                    {
                        var includePath = builder.Actions.PathCombine(include.Value.Split('\\', StringSplitOptions.RemoveEmptyEntries));
                        ret.Add(new Project(builder, builder.Actions.PathCombine(DirectoryName, includePath)));
                    }
                    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);
        }
Exemple #28
0
 private static void AutoBuild()
 {
     Autobuilder.BuildGame();
 }
Exemple #29
0
 /// <summary>
 /// Returns a script that attempts to download relevant version(s) of the
 /// .NET Core SDK, followed by running the script generated by <paramref name="f"/>.
 ///
 /// The argument to <paramref name="f"/> is any additional required environment
 /// variables needed by the installed .NET Core (<code>null</code> when no variables
 /// are needed).
 /// </summary>
 public static BuildScript WithDotNet(Autobuilder builder, Func <IDictionary <string, string>?, BuildScript> f)
 => WithDotNet(builder, (_1, env) => f(env));