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

toSwift 1.7KB

il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. }