Exemplo n.º 1
0
        private void ExecuteInitPs1(string installPath, PackageIdentity identity)
        {
            try
            {
                var toolsPath = Path.Combine(installPath, "tools");
                if (Directory.Exists(toolsPath))
                {
                    AddPathToEnvironment(toolsPath);

                    var scriptPath = Path.Combine(toolsPath, PowerShellScripts.Init);
                    if (File.Exists(scriptPath) &&
                        _scriptExecutor.TryMarkVisited(identity, PackageInitPS1State.FoundAndExecuted))
                    {
                        var request = new ScriptExecutionRequest(scriptPath, installPath, identity, project: null);

                        Runspace.Invoke(
                            request.BuildCommand(),
                            request.BuildInput(),
                            outputResults: true);

                        return;
                    }
                }

                _scriptExecutor.TryMarkVisited(identity, PackageInitPS1State.NotFound);
            }
            catch (Exception ex)
            {
                // If execution of an init.ps1 scripts fails, do not let it crash our console.
                ReportError(ex);

                ExceptionHelper.WriteToActivityLog(ex);
            }
        }
Exemplo n.º 2
0
        private void ExecuteInitPs1(string pathToPackage, PackageIdentity package)
        {
            try
            {
                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);
            }
        }
        async Task ExecuteInitPs1Async(
            string installPath,
            PackageIdentity identity,
            INuGetProjectContext context,
            CancellationToken token)
        {
            try {
                var toolsPath = Path.Combine(installPath, "tools");
                if (Directory.Exists(toolsPath))
                {
                    //AddPathToEnvironment (toolsPath);

                    var relativePath = Path.Combine("tools", PowerShellScripts.Init);

                    if (!scriptHasRun)
                    {
                        // Make sure first messages from running script are not on PowerShell prompt line.
                        scriptHasRun = true;
                        context.Log(MessageLevel.Info, "\n");
                    }

                    await scriptExecutor.ExecuteAsync(
                        identity,
                        installPath,
                        relativePath,
                        null,
                        context,
                        false,
                        token);

                    return;
                }

                scriptExecutor.TryMarkVisited(identity, PackageInitPS1State.NotFound);
            } catch (Exception ex) {
                LoggingService.LogError("ExecuteInitPs1Async error", ex);
            }
        }
Exemplo n.º 4
0
        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);
                        }
                    }
                }
            }
        }