Архитектурный шаблон "Мрак в моделях" на нескольких языках и платформах
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

78 lignes
1.6KB

  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 isP/line:", isProtocol, line);
  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. //console.log("Variable. name/spaceL/parts:", name, spaceLength, parts);
  60. return `${spaces}var ${name}: ${type} { get }\n`;
  61. }
  62. return line;
  63. }