private static string ImportElement <T>(string unpackDirectory, List <string> filesToAddToContent, List <string> codeFiles, string elementName, string extension, ref string desiredNamespace, ref IElement newElement) where T : IElement { string targetCs = null; var whatToDeserialize = unpackDirectory + FileManager.RemovePath(elementName) + "." + extension; newElement = FileManager.XmlDeserialize <T>(whatToDeserialize); #region Get "Entities" or "Screens" depending on whether we're importing a Screen or Entity string ScreensOrEntities = null; if (newElement is EntitySave) { ScreensOrEntities = "Entities"; } else { ScreensOrEntities = "Screens"; } #endregion #region Create necessary directories string subdirectory = null; subdirectory = ScreensOrEntities + "/"; if (!string.IsNullOrEmpty(elementName)) { subdirectory += FileManager.GetDirectory(elementName, RelativeType.Relative); } string destinationDirectory = FileManager.RelativeDirectory + subdirectory; Directory.CreateDirectory(destinationDirectory); string contentDestinationDirectory = FileManager.GetDirectory(ProjectManager.MakeAbsolute("a.scnx", true)) + subdirectory + FileManager.RemovePath(elementName) + "/"; Directory.CreateDirectory(contentDestinationDirectory); #endregion targetCs = destinationDirectory + FileManager.RemovePath(elementName) + ".cs"; desiredNamespace = ProjectManager.ProjectNamespace + "." + subdirectory.Substring(0, subdirectory.Length - 1).Replace("/", ".").Replace("\\", "."); string rootToReplace = null; string rootToReplaceWith = ProjectManager.ProjectNamespace; foreach (string codeFile in codeFiles) { string source = unpackDirectory + FileManager.RemovePath(codeFile); string target = destinationDirectory + FileManager.RemovePath(codeFile); string contents = FileManager.FromFileText(source); string replacedNamespace; contents = CodeWriter.ReplaceNamespace(contents, desiredNamespace, out replacedNamespace); if (string.IsNullOrEmpty(rootToReplace)) { if (replacedNamespace.Contains('.')) { replacedNamespace = replacedNamespace.Substring(0, replacedNamespace.IndexOf('.')); } rootToReplace = replacedNamespace; } // Let's look for using statements for this namespace and replace them contents = contents.Replace("using " + rootToReplace, "using " + rootToReplaceWith); FileManager.SaveText(contents, target); } #region Copy all files to the necessary folder - asking the user to overwrite if the file already exists foreach (string fileToCopyUnmodified in filesToAddToContent) { string fileToCopy = fileToCopyUnmodified; DialogResult dialogResult = DialogResult.Yes; string destinationFolder = contentDestinationDirectory; if (fileToCopy.StartsWith("__external/")) { destinationFolder = FileManager.GetDirectory(ProjectManager.MakeAbsolute("a.scnx", true)); int indexOfSlash = fileToCopy.IndexOf("/"); fileToCopy = fileToCopy.Substring(indexOfSlash + 1); } if (File.Exists(destinationFolder + fileToCopy)) { dialogResult = MessageBox.Show("The following file already exists:\n\n" + destinationFolder + fileToCopy + "\n\nOverwrite this file?", "Overwrite file?", MessageBoxButtons.YesNo); } if (dialogResult == DialogResult.Yes) { string directoryToCreate = FileManager.GetDirectory(destinationFolder + fileToCopy); Directory.CreateDirectory(directoryToCreate); File.Copy(unpackDirectory + fileToCopyUnmodified, destinationFolder + fileToCopy, true); } } #endregion int startOfName = ProjectManager.ProjectNamespace.Length + 1 + (ScreensOrEntities + ".").Length; string prependToName = null; if (startOfName < desiredNamespace.Length) { prependToName = desiredNamespace.Substring(startOfName, desiredNamespace.Length - startOfName); } if (!string.IsNullOrEmpty(prependToName)) { newElement.Name = ScreensOrEntities + "\\" + prependToName + "\\" + newElement.ClassName; } #region Prepend or remove "Content/" if necessary. This is needed depending on which project type the file is being exported from and imported to // Store off the old name to new name // association when and if ReferencedFileSaves // are changed by adding/removing "Content/". Dictionary <string, string> oldNameNewNameDictionary = new Dictionary <string, string>(); foreach (ReferencedFileSave rfs in newElement.ReferencedFiles) { if (rfs.Name.StartsWith("Content/")) { string newName = rfs.Name.Substring("Content/".Length); oldNameNewNameDictionary.Add(rfs.Name, newName); rfs.SetNameNoCall(newName); } } // We may have changed the Name of ReferencedFileSaves // If so, we need to see if any NamedObjectSaves reference // these ReferencedFileSaves, and if so change those as well. foreach (NamedObjectSave nos in newElement.NamedObjects) { if (!string.IsNullOrEmpty(nos.SourceFile) && oldNameNewNameDictionary.ContainsKey(nos.SourceFile)) { nos.SourceFile = oldNameNewNameDictionary[nos.SourceFile]; } foreach (NamedObjectSave containedNos in nos.ContainedObjects) { if (!string.IsNullOrEmpty(containedNos.SourceFile) && oldNameNewNameDictionary.ContainsKey(containedNos.SourceFile)) { containedNos.SourceFile = oldNameNewNameDictionary[containedNos.SourceFile]; } } } #endregion #region Add the Screen or Entity to the ProjectManager if (newElement is ScreenSave) { ProjectManager.AddScreen((ScreenSave)newElement, true); } else { GlueCommands.Self.GluxCommands.EntityCommands.AddEntity((EntitySave)newElement, true); } #endregion string directory = FileManager.GetDirectory(newElement.Name); AddAdditionalCodeFilesToProject(codeFiles, directory); return(targetCs); }
public SaveClasses.ScreenSave AddScreen(string screenName) { return(ProjectManager.AddScreen(screenName)); }