CodeWriter.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const csharp_1 = require("csharp");
  4. const format_1 = require("format");
  5. class CodeWriter {
  6. constructor(config) {
  7. config = config || {};
  8. this.blockStart = config.blockStart || '{';
  9. this.blockEnd = config.blockEnd || '}';
  10. this.blockFromNewLine = config.blockFromNewLine != undefined ? config.blockFromNewLine : true;
  11. if (config.usingTabs)
  12. this.indentStr = '\t';
  13. else
  14. this.indentStr = ' ';
  15. this.endOfLine = config.endOfLine || '\n';
  16. this.fileMark = config.fileMark ||
  17. '/** This is an automatically generated class by FairyGUI. Please do not modify it. **/';
  18. this.lines = [];
  19. this.indent = 0;
  20. this.writeMark();
  21. }
  22. writeMark() {
  23. this.lines.push(this.fileMark, '');
  24. }
  25. writeln(fmt, ...args) {
  26. if (!fmt) {
  27. this.lines.push('');
  28. return;
  29. }
  30. let str = '';
  31. for (let i = 0; i < this.indent; i++) {
  32. str += this.indentStr;
  33. }
  34. str += format_1.format(fmt, ...args);
  35. this.lines.push(str);
  36. return this;
  37. }
  38. startBlock() {
  39. if (this.blockFromNewLine || this.lines.length == 0)
  40. this.writeln(this.blockStart);
  41. else {
  42. let str = this.lines[this.lines.length - 1];
  43. this.lines[this.lines.length - 1] = str + ' ' + this.blockStart;
  44. }
  45. this.indent++;
  46. return this;
  47. }
  48. endBlock() {
  49. this.indent--;
  50. this.writeln(this.blockEnd);
  51. return this;
  52. }
  53. incIndent() {
  54. this.indent++;
  55. return this;
  56. }
  57. decIndent() {
  58. this.indent--;
  59. return this;
  60. }
  61. reset() {
  62. this.lines.length = 0;
  63. this.indent = 0;
  64. this.writeMark();
  65. }
  66. toString() {
  67. return this.lines.join(this.endOfLine);
  68. }
  69. save(filePath) {
  70. let str = this.toString();
  71. csharp_1.System.IO.File.WriteAllText(filePath, str);
  72. }
  73. }
  74. exports.default = CodeWriter;