public static void WriteEnum <T>( string enumName, string group, IEnumerable <T> values, bool hexadecimal, Validator <T> validator, GetField <T> getNameDelegate, GetField <T> getCommentDelegate, GetField <T, string> getDuplNameDelegate, GetField <T> getIdDelegate) { var dir = Path.Combine(Dir, group); Directory.CreateDirectory(dir); string file = Path.Combine(dir, enumName + ".cs"); Console.Write("Writing enum {0} to {1}...", enumName, new DirectoryInfo(file).FullName); bool first = true; using (var writer = new CodeFileWriter(new StreamWriter(file), "WCell.Constants." + group, enumName, "enum")) { var lines = new List <string>(values.Count()); var names = new Dictionary <string, int>(values.Count()); foreach (var item in values) { if (item == null || !validator(item)) { continue; } string name = getNameDelegate(item).Replace("%", "Percent"); name = name.Replace("'s", "s"); string[] parts = Regex.Split(name, @"\s+|[^\w\d_]+", RegexOptions.None); for (int i = 0; i < parts.Length; i++) { string part = parts[i]; if (part.Length == 0) { continue; } //if (part.Length > 1) { // part = part.ToLower(); string firstChar = part[0] + ""; part = firstChar.ToUpper() + part.Substring(1); //} //else { // part = part.ToUpper(); //} parts[i] = part; } name = string.Join("", parts); // against digits at the start Match numMatch = NumRegex.Match(name); if (numMatch.Success) { string num = GetNumString(numMatch.Value); if (name.Length > num.Length) { name = num + name.Substring(numMatch.Value.Length, 1).ToUpper() + name.Substring(numMatch.Value.Length + 1); } else { name = num; } } int count; if (!names.TryGetValue(name, out count)) { names.Add(name, 1); } else { names.Remove(name); names.Add(name, ++count); string duplName = null; if (getDuplNameDelegate != null) { duplName = getDuplNameDelegate(item, name); } if (duplName != null) { name = duplName; } else { name = name + "_" + count; } } string val = getIdDelegate(item); if (hexadecimal) { try { int ival = Convert.ToInt32(val); val = string.Format("0x{0:x}", ival); } catch { }; } if (first) { first = false; if (long.Parse(val) > 0) { writer.WriteLine("None = 0,"); } } string comment = ""; if (getCommentDelegate != null) { comment = getCommentDelegate(item); if (comment != null) { string[] commentLines = comment.Split(new string[] { "\n", "\r\n", "\r" }, StringSplitOptions.RemoveEmptyEntries); if (commentLines.Length > 0) { writer.StartSummary(); foreach (string line in commentLines) { writer.WriteXmlCommentLine(line); } writer.EndSummary(); } } } writer.WriteLine(string.Format("{0} = {1},", name, val)); } writer.WriteLine("End"); writer.Finish(); Console.WriteLine(" Done."); } }
public static void WriteEnum <T>( string enumName, string enumSuffix, string group, IEnumerable <T> values, bool hexadecimal, Validator <T> validator, GetField <T> getNameDelegate, GetField <T> getCommentDelegate, GetField <T, string> getDuplNameDelegate, GetField <T> getIdDelegate) { Init(); var dir = Path.Combine(Dir, group); Directory.CreateDirectory(dir); var file = Path.Combine(dir, enumName + ".cs"); Console.Write("Writing enum {0} to {1}...", enumName, new DirectoryInfo(file).FullName); var first = true; using (var writer = new CodeFileWriter(file, "WCell.Constants." + group, enumName, "enum", enumSuffix)) { try { var names = new Dictionary <string, int>(values.Count()); foreach (T item in values) { if (item == null || item.Equals(default(T)) || !validator(item)) { continue; } var name = getNameDelegate(item); if (name == null) { throw new Exception(string.Format("Name for Item {0} in {1}/{2} was null.", item, group, enumName)); } name = BeautifyName(name); int count; if (!names.TryGetValue(name, out count)) { names.Add(name, 1); } else { names.Remove(name); names.Add(name, ++count); string duplName = null; if (getDuplNameDelegate != null) { duplName = getDuplNameDelegate(item, name); } if (duplName != null) { name = duplName; } else { name = name + "_" + count; } } var val = getIdDelegate(item); if (hexadecimal) { int ival; if (int.TryParse(val, out ival)) { val = string.Format("0x{0:x}", ival); } } if (first) { first = false; long id; if (!long.TryParse(val, out id)) { throw new InvalidDataException("Invalid ID was not numeric: " + val); } if (id > 0) { writer.WriteLine("None = 0,"); } } string comment; if (getCommentDelegate != null) { comment = getCommentDelegate(item); if (comment != null) { var commentLines = comment.Split(new[] { "\n", "\r\n", "\r" }, StringSplitOptions.RemoveEmptyEntries); if (commentLines.Length > 0) { writer.StartSummary(); foreach (string line in commentLines) { writer.WriteXmlCommentLine(line); } writer.EndSummary(); } } } writer.WriteLine(string.Format("{0} = {1},", name, val)); } writer.WriteLine("End"); writer.Finish(); Console.WriteLine(" Done."); } catch (Exception ex) { writer.OnException(ex); } } }