Ejemplo n.º 1
0
        /// <summary>
        /// Deploys the M-Files Application to a client and launches it.
        /// </summary>
        /// <param name="grfLaunch">Launch flags</param>
        /// <returns>Success code</returns>
        public override int DebugLaunch( uint grfLaunch )
        {
            // TODO: For debugging, check the grfLaunch flags.
            // The flags differ for "Launch" and "Launch with debugging".
            CCITracing.TraceCall();

            // Resolve the test vault.
            var clientApp = new MFilesAPI.MFilesClientApplication();
            var vaultName = GetConfigurationProperty( "TestVault", true );
            MFilesAPI.VaultConnection vaultConnection;

            try
            {
                // Try to get the connection.
                vaultConnection = clientApp.GetVaultConnection( vaultName );
            }
            catch
            {
                // Vault wasn't found, ask the user for a new one.
                var selectVaultDialog = new SelectVaultDialog();
                selectVaultDialog.ShowDialog();
                if( selectVaultDialog.Result == System.Windows.Forms.DialogResult.Cancel )
                    return VSConstants.S_FALSE;

                // Get the user answer.
                vaultName = selectVaultDialog.VaultName;
                vaultConnection = clientApp.GetVaultConnection( selectVaultDialog.VaultName );

                // If the user defined this as the default vault, save it in the project file.
                if( selectVaultDialog.SetDefault )
                    this.SetConfigurationProperty( "TestVault", selectVaultDialog.VaultName );
            }

            // Get the M-Files install directory from the registry.
            var apiVersion = clientApp.GetAPIVersion().Display;
            var hklm64 = RegistryKey.OpenBaseKey( RegistryHive.LocalMachine, RegistryView.Registry64 );
            var mfKey = hklm64.OpenSubKey( @"Software\Motive\M-Files\" + apiVersion );
            var installDir = (string)mfKey.GetValue( "InstallDir" );
            mfKey.Close();
            hklm64.Close();

            // Log out to free the current application.
            MFilesAPI.Vault vault = null;
            try
            {
                vault = clientApp.BindToVault( vaultName, IntPtr.Zero, false, true );
                if( vault != null ) vault.LogOutWithDialogs( IntPtr.Zero );
            }
            catch
            {
                // We most likely weren't logged in so everything is okay.
            }

            // Deploy the application.
            string vaultGuid = vaultConnection.GetGUID();
            var relativePath = string.Format( @"Client\Apps\{0}\sysapps\{1}",
                vaultGuid, this.project.GetProjectProperty( "Name" ) ?? "unnamed" );
            var targetDir = Path.Combine( installDir, relativePath );

            // If the directory exists, remove it so there's no residue files left.
            if( Directory.Exists( targetDir ) ) { Directory.Delete( targetDir, true ); }
            Directory.CreateDirectory( targetDir );

            // Extract the Zip contents to the target directory.
            DeployPackage( targetDir );

            // Log back into the application.
            vault = clientApp.BindToVault( vaultName, IntPtr.Zero, true, true );

            // If vault is null, the user cancelled the login -> Exit
            if( vault == null ) return VSConstants.S_FALSE;

            // Figure out the launch mode.
            var launchMode = ( GetConfigurationProperty( "LaunchMode", false ) ?? "" ).ToLowerInvariant();
            if( launchMode == "powershell" )
            {
                // Create a powershell window to launch the application.

                // Create the initial state with the app and vault references.
                var builtInState = InitialSessionState.CreateDefault();
                builtInState.Variables.Add( new SessionStateVariableEntry(
                    "app", clientApp, "M-Files Application" ) );
                builtInState.Variables.Add( new SessionStateVariableEntry(
                    "vault", vault, "M-Files Vault" ) );
                Runspace runspace = RunspaceFactory.CreateRunspace( builtInState );

                // Run the script.
                runspace.Open();
                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript( GetConfigurationProperty( "LaunchPSScript", false ) ?? "" );
                pipeline.Invoke();
                runspace.Close();
            }
            else
            {
                // Launch the application by navigating to a path in the vault.
                var mfilesPath =
                    clientApp.GetDriveLetter() + ":\\" +
                    vaultName + "\\" +
                    GetConfigurationProperty( "LaunchMFilesPath", false );

                Process.Start( "explorer.exe", string.Format( "\"{0}\"", mfilesPath ) );
            }

            return VSConstants.S_OK;
        }
        /// <summary>
        /// Deploys the M-Files Application to a client and launches it.
        /// </summary>
        /// <param name="grfLaunch">Launch flags</param>
        /// <returns>Success code</returns>
        public override int DebugLaunch(uint grfLaunch)
        {
            // TODO: For debugging, check the grfLaunch flags.
            // The flags differ for "Launch" and "Launch with debugging".
            CCITracing.TraceCall();

            // Resolve the test vault.
            var clientApp = new MFilesAPI.MFilesClientApplication();
            var vaultName = GetConfigurationProperty("TestVault", true);

            MFilesAPI.VaultConnection vaultConnection;

            try
            {
                // Try to get the connection.
                vaultConnection = clientApp.GetVaultConnection(vaultName);
            }
            catch
            {
                // Vault wasn't found, ask the user for a new one.
                var selectVaultDialog = new SelectVaultDialog();
                selectVaultDialog.ShowDialog();
                if (selectVaultDialog.Result == System.Windows.Forms.DialogResult.Cancel)
                {
                    return(VSConstants.S_FALSE);
                }

                // Get the user answer.
                vaultName       = selectVaultDialog.VaultName;
                vaultConnection = clientApp.GetVaultConnection(selectVaultDialog.VaultName);

                // If the user defined this as the default vault, save it in the project file.
                if (selectVaultDialog.SetDefault)
                {
                    this.SetConfigurationProperty("TestVault", selectVaultDialog.VaultName);
                }
            }

            // Get the M-Files install directory from the registry.
            var apiVersion = clientApp.GetAPIVersion().Display;
            var hklm64     = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            var mfKey      = hklm64.OpenSubKey(@"Software\Motive\M-Files\" + apiVersion);
            var installDir = (string)mfKey.GetValue("InstallDir");

            mfKey.Close();
            hklm64.Close();

            // Log out to free the current application.
            MFilesAPI.Vault vault = null;
            try
            {
                vault = clientApp.BindToVault(vaultName, IntPtr.Zero, false, true);
                if (vault != null)
                {
                    vault.LogOutWithDialogs(IntPtr.Zero);
                }
            }
            catch
            {
                // We most likely weren't logged in so everything is okay.
            }

            // Deploy the application.
            string vaultGuid    = vaultConnection.GetGUID();
            var    relativePath = string.Format(@"Client\Apps\{0}\sysapps\{1}",
                                                vaultGuid, this.project.GetProjectProperty("Name") ?? "unnamed");
            var targetDir = Path.Combine(installDir, relativePath);

            // If the directory exists, remove it so there's no residue files left.
            if (Directory.Exists(targetDir))
            {
                Directory.Delete(targetDir, true);
            }
            Directory.CreateDirectory(targetDir);

            // Extract the Zip contents to the target directory.
            DeployPackage(targetDir);

            // Log back into the application.
            vault = clientApp.BindToVault(vaultName, IntPtr.Zero, true, true);

            // If vault is null, the user cancelled the login -> Exit
            if (vault == null)
            {
                return(VSConstants.S_FALSE);
            }

            // Figure out the launch mode.
            var launchMode = (GetConfigurationProperty("LaunchMode", false) ?? "").ToLowerInvariant();

            if (launchMode == "powershell")
            {
                // Create a powershell window to launch the application.

                // Create the initial state with the app and vault references.
                var builtInState = InitialSessionState.CreateDefault();
                builtInState.Variables.Add(new SessionStateVariableEntry(
                                               "app", clientApp, "M-Files Application"));
                builtInState.Variables.Add(new SessionStateVariableEntry(
                                               "vault", vault, "M-Files Vault"));
                Runspace runspace = RunspaceFactory.CreateRunspace(builtInState);

                // Run the script.
                runspace.Open();
                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript(GetConfigurationProperty("LaunchPSScript", false) ?? "");
                pipeline.Invoke();
                runspace.Close();
            }
            else
            {
                // Launch the application by navigating to a path in the vault.
                var mfilesPath =
                    clientApp.GetDriveLetter() + ":\\" +
                    vaultName + "\\" +
                    GetConfigurationProperty("LaunchMFilesPath", false);

                Process.Start("explorer.exe", string.Format("\"{0}\"", mfilesPath));
            }

            return(VSConstants.S_OK);
        }