Архитектурный шаблон "Мрак в моделях" на нескольких языках и платформах
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.4KB

  1. #!/usr/bin/env node
  2. if (process.argv.length < 4) {
  3. console.error("Usage: toSwift SRC DST");
  4. process.exit(1);
  5. }
  6. var fs = require("fs");
  7. let fileSrc = process.argv[2];
  8. let fileDst = process.argv[3];
  9. let globalReplacements = {
  10. "function": "func",
  11. "number": "Float",
  12. "):": ") ->",
  13. "interface": "protocol",
  14. "})": "}", // forEach
  15. };
  16. console.log(`Converting '${fileSrc}' to '${fileDst}'`);
  17. var linesDst = [];
  18. let linesSrc = fs.readFileSync(fileSrc).toString().split(/\r?\n/);
  19. for (let i in linesSrc) {
  20. let ln = linesSrc[i];
  21. linesDst.push(convert(ln));
  22. }
  23. let contentsDst = linesDst.join("\r\n");
  24. fs.writeFileSync(fileDst, contentsDst);
  25. // Functions making decisions.
  26. function convert(line) {
  27. var result = line;
  28. for (let src in globalReplacements) {
  29. let dst = globalReplacements[src];
  30. result = result.replace(src, dst);
  31. }
  32. result = protocolReplace(result);
  33. result = typeArrayReplace(result);
  34. result = forEachReplace(result);
  35. return result;
  36. }
  37. // x.forEach(y => { -> x.forEach { y in
  38. function forEachReplace(line) {
  39. if (!line.includes("forEach")) {
  40. return line;
  41. }
  42. let partsFE = line.split("forEach");
  43. let identifier = partsFE[0];
  44. let closure = partsFE[1];
  45. let partsC = closure.split(" => ");
  46. let bracketAndVar = partsC[0];
  47. let bracket = partsC[1];
  48. let variable = bracketAndVar.substring(1);
  49. let result = `${identifier}forEach { ${variable} in`;
  50. return result;
  51. }
  52. var isProtocol = false;
  53. // interface -> protocol
  54. function protocolReplace(line) {
  55. if (line.includes("protocol")) {
  56. isProtocol = true;
  57. }
  58. if (line == "}") {
  59. isProtocol = false;
  60. }
  61. if (!isProtocol) {
  62. return line;
  63. }
  64. var result = line;
  65. if (isProtocol) {
  66. result = protocolReplaceVariable(result);
  67. }
  68. return result;
  69. }
  70. // a: Number -> var a: Float { get }
  71. function protocolReplaceVariable(line) {
  72. let parts = line.split(": ");
  73. if (parts.length == 2) {
  74. let type = parts[1];
  75. let name = parts[0].trim();
  76. let spaceLength = parts[0].length - name.length;
  77. let spaces = " ".repeat(spaceLength);
  78. return `${spaces}var ${name}: ${type} { get }`;
  79. }
  80. return line;
  81. }
  82. // Type[] -> [Type]
  83. function typeArrayReplace(line) {
  84. let parts = line.split(" ");
  85. for (var i in parts) {
  86. let part = parts[i];
  87. if (
  88. part != "[]" &&
  89. part.endsWith("[]")
  90. ) {
  91. let type = part.substring(0, part.length - 2)
  92. let swiftType = `[${type}]`;
  93. return line.replace(part, swiftType);
  94. }
  95. }
  96. return line;
  97. }