コード例 #1
0
        internal int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands, ICompilerServerLogger logger)
        {
            if (ProvideCommandLineArgs)
            {
                CommandLineArgs = GetArguments(commandLineCommands, responseFileCommands)
                                  .Select(arg => new TaskItem(arg)).ToArray();
            }

            if (SkipCompilerExecution)
            {
                return(0);
            }

            try
            {
                var requestId = Guid.NewGuid();
                logger.Log($"Compilation request {requestId}, PathToTool={pathToTool}");

                string workingDirectory = CurrentDirectoryToUse();
                string?tempDirectory    = BuildServerConnection.GetTempPath(workingDirectory);

                if (!UseSharedCompilation ||
                    HasToolBeenOverridden ||
                    !BuildServerConnection.IsCompilerServerSupported)
                {
                    LogCompilationMessage(logger, requestId, CompilationKind.Tool, $"using command line tool by design '{pathToTool}'");
                    return(base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands));
                }

                _sharedCompileCts = new CancellationTokenSource();
                logger.Log($"CommandLine = '{commandLineCommands}'");
                logger.Log($"BuildResponseFile = '{responseFileCommands}'");

                var clientDirectory = Path.GetDirectoryName(PathToManagedTool);
                if (clientDirectory is null || tempDirectory is null)
                {
                    LogCompilationMessage(logger, requestId, CompilationKind.Tool, $"using command line tool because we could not find client or temp directory '{PathToManagedTool}'");
                    return(base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands));
                }

                // Note: using ToolArguments here (the property) since
                // commandLineCommands (the parameter) may have been mucked with
                // (to support using the dotnet cli)
                var buildRequest = BuildServerConnection.CreateBuildRequest(
                    requestId,
                    Language,
                    GetArguments(ToolArguments, responseFileCommands).ToList(),
                    workingDirectory: workingDirectory,
                    tempDirectory: tempDirectory,
                    keepAlive: null,
                    libDirectory: LibDirectoryToUse());

                var pipeName = !string.IsNullOrEmpty(SharedCompilationId)
                    ? SharedCompilationId
                    : BuildServerConnection.GetPipeName(clientDirectory);

                var responseTask = BuildServerConnection.RunServerBuildRequestAsync(
                    buildRequest,
                    pipeName,
                    clientDirectory,
                    logger: logger,
                    cancellationToken: _sharedCompileCts.Token);

                responseTask.Wait(_sharedCompileCts.Token);

                ExitCode = HandleResponse(requestId, responseTask.Result, pathToTool, responseFileCommands, commandLineCommands, logger);
            }
            catch (OperationCanceledException)
            {
                ExitCode = 0;
            }
            catch (Exception e)
            {
                Log.LogErrorWithCodeFromResources("Compiler_UnexpectedException");
                Log.LogErrorFromException(e);
                ExitCode = -1;
            }
            finally
            {
                _sharedCompileCts?.Dispose();
                _sharedCompileCts = null;
            }

            return(ExitCode);
        }