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.

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