void RunScriptInternal(RunScriptParams message)
        {
            var blockingCollection = ConsoleHostServices.ActiveBlockingCollection;

            var version  = NuGet.Versioning.NuGetVersion.Parse(message.PackageVersion);
            var identity = new PackageIdentity(message.PackageId, version);
            var project  = ProjectFactory.CreateProject(message.Project);

            var scriptMessage = new ScriptMessage(
                message.ScriptPath,
                message.InstallPath,
                identity,
                project);

            blockingCollection.Add(scriptMessage);

            WaitHandle.WaitAny(new WaitHandle[] { scriptMessage.EndSemaphore, cancellationTokenSource.Token.WaitHandle });

            if (scriptMessage.Exception == null)
            {
                return;
            }

            if (message.ThrowOnFailure)
            {
                throw scriptMessage.Exception;
            }

            Log(LogLevel.Warning, scriptMessage.Exception.Message);
        }
예제 #2
0
        public async Task <bool> ExecuteAsync(
            PackageIdentity identity,
            string installPath,
            string relativeScriptPath,
            DotNetProject project,
            INuGetProjectContext nuGetProjectContext,
            bool throwOnFailure,
            CancellationToken token)
        {
            var scriptPath = Path.Combine(installPath, relativeScriptPath);

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

                var consoleNuGetProjectContext = nuGetProjectContext as ConsoleHostNuGetProjectContext;
                if (consoleNuGetProjectContext != null &&
                    consoleNuGetProjectContext.IsExecutingPowerShellCommand)
                {
                    var message = new RunScriptParams {
                        ScriptPath     = scriptPath,
                        InstallPath    = installPath,
                        PackageId      = identity.Id,
                        PackageVersion = identity.Version.ToNormalizedString(),
                        Project        = project.CreateProjectInformation(),
                        ThrowOnFailure = throwOnFailure
                    };
                    await RunScriptWithRemotePowerShellConsoleHost(message, nuGetProjectContext, throwOnFailure, token);
                }
                else
                {
                    var logMessage = GettextCatalog.GetString("Executing script file '{0}'...", scriptPath);
                    nuGetProjectContext.Log(MessageLevel.Info, logMessage);

                    var message = new RunInitScriptParams {
                        ScriptPath     = scriptPath,
                        InstallPath    = installPath,
                        PackageId      = identity.Id,
                        PackageVersion = identity.Version.ToNormalizedString(),
                        ThrowOnFailure = throwOnFailure
                    };
                    await RunInitScriptWithRemotePowerShellConsoleHost(message, nuGetProjectContext, throwOnFailure, token);
                }

                return(true);
            }
            else
            {
                if (scriptPath.EndsWith(PowerShellScripts.Init, StringComparison.OrdinalIgnoreCase))
                {
                    TryMarkVisited(identity, PackageInitPS1State.NotFound);
                }
            }
            return(false);
        }
예제 #3
0
 async Task RunScriptWithRemotePowerShellConsoleHost(
     RunScriptParams message,
     INuGetProjectContext nuGetProjectContext,
     bool throwOnFailure,
     CancellationToken token)
 {
     await RunScriptWithRemotePowerShellConsoleHost(Methods.RunScript, message, nuGetProjectContext, throwOnFailure, token);
 }
예제 #4
0
        private void RunScript(object threadParams)
        {
            bool            fatalError = false;
            RunScriptParams runParams  = (RunScriptParams)threadParams;

            do
            {
                foreach (CAPDU capdu in runParams.script)
                {
                    AddToResult("\nCommand:");
                    AddToResult(capdu.AsString(" "), Color.Blue);

                    /* Perform the exchange */
                    RAPDU rapdu = runParams.card.Transmit(capdu);

                    /* Error during the exchange? */
                    if (rapdu == null)
                    {
                        AddToResult("\nError " + BinConvert.ToHex(runParams.card.LastError) + "\n", Color.Red);
                        AddToResult(runParams.card.LastErrorAsString + "\n", Color.Red);
                        if (runParams.stopOnPCSCError)
                        {
                            fatalError = true;
                        }
                        runParams.loop = false;
                        break;
                    }

                    /* Status word? */
                    Color responseColor;
                    bool  responseError = false;
                    if ((rapdu.SW == 0x9000) || (rapdu.SW == 0x9100) || (rapdu.SW == 0x91AF))
                    {
                        responseColor = Color.Green;
                    }
                    else if (rapdu.SW1 != 0x6F)
                    {
                        responseColor = Color.DarkOrange;
                        responseError = true;
                    }
                    else
                    {
                        responseColor = Color.Red;
                        responseError = true;
                    }

                    if (runParams.showHex)
                    {
                        AddToResult("\nResponse: (" + rapdu.Length + "B)\n");
                        AddToResult(rapdu.AsString(" "), responseColor);
                    }

                    if (runParams.showAscii)
                    {
                        if ((rapdu.data != null) && (rapdu.data.Length > 0))
                        {
                            AddToResult("\nResponse (ASCII):\n");
                            AddToResult(ConvertToString(rapdu.data.GetBytes()) + " / SW=" + BinConvert.ToHex(rapdu.SW), responseColor);
                        }
                        else if (!runParams.showHex)
                        {
                            AddToResult("\nResponse:\n");
                            AddToResult("SW=" + BinConvert.ToHex(rapdu.SW), responseColor);
                        }
                    }

                    AddToResult("\n(" + rapdu.SWString + ")\n", responseColor);

                    if (runParams.stopOnSWnot9000 && responseError)
                    {
                        runParams.loop = false;
                        break;
                    }
                }
            }while (runParams.loop);

            ScriptExited(fatalError);
        }
예제 #5
0
        public bool StartScript()
        {
            string scriptText = richTextBoxScript.Text;

            string[] scriptLines = scriptText.Split(new char[] { '\n' });

            List <CAPDU> capduList = new List <CAPDU>();

            int    lineCount = 0;
            string command   = "";

            foreach (string scriptLine in scriptLines)
            {
                string line = scriptLine;
                lineCount++;

                /* Skip comments	*/
                if (line.StartsWith("#"))
                {
                    continue;
                }
                if (line.StartsWith(";"))
                {
                    continue;
                }
                /* Skip blank lines	*/
                line = line.Replace(" ", "");
                if (line.Equals(""))
                {
                    continue;
                }
                /* If line ends with \, concatenation with next line	*/
                if (line.EndsWith("\\"))
                {
                    command += line.Remove(line.Length - 1);
                    continue;
                }
                command += line;

                /* Retrieve the C-APDU */
                CAPDU capdu;
                try
                {
                    capdu = new CAPDU(command);
                }
                catch
                {
                    AddToResult("Syntax error on line " + lineCount + "\n", Color.Red);
                    return(false);
                }
                command = "";

                capduList.Add(capdu);
            }

            if (capduList.Count == 0)
            {
                AddToResult("The script is empty\n", Color.Orange);
                return(false);
            }

            RunScriptParams runParams = new RunScriptParams();

            runParams.card            = Card;
            runParams.script          = capduList;
            runParams.loop            = cbLoop.Checked;
            runParams.stopOnSWnot9000 = cbStopOnStatus.Checked;
            runParams.stopOnPCSCError = cbStopOnError.Checked;
            runParams.showHex         = rbHex.Checked || rbHexAndAscii.Checked;
            runParams.showAscii       = rbAscii.Checked || rbHexAndAscii.Checked;

            this.Enabled = false;

            AddToResult("Execution starting at " + DateTime.Now.ToLongTimeString() + "\n\n");

            ScriptThread = new Thread(RunScript);
            ScriptThread.Start(runParams);
            return(true);
        }