public void Save(string file, IWorkbenchProject project) { XmlDocument doc = new XmlDocument(); XmlNode rootNode = doc.CreateElement("appconfig"); doc.AppendChild(rootNode); XmlNode node = doc.CreateElement("projectpath"); node.InnerText = RelativePaths.GetRelativePath(Path.GetDirectoryName(project.ProjectFile), OutputPath); rootNode.AppendChild(node); node = doc.CreateElement("templatefilename"); node.InnerText = project.GetPathRelativeToProjectFile(TemplateFileName); rootNode.AppendChild(node); node = doc.CreateElement("guid"); node.InnerText = ProjectGuid.ToString("B"); rootNode.AppendChild(node); node = doc.CreateElement("user-template-name"); node.InnerText = UserTemplateName; rootNode.AppendChild(node); doc.Save(file); }
public void DifferentDrives() { string path = @"C:\Temp"; string filename = @"D:\file.txt"; Assert.That(RelativePaths.GetRelativePath(path, filename), Is.EqualTo("D:\\file.txt")); }
public void OneDirectoryDown() { string path = @"C:\Temp"; string filename = @"C:\Temp\T\file.txt"; Assert.That(RelativePaths.GetRelativePath(path, filename), Is.EqualTo(".\\T\\file.txt")); }
public static string GetRelativeFilenameForEntityCSharpFile(Entity entity, string csprojFullFilePath) { if (entity.MappedClass != null) { var filename = entity.EntitySet.MappingSet.CodeParseResults.GetFilenameForParsedClass(entity.MappedClass); // filename is absolute, reduce it to relative to the .csproj file path return(RelativePaths.GetRelativePath(Path.GetDirectoryName(csprojFullFilePath), filename, false)); } return(Path.Combine("Model", entity.Name + ".cs")); }
/// <summary> /// Returns an array of relative paths of enabled items such that they can be /// stored in config files for later retrieval using <see cref="GetItems{TItem}(IEnumerable{Reloaded.Mod.Loader.IO.Structs.PathGenericTuple{TItem}})"/>. /// </summary> public List <string> GetRelativePaths <TItem>(IEnumerable <EnabledPathGenericTuple <TItem> > items) { var enabledModRelativePaths = new List <string>(items.Count()); foreach (var item in items) { if (item.IsEnabled) { string relativePath = RelativePaths.GetRelativePath(item.Path, ItemDirectory); enabledModRelativePaths.Add(relativePath); } } return(enabledModRelativePaths); }
public string GetRelativeDebugProjectFile() { return(RelativePaths.GetRelativePath(Path.GetDirectoryName(ProjectFileName), DebugProjectFile)); }
public string GetPathRelativeToProjectFile(string absolutePath) { return(RelativePaths.GetRelativePath(Path.GetDirectoryName(ProjectFileName), absolutePath)); }
private bool ProcessScriptObject(object scriptObject, string folderName, IScript script, ProjectFileTreeNode parentNode) { bool success = true; string scriptName = UpdateScriptName(scriptObject, script); string fileName = Path.Combine(folderName, scriptName); _Loader.SetGeneratedFileNameOnTemplate(Path.Combine(_Project.ProjectSettings.OutputPath, fileName)); if (scriptName.IndexOf("#") >= 0) { success = false; } if (success) { TextFileInformation fileInfo = new TextFileInformation(); fileInfo.RelativeFilePath = fileName; try { // Reset the SkipCurrentFile and CurrentFileName variable _Loader.CallTemplateFunction(TemplateHelper.ResetSkipCurrentFileFunctionName); _Loader.CallTemplateFunction(TemplateHelper.ResetCurrentFileNameFunctionName); // Call the script file function to get the file text body object[] parameters = new[] { scriptObject }; // Check whether we must skip the current file string templateOutput = (string)_Loader.CallTemplateFunction(script.ScriptName, ref parameters); if (_ProgressHelper.IsCancellationPending()) { return(false); } bool skipCurrentFile = (bool)_Loader.CallTemplateFunction(TemplateHelper.GetSkipCurrentFileFunctionName); if (!skipCurrentFile) { templateOutput = Utility.StandardizeLineBreaks(templateOutput, Utility.LineBreaks.Windows); string codeInsertionFilename = (string)_Loader.CallTemplateFunction(TemplateHelper.GetCurrentFileNameFunctionName); if (string.IsNullOrEmpty(codeInsertionFilename)) { string fullPath = Path.Combine(absoluteBasePath, fileName); _FileController.WriteAllText(fullPath, templateOutput); // The file has successfully been written - add it to the GeneratedFiles _Project.AddGeneratedFile(new GeneratedFile(script.FileName, fullPath, fileName, script.ScriptName, script.IteratorName)); // Set the NewGen text to point to that file. fileInfo.NewGenFile = new TextFile(fullPath, false); string versionNumberString = _Loader.GetAssemblyVersionNumber(); VersionNumber versionNumber; if (VersionNumber.TryParse(versionNumberString, out versionNumber)) { // Get the template language from the template function. string templateLanguageString = _Loader.GetTemplateFunctionLanguage(script.ScriptName); try { fileInfo.TemplateLanguage = SyntaxEditorHelper.LanguageEnumFromName(templateLanguageString); } catch (NotImplementedException) { fileInfo.TemplateLanguage = null; } } if (addToProjectFileTree) { parentNode.AddChildNode(fileInfo, scriptName); } } else { // Code insertions were performed. Need to update a node in the tree. // expand the path if (Path.IsPathRooted(codeInsertionFilename) == false) { codeInsertionFilename = RelativePaths.RelativeToAbsolutePath(absoluteBasePath, codeInsertionFilename); } codeInsertionFilename = _FileController.GetFullPath(codeInsertionFilename); // Get the relative path string relativeCodeInsertionFilename = RelativePaths.GetRelativePath(_Project.ProjectSettings.OutputPath, codeInsertionFilename); // If the file is not under the output path, then reset it to the full path if (relativeCodeInsertionFilename.StartsWith("..")) { relativeCodeInsertionFilename = codeInsertionFilename; // We need to add its folder as a root node of the tree if (!parentNode.IsTreeRoot) { parentNode = parentNode.ParentTree; } } fileInfo.RelativeFilePath = relativeCodeInsertionFilename; string relPathStart = "." + Path.DirectorySeparatorChar; if (fileInfo.RelativeFilePath.StartsWith(relPathStart)) { fileInfo.RelativeFilePath = fileInfo.RelativeFilePath.Substring(2); } if (addToProjectFileTree) { ProjectFileTree tree; if (!parentNode.IsTreeRoot) { tree = parentNode.ParentTree; } else { tree = (ProjectFileTree)parentNode; } var possibleNode = tree.GetNodeAtPath(codeInsertionFilename); if (possibleNode == null) { // Need to create this node. // Create the node and it's parent folders if need be var node = tree.CreateFileNodeForPath(relativeCodeInsertionFilename); node.AssociatedFile = fileInfo; } else { // Update the NewGen text, don't add it to the generated files list. fileInfo = possibleNode.AssociatedFile as TextFileInformation; if (fileInfo == null) { throw new NotSupportedException("Cannot code merge a binary file"); } } } else { // Write the file to disk _FileController.WriteAllText(codeInsertionFilename, templateOutput); } fileInfo.NewGenFile = new TextFile(templateOutput); } AddFileCountToPreviousEventAndRefire(_ProgressHelper, 1); } } catch (TemplateFunctionException ex) { success = false; string message = "<span class='error'>" + ex.Message + "</span>"; if (ex.InnerException != null) { message += ":<br/>" + Environment.NewLine + "<b>" + ex.InnerException.Message + "</b>" + Environment.NewLine + GetCleanTemplateFunctionErrorStackTrace(ex) + Environment.NewLine + "Target Site: " + ex.InnerException.TargetSite; } RaiseTemplateFunctionCallErrorEvent(ex); // Do nothing, just skip the file because the error will get reported to the user. if (addToProjectFileTree) { parentNode.AddChildNode(fileInfo, scriptName).GenerationError = new GenerationError(fileName, message); } } catch (Exception ex) { string message = "<span class='error'>" + ex.Message + "</span>"; if (ex.InnerException != null) { message += ":<br/>" + Environment.NewLine + "<b>" + ex.InnerException.Message + "</b>" + Environment.NewLine + GetCleanTemplateFunctionErrorStackTrace(ex) + Environment.NewLine + "Target Site: " + ex.InnerException.TargetSite; } if (addToProjectFileTree) { parentNode.AddChildNode(fileInfo, scriptName).GenerationError = new GenerationError(fileName, message); } // Make sure any other type of exception gets thrown throw; } } return(success); }