Beispiel #1
0
        private static void SetProcessPipDetails(Process pip, PipDetails result, PathTable pathTable, PipEnvironment pipEnvironment)
        {
            var args = new List <string>();

            args.Add(pip.Executable.Path.ToString(pathTable));
            args.AddRange(ConvertArgumentsToStringArray(pip.Arguments, pathTable));

            result.Executable = new ToolReference()
            {
                Id = pip.Executable.Path.Value.Value, Path = pip.Executable.Path.ToString(pathTable)
            };
            result.CommandLineFragments = args;
            result.CommandLine          = GetPipCommandLine(pip, pathTable);
            result.WarningTimeout       = pip.WarningTimeout;
            result.Timeout = pip.Timeout;

            bool includeDefaultOutputs = result.State == PipState.Failed;

            result.StandardInput = pip.StandardInput.IsFile
                ? StandardInput.CreateFromFile(FileReference.FromAbsolutePath(pathTable, pip.StandardInput.File.Path))
                : pip.StandardInput.IsData
                    ? StandardInput.CreateFromData(
                pip.StandardInput.Data.ToString(pathTable).Split(new[] { Environment.NewLine }, StringSplitOptions.None))
                    : StandardInput.Invalid;

            result.StandardOutput            = GetStdFilePath(pip, SandboxedProcessFile.StandardOutput, pathTable, includeDefaultOutputs);
            result.StandardError             = GetStdFilePath(pip, SandboxedProcessFile.StandardError, pathTable, includeDefaultOutputs);
            result.StandardDirectory         = pip.StandardDirectory.IsValid ? pip.StandardDirectory.ToString(pathTable) : null;
            result.Dependencies              = pip.Dependencies.Select(d => FileArtifactToFileDetails(d, pathTable)).OrderBy(d => d).ToList();
            result.DirectoryDependencies     = pip.DirectoryDependencies.Select(d => d.Path.ToString(pathTable)).OrderBy(d => d).ToList();
            result.AdditionalTempDirectories = pip.AdditionalTempDirectories.Select(d => d.ToString(pathTable)).OrderBy(d => d).ToList();
            result.UntrackedPaths            = pip.UntrackedPaths.Select(f => f.ToString(pathTable)).OrderBy(f => f).ToList();
            result.UntrackedScopes           = pip.UntrackedScopes.Select(f => f.ToString(pathTable)).OrderBy(f => f).ToList();
            result.Outputs = pip.FileOutputs.Select(f => FileArtifactToFileDetails(f.ToFileArtifact(), pathTable)).OrderBy(f => f).ToList();

            result.WorkingDirectory = pip.WorkingDirectory.ToString(pathTable);
            if (!result.WorkingDirectory.EndsWith(@"\", StringComparison.Ordinal))
            {
                result.WorkingDirectory += @"\";
            }

            if (pip.ResponseFile.IsValid)
            {
                result.ResponseFile = FileArtifactToFileDetails(pip.ResponseFile, pathTable);
            }

            var vars = new List <Tuple <string, string> >();

            foreach (var env in pipEnvironment.GetEffectiveEnvironmentVariables(pathTable, pip).ToDictionary().OrderBy(kvp => kvp.Key, StringComparer.OrdinalIgnoreCase))
            {
                vars.Add(new Tuple <string, string>(env.Key, env.Value));
            }

            vars.Sort((first, second) => string.Compare(first.Item1, second.Item1, StringComparison.OrdinalIgnoreCase));
            result.EnvironmentVariables = vars;
        }
        private void SetupEnvironment(StreamWriter writer, Process pip, string outputFile)
        {
            var pipEnviornment = new PipEnvironment(LoggingContext);
            var environment    = pipEnviornment.GetEffectiveEnvironmentVariables(PathTable, pip).ToDictionary();

            writer.WriteLine($"{CommentPrefix} Environment Variables");
            if (string.IsNullOrEmpty(JsonEnvironmentScript))
            {
                writer.WriteLine($"{CommentPrefix} Clear Existing Environment Variables");

#if PLATFORM_WIN
                writer.WriteLine(@"for /f ""tokens=1* delims=="" %%a in ('set') do (");
                writer.WriteLine("    set %%a=");
                writer.WriteLine(")");
#else
                writer.WriteLine(@"for c in $(set | cut -d '=' -f 1); do unset $c 2> /dev/null; done");
#endif
                writer.WriteLine();

                writer.WriteLine($"{CommentPrefix} Setting PIP Environment Variables");
                foreach (var environmentVariable in environment)
                {
                    writer.WriteLine($"{ScriptVariableExportKeyword} {SanitizeEnvironmentVariableValue(environmentVariable.Key)}={SanitizeEnvironmentVariableValue(environmentVariable.Value)}");
                }
            }
            else
            {
                FileUtilities.CreateDirectory(Path.GetDirectoryName(outputFile));
                using (var jsonFile = File.Create($"{outputFile}.env.json"))
                    using (var jsonStream = new StreamWriter(jsonFile))
                        using (var json = new JsonTextWriter(jsonStream))
                        {
                            json.WriteStartObject();
                            foreach (var environmentVariable in environment)
                            {
                                json.WritePropertyName(environmentVariable.Key);
                                json.WriteValue(environmentVariable.Value);
                            }
                            json.WriteEndObject();
                        }
                writer.WriteLine($"{ExecuteScriptCommand} {JsonEnvironmentScript} {outputFile}.env.json");
            }
            writer.WriteLine();
        }
        private void SetupEnvironment(StreamWriter writer, Process pip, string outputFile)
        {
            var pipEnviornment = new PipEnvironment();
            var environment    = pipEnviornment.GetEffectiveEnvironmentVariables(PathTable, pip).ToDictionary();

            writer.WriteLine(":::: Environment Variables");
            if (string.IsNullOrEmpty(JsonEnvironmentScript))
            {
                writer.WriteLine(":: Clear Existing Environment Variables");
                writer.WriteLine(@"for /f ""tokens=1* delims=="" %%a in ('set') do (");
                writer.WriteLine("    set %%a=");
                writer.WriteLine(")");
                writer.WriteLine();

                writer.WriteLine(":: Setting PIP Environment Variables");
                foreach (var environmentVariable in environment)
                {
                    writer.WriteLine("set {0}={1}", SanitizeEnvironmentVariableValue(environmentVariable.Key), SanitizeEnvironmentVariableValue(environmentVariable.Value));
                }
            }
            else
            {
                FileUtilities.CreateDirectory(Path.GetDirectoryName(outputFile));
                using (var jsonFile = File.Create($"{outputFile}.env.json"))
                    using (var jsonStream = new StreamWriter(jsonFile))
                        using (var json = new JsonTextWriter(jsonStream))
                        {
                            json.WriteStartObject();
                            foreach (var environmentVariable in environment)
                            {
                                json.WritePropertyName(environmentVariable.Key);
                                json.WriteValue(environmentVariable.Value);
                            }
                            json.WriteEndObject();
                        }
                writer.WriteLine($"call {JsonEnvironmentScript} {outputFile}.env.json");
            }
            writer.WriteLine();
        }