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.

188 lines
5.2 KiB

4 years ago
  1. "use strict";
  2. Error.stackTraceLimit = 100;
  3. var astPasses = require("./ast_passes.js");
  4. module.exports = function( grunt ) {
  5. var isCI = !!grunt.option("ci");
  6. var license;
  7. function getLicense() {
  8. if( !license ) {
  9. var fs = require("fs");
  10. var text = fs.readFileSync("LICENSE", "utf8");
  11. text = text.split("\n").map(function(line, index){
  12. return " * " + line;
  13. }).join("\n")
  14. license = "/**\n" + text + "\n */\n";
  15. }
  16. return license
  17. }
  18. function writeFile( dest, content ) {
  19. grunt.file.write( dest, content );
  20. grunt.log.writeln('File "' + dest + '" created.');
  21. }
  22. var gruntConfig = {};
  23. var getGlobals = function() {
  24. var fs = require("fs");
  25. var file = "./src/constants.js";
  26. var contents = fs.readFileSync(file, "utf8");
  27. var rconstantname = /CONSTANT\(\s*([^,]+)/g;
  28. var m;
  29. var globals = {
  30. "console": false,
  31. "require": false,
  32. "module": false,
  33. "define": false
  34. };
  35. while( ( m = rconstantname.exec( contents ) ) ) {
  36. globals[m[1]] = false;
  37. }
  38. return globals;
  39. }
  40. gruntConfig.pkg = grunt.file.readJSON("package.json");
  41. gruntConfig.jshint = {
  42. all: {
  43. options: {
  44. globals: getGlobals(),
  45. "bitwise": false,
  46. "camelcase": true,
  47. "curly": true,
  48. "eqeqeq": true,
  49. "es3": true,
  50. "forin": true,
  51. "immed": true,
  52. "latedef": false,
  53. "newcap": true,
  54. "noarg": true,
  55. "noempty": true,
  56. "nonew": true,
  57. "plusplus": false,
  58. "quotmark": "double",
  59. "undef": true,
  60. "unused": true,
  61. "strict": false,
  62. "trailing": true,
  63. "maxparams": 7,
  64. "maxlen": 80,
  65. "asi": false,
  66. "boss": true,
  67. "eqnull": true,
  68. "evil": true,
  69. "expr": false,
  70. "funcscope": false,
  71. "globalstrict": false,
  72. "lastsemic": false,
  73. "laxcomma": false,
  74. "laxbreak": false,
  75. "loopfunc": true,
  76. "multistr": true,
  77. "proto": false,
  78. "scripturl": true,
  79. "smarttabs": false,
  80. "shadow": true,
  81. "sub": true,
  82. "supernew": false,
  83. "validthis": true,
  84. "browser": true,
  85. "jquery": true,
  86. "devel": true,
  87. '-W014': true,
  88. '-W116': true,
  89. '-W106': true,
  90. '-W064': true,
  91. '-W097': true
  92. },
  93. files: {
  94. src: [
  95. "./src/deque.js"
  96. ]
  97. }
  98. }
  99. };
  100. if( !isCI ) {
  101. gruntConfig.jshint.all.options.reporter = require("jshint-stylish");
  102. }
  103. gruntConfig.bump = {
  104. options: {
  105. files: ['package.json'],
  106. updateConfigs: [],
  107. commit: true,
  108. commitMessage: 'Release v%VERSION%',
  109. commitFiles: ['-a'],
  110. createTag: true,
  111. tagName: 'v%VERSION%',
  112. tagMessage: 'Version %VERSION%',
  113. false: true,
  114. pushTo: 'master',
  115. gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d' // options to use with '$ git describe'
  116. }
  117. };
  118. grunt.initConfig(gruntConfig);
  119. grunt.loadNpmTasks('grunt-contrib-jshint');
  120. grunt.loadNpmTasks('grunt-bump');
  121. grunt.registerTask( "build", function() {
  122. var fs = require("fs");
  123. var CONSTANTS_FILE = "./src/constants.js";
  124. astPasses.readConstants(fs.readFileSync(CONSTANTS_FILE, "utf8"), CONSTANTS_FILE);
  125. var fileNames = ["deque.js"];
  126. fileNames.forEach(function(fileName){
  127. var src = fs.readFileSync("./src/" + fileName, "utf8");
  128. src = astPasses.removeComments(src, fileName);
  129. src = astPasses.expandConstants(src, fileName);
  130. src = getLicense() + src;
  131. writeFile("./js/" + fileName, src);
  132. });
  133. });
  134. grunt.registerTask( "testrun", function() {
  135. var fs = require("fs");
  136. var done = this.async();
  137. var Mocha = require("mocha");
  138. var mochaOpts = {
  139. reporter: "spec",
  140. timeout: 500,
  141. slow: Infinity
  142. };
  143. var mocha = new Mocha(mochaOpts);
  144. fs.readdirSync("./test").forEach(function(fileName) {
  145. mocha.addFile("./test/" + fileName);
  146. });
  147. mocha.run(function(err){
  148. if( err ) {
  149. process.stderr.write(test.title + "\n" + err.stack + "\n");
  150. done(err);
  151. }
  152. else {
  153. done();
  154. }
  155. }).on( "fail", function( test, err ) {
  156. process.stderr.write(test.title + "\n" + err.stack + "\n");
  157. done(err);
  158. });
  159. });
  160. grunt.registerTask( "test", ["jshint", "build", "testrun"] );
  161. grunt.registerTask( "default", ["jshint", "build"] );
  162. };