You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
6.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. try {
  2. console.log("start")
  3. const vscode = require("vscode");
  4. function goplsfix(path) {
  5. // axios.post("http://127.0.0.1:12677/gopls/fix.json", {
  6. // path: path,
  7. // }).then(res => {
  8. // }).catch(error => {
  9. // console.log("fix:", error);
  10. // })
  11. // vscode.commands.executeCommand('editor.action.quickFix', vscode.Uri.file(path), new vscode.Range((new vscode.Position(0, 0)), (new vscode.Position(100, 0))));
  12. vscode.window.createTerminal("go:fix", "cmd.exe", ["/c", `gopls fix -a -w ${path}`])
  13. }
  14. const url = vscode.workspace.getConfiguration().get('go-vscode-config.url');
  15. const fs = require("fs")
  16. const path = require('path');
  17. let start = vscode.window.activeTextEditor.selection.start.line
  18. let fileName = vscode.window.activeTextEditor.document.fileName
  19. console.log(start)
  20. console.log(fileName)
  21. let fileDir = path.dirname(fileName)
  22. let fileBaseName = path.basename(fileName)
  23. fs.readFile(fileName, 'utf8', (err, data) => {
  24. if (err) throw err;
  25. var context_arr = data.split("\n")
  26. let a = context_arr[start].trim(" ")
  27. console.log(a)
  28. for (; a.indexOf(" ") > 0;) {
  29. a = a.replace(" ", " ")
  30. }
  31. let b = a.split(" ")
  32. if (b[0] != "type" || b[2] != "struct" || b[3] != "{") {
  33. vscode.window.showErrorMessage("找不到结构")
  34. return
  35. }
  36. let struct = b[1]
  37. var stack = ['{']
  38. var endLine = 0
  39. var endChar = 0;
  40. (function () {
  41. for (var i = start + 1; i < context_arr.length; i++) {
  42. for (let k = 0; k < context_arr[i].length; k++) {
  43. const element = context_arr[i][k];
  44. console.log(element)
  45. if (element == '{') {
  46. stack.push('{')
  47. console.log(stack)
  48. }
  49. if (element == '}') {
  50. stack.pop()
  51. console.log(stack)
  52. }
  53. if (stack.length == 0) {
  54. endLine = i
  55. endChar = k + 1
  56. return
  57. }
  58. }
  59. }
  60. })()
  61. if (endLine < start) {
  62. vscode.window.showErrorMessage("结构异常")
  63. return
  64. }
  65. vscode.window.showInputBox({
  66. placeHolder: "请输入接口前缀,例如输入admin则填充为/admin/结构名/get|save..."
  67. }).then((prefix) => {
  68. for (; prefix.indexOf(" ") > 0;) {
  69. prefix = prefix.replace(" ", " ")
  70. }
  71. let rearr = prefix.toLocaleLowerCase().split(" ");
  72. let underscore_naming = rearr.join("_");
  73. let lowercase_first_letter = "";
  74. let capitalize_the_first_letter = "";
  75. for (var i = 0; i < rearr.length; i++) {
  76. let tmp = rearr[i].split("");
  77. tmp[0] = tmp[0].toLocaleUpperCase();
  78. capitalize_the_first_letter += tmp.join("");
  79. if (i == 0) {
  80. lowercase_first_letter += rearr[i];
  81. } else {
  82. lowercase_first_letter += tmp.join("");;
  83. }
  84. }
  85. prefix = prefix.replace(/ /g, "/")
  86. let endPosition = new vscode.Position(endLine, endChar)
  87. axios({
  88. method: 'get',
  89. url: 'http://git.ouxuan.net/3136352472/vscode-helper-js/raw/branch/master/hasaki-gen-crud/template.go',
  90. responseType: 'text'
  91. }).then(res => {
  92. console.log("123")
  93. let text = "" + res['data']
  94. text = text.replace(/Temp3A60/g, struct)
  95. let structHeadLower = struct[0].toLocaleLowerCase() + struct.slice(1)
  96. console.log("structHeadLower", structHeadLower, struct[0].toLocaleLowerCase())
  97. text = text.replace(/TempLowHead3A60/g, structHeadLower)
  98. text = text.replace(/Prefix3A60/g, prefix)
  99. text = text.replace(/PrefixHigHead3A60/g, capitalize_the_first_letter)
  100. console.log(text)
  101. console.log(endPosition)
  102. let tmp = text.split("// ----------------------------temp------------------------------------")
  103. if (data.indexOf(`${struct}) wherePrimaryKey`) < 0) {
  104. vscode.window.activeTextEditor.edit(function (textEditorEdit) {
  105. textEditorEdit.insert(
  106. endPosition,
  107. tmp[1],
  108. )
  109. })
  110. }
  111. let package_src = ""
  112. for (let i = 0; i < context_arr.length; i++) {
  113. const element = context_arr[i].trim(" ");
  114. if (element.indexOf("package") == 0) {
  115. package_src = element
  116. break
  117. }
  118. }
  119. let crudFileName = path.join(fileDir, `auto_generated_crud_${struct}_${fileBaseName}`)
  120. let httpFileName = path.join(fileDir, `auto_generated_http_crud_${underscore_naming}_${struct}_${fileBaseName}`)
  121. let mark = `//${package_src} 该文件由插件自动生成,无特殊情况请勿改动`;
  122. fs.writeFile(crudFileName, `${mark}\n\n${package_src}\n\n${tmp[2]}`, function () {
  123. goplsfix(crudFileName);
  124. })
  125. fs.writeFile(httpFileName, `${mark}\n\n${package_src}\n\n${tmp[3]}`, function () {
  126. goplsfix(httpFileName);
  127. })
  128. goplsfix(fileName);
  129. // vscode.window.showErrorMessage(res)
  130. }).catch(error => {
  131. vscode.window.showErrorMessage(`err:${error.message}`)
  132. })
  133. });
  134. })
  135. } catch (error) {
  136. console.log(error)
  137. }