Example #1
0
        internal override int Execute(IServiceProvider serviceProvider, IConsole console)
        {
            var generator = serviceProvider.GetRequiredService <IBuildScriptGenerator>();

            var ctx         = BuildScriptGenerator.CreateContext(serviceProvider, operationId: null);
            var compatPlats = generator.GetCompatiblePlatforms(ctx);

            if (compatPlats != null && compatPlats.Any())
            {
                console.WriteLine("Detected platforms:");
                console.WriteLine(string.Join(' ', compatPlats.Select(pair => $"{pair.Item1.Name}=\"{pair.Item2}\"")));

                // Write the detected platforms into the build plan as TOML
                File.WriteAllLines(PlanPath, compatPlats.Select(pair => $"{pair.Item1.Name} = {{ version = \"{pair.Item2}\" }}"));

                return(ProcessConstants.ExitSuccess);
            }

            return(DetectorFailCode);
        }
        internal override int Execute(IServiceProvider serviceProvider, IConsole console)
        {
            var generator = serviceProvider.GetRequiredService <IBuildScriptGenerator>();

            var options = serviceProvider.GetRequiredService <IOptions <BuildScriptGeneratorOptions> >().Value;
            var env     = serviceProvider.GetRequiredService <CliEnvironmentSettings>();
            var repo    = serviceProvider.GetRequiredService <ISourceRepoProvider>().GetSourceRepo();

            var ctx         = BuildScriptGenerator.CreateContext(options, env, repo, operationId: null);
            var compatPlats = generator.GetCompatiblePlatforms(ctx);

            if (compatPlats != null && compatPlats.Any())
            {
                console.WriteLine("# Detected platforms:");
                console.WriteLine(string.Join(' ', compatPlats.Select(pair => $"{pair.Item1.Name}=\"{pair.Item2}\"")));
                return(ProcessConstants.ExitSuccess);
            }

            return(DetectorFailCode);
        }
Example #3
0
        internal override int Execute(IServiceProvider serviceProvider, IConsole console)
        {
            var logger = serviceProvider.GetRequiredService <ILogger <ExecCommand> >();
            var env    = serviceProvider.GetRequiredService <IEnvironment>();
            var opts   = serviceProvider.GetRequiredService <IOptions <BuildScriptGeneratorOptions> >().Value;

            var beginningOutputLog = GetBeginningCommandOutputLog();

            console.WriteLine(beginningOutputLog);

            if (string.IsNullOrWhiteSpace(Command))
            {
                logger.LogDebug("Command is empty; exiting");
                return(ProcessConstants.ExitSuccess);
            }

            var shellPath         = env.GetEnvironmentVariable("BASH") ?? FilePaths.Bash;
            var context           = BuildScriptGenerator.CreateContext(serviceProvider, operationId: null);
            var detector          = serviceProvider.GetRequiredService <DefaultPlatformDetector>();
            var detectedPlatforms = detector.DetectPlatforms(context);

            if (!detectedPlatforms.Any())
            {
                return(ProcessConstants.ExitFailure);
            }

            int exitCode;

            using (var timedEvent = logger.LogTimedEvent("ExecCommand"))
            {
                // Build envelope script
                var scriptBuilder = new ShellScriptBuilder("\n")
                                    .AddShebang(shellPath)
                                    .AddCommand("set -e");

                var envSetupProvider   = serviceProvider.GetRequiredService <PlatformsInstallationScriptProvider>();
                var installationScript = envSetupProvider.GetBashScriptSnippet(
                    context,
                    detectedPlatforms);
                if (!string.IsNullOrEmpty(installationScript))
                {
                    scriptBuilder.AddCommand(installationScript);
                }

                scriptBuilder.Source(
                    $"{FilePaths.Benv} " +
                    $"{string.Join(" ", detectedPlatforms.Select(p => $"{p.Platform}={p.PlatformVersion}"))}");

                scriptBuilder
                .AddCommand("echo Executing supplied command...")
                .AddCommand(Command);

                // Create temporary file to store script
                // Get the path where the generated script should be written into.
                var tempDirectoryProvider = serviceProvider.GetRequiredService <ITempDirectoryProvider>();
                var tempScriptPath        = Path.Combine(tempDirectoryProvider.GetTempDirectory(), "execCommand.sh");
                var script = scriptBuilder.ToString();
                File.WriteAllText(tempScriptPath, script);
                console.WriteLine("Finished generating script.");

                timedEvent.AddProperty(nameof(tempScriptPath), tempScriptPath);

                if (DebugMode)
                {
                    console.WriteLine($"Temporary script @ {tempScriptPath}:");
                    console.WriteLine("---");
                    console.WriteLine(script);
                    console.WriteLine("---");
                }

                console.WriteLine();
                console.WriteLine("Executing generated script...");
                console.WriteLine();

                exitCode = ProcessHelper.RunProcess(
                    shellPath,
                    new[] { tempScriptPath },
                    opts.SourceDir,
                    (sender, args) =>
                {
                    if (args.Data != null)
                    {
                        console.WriteLine(args.Data);
                    }
                },
                    (sender, args) =>
                {
                    if (args.Data != null)
                    {
                        console.Error.WriteLine(args.Data);
                    }
                },
                    waitTimeForExit: null);
                timedEvent.AddProperty("exitCode", exitCode.ToString());
            }

            return(exitCode);
        }
Example #4
0
        internal override int Execute(IServiceProvider serviceProvider, IConsole console)
        {
            var logger  = serviceProvider.GetRequiredService <ILogger <PrepareEnvironmentCommand> >();
            var options = serviceProvider.GetRequiredService <IOptions <BuildScriptGeneratorOptions> >().Value;

            var beginningOutputLog = GetBeginningCommandOutputLog();

            console.WriteLine(beginningOutputLog);

            int exitCode;

            using (var timedEvent = logger.LogTimedEvent("EnvSetupCommand"))
            {
                var context = BuildScriptGenerator.CreateContext(serviceProvider, operationId: null);

                IEnumerable <PlatformDetectorResult> detectedPlatforms = null;
                if (SkipDetection)
                {
                    console.WriteLine(
                        $"Skipping platform detection since '{SkipDetectionTemplate}' switch was used...");

                    var platforms = serviceProvider.GetRequiredService <IEnumerable <IProgrammingPlatform> >();
                    if (TryValidateSuppliedPlatformsAndVersions(
                            platforms,
                            PlatformsAndVersions,
                            PlatformsAndVersionsFile,
                            console,
                            out var results))
                    {
                        detectedPlatforms = results;
                    }
                    else
                    {
                        console.WriteErrorLine(
                            $"Invalid value for switch '{PlatformsAndVersionsTemplate}'.");
                        return(ProcessConstants.ExitFailure);
                    }
                }
                else
                {
                    var detector      = serviceProvider.GetRequiredService <DefaultPlatformsInformationProvider>();
                    var platformInfos = detector.GetPlatformsInfo(context);
                    if (!platformInfos.Any())
                    {
                        return(ProcessConstants.ExitFailure);
                    }
                    detectedPlatforms = platformInfos.Select(pi => pi.DetectorResult);
                }

                var environmentScriptProvider = serviceProvider.GetRequiredService <PlatformsInstallationScriptProvider>();
                var snippet = environmentScriptProvider.GetBashScriptSnippet(context, detectedPlatforms);

                var scriptBuilder = new StringBuilder()
                                    .AppendLine($"#!{FilePaths.Bash}")
                                    .AppendLine("set -e")
                                    .AppendLine();

                if (!string.IsNullOrEmpty(snippet))
                {
                    scriptBuilder
                    .AppendLine("echo")
                    .AppendLine("echo Setting up environment...")
                    .AppendLine("echo")
                    .AppendLine(snippet)
                    .AppendLine("echo")
                    .AppendLine("echo Done setting up environment.")
                    .AppendLine("echo");
                }

                // Create temporary file to store script
                // Get the path where the generated script should be written into.
                var tempDirectoryProvider = serviceProvider.GetRequiredService <ITempDirectoryProvider>();
                var tempScriptPath        = Path.Combine(tempDirectoryProvider.GetTempDirectory(), "setupEnvironment.sh");
                var script = scriptBuilder.ToString();
                File.WriteAllText(tempScriptPath, script);
                timedEvent.AddProperty(nameof(tempScriptPath), tempScriptPath);

                if (DebugMode)
                {
                    console.WriteLine($"Temporary script @ {tempScriptPath}:");
                    console.WriteLine("---");
                    console.WriteLine(scriptBuilder);
                    console.WriteLine("---");
                }

                var environment = serviceProvider.GetRequiredService <IEnvironment>();
                var shellPath   = environment.GetEnvironmentVariable("BASH") ?? FilePaths.Bash;

                exitCode = ProcessHelper.RunProcess(
                    shellPath,
                    new[] { tempScriptPath },
                    options.SourceDir,
                    (sender, args) =>
                {
                    if (args.Data != null)
                    {
                        console.WriteLine(args.Data);
                    }
                },
                    (sender, args) =>
                {
                    if (args.Data != null)
                    {
                        console.Error.WriteLine(args.Data);
                    }
                },
                    waitTimeForExit: null);
                timedEvent.AddProperty("exitCode", exitCode.ToString());
            }

            return(exitCode);
        }
Example #5
0
        internal override int Execute(IServiceProvider serviceProvider, IConsole console)
        {
            var logger    = serviceProvider.GetRequiredService <ILogger <ExecCommand> >();
            var env       = serviceProvider.GetRequiredService <IEnvironment>();
            var generator = serviceProvider.GetRequiredService <IBuildScriptGenerator>();
            var opts      = serviceProvider.GetRequiredService <IOptions <BuildScriptGeneratorOptions> >().Value;

            if (string.IsNullOrWhiteSpace(Command))
            {
                logger.LogDebug("Command is empty; exiting");
                return(ProcessConstants.ExitSuccess);
            }

            var shellPath = env.GetEnvironmentVariable("BASH") ?? FilePaths.Bash;
            var ctx       = BuildScriptGenerator.CreateContext(serviceProvider, operationId: null);

            ctx.DisableMultiPlatformBuild = false;
            var tools = generator.GetRequiredToolVersions(ctx);

            int exitCode;

            using (var timedEvent = logger.LogTimedEvent("ExecCommand"))
            {
                // Build envelope script
                var scriptBuilder = new ShellScriptBuilder("\n")
                                    .AddShebang(shellPath)
                                    .AddCommand("set -e");

                if (tools.Count > 0)
                {
                    scriptBuilder.Source($"{FilePaths.Benv} {StringExtensions.JoinKeyValuePairs(tools)}");
                }

                var script = scriptBuilder.AddCommand(Command).ToString();

                // Create temporary file to store script
                var tempScriptPath = Path.GetTempFileName();
                File.WriteAllText(tempScriptPath, script);
                timedEvent.AddProperty(nameof(tempScriptPath), tempScriptPath);

                if (DebugMode)
                {
                    console.WriteLine($"Temporary script @ {tempScriptPath}:");
                    console.WriteLine("---");
                    console.WriteLine(script);
                    console.WriteLine("---");
                }

                exitCode = ProcessHelper.RunProcess(
                    shellPath,
                    new[] { tempScriptPath },
                    opts.SourceDir,
                    (sender, args) => { if (args.Data != null)
                                        {
                                            console.WriteLine(args.Data);
                                        }
                    },
                    (sender, args) => { if (args.Data != null)
                                        {
                                            console.Error.WriteLine(args.Data);
                                        }
                    },
                    waitTimeForExit: null);
                timedEvent.AddProperty("exitCode", exitCode.ToString());
            }

            return(exitCode);
        }