Gruntfile.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*jshint -W106 */
  2. /*jshint node:true, maxstatements: false, maxlen: false */
  3. var os = require("os");
  4. var path = require("path");
  5. var loadGruntTasks = require("load-grunt-tasks");
  6. module.exports = function(grunt) {
  7. "use strict";
  8. // Load necessary tasks
  9. loadGruntTasks(grunt);
  10. // Metadata
  11. var pkg = grunt.file.readJSON("package.json");
  12. // Make a temp dir for Flash compilation
  13. var tmpDir = os.tmpdir ? os.tmpdir() : os.tmpDir();
  14. var flashTmpDir = path.join(tmpDir, "zcflash");
  15. // Shared configuration
  16. var localPort = 7320; // "ZERO"
  17. // Project configuration.
  18. var config = {
  19. // Task configuration
  20. jshint: {
  21. options: {
  22. jshintrc: true
  23. },
  24. gruntfile: ["Gruntfile.js"],
  25. component: ["index.js"],
  26. js: ["src/js/**/*.js", "!src/js/start.js", "!src/js/end.js"],
  27. test: ["test/**/*.js"],
  28. dist: ["dist/*.js", "!dist/*.min.js"]
  29. },
  30. flexpmd: {
  31. flash: {
  32. src: [flashTmpDir]
  33. }
  34. },
  35. clean: {
  36. dist: ["ZeroClipboard.*", "dist/ZeroClipboard.*"],
  37. flash: {
  38. options: {
  39. // Force is required when trying to clean outside of the project dir
  40. force: true
  41. },
  42. src: [flashTmpDir]
  43. },
  44. meta: ["bower.json", "composer.json", "LICENSE"],
  45. coveralls: ["tmp/", "coverage/"]
  46. },
  47. concat: {
  48. options: {
  49. stripBanners: false,
  50. process: {
  51. data: pkg
  52. }
  53. },
  54. core: {
  55. src: [
  56. "src/meta/source-banner.tmpl",
  57. "src/js/start.js",
  58. "src/js/shared/state.js",
  59. "src/js/shared/private.js",
  60. "src/js/core/state.js",
  61. "src/js/core/private.js",
  62. "src/js/core/api.js",
  63. "src/js/end.js"
  64. ],
  65. dest: "dist/ZeroClipboard.Core.js"
  66. },
  67. client: {
  68. src: [
  69. "src/meta/source-banner.tmpl",
  70. "src/js/start.js",
  71. "src/js/shared/state.js",
  72. "src/js/shared/private.js",
  73. "src/js/core/state.js",
  74. "src/js/core/private.js",
  75. "src/js/core/api.js",
  76. "src/js/client/state.js",
  77. "src/js/client/private.js",
  78. "src/js/client/api.js",
  79. "src/js/end.js"
  80. ],
  81. dest: "dist/ZeroClipboard.js"
  82. },
  83. flash: {
  84. files: [
  85. {
  86. src: [
  87. "src/meta/source-banner.tmpl",
  88. "src/flash/ZeroClipboard.as"
  89. ],
  90. dest: path.join(flashTmpDir, "ZeroClipboard.as")
  91. },
  92. {
  93. src: [
  94. "src/meta/source-banner.tmpl",
  95. "src/flash/ClipboardInjector.as"
  96. ],
  97. dest: path.join(flashTmpDir, "ClipboardInjector.as")
  98. },
  99. {
  100. src: [
  101. "src/meta/source-banner.tmpl",
  102. "src/flash/JsProxy.as"
  103. ],
  104. dest: path.join(flashTmpDir, "JsProxy.as")
  105. },
  106. {
  107. src: [
  108. "src/meta/source-banner.tmpl",
  109. "src/flash/XssUtils.as"
  110. ],
  111. dest: path.join(flashTmpDir, "XssUtils.as")
  112. }
  113. ]
  114. }
  115. },
  116. uglify: {
  117. options: {
  118. report: "min"
  119. },
  120. js: {
  121. options: {
  122. preserveComments: function(node, comment) {
  123. return comment &&
  124. comment.type === "comment2" &&
  125. /^(!|\*|\*!)\r?\n/.test(comment.value);
  126. },
  127. beautify: {
  128. beautify: true,
  129. // `indent_level` requires jshint -W106
  130. indent_level: 2
  131. },
  132. mangle: false,
  133. compress: false
  134. },
  135. files: [
  136. {
  137. src: ["<%= concat.core.dest %>"],
  138. dest: "<%= concat.core.dest %>"
  139. },
  140. {
  141. src: ["<%= concat.client.dest %>"],
  142. dest: "<%= concat.client.dest %>"
  143. }
  144. ]
  145. },
  146. minjs: {
  147. options: {
  148. preserveComments: function(node, comment) {
  149. return comment &&
  150. comment.type === "comment2" &&
  151. /^(!|\*!)\r?\n/.test(comment.value);
  152. },
  153. sourceMap: true,
  154. sourceMapName: function(dest) {
  155. return dest.replace(".min.js", ".min.map");
  156. },
  157. // Bundles the contents of "`src`" into the "`dest`.map" source map file. This way,
  158. // consumers only need to host the "*.min.js" and "*.min.map" files rather than
  159. // needing to host all three files: "*.js", "*.min.js", and "*.min.map".
  160. sourceMapIncludeSources: true
  161. },
  162. files: [
  163. {
  164. src: ["<%= concat.core.dest %>"],
  165. dest: "dist/ZeroClipboard.Core.min.js"
  166. },
  167. {
  168. src: ["<%= concat.client.dest %>"],
  169. dest: "dist/ZeroClipboard.min.js"
  170. }
  171. ]
  172. }
  173. },
  174. mxmlc: {
  175. options: {
  176. rawConfig: "-target-player=11.0.0 -static-link-runtime-shared-libraries=true -actionscript-file-encoding=UTF-8"
  177. },
  178. swf: {
  179. files: {
  180. "dist/ZeroClipboard.swf": ["<%= concat.flash.files[0].dest %>"]
  181. }
  182. }
  183. },
  184. template: {
  185. options: {
  186. data: pkg
  187. },
  188. bower: {
  189. files: {
  190. "bower.json": ["src/meta/bower.json.tmpl"]
  191. }
  192. },
  193. composer: {
  194. files: {
  195. "composer.json": ["src/meta/composer.json.tmpl"]
  196. }
  197. },
  198. LICENSE: {
  199. files: {
  200. "LICENSE": ["src/meta/LICENSE.tmpl"]
  201. }
  202. }
  203. },
  204. chmod: {
  205. options: {
  206. mode: "444"
  207. },
  208. dist: ["dist/ZeroClipboard.*"],
  209. meta: ["bower.json", "composer.json", "LICENSE"]
  210. },
  211. connect: {
  212. server: {
  213. options: {
  214. port: localPort
  215. }
  216. }
  217. },
  218. qunit: {
  219. file: [
  220. "test/shared/private.tests.js.html",
  221. "test/core/private.tests.js.html",
  222. "test/core/api.tests.js.html",
  223. "test/client/private.tests.js.html",
  224. "test/client/api.tests.js.html",
  225. "test/built/ZeroClipboard.Core.tests.js.html",
  226. "test/built/ZeroClipboard.tests.js.html"
  227. //"test/**/*.tests.js.html"
  228. ],
  229. http: {
  230. options: {
  231. urls:
  232. grunt.file.expand([
  233. "test/shared/private.tests.js.html",
  234. "test/core/private.tests.js.html",
  235. "test/core/api.tests.js.html",
  236. "test/client/private.tests.js.html",
  237. "test/client/api.tests.js.html",
  238. "test/built/ZeroClipboard.Core.tests.js.html",
  239. "test/built/ZeroClipboard.tests.js.html"
  240. //"test/**/*.tests.js.html"
  241. ]).map(function(testPage) {
  242. return "http://localhost:" + localPort + "/" + testPage + "?noglobals=true";
  243. })
  244. }
  245. },
  246. coveralls: {
  247. options: {
  248. "--web-security": false,
  249. timeout: 10000,
  250. coverage: {
  251. baseUrl: ".",
  252. src: [
  253. "src/js/**/*.js",
  254. "!src/js/start.js",
  255. "!src/js/end.js",
  256. "dist/*.js",
  257. "!dist/*.min.js"
  258. ],
  259. instrumentedFiles: "tmp",
  260. htmlReport: "coverage/html",
  261. lcovReport: "coverage/lcov",
  262. statementsThresholdPct: 60.0,
  263. disposeCollector: true
  264. },
  265. urls:
  266. grunt.file.expand([
  267. "test/shared/private.tests.js.html",
  268. "test/core/private.tests.js.html",
  269. "test/core/api.tests.js.html",
  270. "test/client/private.tests.js.html",
  271. "test/client/api.tests.js.html",
  272. "test/built/ZeroClipboard.Core.tests.js.html",
  273. "test/built/ZeroClipboard.tests.js.html"
  274. //"test/**/*.tests.js.html"
  275. ]).map(function(testPage) {
  276. return "http://localhost:" + localPort + "/" + testPage + "?noglobals=true";
  277. })
  278. }
  279. }
  280. },
  281. coveralls: {
  282. options: {
  283. force: true
  284. },
  285. all: {
  286. src: "<%= qunit.coveralls.options.coverage.lcovReport %>/lcov.info"
  287. }
  288. },
  289. watch: {
  290. options: {
  291. spawn: false
  292. },
  293. gruntfile: {
  294. files: "<%= jshint.Gruntfile %>",
  295. tasks: ["jshint:Gruntfile"]
  296. },
  297. js: {
  298. files: "<%= jshint.js %>",
  299. tasks: ["jshint:js", "unittest"]
  300. },
  301. test: {
  302. files: "<%= jshint.test %>",
  303. tasks: ["jshint:test", "unittest"]
  304. }
  305. }
  306. };
  307. grunt.initConfig(config);
  308. // Task aliases and chains
  309. grunt.registerTask("jshint-prebuild", ["jshint:gruntfile", "jshint:component", "jshint:js", "jshint:test"]);
  310. grunt.registerTask("prep-flash", ["clean:flash", "concat:flash"]);
  311. grunt.registerTask("validate", ["jshint-prebuild", "prep-flash", "flexpmd"]);
  312. grunt.registerTask("build", ["clean", "concat", "jshint:dist", "uglify", "mxmlc", "template", "chmod"]);
  313. grunt.registerTask("build-travis", ["clean", "concat", "jshint:dist", "mxmlc", "chmod:dist"]);
  314. grunt.registerTask("test", ["connect", "qunit:file", "qunit:http"]);
  315. // Default task
  316. grunt.registerTask("default", ["validate", "build", "test"]);
  317. // Travis CI task
  318. grunt.registerTask("travis", ["validate", "build-travis", "test", "qunit:coveralls", "coveralls"]);
  319. // Local Flash dev
  320. grunt.registerTask("asdev", ["validate", "build"]);
  321. };