build.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Copyright 2013 Daniel Wirtz <dcode@dcode.io>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. var MetaScript = require("metascript"),
  14. path = require("path"),
  15. fs = require("fs");
  16. var rootDir = path.join(__dirname, ".."),
  17. srcDir = path.join(__dirname, "..", "src"),
  18. distDir = path.join(__dirname, "..", "dist"),
  19. pkg = require(path.join(rootDir, "package.json")),
  20. filename;
  21. var scope = {
  22. VERSION : pkg.version, // Version
  23. // Encodings
  24. ENCODINGS : true, // Include encodings in general (catches all)
  25. BASE64 : true, // Include base64 encoding
  26. BINARY : true, // Include binary encoding
  27. DEBUG : true, // Include debug encoding
  28. HEX : true, // Include hex encoding
  29. UTF8 : true, // Include utf8 encoding (required for STRINGS)
  30. // Primitive types
  31. BYTES : true, // Include bytes
  32. INTS : true, // Include int types in general (catches all)
  33. INT8 : true, // Include int8/uint8
  34. INT16 : true, // Include int16/uint16
  35. INT32 : true, // Include int32/uint32
  36. INT64 : true, // Include int64/uint64 with Long.js
  37. FLOATS : true, // Include float types in general (catches all)
  38. FLOAT32 : true, // Include float32
  39. FLOAT64 : true, // Include float64
  40. // Varint encoding
  41. VARINTS : true, // Include varint encoding in general (catches all)
  42. VARINT32 : true, // Include varint32/zigZagVarint32
  43. VARINT64 : true, // Include varint64/zigZagVarint32 with Long.js
  44. // String support
  45. STRINGS : true, // Include string support in general (catches all)
  46. UTF8STRING : true, // Include UTF8 encoded strings
  47. CSTRING : true, // Include C-like null terminated strings
  48. VSTRING : true, // Include varint32 length prefixed strings
  49. ISTRING : true, // Include uint32 length prefixed strings
  50. // Other
  51. ALIASES : true, // Include aliases like writeByte, writeShort ..
  52. INLINE : true, // Inline any assertion code
  53. VERBOSE_MS : false // Include MetaScript details as comments
  54. };
  55. // Optimize for size : INLINE=false, ALIASES=false, VERBOSE_MS=false, WHATEVERYOUDONTNEED=false
  56. // Optimize for speed : INLINE=true
  57. if (!scope.UTF8) scope.STRINGS = false;
  58. // Build node version using Buffers
  59. scope.NODE = true;
  60. console.log("Building bytebuffer-node with scope", JSON.stringify(scope, null, 2));
  61. fs.writeFileSync(
  62. path.join(distDir, "bytebuffer-node.js"),
  63. MetaScript.transform(fs.readFileSync(filename = path.join(srcDir, "wrap-node.js")), filename, scope, srcDir)
  64. );
  65. // Build browser version using Typed Arrays
  66. scope.NODE = false;
  67. scope.DATAVIEW = false;
  68. delete scope.BUFFERVIEW;
  69. console.log("Building bytebuffer.js with scope", JSON.stringify(scope, null, 2));
  70. fs.writeFileSync(
  71. path.join(distDir, "bytebuffer.js"),
  72. MetaScript.transform(fs.readFileSync(filename = path.join(srcDir, "wrap.js")), filename, scope)
  73. );
  74. // Build alternative browser version using a DataView
  75. scope.NODE = false;
  76. scope.DATAVIEW = true;
  77. console.log("Building bytebuffer-dataview with scope", JSON.stringify(scope, null, 2));
  78. fs.writeFileSync(
  79. path.join(distDir, "bytebuffer-dataview.js"),
  80. MetaScript.transform(fs.readFileSync(filename = path.join(srcDir, "wrap.js")), filename, scope)
  81. );
  82. // Update bower.json
  83. scope = { VERSION: pkg.version };
  84. console.log("Updating bower.json with scope", JSON.stringify(scope, null, 2));
  85. fs.writeFileSync(
  86. path.join(rootDir, "bower.json"),
  87. MetaScript.transform(fs.readFileSync(filename = path.join(srcDir, "bower.json")), filename, scope, srcDir)
  88. );
  89. console.log("Done");