public void Restore(string projectPath, Action onFailure)
        {
            Task.Factory.StartNew(() =>
            {
                _logger.LogInformation($"Begin restoring project {projectPath}");

                var projectLock = _projectLocks.GetOrAdd(projectPath, new object());
                lock (projectLock)
                {
                    var exitCode = -1;
                    _emitter.RestoreStarted(projectPath);
                    _semaphore.Wait();
                    try
                    {
                        // A successful restore will update the project lock file which is monitored
                        // by the dotnet project system which eventually update the Roslyn model
                        exitCode = RunRestoreProcess(projectPath);
                    }
                    finally
                    {
                        _semaphore.Release();

                        object removedLock;
                        _projectLocks.TryRemove(projectPath, out removedLock);

                        _emitter.RestoreFinished(projectPath, exitCode == 0);

                        if (exitCode != 0)
                        {
                            onFailure();
                        }

                        _logger.LogInformation($"Finish restoring project {projectPath}. Exit code {exitCode}");
                    }
                }
            });
        }
예제 #2
0
        public Task RestoreAsync(string workingDirectory, string arguments = null, Action onFailure = null)
        {
            return(Task.Factory.StartNew(() =>
            {
                _logger.LogInformation($"Begin dotnet restore in '{workingDirectory}'");

                var restoreLock = _locks.GetOrAdd(workingDirectory, new object());
                lock (restoreLock)
                {
                    var exitStatus = new ProcessExitStatus(-1);
                    _eventEmitter.RestoreStarted(workingDirectory);
                    _semaphore.Wait();
                    try
                    {
                        // A successful restore will update the project lock file which is monitored
                        // by the dotnet project system which eventually update the Roslyn model
                        exitStatus = ProcessHelper.Run(DotNetPath, $"restore {arguments}", workingDirectory, updateEnvironment: RemoveMSBuildEnvironmentVariables);
                    }
                    finally
                    {
                        _semaphore.Release();

                        _locks.TryRemove(workingDirectory, out _);

                        _eventEmitter.RestoreFinished(workingDirectory, exitStatus.Succeeded);

                        if (exitStatus.Failed && onFailure != null)
                        {
                            onFailure();
                        }

                        _logger.LogInformation($"Finish restoring project {workingDirectory}. Exit code {exitStatus}");
                    }
                }
            }));
        }