コード例 #1
0
        //=====================================================================

        /// <summary>
        /// This is the main program entry point
        /// </summary>
        /// <param name="args">The command line arguments</param>
        /// <returns>An exit code that indicates the success or failure of the process</returns>
        public static int Main(string[] args)
        {
            List <CommandLineArgument> allArgs = new List <CommandLineArgument>();
            List <string> execArgs = new List <string>();
            string        product = null, version = null, locale = null, catalogName = null, commandLine;
            int           result = HelpLibraryManagerException.Success;
            bool          isInstall = false, isSilent = false, showHelp = false;
            Version       viewerVersion = new Version(1, 0);

            Assembly asm = Assembly.GetExecutingAssembly();

            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);

            Console.WriteLine("{0}, version {1}\r\n{2}\r\nE-Mail: [email protected]\r\n",
                              fvi.ProductName, fvi.ProductVersion, fvi.LegalCopyright);

            try
            {
                // Parse the command line arguments
                foreach (string arg in args)
                {
                    allArgs.Add(new CommandLineArgument(arg));
                }

                for (int idx = 0; idx < allArgs.Count; idx++)
                {
                    if (allArgs[idx].IsSwitch)
                    {
                        // This is used internally and isn't passed on
                        if (allArgs[idx].MatchesSwitch("viewerVersion") && idx < allArgs.Count - 1)
                        {
                            idx++;
                            viewerVersion = new Version(allArgs[idx].Value);
                            continue;
                        }

                        if (allArgs[idx].MatchesSwitch("catalogName") && idx < allArgs.Count - 1)
                        {
                            catalogName = allArgs[idx + 1].Value;
                        }

                        if (allArgs[idx].MatchesSwitch("product") && idx < allArgs.Count - 1)
                        {
                            product = allArgs[idx + 1].Value;
                        }

                        if (allArgs[idx].MatchesSwitch("version") && idx < allArgs.Count - 1)
                        {
                            version = allArgs[idx + 1].Value;
                        }

                        if (allArgs[idx].MatchesSwitch("locale") && idx < allArgs.Count - 1)
                        {
                            locale = allArgs[idx + 1].Value;
                        }

                        if (allArgs[idx].MatchesSwitch("install") || allArgs[idx].MatchesSwitch("sourceMedia") ||
                            allArgs[idx].MatchesSwitch("sourceUri"))
                        {
                            isInstall = true;
                        }

                        if (allArgs[idx].MatchesSwitch("silent"))
                        {
                            isSilent = true;
                        }

                        if (allArgs[idx].MatchesSwitch("help") || allArgs[idx].Value[1] == '?')
                        {
                            showHelp = true;
                        }
                    }

                    execArgs.Add(allArgs[idx].ToCommandLineOption());
                }

                if (allArgs.Count == 0 || showHelp)
                {
                    ShowHelp();
                    return(HelpLibraryManagerException.MissingCommandLineArgument);
                }

                if (viewerVersion.Major == 1)
                {
                    // These two are required for Help Viewer 1.0
                    if (String.IsNullOrEmpty(product))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.MissingCommandLineArgument, "/product");
                    }

                    if (String.IsNullOrEmpty(version))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.MissingCommandLineArgument, "/version");
                    }

                    // This is only used by Help Viewer 2.0
                    if (!String.IsNullOrEmpty(catalogName))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.InvalidCmdArgs,
                                                              "/catalogName is only valid for Help Viewer 2.0");
                    }
                }
                else
                {
                    if (!String.IsNullOrEmpty(product))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.InvalidCmdArgs,
                                                              "/product is only valid for Help Viewer 1.0");
                    }

                    if (!String.IsNullOrEmpty(version))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.InvalidCmdArgs,
                                                              "/version is only valid for Help Viewer 1.0");
                    }

                    // If not specified, default the catalog name based on the viewer version
                    if (String.IsNullOrEmpty(catalogName))
                    {
                        catalogName = HelpLibraryManager.DefaultCatalogName(viewerVersion);

                        if (catalogName == null)
                        {
                            throw new HelpLibraryManagerException(viewerVersion,
                                                                  HelpLibraryManagerException.MissingCommandLineArgument, "/catalogName");
                        }

                        Console.WriteLine("Catalog name not specified, the default catalog name '{0}' will " +
                                          "be used.", catalogName);
                    }
                }

                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Can't do anything if the Help Library Manager is not installed
                if (hlm.HelpLibraryManagerPath == null)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerNotFound);
                }

                // Can't do anything if the Help Library Manager is already running
                if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);
                }

                // Can't do anything if the local store is not initialized
                if (!hlm.LocalStoreInitialized)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.LocalStoreNotInitialized);
                }

                // If not specified, try to figure out the default locale
                if (String.IsNullOrEmpty(locale))
                {
                    if (viewerVersion.Major == 1)
                    {
                        locale = hlm.FindLocaleFor(product, version);

                        if (locale == null)
                        {
                            throw new HelpLibraryManagerException(viewerVersion,
                                                                  HelpLibraryManagerException.CatalogNotInstalled, String.Format(
                                                                      CultureInfo.InvariantCulture, "Product: {0}  Version: {1}", product, version));
                        }
                    }
                    else
                    {
                        locale = hlm.FindLocaleFor(catalogName);

                        if (locale == null)
                        {
                            throw new HelpLibraryManagerException(viewerVersion,
                                                                  HelpLibraryManagerException.CatalogNotInstalled, String.Format(
                                                                      CultureInfo.InvariantCulture, "Catalog Name: {0}", catalogName));
                        }
                    }

                    Console.WriteLine("No locale specified, the default locale '{0}' will be used", locale);

                    execArgs.Add("/locale");
                    execArgs.Add(locale);
                }

                // Execute the request
                Console.WriteLine("Running Help Library Manager to perform the requested action.  Please wait...");

                commandLine = String.Join(" ", execArgs.ToArray());

                try
                {
                    // If installing, we must always run as administrator.  Everything else can run as a normal
                    // user.
                    if (isInstall)
                    {
                        if (isSilent)
                        {
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Minimized);
                        }
                        else
                        {
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Normal);
                        }
                    }
                    else
                    {
                        result = hlm.RunAsNormalUser(commandLine, ProcessWindowStyle.Minimized);

                        // For content manager for Help Viewer 2.0 returns almost immediately.  If there is not
                        // content to uninstall, a subsequent install attempt can fail because the other instance
                        // hasn't quite shutdown yet.  This works around the issue by pausing for a couple of
                        // seconds.
                        if (viewerVersion.Major == 2)
                        {
                            System.Threading.Thread.Sleep(2000);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new HelpLibraryManagerException(viewerVersion, HelpLibraryManagerException.UnknownError,
                                                          String.Format(CultureInfo.InvariantCulture, "Failed to execute \"{0}\" {1}.\r\nError: {2}",
                                                                        hlm.HelpLibraryManagerPath, commandLine, ex.Message));
                }

                if (result != HelpLibraryManagerException.Success)
                {
                    throw new HelpLibraryManagerException(viewerVersion, result);
                }

                Console.WriteLine("The operation completed successfully");
            }
            catch (HelpLibraryManagerException hlmEx)
            {
                result = hlmEx.ErrorCode;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                                  hlmEx.Message);
            }
            catch (Exception ex)
            {
                result = HelpLibraryManagerException.UnknownError;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                                  ex.ToString());
            }
#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadLine();
            }
#endif
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// This is used to determine the state of the help content and set the form options when a help viewer
        /// version is selected.
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void cboHelpViewerVersion_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtInfo.Text = null;

            grpOptions.Enabled  = rbInstall.Enabled = true;
            lastVersionSelected = cboHelpViewerVersion.SelectedIndex;

            // If there are substitution tags present, have a go at resolving them
            if (helpFilePath.IndexOf("{@", StringComparison.Ordinal) != -1)
            {
                try
                {
                    var bp = new BuildProcess(project);
                    helpFilePath = bp.SubstitutionTags.TransformText(helpFilePath);
                    setupFile    = Path.ChangeExtension(helpFilePath, ".msha");
                }
                catch
                {
                    // Ignore errors
                    txtInfo.AppendText("The help filename appears to contain substitution tags but they could " +
                                       "not be resolved to determine the actual file to use for installation.  Building " +
                                       "website output and viewing it can be used to work around this issue.\r\n\r\n");
                    rbInstall.Enabled = false;
                }
            }

            if (rbInstall.Enabled && (!File.Exists(helpFilePath) || !File.Exists(setupFile)))
            {
                txtInfo.AppendText("A copy of the help file does not appear to exist yet.  It may need to be built.\r\n\r\n");
                rbInstall.Enabled = false;
            }

            try
            {
                viewerVersion = new Version((string)cboHelpViewerVersion.SelectedItem);

                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Can't do anything if the Help Library Manager is not installed
                if (hlm.HelpLibraryManagerPath == null)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerNotFound);
                }

                // Can't do anything if the Help Library Manager is already running
                if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);
                }

                // Can't do anything if the local store is not initialized
                if (!hlm.LocalStoreInitialized)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.LocalStoreNotInitialized);
                }

                if (hlm.HelpContentFileInstalled(helpFilePath))
                {
                    rbOpenCurrent.Enabled = rbRemove.Enabled = true;
                }
                else
                {
                    txtInfo.AppendText("The help file does not appear to be installed yet.\r\n");
                    rbOpenCurrent.Enabled = rbRemove.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                txtInfo.AppendText("Problem: " + ex.Message + "\r\n");
                rbOpenCurrent.Enabled = rbRemove.Enabled = false;
            }

            if (rbOpenCurrent.Enabled)
            {
                rbOpenCurrent.Checked = true;
            }
            else
            if (rbInstall.Enabled)
            {
                rbInstall.Checked = true;
            }
            else
            {
                rbLaunchContentManager.Checked = true;
            }

            if (!rbOpenCurrent.Enabled && !rbInstall.Enabled && !rbRemove.Enabled)
            {
                txtInfo.AppendText("\r\nNo action can be taken with the help content.");
            }

            // Determine the catalog name here as it's used in a lot of places and varies by version if not
            // defined in the project.
            catalogName = !String.IsNullOrWhiteSpace(project.CatalogName) ? project.CatalogName :
                          HelpLibraryManager.DefaultCatalogName(viewerVersion);

            // If it looks like a default value, warn the user if it doesn't match.  It may need to be cleared.
            if (!String.IsNullOrWhiteSpace(project.CatalogName) && project.CatalogName.StartsWith("VisualStudio",
                                                                                                  StringComparison.Ordinal) && project.CatalogName != HelpLibraryManager.DefaultCatalogName(viewerVersion))
            {
                txtInfo.AppendText("\r\n\r\nWARNING:  The project's catalog name property is set to '" +
                                   project.CatalogName + "' which does not match the default catalog name for the selected " +
                                   "version of the help viewer.  If necessary, clear the catalog name property value.");
            }
        }