MainWindow.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace DocTools.WPF
  18. {
  19. /// <summary>
  20. /// Interaction logic for MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. }
  28. /// <summary>
  29. /// 文档工具生成
  30. /// </summary>
  31. /// <param name="sender"></param>
  32. /// <param name="e"></param>
  33. private void GeneralDoc_Button_Click(object sender, RoutedEventArgs e)
  34. {
  35. try
  36. {
  37. this.GeneralDoc_Button.IsEnabled = false;
  38. var tuple = ValidateDirectory();
  39. if (!tuple.Item1)
  40. {
  41. MessageBox.Show(tuple.Item2);
  42. return;
  43. }
  44. tuple = ValidateFile();
  45. if (!tuple.Item1)
  46. {
  47. MessageBox.Show(tuple.Item2);
  48. return;
  49. }
  50. //先copy文件到自己目录
  51. this.Logs_Text.Text += "开始Copy RPC服务文件 到 DOCS文档工具目录" + Environment.NewLine;
  52. var rpcDLLPath = RPCFilePath_Text.Text + "\\WingInterfaceLibrary.dll";
  53. var rpcXMLPath = RPCFilePath_Text.Text + "\\WingInterfaceLibrary.xml";
  54. string destinationDLLPath = FilePath_Text.Text + "\\WingInterfaceLibrary.dll";
  55. string destinationXMLPath = FilePath_Text.Text + "\\WingInterfaceLibrary.xml";
  56. File.Copy(rpcDLLPath, destinationDLLPath, true);
  57. File.Copy(rpcXMLPath, destinationXMLPath, true);
  58. this.Logs_Text.Text += "RPC服务文件 Copy 成功" + Environment.NewLine;
  59. this.Logs_Text.Text += "文档工具开始读取 RPC服务文件内容" + Environment.NewLine;
  60. LoadData.SetInterfaceConfigToCache(FilePath_Text.Text + "\\");
  61. this.Logs_Text.Text += "RPC服务文件内容成功读取到内存" + Environment.NewLine;
  62. this.Logs_Text.Text += "文档工具开始生成文档JSON数据" + Environment.NewLine;
  63. CreateDocHtml doc = new CreateDocHtml();
  64. var resEntity = doc.CreateDocProjectString();
  65. var json = JsonConvert.SerializeObject(resEntity).Replace("\"Event\":", "\"event\":");
  66. this.Logs_Text.Text += "文档工具生成JSON数据成功" + Environment.NewLine;
  67. this.Logs_Text.Text += "文档工具开始生成HTML文档" + Environment.NewLine;
  68. var readXMLPath = FilePath_Text.Text + "\\XML.htm";
  69. var htmlContent = File.ReadAllText(readXMLPath);
  70. var resultHtml = htmlContent.Replace("$GetExportDocHtml()", json);
  71. var resPath = Description_Text.Text + "\\DocHtml.html";
  72. if (File.Exists(resPath))
  73. {
  74. File.Delete(resPath);
  75. }
  76. byte[] myByte = System.Text.Encoding.UTF8.GetBytes(resultHtml);
  77. using (FileStream fsWrite = new FileStream(resPath, FileMode.Append))
  78. {
  79. fsWrite.Write(myByte, 0, myByte.Length);
  80. };
  81. this.Logs_Text.Text += "文档工具生成HTML文档成功,并写入到指定目录" + Environment.NewLine;
  82. this.GeneralDoc_Button.IsEnabled = true;
  83. MessageBox.Show("生成成功");
  84. }
  85. catch (Exception ex)
  86. {
  87. this.Logs_Text.Text += ex.InnerException?.ToString() + Environment.NewLine;
  88. }
  89. }
  90. /// <summary>
  91. /// 验证输入
  92. /// </summary>
  93. /// <returns></returns>
  94. private Tuple<bool,string> ValidateDirectory()
  95. {
  96. bool res = false;
  97. var msg = "";
  98. if (string.IsNullOrEmpty(RPCFilePath_Text.Text) || !Directory.Exists(RPCFilePath_Text.Text))
  99. {
  100. msg = "杏聆荟RPC服务目录无效";
  101. }
  102. else if (string.IsNullOrEmpty(FilePath_Text.Text) || !Directory.Exists(FilePath_Text.Text))
  103. {
  104. msg = "DOCS文档工具目录无效";
  105. }
  106. else if (string.IsNullOrEmpty(Description_Text.Text) || !Directory.Exists(Description_Text.Text))
  107. {
  108. msg = "HTML文档输出目录无效";
  109. }
  110. else
  111. {
  112. res = true;
  113. }
  114. return Tuple.Create(res, msg);
  115. }
  116. /// <summary>
  117. /// 验证输入文件
  118. /// </summary>
  119. /// <returns></returns>
  120. private Tuple<bool, string> ValidateFile()
  121. {
  122. bool res = false;
  123. var msg = "";
  124. var rpcDLLPath = RPCFilePath_Text.Text + "\\WingInterfaceLibrary.dll";
  125. var rpcXMLPath = RPCFilePath_Text.Text + "\\WingInterfaceLibrary.xml";
  126. var templateXMLPath = FilePath_Text.Text + "\\XML.htm";
  127. if (!File.Exists(rpcDLLPath) || !File.Exists(rpcXMLPath))
  128. {
  129. msg = "杏聆荟指定RPC服务DLL和XML文件不存在";
  130. }
  131. else if (!File.Exists(templateXMLPath))
  132. {
  133. msg = "HTML系统模板文件不存在";
  134. }
  135. else
  136. {
  137. res = true;
  138. }
  139. return Tuple.Create(res, msg);
  140. }
  141. }
  142. }