private static async Task <TemplateInfo> WriteFileAsync(Project project, string file) { TemplateInfo template = await TemplateMap.GetTemplateInfoAsync(project, file); string content = template.Content; if (!string.IsNullOrEmpty(content)) { int index = content.IndexOf('$'); if (index > -1) { content = content.Remove(index, 1); } await WriteToDiskAsync(template.WritePath, content); template.CursorPosition = index; return(template); } await WriteToDiskAsync(template.WritePath, string.Empty); template.CursorPosition = 0; return(template); }
private static async Task<int> WriteFileAsync(Project project, string file) { string extension = Path.GetExtension(file); string template = await TemplateMap.GetTemplateFilePathAsync(project, file); if (!string.IsNullOrEmpty(template)) { int index = template.IndexOf('$'); if (index > -1) { template = template.Remove(index, 1); } await WriteToDiskAsync(file, template); return index; } await WriteToDiskAsync(file, string.Empty); return 0; }
private static async Task <int> WriteFileAsync(Project project, string file, FileNameDialogResult input) { var extension = Path.GetExtension(file); var template = await TemplateMap.GetTemplateFilePathAsync(project, file, input); if (!string.IsNullOrEmpty(template)) { var index = template.IndexOf('$'); if (index > -1) { template = template.Remove(index, 1); } await WriteToDiskAsync(file, template); return(index); } await WriteToDiskAsync(file, string.Empty); return(0); }
private static async Task <int> WriteFile(Project project, string file) { Encoding encoding = new UTF8Encoding(false); string extension = Path.GetExtension(file); string template = await TemplateMap.GetTemplateFilePath(project, file); var props = new Dictionary <string, string>() { { "extension", extension.ToLowerInvariant() } }; Telemetry.TrackEvent("File added", props); if (!string.IsNullOrEmpty(template)) { int index = template.IndexOf('$'); template = template.Remove(index, 1); File.WriteAllText(file, template, encoding); return(index); } File.WriteAllText(file, string.Empty, encoding); return(0); }
private static TemplateMap GetTemplateMap() { TemplateMap templates = null; if (_templates == null) { lock (_templateLock) if (_templates == null) { string path = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "VisualStudio.AddAnyFile", "Patterns.json"); if ((templates = TemplateMap.LoadFromFile(path)) == null) { templates = new TemplateMap(); templates.LoadDefaultMappings(); TemplateMap.WriteToFile(templates, path); } _templates = templates; } } return(_templates); }
private void MenuItemCallback(object sender, EventArgs e) { UIHierarchyItem item = GetSelectedItem(); if (item == null) { return; } string folder = FindFolder(item); if (string.IsNullOrEmpty(folder) || !Directory.Exists(folder)) { return; } // See if the user has a valid selection on the Solution Tree and avoid prompting the user // for a file name. Project project = GetActiveProject(); if (project == null) { return; } string defaultExt = GetProjectDefaultExtension(project); string input = PromptForFileName( folder, defaultExt ).TrimStart('/', '\\').Replace("/", "\\"); if (string.IsNullOrEmpty(input)) { return; } if (input.EndsWith("\\", StringComparison.Ordinal)) { input = input + "__dummy__"; } TemplateMap templates = GetTemplateMap(); string projectPath = Path.GetDirectoryName(project.FullName); string relativePath; if (folder.StartsWith(projectPath, StringComparison.OrdinalIgnoreCase) && folder.Length > projectPath.Length) { relativePath = CombinePaths(folder.Substring(projectPath.Length + 1), input); // I'm intentionally avoiding the use of Path.Combine because input may contain pattern characters // such as ':' which will cause Path.Combine to handle differently. We simply need a string concat here. } else { relativePath = input; } try { var itemManager = new ProjectItemManager(_dte, templates); var creator = itemManager.GetCreator(project, projectPath, relativePath); var info = creator.Create(project); SelectCurrentItem(); if (info != ItemInfo.Empty && _lastUsedExtension != defaultExt && info.Extension == _lastUsedExtension) { // TODO: Save extension to project-specific storage _overridingExtension = info.Extension; } _lastUsedExtension = info.Extension; } catch (Exception ex) { MessageBox.Show(ex.Message, "Cannot Add New File", MessageBoxButton.OK, MessageBoxImage.Error); } }