Example #1
0
        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 == null)
                        {
                            env = new Dictionary <string, string>();
                        }
                        env.Add("UseSharedCompilation", "false");
                    }

                    return f(installDir, env, compatibleClr);
                });
            }));
        }
Example #2
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>
        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);
                }
            }));
        }