internal static List <string> GetMemberNamesFrom(ReferencedFileSave rfs) { List <string> toReturn = new List <string>(); string fileName = rfs.Name; fileName = ProjectManager.MakeAbsolute(fileName); RuntimeCsvRepresentation rcr = CsvFileManager.CsvDeserializeToRuntime( fileName); for (int i = 0; i < rcr.Headers.Length; i++) { string memberName = rcr.Headers[i].Name; if (memberName.Trim().StartsWith("//")) { continue; } memberName = StringFunctions.RemoveWhitespace(memberName); if (memberName.Contains("(")) { memberName = memberName.Substring(0, memberName.IndexOfAny(new char[] { '(' })); } toReturn.Add(memberName); } return(toReturn); }
public static void GetDictionaryTypes(ReferencedFileSave referencedFileSave, out string keyType, out string valueType) { valueType = referencedFileSave.GetTypeForCsvFile(); // To know the value type, we gotta pop this bad boy open and find the first requied type keyType = null; char oldDelimiter = CsvFileManager.Delimiter; switch (referencedFileSave.CsvDelimiter) { case AvailableDelimiters.Comma: CsvFileManager.Delimiter = ','; break; case AvailableDelimiters.Tab: CsvFileManager.Delimiter = '\t'; break; case AvailableDelimiters.Pipe: CsvFileManager.Delimiter = '|'; break; } string absoluteFileName = ProjectManager.MakeAbsolute(referencedFileSave.Name); // If the file doesn't exist this will generate bad code. But this isn't // considered a silent failure because Glue will raise flags about missing // files earlier (like when it first starts up). We don't want to crash the // entire application in this case. if (System.IO.File.Exists(absoluteFileName)) { RuntimeCsvRepresentation rcr = CsvFileManager.CsvDeserializeToRuntime(absoluteFileName); // See if any of the headers are required foreach (CsvHeader header in rcr.Headers) { int indexOfOpeningParen = header.Name.IndexOf("("); if (indexOfOpeningParen != -1) { if (header.Name.IndexOf("required", indexOfOpeningParen) != -1) { keyType = CsvHeader.GetClassNameFromHeader(header.Name); break; } } } } CsvFileManager.Delimiter = oldDelimiter; }
private static string GetClassInfo(string fileName, RuntimeCsvRepresentation rcr, CustomClassSave customClass, out List <TypedMemberBase> members, out Dictionary <string, string> untypedMembers) { bool usesCustomClass = customClass != null; List <RuntimeCsvRepresentation> rcrsForClass = new List <RuntimeCsvRepresentation>(); if (usesCustomClass) { foreach (string name in customClass.CsvFilesUsingThis) { ReferencedFileSave foundRfs = ObjectFinder.Self.GetReferencedFileSaveFromFile(name); if (foundRfs == null) { int m = 3; } else { fileName = foundRfs.Name; fileName = ProjectManager.MakeAbsolute(fileName); RuntimeCsvRepresentation runtimeToAdd = null; try { runtimeToAdd = CsvFileManager.CsvDeserializeToRuntime(fileName); } catch (Exception e) { MessageBox.Show("Error trying to parse CSV:\n" + e.ToString()); } if (runtimeToAdd != null) { rcrsForClass.Add(runtimeToAdd); } } } } else if (rcr != null) { rcrsForClass.Add(rcr); } GetClassInfoFromCsv(rcrsForClass, customClass, out members, out untypedMembers); return(fileName); }
private static bool CreateConstsForCsvEntries(ReferencedFileSave initialRfs, List <TypedMemberBase> members, Dictionary <string, string> untypedMembers, ICodeBlock codeBlock) { bool succeeded = true; bool addToOrderedLists = true; CustomClassSave customClass = GetCustomClassForCsv(initialRfs.Name); bool usesCustomClass = customClass != null; List <ReferencedFileSave> rfsesForClass = new List <ReferencedFileSave>(); if (usesCustomClass) { foreach (string name in customClass.CsvFilesUsingThis) { ReferencedFileSave foundRfs = ObjectFinder.Self.GetReferencedFileSaveFromFile(name); if (foundRfs != null) { rfsesForClass.Add(foundRfs); } } } else { rfsesForClass.Add(initialRfs); } foreach (ReferencedFileSave rfs in rfsesForClass) { if (rfs.CreatesDictionary) { string fileName = rfs.Name; fileName = ProjectManager.MakeAbsolute(fileName); var rcr = CsvFileManager.CsvDeserializeToRuntime(fileName); rcr.RemoveHeaderWhitespaceAndDetermineIfRequired(); int requiredIndex = rcr.GetRequiredIndex(); if (requiredIndex == -1) { succeeded = false; GlueGui.ShowMessageBox("The file " + rfs.Name + " is marked as a dictionary but has no column marked as required"); } else { string type = GetRequiredKeyType(rcr, members, untypedMembers, requiredIndex); FillCodeBlockWithKeys(codeBlock, type, rcr); // The first entry will be the "primary" one? if (addToOrderedLists) { FillOrderedListWithKeys(codeBlock, type, rcr); addToOrderedLists = false; } } } } return(succeeded); }
public static void GenerateAndSaveDataClass(ReferencedFileSave rfs, AvailableDelimiters delimiter) { string fileName = rfs.Name; fileName = ProjectManager.MakeAbsolute(fileName); #region See if the CSV file doesn't exist and warn the user if not if (!System.IO.File.Exists(fileName)) { MessageBox.Show("Could not find the CSV file " + fileName + " when trying to generate a data file"); } #endregion else // CSV exists { #region Save off the old delimiter and switch to using the new one - we need the old one so we can switch back after this function finishes char oldDelimiter = CsvFileManager.Delimiter; CsvFileManager.Delimiter = delimiter.ToChar(); #endregion if (!string.IsNullOrEmpty(rfs.UniformRowType)) { // This simply // checks to make // sure the CSV is // set up right - it // doesn't actually generate // any code because the CSV will // deserialize to an array of primitives. CheckUniformTypeValidity(rfs, fileName, oldDelimiter); } else { RuntimeCsvRepresentation rcr; bool succeeded; DeserializeToRcr(delimiter, fileName, out rcr, out succeeded); if (succeeded) { CsvFileManager.Delimiter = oldDelimiter; string whyIsCsvWrong = GetWhyCsvIsWrong(rcr, rfs.CreatesDictionary, fileName); if (!string.IsNullOrEmpty(whyIsCsvWrong)) { GlueGui.ShowMessageBox(whyIsCsvWrong); succeeded = false; } else { string className; List <TypedMemberBase> members; Dictionary <string, string> untypedMembers; CustomClassSave customClass = GetCustomClassForCsv(rfs.Name); if (customClass == null || customClass.GenerateCode) { fileName = GetClassInfoFromCsvs(rfs, fileName, rcr, out className, out members, out untypedMembers); succeeded = GenerateClassFromMembers(rfs, succeeded, className, members, untypedMembers); } } } } } }
private static bool CreateConstsForCsvEntries(ReferencedFileSave initialRfs, List <TypedMemberBase> members, Dictionary <string, string> untypedMembers, ICodeBlock codeBlock) { bool succeeded = true; bool addToOrderedLists = true; CustomClassSave customClass = GetCustomClassForCsv(initialRfs.Name); bool usesCustomClass = customClass != null; List <ReferencedFileSave> rfsesForClass = new List <ReferencedFileSave>(); if (usesCustomClass) { foreach (string name in customClass.CsvFilesUsingThis) { ReferencedFileSave foundRfs = ObjectFinder.Self.GetReferencedFileSaveFromFile(name); // A dupe was added one during a Glue crash, so let's protect against that: if (foundRfs != null && rfsesForClass.Contains(foundRfs) == false) { rfsesForClass.Add(foundRfs); } } } else { rfsesForClass.Add(initialRfs); } Dictionary <ReferencedFileSave, RuntimeCsvRepresentation> representations = new Dictionary <ReferencedFileSave, RuntimeCsvRepresentation>(); List <string> allKeys = new List <string>(); foreach (ReferencedFileSave rfs in rfsesForClass) { if (rfs.CreatesDictionary) { string fileName = rfs.Name; fileName = ProjectManager.MakeAbsolute(fileName); var rcr = CsvFileManager.CsvDeserializeToRuntime(fileName); representations.Add(rfs, rcr); rcr.RemoveHeaderWhitespaceAndDetermineIfRequired(); int requiredIndex = rcr.GetRequiredIndex(); if (requiredIndex == -1) { succeeded = false; GlueGui.ShowMessageBox("The file " + rfs.Name + " is marked as a dictionary but has no column marked as required"); } else { var requiredValues = RequiredColumnValues(rcr); allKeys.AddRange(requiredValues); } } } if (allKeys.Any()) { var distinct = allKeys.Distinct(); var firstRcr = representations.First().Value; int requiredIndex = firstRcr.GetRequiredIndex(); string type = GetRequiredKeyType(firstRcr, members, untypedMembers, requiredIndex); FillCodeBlockWithKeys(codeBlock, type, firstRcr, allKeys.Distinct().ToArray()); // the first rcr defines the ordered keys. Others won't add themselves to this list FillOrderedListWithKeys(codeBlock, type, firstRcr); } return(succeeded); }