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.

88 lines
3.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. module.exports = function (grunt) {
  2. "use strict";
  3. grunt.initConfig({
  4. clean: ["./dist", "./tmp"],
  5. copy: {
  6. build: {
  7. files: [{
  8. expand: true,
  9. cwd: "./src",
  10. src: ["**/license*", "**/*.lua", "**/*.cfg"],
  11. dest: "dist/",
  12. filter: 'isFile'
  13. }]
  14. }
  15. }
  16. });
  17. grunt.loadNpmTasks('grunt-contrib-clean');
  18. grunt.loadNpmTasks("grunt-contrib-copy");
  19. grunt.loadNpmTasks('grunt-file-append');
  20. grunt.loadNpmTasks('grunt-exec');
  21. grunt.registerTask("compile-moonscript", "compiles moonscript files to lua", function() {
  22. grunt.file.expand({filter: 'isDirectory'}, "src/**").forEach(function (dir) {
  23. grunt.file.expand({filter: 'isFile'}, dir + "/*.moon").forEach(function (file) {
  24. const directory = file.substr(0, file.lastIndexOf('/') + 1).replace("src/","");
  25. const file_name = file.substr(file.lastIndexOf('/') + 1);
  26. const file_name_sans_ext = file_name.substr(0, file_name.lastIndexOf('.'));
  27. const output_path = "dist/" + directory + file_name_sans_ext + '.lua';
  28. const exec_task = grunt.config.get('exec') || {};
  29. exec_task[file] = {
  30. command: "moonc -o " + output_path + " " + file
  31. }
  32. console.log("Added exec task: moonc -o " + output_path + " " + file)
  33. grunt.config.set('exec', exec_task);
  34. });
  35. });
  36. });
  37. grunt.registerTask("minify-lua-modules", "minifies lua modules", function () {
  38. const fs = require('fs');
  39. const luamin = require('luamin');
  40. const util = require('util');
  41. grunt.file.expand({filter: 'isDirectory'}, "dist/**").forEach(function (dir) {
  42. grunt.file.expand({filter: 'isFile'}, dir + "/*.lua").forEach(function (file) {
  43. const directory = file.substr(0, file.lastIndexOf('/'));
  44. const file_name = file.substr(file.lastIndexOf('/') + 1);
  45. const file_name_sans_ext = file_name.substr(0, file_name.lastIndexOf('.'));
  46. const lua = fs.readFileSync(file, 'utf8');
  47. const minified_contents = luamin.minify(lua);
  48. const minified_file_name = file_name_sans_ext + '.min.lua';
  49. const minified_file = directory + '/' + minified_file_name;
  50. fs.writeFileSync(minified_file, minified_contents);
  51. const file_stats = fs.statSync(file);
  52. const minified_file_stats = fs.statSync(minified_file);
  53. const saved_space = file_stats.size - minified_file_stats.size;
  54. const saved_space_percent = (100 * (1 - minified_file_stats.size / file_stats.size)).toFixed(2);
  55. console.log(util.format("Minified: %s: %f KB -> %s: %f KB. SAVED %d%% %d KB", file_name, file_stats.size / 1000, minified_file_name, minified_file_stats.size / 1000, saved_space_percent, saved_space / 1000));
  56. });
  57. });
  58. });
  59. grunt.registerTask("build-spm-bootstrap", "Make sure a single bootstrap file exists for SPM", function () {
  60. const fs = require('fs');
  61. grunt.file.expand({filter: 'isFile'}, "dist/**/bootstrap.min.lua").forEach(function (minified_file) {
  62. const directory = minified_file.substr(0, minified_file.lastIndexOf('/'));
  63. const minified_contents = fs.readFileSync(minified_file, 'utf8');
  64. fs.writeFileSync(directory + '/bootstrap.lua', minified_contents);
  65. fs.unlinkSync(minified_file);
  66. console.log("Removed unminified bootstrap file: " + directory + '/bootstrap.lua')
  67. });
  68. });
  69. grunt.registerTask("default", [
  70. "clean",
  71. "copy",
  72. "compile-moonscript",
  73. "exec",
  74. "minify-lua-modules",
  75. "build-spm-bootstrap"
  76. ]);
  77. };