Архитектурный шаблон "Мрак в моделях" на нескольких языках и платформах
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

96 lines
1.9KB

  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. };
  15. console.log(`Converting '${fileSrc}' to '${fileDst}'`);
  16. var linesDst = [];
  17. let linesSrc = fs.readFileSync(fileSrc).toString().split(/\r?\n/);
  18. for (let i in linesSrc) {
  19. let ln = linesSrc[i];
  20. linesDst.push(convert(ln));
  21. }
  22. let contentsDst = linesDst.join("\r\n");
  23. fs.writeFileSync(fileDst, contentsDst);
  24. // Functions making decisions.
  25. function convert(line) {
  26. var result = line;
  27. for (let src in globalReplacements) {
  28. let dst = globalReplacements[src];
  29. result = result.replace(src, dst);
  30. }
  31. result = protocolReplace(result);
  32. result = typeArrayReplace(result);
  33. return result;
  34. }
  35. var isProtocol = false;
  36. function protocolReplace(line) {
  37. if (line.includes("protocol")) {
  38. isProtocol = true;
  39. }
  40. if (line == "}") {
  41. isProtocol = false;
  42. }
  43. if (!isProtocol) {
  44. return line;
  45. }
  46. var result = line;
  47. if (isProtocol) {
  48. result = protocolReplaceVariable(result);
  49. }
  50. return result;
  51. }
  52. function protocolReplaceVariable(line) {
  53. let parts = line.split(": ");
  54. if (parts.length == 2) {
  55. let type = parts[1];
  56. let name = parts[0].trim();
  57. let spaceLength = parts[0].length - name.length;
  58. let spaces = " ".repeat(spaceLength);
  59. return `${spaces}var ${name}: ${type} { get }`;
  60. }
  61. return line;
  62. }
  63. // func memoryItemPositions(c: Context) -> Position[] {
  64. // var pos: Position[] = []
  65. function typeArrayReplace(line) {
  66. let parts = line.split(" ");
  67. for (var i in parts) {
  68. let part = parts[i];
  69. if (
  70. part != "[]" &&
  71. part.endsWith("[]")
  72. ) {
  73. let type = part.substring(0, part.length - 2)
  74. let swiftType = `[${type}]`;
  75. return line.replace(part, swiftType);
  76. }
  77. }
  78. return line;
  79. }