FileCreateBase.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Community.VisualStudio.Toolkit;
  6. using VberAdmin.Models;
  7. using VberAdmin.Templates.T4;
  8. namespace VberAdmin.Generating;
  9. public abstract class FileCreateBase
  10. {
  11. /// <summary>
  12. /// 运用T4模板生成文件
  13. /// </summary>
  14. /// <typeparam name="T"></typeparam>
  15. /// <param name="viewModel"></param>
  16. /// <param name="fileName"></param>
  17. /// <param name="fileType"></param>
  18. protected virtual async Task<bool> CreateT4Async<T>(TemplateViewModel viewModel, string fileName, int fileType = 0)
  19. where T : IT4CodeBase, new()
  20. {
  21. T t4 = new T { Model = viewModel };
  22. string pageContent = t4.TransformText();
  23. string path = viewModel.DtoFolder;
  24. switch (fileType)
  25. {
  26. case 0:
  27. path = viewModel.DtoFolder;
  28. break;
  29. case 1:
  30. path = viewModel.ServiceFolder;
  31. break;
  32. case 2:
  33. path = viewModel.ControllerFolder;
  34. break;
  35. case 3:
  36. path = viewModel.ViewsFolder;
  37. break;
  38. }
  39. return await CreateFileAsync(viewModel, path, fileName, pageContent);
  40. }
  41. /// <summary>
  42. /// 创建文件
  43. /// </summary>
  44. /// <param name="viewModel"></param>
  45. /// <param name="sourcePath"></param>
  46. /// <param name="fileName"></param>
  47. /// <param name="content"></param>
  48. protected virtual async Task<bool> CreateFileAsync(TemplateViewModel viewModel, string sourcePath, string fileName, string content)
  49. {
  50. string path = Path.GetTempPath();
  51. Directory.CreateDirectory(path);
  52. string file = Path.Combine(path, fileName);
  53. File.WriteAllText(file, content, Encoding.UTF8);
  54. try
  55. {
  56. if (string.IsNullOrEmpty(sourcePath))
  57. sourcePath = @"..\..\";
  58. if (!File.Exists(sourcePath))
  59. Directory.CreateDirectory(sourcePath);
  60. string sourceUrl = Path.Combine(sourcePath, fileName);
  61. if (viewModel.IsReplace)
  62. File.Delete(sourceUrl);
  63. if (!File.Exists(sourceUrl))
  64. {
  65. File.Copy(file, sourceUrl);
  66. await VS.StatusBar.ShowMessageAsync($"VberCode:{fileName}代码已生成。");
  67. return true;
  68. }
  69. }
  70. finally
  71. {
  72. File.Delete(file);
  73. }
  74. return false;
  75. }
  76. /// <summary>
  77. /// 添加文件到项目中
  78. /// </summary>
  79. /// <param name="projectName"></param>
  80. /// <param name="files"></param>
  81. protected virtual async Task AddFilesToProjectAsync(string projectName, List<string> files)
  82. {
  83. try
  84. {
  85. var projects = await VS.Solutions.GetAllProjectsAsync();
  86. foreach (var project in projects)
  87. {
  88. if (project.Name == projectName)
  89. {
  90. foreach (var file in files)
  91. {
  92. await VS.StatusBar.ShowMessageAsync($"VberCode:添加文件[{file}]...");
  93. await project.AddExistingFilesAsync(file);
  94. }
  95. await project.SaveAsync();
  96. break;
  97. }
  98. }
  99. }
  100. catch
  101. {
  102. //ignored
  103. //Debug.WriteLine(e);
  104. }
  105. }
  106. }