Example #1
0
        private async Task ExecuteInitPs1Async(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))
                    {
                        NuGetPowerShellUsage.RaiseInitPs1LoadEvent(isPMC: _activeConsole is IWpfConsole);

                        if (_scriptExecutor.Value.TryMarkVisited(identity, PackageInitPS1State.FoundAndExecuted))
                        {
                            // always execute init script on a background thread
                            await TaskScheduler.Default;

                            var request = new ScriptExecutionRequest(scriptPath, installPath, identity, project: null);

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

                            return;
                        }
                    }
                }

                _scriptExecutor.Value.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.WriteErrorToActivityLog(ex);
            }
        }
        void ExecutePSScriptInternal(ScriptMessage message)
        {
            try {
                var request = new ScriptExecutionRequest(
                    message.ScriptPath,
                    message.InstallPath,
                    message.Identity,
                    message.Project);

                var psVariable = SessionState.PSVariable;

                // set temp variables to pass to the script
                psVariable.Set("__rootPath", request.InstallPath);
                psVariable.Set("__toolsPath", request.ToolsPath);
                psVariable.Set("__package", request.ScriptPackage);
                psVariable.Set("__project", request.Project);

                if (request.ScriptPath != null)
                {
                    string command = "& " + PathUtility.EscapePSPath(request.ScriptPath) + " $__rootPath $__toolsPath $__package $__project";
                    LogCore(MessageLevel.Info, String.Format(CultureInfo.CurrentCulture, "Executing script file '{0}'", request.ScriptPath));

                    InvokeCommand.InvokeScript(command, false, PipelineResultTypes.Error, null, null);
                }

                // clear temp variables
                SessionState.PSVariable.Remove("__rootPath");
                SessionState.PSVariable.Remove("__toolsPath");
                SessionState.PSVariable.Remove("__package");
                SessionState.PSVariable.Remove("__project");
            } catch (Exception ex) {
                message.Exception = ex;
            } finally {
                message.EndSemaphore.Release();
            }
        }
Example #3
0
        public async Task <bool> ExecuteAsync(
            PackageIdentity identity,
            string installPath,
            string relativeScriptPath,
            EnvDTEProject project,
            INuGetProjectContext nuGetProjectContext,
            bool throwOnFailure)
        {
            var scriptPath = Path.Combine(installPath, relativeScriptPath);

            if (File.Exists(scriptPath))
            {
                if (scriptPath.EndsWith(PowerShellScripts.Init, StringComparison.OrdinalIgnoreCase) &&
                    !TryMarkVisited(identity, PackageInitPS1State.FoundAndExecuted))
                {
                    return(true);
                }

                var request = new ScriptExecutionRequest(scriptPath, installPath, identity, project);

                var psNuGetProjectContext = nuGetProjectContext as IPSNuGetProjectContext;
                if (psNuGetProjectContext != null &&
                    psNuGetProjectContext.IsExecuting &&
                    psNuGetProjectContext.CurrentPSCmdlet != null)
                {
                    var psVariable = psNuGetProjectContext.CurrentPSCmdlet.SessionState.PSVariable;

                    // set temp variables to pass to the script
                    psVariable.Set("__rootPath", request.InstallPath);
                    psVariable.Set("__toolsPath", request.ToolsPath);
                    psVariable.Set("__package", request.ScriptPackage);
                    psVariable.Set("__project", request.Project);

                    psNuGetProjectContext.ExecutePSScript(request.ScriptPath, throwOnFailure);
                }
                else
                {
                    string logMessage = string.Format(CultureInfo.CurrentCulture, Resources.ExecutingScript, scriptPath);
                    // logging to both the Output window and progress window.
                    nuGetProjectContext.Log(MessageLevel.Info, logMessage);
                    try
                    {
                        await ExecuteScriptCoreAsync(request);
                    }
                    catch (Exception ex)
                    {
                        // throwFailure is set by Package Manager.
                        if (throwOnFailure)
                        {
                            throw;
                        }
                        nuGetProjectContext.Log(MessageLevel.Warning, ex.Message);
                    }
                }

                return(true);
            }
            else
            {
                if (scriptPath.EndsWith(PowerShellScripts.Init, StringComparison.OrdinalIgnoreCase))
                {
                    TryMarkVisited(identity, PackageInitPS1State.NotFound);
                }
            }
            return(false);
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <exception cref="Client.ApiException">Thrown when fails to make API call</exception>
        /// <param name="value"> (optional)</param>
        /// <returns>Task of ApiResponse (ScriptExecutionResponse)</returns>
        public async System.Threading.Tasks.Task <ApiResponse <ScriptExecutionResponse> > PostAsyncWithHttpInfo(ScriptExecutionRequest value = null)
        {
            var    localVarPath         = "/api/script";
            var    localVarPathParams   = new Dictionary <String, String>();
            var    localVarQueryParams  = new List <KeyValuePair <String, String> >();
            var    localVarHeaderParams = new Dictionary <String, String>(this.Configuration.DefaultHeader);
            var    localVarFormParams   = new Dictionary <String, String>();
            var    localVarFileParams   = new Dictionary <String, FileParameter>();
            Object localVarPostBody     = null;

            // to determine the Content-Type header
            String[] localVarHttpContentTypes = new String[] {
                "application/json-patch+json",
                "application/json",
                "text/json",
                "application/_*+json"
            };
            String localVarHttpContentType = this.Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes);

            // to determine the Accept header
            String[] localVarHttpHeaderAccepts = new String[] {
                "text/plain",
                "application/json",
                "text/json"
            };
            String localVarHttpHeaderAccept = this.Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts);

            if (localVarHttpHeaderAccept != null)
            {
                localVarHeaderParams.Add("Accept", localVarHttpHeaderAccept);
            }

            if (value != null && value.GetType() != typeof(byte[]))
            {
                localVarPostBody = this.Configuration.ApiClient.Serialize(value); // http body (model) parameter
            }
            else
            {
                localVarPostBody = value; // byte array
            }


            // make the HTTP request
            IRestResponse localVarResponse = (IRestResponse)await this.Configuration.ApiClient.CallApiAsync(localVarPath,
                                                                                                            Method.POST, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams,
                                                                                                            localVarPathParams, localVarHttpContentType);

            int localVarStatusCode = (int)localVarResponse.StatusCode;

            if (ExceptionFactory != null)
            {
                Exception exception = ExceptionFactory("Post", localVarResponse);
                if (exception != null)
                {
                    throw exception;
                }
            }

            return(new ApiResponse <ScriptExecutionResponse>(localVarStatusCode,
                                                             localVarResponse.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()),
                                                             (ScriptExecutionResponse)this.Configuration.ApiClient.Deserialize(localVarResponse, typeof(ScriptExecutionResponse))));
        }
Example #5
0
        /// <summary>
        ///
        /// </summary>
        /// <exception cref="Client.ApiException">Thrown when fails to make API call</exception>
        /// <param name="value"> (optional)</param>
        /// <returns>Task of ScriptExecutionResponse</returns>
        public async System.Threading.Tasks.Task <ScriptExecutionResponse> PostAsync(ScriptExecutionRequest value = null)
        {
            ApiResponse <ScriptExecutionResponse> localVarResponse = await PostAsyncWithHttpInfo(value);

            return(localVarResponse.Data);
        }
Example #6
0
        /// <summary>
        ///
        /// </summary>
        /// <exception cref="Client.ApiException">Thrown when fails to make API call</exception>
        /// <param name="value"> (optional)</param>
        /// <returns>ScriptExecutionResponse</returns>
        public ScriptExecutionResponse Post(ScriptExecutionRequest value = null)
        {
            ApiResponse <ScriptExecutionResponse> localVarResponse = PostWithHttpInfo(value);

            return(localVarResponse.Data);
        }