Архитектурный шаблон "Мрак в моделях" на нескольких языках и платформах
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

119 řádky
2.5KB

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