Esempio n. 1
0
        static void serialize(EnumModel model)
        {
            FileInfo fileInfo = new FileInfo(model.Name + ".json");

            if (fileInfo.Exists)
            {
                fileInfo.Delete();
            }

            using (FileStream fileStream = fileInfo.OpenWrite())
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(EnumModel));
                serializer.WriteObject(fileStream, model);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                EnumModel model = new EnumModel();
                model.Name  = "Template";
                model.Items = new[] { new EnumItemModel()
                                      {
                                          Command     = new[] { "TEMPLATE" },
                                          Value       = 1249,
                                          Description = "One Template",
                                          Name        = "Template",
                                          Remarks     = new[] { "Nothing" },
                                          Results     = new[] { "Nothing" }
                                      } };
                serialize(model);
            }
            else
            {
                bool   @enum      = false;
                bool   dictionary = false;
                bool   assembly   = false;
                string outputFile = "";
                string file       = "";

                for (int i = 0; i < args.Length; ++i)
                {
                    string arg = args[i].Replace("-", "").Replace("/", "");

                    switch (arg)
                    {
                    case "e":
                    case "enum":
                        @enum = true;
                        break;

                    case "d":
                    case "dictionary":
                        dictionary = true;
                        break;

                    case "a":
                    case "assembly":
                        assembly = true;
                        break;

                    case "i":
                    case "input":
                        file = args[++i];
                        break;

                    case "o":
                    case "output":
                        outputFile = args[++i];
                        break;
                    }
                }

                if (string.IsNullOrWhiteSpace(file) | !(@enum ^ assembly ^ dictionary))
                {
                    Console.WriteLine("Input or (class|assembly|dictionary) not specified. Review your command prompt and try again.");
                    return;
                }
                FileInfo fileInfo = new FileInfo(file);
                if (!fileInfo.Exists)
                {
                    Console.WriteLine("File {0} does not exist. Exiting.", fileInfo.FullName);
                    return;
                }
                FileInfo outputFileInfo = null;

                if (!string.IsNullOrWhiteSpace(outputFile))
                {
                    outputFileInfo = new FileInfo(outputFile);
                    if (outputFileInfo.Exists)
                    {
                        Console.WriteLine("Output already exists. Overwrite?");
                        switch (Console.ReadLine())
                        {
                        case "n":
                            outputFile     = null;
                            outputFileInfo = null;
                            break;

                        case "y":
                            outputFileInfo.Delete();
                            break;
                        }
                    }
                }
                if (@enum)
                {
                    EnumModel model = deserialize(fileInfo);
                    if (model != null)
                    {
                        StringBuilder builder = new StringBuilder();

                        // Create the enum.
                        builder.AppendFormat("public enum {0} {{\n", model.Name);

                        for (int i = 0; i < model.Items.Length; ++i)
                        {
                            // Create summary-entry for Documentation.
                            // Have to be HTML-Encoded.
                            builder.AppendFormat("/// <summary>\n" +
                                                 "/// {0}\n" +
                                                 "/// </summary>\n", HttpUtility.HtmlEncode(model.Items[i].Description));

                            // Create remarks-section for Documentation.
                            // Have to be HTML-Encoded
                            builder.AppendLine("///<remarks>");
                            for (int j = 0; j < model.Items[i].Remarks.Length; ++j)
                            {
                                builder.AppendFormat("/// <para>{0}</para>\n", HttpUtility.HtmlEncode(model.Items[i].Remarks[j]));
                            }
                            builder.AppendLine("/// <para>Can be invoked by:</para>");
                            for (int j = 0; j < model.Items[i].Command.Length; ++j)
                            {
                                builder.AppendFormat("/// <para>{0}</para>\n", HttpUtility.HtmlEncode(model.Items[i].Command[j]));
                            }
                            builder.AppendLine("///</remarks>");

                            // create the exceptions.
                            for (int j = 0; j < model.Items[i].Results.Length; ++j)
                            {
                                builder.AppendFormat("/// <exception cref=\"{0}\" />\n", HttpUtility.HtmlEncode(model.Items[i].Results[j]));
                            }

                            builder.AppendFormat("{0} = {1},\n", model.Items[i].Name, model.Items[i].Value);
                        }

                        builder.Append("}");
                        if (outputFileInfo != null)
                        {
                            using (FileStream fileStream = outputFileInfo.OpenWrite())
                                using (StreamWriter writer = new StreamWriter(fileStream))
                                    writer.WriteLine(builder);
                        }
                        else
                        {
                            Console.Out.WriteLine(builder);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error while reading file.");
                        return;
                    }
                }
                if (dictionary)
                {
                    EnumModel model = deserialize(fileInfo);
                    if (model != null)
                    {
                        StringBuilder builder = new StringBuilder();

                        // Create the dicionary.
                        builder.AppendFormat("public Dictionary<object, string> {0}LookUp = new Dictionary<object, string> {{\n", model.Name);

                        for (int i = 0; i < model.Items.Length; ++i)
                        {
                            builder.AppendFormat("{{{0}.{1}, \"{2}\"}},\n", model.Name, model.Items[i].Name, model.Items[i].Remarks[0]);
                        }

                        builder.Append("};");
                        if (outputFileInfo != null)
                        {
                            using (FileStream fileStream = outputFileInfo.OpenWrite())
                                using (StreamWriter writer = new StreamWriter(fileStream))
                                    writer.WriteLine(builder);
                        }
                        else
                        {
                            Console.Out.WriteLine(builder);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error while reading file.");
                        return;
                    }
                }
                if (assembly)
                {
                    Console.WriteLine("Assembly is not implemented. To be finished.");
                    return;
                }
            }
        }