private void ExecuteInitScripts() { // Fix for Bug 1426 Disallow ExecuteInitScripts from being executed concurrently by multiple threads. lock (_initScriptsLock) { if (!_solutionManager.IsSolutionOpen) { return; } Debug.Assert(_settings != null); if (_settings == null) { return; } try { // invoke init.ps1 files in the order of package dependency. // if A -> B, we invoke B's init.ps1 before A's. IEnumerable <NuGetProject> projects = _solutionManager.GetNuGetProjects(); NuGetPackageManager packageManager = new NuGetPackageManager(_sourceRepositoryProvider, _settings, _solutionManager); List <PackageIdentity> sortedPackages = new List <PackageIdentity>(); foreach (NuGetProject project in projects) { // Skip project K projects. if (project is NuGet.ProjectManagement.Projects.ProjectKNuGetProjectBase) { continue; } IEnumerable <PackageReference> installedRefs = project.GetInstalledPackagesAsync(CancellationToken.None).Result; if (installedRefs != null && installedRefs.Any()) { IEnumerable <PackageIdentity> installedPackages = packageManager.GetInstalledPackagesInDependencyOrder(project, new EmptyNuGetProjectContext(), CancellationToken.None).Result; sortedPackages.AddRange(installedPackages); } } // Get the path to the Packages folder. string packagesFolderPath = packageManager.PackagesFolderSourceRepository.PackageSource.Source; foreach (var package in sortedPackages) { PackagePathResolver packagePathResolver = new PackagePathResolver(packagesFolderPath); string pathToPackage = packagePathResolver.GetInstalledPath(package); string toolsPath = Path.Combine(pathToPackage, "tools"); AddPathToEnvironment(toolsPath); Runspace.ExecuteScript(toolsPath, PowerShellScripts.Init, package); } } catch (Exception ex) { // When Packages folder is not present, NuGetResolverInputException will be thrown // as resolving DependencyInfo requires the presence of Packages folder. if (ex.InnerException is NuGetResolverInputException) { // Silently fail. } else { // if execution of Init scripts fails, do not let it crash our console ReportError(ex); } ExceptionHelper.WriteToActivityLog(ex); } } }
private async Task ExecuteInitScriptsAsync() { // Fix for Bug 1426 Disallow ExecuteInitScripts from being executed concurrently by multiple threads. using (await _initScriptsLock.EnterAsync()) { if (!_solutionManager.IsSolutionOpen) { return; } Debug.Assert(_settings != null); if (_settings == null) { return; } // invoke init.ps1 files in the order of package dependency. // if A -> B, we invoke B's init.ps1 before A's. var sortedPackages = new List <PackageIdentity>(); var packagesFolderPackages = new HashSet <PackageIdentity>(PackageIdentity.Comparer); var globalPackages = new HashSet <PackageIdentity>(PackageIdentity.Comparer); var projects = _solutionManager.GetNuGetProjects().ToList(); var packageManager = new NuGetPackageManager( _sourceRepositoryProvider, _settings, _solutionManager, _deleteOnRestartManager); foreach (var project in projects) { // Skip project K projects. if (project is ProjectKNuGetProjectBase) { continue; } var buildIntegratedProject = project as BuildIntegratedNuGetProject; if (buildIntegratedProject != null) { var packages = BuildIntegratedProjectUtility.GetOrderedProjectDependencies(buildIntegratedProject); sortedPackages.AddRange(packages); globalPackages.UnionWith(packages); } else { var installedRefs = await project.GetInstalledPackagesAsync(CancellationToken.None); if (installedRefs != null && installedRefs.Any()) { // This will be an empty list if packages have not been restored var installedPackages = await packageManager.GetInstalledPackagesInDependencyOrder(project, CancellationToken.None); sortedPackages.AddRange(installedPackages); packagesFolderPackages.UnionWith(installedPackages); } } } // Get the path to the Packages folder. var packagesFolderPath = packageManager.PackagesFolderSourceRepository.PackageSource.Source; var packagePathResolver = new PackagePathResolver(packagesFolderPath); var globalFolderPath = SettingsUtility.GetGlobalPackagesFolder(_settings); var globalPathResolver = new VersionFolderPathResolver(globalFolderPath); var finishedPackages = new HashSet <PackageIdentity>(PackageIdentity.Comparer); foreach (var package in sortedPackages) { // Packages may occur under multiple projects, but we only need to run it once. if (!finishedPackages.Contains(package)) { finishedPackages.Add(package); try { string pathToPackage = null; // If the package exists in both the global and packages folder, use the packages folder copy. if (packagesFolderPackages.Contains(package)) { // Local package in the packages folder pathToPackage = packagePathResolver.GetInstalledPath(package); } else { // Global package pathToPackage = globalPathResolver.GetInstallPath(package.Id, package.Version); } if (!string.IsNullOrEmpty(pathToPackage)) { var toolsPath = Path.Combine(pathToPackage, "tools"); var scriptPath = Path.Combine(toolsPath, PowerShellScripts.Init); if (Directory.Exists(toolsPath)) { AddPathToEnvironment(toolsPath); if (File.Exists(scriptPath)) { if (_scriptExecutor.TryMarkVisited( package, PackageInitPS1State.FoundAndExecuted)) { var scriptPackage = new ScriptPackage( package.Id, package.Version.ToString(), pathToPackage); Runspace.ExecuteScript(pathToPackage, scriptPath, scriptPackage); } } else { _scriptExecutor.TryMarkVisited(package, PackageInitPS1State.NotFound); } } } } catch (Exception ex) { // if execution of Init scripts fails, do not let it crash our console ReportError(ex); ExceptionHelper.WriteToActivityLog(ex); } } } } }