Beispiel #1
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);
        }
Beispiel #2
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);
        }