예제 #1
0
        protected int TrackerExecuteTool(
            string toolPath, string responseFileCommands, string commandLineCommands)
        {
            bool   trackFileAccess = TrackFileAccess;
            string command         = Environment.ExpandEnvironmentVariables(toolPath);
            string rspFileCommands = responseFileCommands;
            string arguments       = Environment.ExpandEnvironmentVariables(commandLineCommands);

            string rspFile = null;

            try {
                string trackerPath;
                string dllName = null;
                pathToLog = command;
                if (trackFileAccess)
                {
                    var toolType = ExecutableType.SameAsCurrentProcess;
                    if (!string.IsNullOrEmpty(ToolArchitecture))
                    {
                        if (!Enum.TryParse(ToolArchitecture, out toolType))
                        {
                            Log.LogErrorWithCodeFromResources(
                                nameof(Strings.General_InvalidValue),
                                "ToolArchitecture",
                                GetType().Name);
                            return(-1);
                        }
                    }
                    else if (ToolType.HasValue)
                    {
                        toolType = ToolType.Value;
                    }

                    try {
                        trackerPath = FileTracker.GetTrackerPath(toolType, TrackerSdkPath);
                        if (trackerPath == null)
                        {
                            Log.LogErrorFromResources(
                                nameof(Strings.Error_MissingFile),
                                "tracker.exe");
                        }
                    } catch (Exception ex) when(!ExceptionHandling.NotExpectedException(ex))
                    {
                        Log.LogErrorWithCodeFromResources(
                            nameof(Strings.General_InvalidValue),
                            "TrackerSdkPath",
                            GetType().Name);
                        return(-1);
                    }

                    try {
                        dllName = FileTracker.GetFileTrackerPath(
                            toolType, TrackerFrameworkPath);
                    } catch (Exception ex) when(!ExceptionHandling.NotExpectedException(ex))
                    {
                        Log.LogErrorWithCodeFromResources(
                            nameof(Strings.General_InvalidValue),
                            "TrackerFrameworkPath",
                            GetType().Name);
                        return(-1);
                    }
                }
                else
                {
                    trackerPath = command;
                }

                if (string.IsNullOrEmpty(trackerPath))
                {
                    return(-1);
                }

                ErrorUtilities.VerifyThrowInternalRooted(trackerPath);
                string cmdLineCommands;
                if (trackFileAccess)
                {
                    string trackerArgs = FileTracker.TrackerArguments(
                        command, arguments, dllName,
                        TrackerIntermediateDirectory, RootSource,
                        CancelEventName);

                    Log.LogMessageFromResources(
                        MessageImportance.Low,
                        nameof(Strings.Native_TrackingCommandMessage));
                    Log.LogMessage(
                        MessageImportance.Low,
                        trackerPath + (AttributeFileTracking ? " /a " : " ") +
                        trackerArgs + " " + rspFileCommands);

                    rspFile = FileUtilities.GetTemporaryFile();
                    using (var writer = CreateUnicodeWriter(rspFile)) {
                        writer.Write(FileTracker.TrackerResponseFileArguments(
                                         dllName, TrackerIntermediateDirectory, RootSource, CancelEventName));
                    }

                    cmdLineCommands =
                        (AttributeFileTracking ? "/a @\"" : "@\"") + rspFile +
                        "\"" + FileTracker.TrackerCommandArguments(command, arguments);
                }
                else
                {
                    cmdLineCommands = arguments;
                }

                return(base.ExecuteTool(trackerPath, rspFileCommands, cmdLineCommands));
            } finally {
                if (rspFile != null)
                {
                    DeleteTempFile(rspFile);
                }
            }
        }
예제 #2
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        protected virtual int TrackedExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
        {
            //
            // Thread body. Generate required command line and launch tool.
            //

            int exitCode = -1;

            try
            {
                //
                // If response files arguments are used/supported, migrate shared flags to a file and strip them from the command line.
                //

                var commandLineSwitchesBuffer = new StringBuilder(commandLineCommands);

                var responseFileSwitchesBuffer = new StringBuilder(responseFileCommands);

                if (responseFileSwitchesBuffer.Length > 0)
                {
                    commandLineSwitchesBuffer.Replace(responseFileSwitchesBuffer.ToString(), "");
                }

#if DEBUG
                Log.LogMessageFromText($"[{ToolName}] Tool: {pathToTool}", MessageImportance.High);

                Log.LogMessageFromText($"[{ToolName}] Command line: {commandLineSwitchesBuffer}", MessageImportance.High);

                Log.LogMessageFromText($"[{ToolName}] Response file commands: {responseFileSwitchesBuffer}", MessageImportance.High);
#endif

                var trackerToolPath = FileTracker.GetTrackerPath(ExecutableType.Native64Bit); // @"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Tracker.exe";

                var trackerCommandLineSwitches = FileTracker.TrackerCommandArguments(pathToTool, commandLineSwitchesBuffer.ToString());

                if (!string.IsNullOrEmpty(trackerToolPath))
                {
                    var trackerRootingMarker = OutOfDateInputFiles.Length > 0 ? FileTracker.FormatRootingMarker(OutOfDateInputFiles) : null;

                    var trackerResponseFileArguments = FileTracker.TrackerResponseFileArguments(FileTracker.GetFileTrackerPath(ExecutableType.Native64Bit), new TaskItemHelper(TrackerIntermediateDirectory).FullPath, trackerRootingMarker, null);

                    var trackerResponseFile = Path.GetTempFileName();

                    File.WriteAllText(trackerResponseFile, trackerResponseFileArguments, ResponseFileEncoding);

                    // /a : Enable extended tracking: GetFileAttributes, GetFileAttributesEx
                    // /e : Enable extended tracking: GetFileAttributes, GetFileAttributesEx, RemoveDirectory, CreateDirectory
                    // /k : Keep the full tool chain in tlog filenames.
                    // /t : Track command lines (will expand response files specified with the '@filename' syntax)
                    // (specifying /t will export *.command.*.tlog files)

                    trackerCommandLineSwitches = $"{PathUtils.QuoteIfNeeded("@" + trackerResponseFile)} /k {trackerCommandLineSwitches}";
                }

#if DEBUG
                Log.LogMessageFromText($"[{ToolName}] Tracker tool: {trackerToolPath}", MessageImportance.High);

                Log.LogMessageFromText($"[{ToolName}] Tracker command line: {trackerCommandLineSwitches}", MessageImportance.High);
#endif

                //
                //
                //

                responseFileSwitchesBuffer.Replace("\\", "\\\\");

                responseFileSwitchesBuffer.Replace("\\\\\\\\ ", "\\\\ ");

                if (true)
                {
                    exitCode = base.ExecuteTool(trackerToolPath, responseFileSwitchesBuffer.ToString(), trackerCommandLineSwitches);
                }
#if false
                else
                {
                    string responseFileSwitch = string.Empty;

                    if (responseFileSwitchesBuffer.Length > 0)
                    {
                        string responseFilePath = Path.Combine(TrackerLogDirectory, string.Format("{0}_{1}.rsp", ToolName, Guid.NewGuid().ToString()));

                        Directory.CreateDirectory(Path.GetDirectoryName(responseFilePath));

                        File.WriteAllText(responseFilePath, responseFileSwitchesBuffer.ToString(), ResponseFileEncoding);

                        responseFileSwitch = GetResponseFileSwitch(responseFilePath);
                    }

                    using var trackedProcess = new Process();

                    trackedProcess.StartInfo = base.GetProcessStartInfo(trackerToolPath, trackerCommandLineSwitches, responseFileSwitch);

                    trackedProcess.StartInfo.CreateNoWindow = true;

                    trackedProcess.StartInfo.UseShellExecute = false;

                    trackedProcess.StartInfo.ErrorDialog = false;

                    trackedProcess.StartInfo.RedirectStandardOutput = true;

                    trackedProcess.StartInfo.RedirectStandardError = true;

                    trackedProcess.OutputDataReceived += (object sender, DataReceivedEventArgs args) => LogEventsFromTextOutput(args.Data, MessageImportance.Low);

                    trackedProcess.ErrorDataReceived += (object sender, DataReceivedEventArgs args) => LogEventsFromTextOutput(args.Data, MessageImportance.Low);

                    trackedProcess.EnableRaisingEvents = true;

                    if (OutputCommandLine)
                    {
                        Log.LogMessageFromText(string.Format("[{0}] Process started: {1} {2}", ToolName, trackedProcess.StartInfo.FileName, trackedProcess.StartInfo.Arguments), MessageImportance.High);
                    }

                    if (!trackedProcess.Start())
                    {
                        throw new InvalidOperationException("Could not start tracked child process.");
                    }

                    trackedProcess.BeginOutputReadLine();

                    trackedProcess.BeginErrorReadLine();

                    trackedProcess.WaitForExit();

                    exitCode = trackedProcess.ExitCode;
                }
#endif
            }
            catch (Exception e)
            {
                Log.LogErrorFromException(e, true);

                exitCode = -1;
            }

            return(exitCode);
        }
예제 #3
0
        protected int TrackerExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
        {
            string dllName               = (string)null;
            string str1                  = (string)null;
            bool   trackFileAccess       = this.TrackFileAccess;
            string str2                  = Environment.ExpandEnvironmentVariables(pathToTool);
            string responseFileCommands1 = responseFileCommands;
            string arguments             = Environment.ExpandEnvironmentVariables(commandLineCommands);

            try
            {
                this.pathToLog = str2;
                string pathToTool1;
                if (trackFileAccess)
                {
                    ExecutableType result = ExecutableType.SameAsCurrentProcess;
                    if (!string.IsNullOrEmpty(this.ToolArchitecture))
                    {
                        if (!Enum.TryParse <ExecutableType>(this.ToolArchitecture, out result))
                        {
                            this.Log.LogErrorWithCodeFromResources("General.InvalidValue", (object)"ToolArchitecture", (object)this.GetType().Name);
                            return(-1);
                        }
                    }
                    else
                    {
                        ExecutableType?toolType = this.ToolType;
                        if (toolType.HasValue)
                        {
                            toolType = this.ToolType;
                            result   = toolType.Value;
                        }
                    }
                    bool is64bit;
                    if ((result == ExecutableType.Native32Bit || result == ExecutableType.Native64Bit) && NativeMethodsShared.Is64bitApplication(str2, out is64bit))
                    {
                        result = is64bit ? ExecutableType.Native64Bit : ExecutableType.Native32Bit;
                    }
                    try
                    {
                        pathToTool1 = FileTracker.GetTrackerPath(result, this.TrackerSdkPath);
                        if (pathToTool1 == null)
                        {
                            this.Log.LogErrorFromResources("Error.MissingFile", (object)"tracker.exe");
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ExceptionHandling.NotExpectedException(ex))
                        {
                            throw;
                        }
                        else
                        {
                            this.Log.LogErrorWithCodeFromResources("General.InvalidValue", (object)"TrackerSdkPath", (object)this.GetType().Name);
                            return(-1);
                        }
                    }
                    try
                    {
                        dllName = FileTracker.GetFileTrackerPath(result, this.TrackerFrameworkPath);
                    }
                    catch (Exception ex)
                    {
                        if (ExceptionHandling.NotExpectedException(ex))
                        {
                            throw;
                        }
                        else
                        {
                            this.Log.LogErrorWithCodeFromResources("General.InvalidValue", (object)"TrackerFrameworkPath", (object)this.GetType().Name);
                            return(-1);
                        }
                    }
                }
                else
                {
                    pathToTool1 = str2;
                }
                if (string.IsNullOrEmpty(pathToTool1))
                {
                    return(-1);
                }
                ErrorUtilities.VerifyThrowInternalRooted(pathToTool1);
                string commandLineCommands1;
                if (trackFileAccess)
                {
                    string str3 = FileTracker.TrackerArguments(str2, arguments, dllName, this.TrackerIntermediateDirectory, this.RootSource, this.CancelEventName);
                    this.Log.LogMessageFromResources(MessageImportance.Low, "Native_TrackingCommandMessage", new object[0]);
                    this.Log.LogMessage(MessageImportance.Low, pathToTool1 + (this.AttributeFileTracking ? " /a " : " ") + str3 + " " + responseFileCommands1, new object[0]);
                    str1 = FileUtilities.GetTemporaryFile();
                    using (StreamWriter streamWriter = new StreamWriter(str1, false, Encoding.Unicode))
                        streamWriter.Write(FileTracker.TrackerResponseFileArguments(dllName, this.TrackerIntermediateDirectory, this.RootSource, this.CancelEventName));
                    commandLineCommands1 = (this.AttributeFileTracking ? "/a @\"" : "@\"") + str1 + "\"" + FileTracker.TrackerCommandArguments(str2, arguments);
                }
                else
                {
                    commandLineCommands1 = arguments;
                }
                return(base.ExecuteTool(pathToTool1, responseFileCommands1, commandLineCommands1));
            }
            finally
            {
                if (str1 != null)
                {
                    this.DeleteTempFile(str1);
                }
            }
        }