Пример #1
0
 public static void InfoResultsCallbackFn(uint cmdId, int msgId, int level, string data)
 {
     if (logOutput)
     {
         // level is generally from 0-9, though for some reason trying to add an
         // ignored file sends a message with a level of 34, so ignor level of 34
         if (level == 34)
         {
             level = 0;
         }
         try
         {
             string msg = "--->";
             for (int idx = 0; idx < level; idx++)
             {
                 msg += ". ";
             }
             msg += (data != null) ? data : string.Empty;
             P4VsOutputWindow.AppendMessage(msg);
             FileLogger.LogMessage(3, "P4API.NET", msg);
         }
         catch (Exception ex)
         {
             P4VsOutputWindow.AppendMessage(ex.Message);
             FileLogger.LogException("CommandLine_InfoResultsCallbackFn", ex);
         }
     }
 }
Пример #2
0
 public static void TaggedOutputCallbackFn(uint cmdId, int ObjId, P4.TaggedObject Obj)
 {
     if (logOutput)
     {
         try
         {
             if (Obj == null)
             {
                 return;
             }
             string msg = "--->Tagged Data: { ";
             foreach (string key in Obj.Keys)
             {
                 msg += string.Format("{{{0}:{1}}} ", key, Obj[key]);
             }
             msg += "}";
             P4VsOutputWindow.AppendMessage(msg);
             FileLogger.LogMessage(3, "P4API.NET", msg);
         }
         catch (Exception ex)
         {
             P4VsOutputWindow.AppendMessage(ex.Message);
             FileLogger.LogException("CommandLine_TaggedOutputCallbackFn", ex);
         }
     }
 }
Пример #3
0
 public static void TextResultsCallbackFn(uint cmdId, string data)
 {
     if (logOutput)
     {
         string msg = string.Format("--->{0}", data);
         P4VsOutputWindow.AppendMessage(msg);
         FileLogger.LogMessage(3, "P4API.NET", msg);
     }
 }
Пример #4
0
        private void LaunchApp()
        {
            Process launchHelixMFA = new Process();

            launchHelixMFA.StartInfo.Arguments = port + " " + user;
            launchHelixMFA.StartInfo.FileName  = path;
            string msg = string.Format("==>{0} {1}", path, launchHelixMFA.StartInfo.Arguments);

            P4VsOutputWindow.AppendMessage(msg);
            launchHelixMFA.StartInfo.CreateNoWindow = true;
            launchHelixMFA.EnableRaisingEvents      = true;
            launchHelixMFA.Start();
            launchHelixMFA.Exited += (sender, e) => { exitCode = launchHelixMFA.ExitCode; closeBtn_Click(sender, e); };
        }
Пример #5
0
        /// <summary>Get Perforce Connection details from Config file or Environment</summary>
        /// <param name="path">Path to P4CONFIG file</param>
        private void getConfiguration(string path)
        {
            string msg;

            if ((Port == null) && (User == null) && (Workspace == null))
            {
                try
                {
                    P4.P4Server sslCheck = new P4.P4Server(path);
                }
                catch (P4Exception ex)
                {
                    if (ex.ErrorCode == P4.P4ClientError.MsgRpc_HostKeyUnknown)
                    {
                        string[] message = ex.Message.Split('\'');
                        string   port    = "ssl:" + message[1];
                        RepositoryFactory.get(port, null, null);
                    }
                }

                using (P4.P4Server ps = new P4.P4Server(path))
                {
                    Port      = ps.Port;
                    User      = ps.User;
                    Workspace = ps.Client;

                    string config = ps.Config;
                    if ((string.IsNullOrEmpty(config) == false) && (config != "noconfig"))
                    {
                        // api is using a config file
                        msg = String.Format(Resources.P4ScmProvider_UsingConfigSettingsToConnect, Port, User, Workspace, config);
                    }
                    else
                    {
                        msg = String.Format(Resources.P4ScmProvider_UsingEnvironmentSettingsToConnect, Port, User, Workspace);
                    }
                }
            }
            else
            {
                msg = String.Format(Resources.P4ScmProvider_ConnectingToPerforceServer, Port, User, Workspace);
            }

            // Log configuration settings
            P4VsOutputWindow.AppendMessage(msg);

            logger.Trace("scm ID:{0}", ID);
            FileLogger.LogMessage(3, "P4API.NET", msg);
        }
Пример #6
0
        public static void ErrorCallbackFn(uint cmdId, int severity, int errorNumber, string data)
        {
            try
            {
                if ((commandLogged == false) && (lastCommand != null))
                {
                    FileLogger.LogMessage(4, "P4API.NET", lastCommand);
                }

                string severityStr = string.Empty;
                if ((severity < 0) || (severity >= ErrorSeverity.Length))
                {
                    severityStr = string.Format("E_{0}", severity);
                }
                else
                {
                    severityStr = ErrorSeverity[severity];
                }

                string msg = string.Format("{0}: {1}", severityStr, data);

                if (logOutput)
                {
                }
                if ((severity < 0) || (severity >= ErrorSeverity.Length))
                {
                    FileLogger.LogMessage(0, "P4API.NET", msg);
                }
                else
                {
                    FileLogger.LogMessage(MessageSeverity[severity], "P4API.NET", msg);
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("Error trying to log message: [{0}]: {1}", severity, data);
                P4VsOutputWindow.AppendMessage(msg);
                FileLogger.LogMessage(0, "P4API.NET", msg);
                P4VsOutputWindow.AppendMessage(ex.Message);
                FileLogger.LogException("CommandLine_InfoResultsCallbackFn", ex);
            }
        }
Пример #7
0
        private void initConnection(bool noUI, string path, bool clientNotRequired)
        {
            ID = _id++;

            if (P4.LogFile.ExternalLogFn == null)
            {
                P4.LogFile.LogMessageDelgate logfn = new LogFile.LogMessageDelgate(FileLogger.LogMessage);
                P4.LogFile.SetLoggingFunction(logfn);
            }

            // reset bool on init
            ssoSuccess = true;

            _disconnected = true;

            if (Repository != null)
            {
                Repository.Dispose();
                Repository = null;
            }

            while (Repository == null)
            {
                // Get connection settings from dialog, if UI required
                if (!noUI)
                {
                    // reset the SSO bool, so login will be attempted again
                    ssoSuccess = true;
                    // Open Connection dialog
                    DialogResult result = connectionDialog();
                    if (result == DialogResult.Cancel)
                    {
                        return;
                    }
                }

                // Get the credentials for the Perforce Server
                try
                {
                    // Get configuration
                    getConfiguration(path);

                    // Create repository from server connection
                    Repository = RepositoryFactory.get(Port, User, Workspace);

                    // set Swarm here since Login will check Swarm availability
                    Swarm = new Swarm(Repository, User);
                    if (string.IsNullOrEmpty(Workspace) && !clientNotRequired)
                    {
                        DialogResult res = P4ErrorDlg.Show(Resources.P4ScmProvider_WorkspaceEnvUnset,
                                                           false, false);
                        P4VsProvider.BroadcastNewConnection(null);
                        return;
                    }

                    if (Repository == null ||
                        Repository.Connection.Status == P4.ConnectionStatus.Disconnected)
                    {
                        Repository = null;
                        continue;
                    }

                    // Set configuration
                    setConfiguration();

                    // Check API level of server is greater than 28 (2009.2)
                    checkApiLevel(28);

                    // set Swarm here since Login will check Swarm availability
                    Swarm = new Swarm(Repository, User);

                    // Login if required
                    if (!string.IsNullOrEmpty(User) && !isLoggedIn())
                    {
                        if (!ssoSuccess)
                        {
                            break;
                        }
                        LoginResult loginResult = Login();
                        if (loginResult == LoginResult.HASTimeout)
                        {
                            P4VS.UI.P4VSMessage p4VSMessage = new P4VS.UI.P4VSMessage(Resources.P4VS,
                                                                                      string.Format(Resources.HAS_Auth_Fail,
                                                                                                    Repository.Connection.UserName, Repository.Connection.Server.Address.Uri));
                            p4VSMessage.ShowDialog();
                            return;
                        }
                        if (loginResult == LoginResult.Fail)
                        {
                            Repository = null;
                            continue;
                        }
                    }

                    // Exit if no client defined
                    if (!string.IsNullOrEmpty(User) && !string.IsNullOrEmpty(Workspace))
                    {
                        setWorkspace();

                        // Save Connection as User and Workspace are good
                        saveRecentConnection();
                    }
                    else
                    {
                        logger.Warn("Workspace not initialised.");
                    }

                    if (Repository.Connection.Server.Metadata.UnicodeEnabled)
                    {
                        string cs = Repository.Connection.CharacterSetName;
                        string m  = string.Format(Resources.P4ScmProvider_ConnectingToUnicodeServer, cs);
                        P4VsOutputWindow.AppendMessage(m);
                        FileLogger.LogMessage(3, "P4ScmProvider", m);
                    }

                    // Subscribe to the output events to display results in the command window
                    Repository.Connection.InfoResultsReceived  += CommandLine.InfoResultsCallbackFn;
                    Repository.Connection.ErrorReceived        += CommandLine.ErrorCallbackFn;
                    Repository.Connection.TextResultsReceived  += CommandLine.TextResultsCallbackFn;
                    Repository.Connection.TaggedOutputReceived += CommandLine.TaggedOutputCallbackFn;
                    Repository.Connection.CommandEcho          += CommandLine.CommandEchoCallbackFn;

                    _disconnected = false;

                    // Connection OK, break out of loop
                    break;
                }
                catch (P4Exception ex)
                {
                    if (ex.ErrorCode == P4.P4ClientError.MsgServer_Login2Required)
                    {
                        P4VsProvider.CurrentScm = new P4ScmProvider(null);
                        if (P4VsProvider.CurrentScm.LaunchHelixMFA(User, Port) == 0)
                        {
                            noUI = true;
                            initConnection(noUI, path, clientNotRequired);
                        }
                    }
                    else
                    {
                        logger.Trace("caught an Exception: {0}\r\n{1}", ex.Message, ex.StackTrace);
                        if (Repository != null)
                        {
                            Repository.Dispose();
                            Repository = null;
                        }
                        MessageBox.Show(ex.Message, Resources.P4VS, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        P4VsOutputWindow.AppendMessage(ex.Message);
                    }
                    // If we're not showing the connection dialog return a null
                    if (noUI)
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    logger.Trace("caught an Exception: {0}\r\n{1}", ex.Message, ex.StackTrace);
                    if (Repository != null)
                    {
                        Repository.Dispose();
                        Repository = null;
                    }
                    MessageBox.Show(ex.Message, Resources.P4VS, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    P4VsOutputWindow.AppendMessage(ex.Message);

                    // If we're not showing the login dialog return a null as the credentials are bad
                    if (noUI)
                    {
                        return;
                    }
                }
            }

            Repository.Connection.KeepAlive    = new KeepAliveMonitor();
            JobsToolWindowControl.GotJobFields = false;
        }
Пример #8
0
        private bool Login(string password)
        {
            // if Swarm is null, don't attempt to set this
            if (Swarm != null)
            {
                Swarm.SwarmCredential = null;
            }

            // unsure if this currently works with legacy SSO script support, but
            // leaving it in as it is harmless if ssoScript is null and if not,
            // it should be the same as the ssoVar found in Login()
            string ssoScript = Environment.GetEnvironmentVariable("P4LOGINSSO");

            if (ssoScript != null)
            {
                password = "******"; // dummy value server will not prompt for a password when using SSO
                logger.Debug("Using SSO: {0}", ssoScript);
            }

            try
            {
                bool UseAllHostTicket = !Preferences.LocalSettings.GetBool("Use_IP", false);
                if (string.IsNullOrEmpty(password) != true)
                {
                    // always first login on the local machine for an 'all machines token, as we can't read the property
                    // to see if swarm is enabled till we log in
                    logger.Debug("Logging in to local machine");
                    LoginCmdOptions opts       = new LoginCmdOptions(LoginCmdFlags.AllHosts, null);
                    P4.Credential   Credential = Repository.Connection.Login(password, opts);
                    if (Credential != null)
                    {
                        // Now we can see if Swarm is enabled as since we logged in
                        // the property to see if swarm is enabled
                        Swarm.CheckForSwarm();
                        if (Swarm.SwarmEnabled)
                        {
                            Swarm.SwarmCredential = Credential;
                        }
                        if (Credential.Expires != DateTime.MaxValue)
                        {
                            string msg = string.Format(Resources.P4ScmProvider_LoginLoginExpireInfo, Credential.Expires);
                            P4VsOutputWindow.AppendMessage(msg);
                        }
                        return(true);
                    }
                    else if (Repository.Connection.LastResults.ErrorList != null)
                    {
                        // the error code for an SSO login fail will be sent by the trigger
                        // so it will be a trigger failed message.
                        if (Repository.Connection.LastResults.ErrorList[0].ErrorCode == P4.P4ClientError.MsgServer_TriggerFailed)
                        {
                            P4VsOutputWindow.AppendMessage(Repository.Connection.LastResults.ErrorList[0].ErrorMessage);
                            MessageBox.Show(Repository.Connection.LastResults.ErrorList[0].ErrorMessage,
                                            Resources.P4VS, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            ssoSuccess = false;
                            return(true);
                        }
                        // Or if it is a fatal error, display that as well. This can
                        // happen if the P4LOGINSSO is set but the agent does not
                        // exist. Likely an edge case.
                        foreach (P4ClientError e in Repository.Connection.LastResults.ErrorList)
                        {
                            if (e.SeverityLevel == ErrorSeverity.E_FATAL)
                            {
                                P4VsOutputWindow.AppendMessage(Repository.Connection.LastResults.ErrorList[0].ErrorMessage);
                                MessageBox.Show(Repository.Connection.LastResults.ErrorList[0].ErrorMessage,
                                                Resources.P4VS, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                ssoSuccess = false;
                                return(true);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                P4Exception p4ex = ex as P4Exception;
                if (p4ex.ErrorCode == P4.P4ClientError.MsgServer_Login2Required)
                {
                    throw p4ex;
                }
                P4VsOutputWindow.AppendMessage(ex.Message);
                MessageBox.Show(ex.Message, Resources.P4VS, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return(false);
        }
Пример #9
0
        public void CheckForSwarm()
        {
            SwarmUrl = null;

            if ((repository == null) || (repository.Connection == null) ||
                repository.Connection.Status == ConnectionStatus.Disconnected || checkedForSwarm)
            {
                return;
            }
            P4.P4Command propertyCmd = repository.Connection.CreateCommand("property", true);
            P4.Options   opts        = new P4.Options();
            opts["-l"] = null;
            opts["-n"] = P4SwarmPropertyName;

            P4.P4CommandResult results = null;
            try
            {
                results = propertyCmd.Run(opts);
            }
            catch (P4Exception ex)
            {
                if (ex.ErrorCode == P4.P4ClientError.MsgServer_Login2Required)
                {
                    throw ex;
                }
                // error getting property, likely not logged in
                return;
            }
            catch
            {
                // error getting property, likely not logged in
                return;
            }
            //command ran, so if no property than not attached to swarm
            checkedForSwarm = true;

            if (results.TaggedOutput != null)
            {
                foreach (TaggedObject tag in results.TaggedOutput)
                {
                    if (tag.ContainsKey("name") && tag["name"].StartsWith(P4SwarmPropertyName) && tag.ContainsKey("value"))
                    {
                        SwarmUrl = tag["value"].TrimEnd('/');

                        SwarmApi.SwarmServer sw = new SwarmApi.SwarmServer(SwarmUrl, user, SwarmPassword);

                        if (certHandler == null)
                        {
                            certHandler = new ScmSSLCertificateHandler();
                            certHandler.Init(SwarmUrl);
                        }
                        SwarmVersion = sw.GetVersion;

                        if (SwarmVersion == null)
                        {
                            SwarmUrl = null;
                            return;
                        }

                        string msg = String.Format(Resources.P4ScmProvider_ConnectedToSwarmServer,
                                                   SwarmUrl, SwarmVersion.version);
                        P4VsOutputWindow.AppendMessage(msg);

                        FileLogger.LogMessage(3, "P4API.NET", msg);
                        return;
                    }
                }
            }
            return;
        }
Пример #10
0
        public static void CommandEchoCallbackFn(string data)
        {
            if (data.StartsWith("DataSet set to:"))
            {
                // echoing the commands data set, record this only to the log file.
                FileLogger.LogMessage(3, "P4API.NET", data);
                return;
            }
            lastCommand = data;

            string[] cmds = data.Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
            string   cmd  = cmds[0];

            bool log = false;

            switch (cmd)
            {
            case "edit":
            case "add":
            case "delete":
            case "move":
            case "copy":
            case "merge":
            case "integ":
            case "sync":
            case "revert":
            case "submit":
            case "fix":
            case "shelve":
            case "resolve":
            case "unshelve":
            case "Lock":
            case "Unlock":
            case "Attribute":
            case "Counter":
            case "Labelsync":
            case "List":
            case "Populate":
            case "Reconcile":
            case "Status":
            case "Tag":
            case "Reopen":
                log = true;
                break;

            case "depot":
            case "client":
            case "workspace":
            case "user":
            case "group":
            case "changelist":
            case "job":
            case "branch":
            case "label":
            case "stream":
            case "protect":
            case "triggers":
                if ((data.IndexOf(" -i ") > 0) || (data.EndsWith(" -i")) ||
                    (data.IndexOf(" -d ") > 0) || (data.EndsWith(" -d")))
                {
                    log = true;
                }
                break;

            case "fstat":
                logger.Trace(data);
                break;

            default:
                break;
            }
            if ((log) || (Preferences.LocalSettings.GetBool("Log_reporting", false) == true))
            {
                logOutput = log && Preferences.LocalSettings.GetBool("Log_command", false);
                string msg = string.Format("->{0}", data);
                logger.Trace("{0}: {1}", DateTime.Now.ToString("hh:mm:ss.fff"), msg);
                P4VsOutputWindow.AppendMessage(msg);
                FileLogger.LogMessage(3, "P4API.NET", msg);
                commandLogged = true;
                return;
            }
            commandLogged = false;
            logOutput     = false;
        }