Архитектурный шаблон "Мрак в моделях" на нескольких языках и платформах
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.

79 lines
1.7KB

  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. return result;
  33. }
  34. var isProtocol = false;
  35. function protocolReplace(line) {
  36. if (line.includes("protocol")) {
  37. isProtocol = true;
  38. }
  39. if (line == "}") {
  40. isProtocol = false;
  41. }
  42. console.log("ИГР protocolR-1 isP/line:", isProtocol, line);
  43. if (!isProtocol) {
  44. return line;
  45. }
  46. var result = line;
  47. if (isProtocol) {
  48. result = protocolReplaceVariable(result);
  49. }
  50. console.log("ИГР protocolR-2 isP/line:", isProtocol, line);
  51. return result;
  52. }
  53. function protocolReplaceVariable(line) {
  54. let parts = line.split(": ");
  55. if (parts.length == 2) {
  56. let type = parts[1];
  57. let name = parts[0].trim();
  58. let spaceLength = parts[0].length - name.length;
  59. let spaces = " ".repeat(spaceLength);
  60. //console.log("Variable. name/spaceL/parts:", name, spaceLength, parts);
  61. return `${spaces}var ${name}: ${type} { get }`;
  62. }
  63. return line;
  64. }