示例#1
0
        // @RIBBON TODO: For now the union of the command in Commands.xml and Ribbon.xml will go into the CommandId enum.
        private static bool GenerateEnum(HashSet commandIds, string enumName, string enumPath, Hashtable descriptions, Hashtable values)
        {
            const string TEMPLATE = @"namespace OpenLiveWriter.Localization
                            {{
                                public enum {0}
                                {{
                                    None,
                                    {1}
                                }}
                            }}
                            ";

            ArrayList commandList = commandIds.ToArrayList();
            commandList.Sort(new CaseInsensitiveComparer(CultureInfo.InvariantCulture));
            using (StreamWriter sw = new StreamWriter(Path.GetFullPath(enumPath)))
            {
                if (descriptions == null && values == null)
                {
                    sw.Write(string.Format(CultureInfo.InvariantCulture, TEMPLATE, enumName, StringHelper.Join(commandList.ToArray(), ",\r\n\t\t")));
                }
                else if (descriptions == null)
                {
                    // insert values
                    const string VALUE_TEMPLATE = "{0} = {1}";
                    const string VALUELESS_TEMPLATE = "{0}";
                    ArrayList pairs = new ArrayList();
                    ArrayList unmappedCommands = new ArrayList();
                    foreach (string command in commandList.ToArray())
                    {
                        string value = values[command] as string;
                        if (value != null)
                        {
                            pairs.Add(string.Format(CultureInfo.InvariantCulture, VALUE_TEMPLATE, command, value));
                        }
                        else
                        {
                            ConsoleColor color = Console.ForegroundColor;
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Error.WriteLine("ERROR: Command " + command + " is missing an Id (required for instrumentation)");
                            Console.Beep();
                            Console.ForegroundColor = color;
                            Console.Error.Flush();

                            // This command is not mapped to a ribbon command
                            // We'll keep track of these and put them at the end
                            unmappedCommands.Add(command);
                        }
                    }

                    if (unmappedCommands.Count > 0)
                    {
                        return false;
                    }

                    // Now add the commands that were not mapped to a value
                    int index = 1;
                    foreach (string command in unmappedCommands.ToArray())
                    {
                        if (index == 1)
                            pairs.Add(string.Format(CultureInfo.InvariantCulture, VALUE_TEMPLATE, command, index));
                        else
                            pairs.Add(string.Format(CultureInfo.InvariantCulture, VALUELESS_TEMPLATE, command));
                        index++;
                    }

                    sw.Write(string.Format(CultureInfo.InvariantCulture, TEMPLATE, enumName, StringHelper.Join(pairs.ToArray(), ",\r\n\t\t")));
                }
                else if (values == null)
                {
                    const string DESC_TEMPLATE = @"/// <summary>
                                                    /// {0}
                                                    /// </summary>
                                                    {1}";
                    ArrayList descs = new ArrayList();
                    foreach (string command in commandList.ToArray())
                    {
                        string description = ((Values)descriptions[command]).Val as string;
                        description = description.Replace("\n", "\n\t\t/// ");
                        descs.Add(string.Format(CultureInfo.InvariantCulture, DESC_TEMPLATE, description, command));
                    }
                    sw.Write(string.Format(CultureInfo.InvariantCulture, TEMPLATE, enumName, StringHelper.Join(descs.ToArray(), ",\r\n\t\t")));
                }
                else
                {
                    // insert values and descriptions
                    throw new NotImplementedException("Inserting values and descriptions not supported presently");
                }
            }

            return true;
        }