Ejemplo n.º 1
0
        private static void ReactToRemovalIfCsv(ReferencedFileSave referencedFileToRemove, List <string> additionalFilesToRemove)
        {
            if (referencedFileToRemove.IsCsvOrTreatedAsCsv)
            {
                string name = referencedFileToRemove.GetTypeForCsvFile();
                // If the CSV uses a custom class then the user should not
                // be asked if the file for that class should be removed.  That
                // class was created independent of any CSV (perhaps by hand, perhaps
                // by plugin), so the user should have to explicitly remove the class.

                var customClass = ObjectFinder.Self.GlueProject.GetCustomClassReferencingFile(referencedFileToRemove.Name);

                if (customClass == null)
                {
                    var first = ObjectFinder.Self.GetAllReferencedFiles().FirstOrDefault(item => item.IsCsvOrTreatedAsCsv && item.GetTypeForCsvFile() == name);

                    if (first == null)
                    {
                        // Remove the class
                        string whatToRemove = "DataTypes/" + name + ".Generated.cs";

                        additionalFilesToRemove.Add(whatToRemove);
                    }
                }

                // See if this uses a custom class.  If so, remove the CSV from
                // the class' list.
                if (customClass != null)
                {
                    customClass.CsvFilesUsingThis.Remove(referencedFileToRemove.Name);
                }
            }
        }
Ejemplo n.º 2
0
        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;
        }
Ejemplo n.º 3
0
        public static string GetEntireGenericTypeForCsvFile(ReferencedFileSave referencedFileSave)
        {
            string genericType = referencedFileSave.GetTypeForCsvFile();

            if (referencedFileSave.CreatesDictionary)
            {
                string keyType;
                string valueType;

                GetDictionaryTypes(referencedFileSave, out keyType, out valueType);
                return("System.Collections.Generic.Dictionary<" + keyType + ", " + valueType + ">");
            }
            else
            {
                return("System.Collections.Generic.List<" + genericType + ">");
            }
        }
Ejemplo n.º 4
0
        public static bool IsError(ReferencedFileSave file, CustomClassSave customClass)
        {
            var glueProject = GlueState.Self.CurrentGlueProject;


            if (glueProject == null)
            {
                return(false);
            }
            string className;

            if (!string.IsNullOrEmpty(customClass.CustomNamespace))
            {
                className = customClass.CustomNamespace + "." + customClass.Name;
            }
            else
            {
                className = EditorObjects.IoC.Container.Get <IVsProjectState>().DefaultNamespace +
                            ".DataTypes." + customClass.Name;
            }
            var areSameName = file.GetTypeForCsvFile() == className;

            if (areSameName == false)
            {
                return(false);
            }
            if (ObjectFinder.Self.GetAllReferencedFiles().Contains(file) == false)
            {
                return(false);
            }
            if (glueProject.CustomClasses.Contains(customClass) == false)
            {
                return(false);
            }
            if (customClass.CsvFilesUsingThis.Contains(file.Name))
            {
                return(false);
            }

            return(true);
        }
        private static void RemoveCodeForCsv(ReferencedFileSave rfs, string alternativeName = null, bool saveProject = true)
        {
            if (alternativeName == null)
            {
                alternativeName = rfs.Name;
            }
            string className = rfs.GetTypeForCsvFile(alternativeName);

            // the class name will be fully qualified, but we don't want that, we want just the end:
            if (className.Contains("."))
            {
                // provides the name after the dot:
                className = FileManager.GetExtension(className);
            }

            string whatToRemove = "DataTypes/" + className + ".Generated.cs";


            if (ProjectManager.ProjectBase.RemoveItem(whatToRemove) && saveProject)
            {
                ProjectManager.SaveProjects();
            }
            string fileToDelete = whatToRemove;

            fileToDelete = ProjectManager.MakeAbsolute(fileToDelete);
            if (System.IO.File.Exists(fileToDelete))
            {
                try
                {
                    FileHelper.DeleteFile(fileToDelete);
                }
                catch (Exception e)
                {
                    GlueGui.ShowMessageBox("Could not delete the file " + fileToDelete + "\n\nThe file is no longer referneced by the project so it is not necessary to delete this file manually.");
                }
            }
        }
Ejemplo n.º 6
0
        private bool SetRfsToUseNonNullClass(ReferencedFileSave currentReferencedFile, CustomClassSave classToUse, bool force)
        {
            bool succeeded = false;
            // See if this file is already using one Custom class, and if so, change it
            CustomClassSave customClassAlreadyBeingUsed = GetCustomClassSaveIncludingThis(
                currentReferencedFile.Name);

            if (customClassAlreadyBeingUsed != null)
            {
                // This could fail due to threading issues, so ignore failures:
                try
                {
                    customClassAlreadyBeingUsed.CsvFilesUsingThis.Remove(currentReferencedFile.Name);
                }
                catch (Exception e)
                {
                    int m = 3;
                }
                succeeded = true;
            }
            else
            {
                // This guy was using its own class, so let's tell the user and see if it should be removed
                string file = currentReferencedFile.GetTypeForCsvFile();

                // "file" is fully qualified, but we only want the non-qualified
                if (file.Contains("."))
                {
                    int lastDot = file.LastIndexOf('.');
                    file = file.Substring(lastDot + 1);
                }

                file = FlatRedBall.IO.FileManager.RelativeDirectory + "DataTypes/" + file + ".Generated.cs";

                if (ProjectManager.ProjectBase.IsFilePartOfProject(file, BuildItemMembershipType.CompileOrContentPipeline))
                {
                    DialogResult result;

                    if (force)
                    {
                        result = DialogResult.Yes;
                    }
                    else
                    {
                        result = MessageBox.Show("The CSV\n\n" + currentReferencedFile.Name + "\n\nwas using the file\n\n" +
                                                 file + "\n\nThis file is no associated with this CSV file.  Would you like to remove this file?", "Remove unused file?", MessageBoxButtons.YesNo);
                    }
                    if (result == System.Windows.Forms.DialogResult.Yes)
                    {
                        ProjectManager.ProjectBase.RemoveItem(file);
                        try
                        {
                            FileHelper.DeleteFile(file);
                        }
                        catch
                        {
                            PluginManager.ReceiveError("Could not delete file " + file);
                            // Even though the file couldn't be removed, we're going to succeed - the
                            // old file will remain there, and the CSV will use the new one.
                        }
                    }
                    succeeded = true;
                }
                else
                {
                    succeeded = true;
                }
            }

            classToUse.CsvFilesUsingThis.Add(currentReferencedFile.Name);
            return(succeeded);
        }