Fork of the espurna firmware for `mhsw` switches
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.

216 lines
6.2 KiB

8 years ago
8 years ago
6 years ago
8 years ago
8 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
8 years ago
8 years ago
6 years ago
  1. /*
  2. ESP8266 file system builder
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*eslint quotes: ["error", "single"]*/
  16. /*eslint-env es6*/
  17. // -----------------------------------------------------------------------------
  18. // Dependencies
  19. // -----------------------------------------------------------------------------
  20. const fs = require('fs');
  21. const gulp = require('gulp');
  22. const htmlmin = require('gulp-htmlmin');
  23. const uglify = require('gulp-uglify');
  24. const gzip = require('gulp-gzip');
  25. const inline = require('gulp-inline');
  26. const inlineImages = require('gulp-css-base64');
  27. const favicon = require('gulp-base64-favicon');
  28. const htmllint = require('gulp-htmllint');
  29. const csslint = require('gulp-csslint');
  30. const crass = require('gulp-crass');
  31. const replace = require('gulp-replace');
  32. const remover = require('gulp-remove-code');
  33. const map = require('map-stream');
  34. const rename = require('gulp-rename');
  35. const runSequence = require('run-sequence');
  36. const path = require('path');
  37. // -----------------------------------------------------------------------------
  38. // Configuration
  39. // -----------------------------------------------------------------------------
  40. const dataFolder = 'espurna/data/';
  41. const staticFolder = 'espurna/static/';
  42. // -----------------------------------------------------------------------------
  43. // Methods
  44. // -----------------------------------------------------------------------------
  45. var buildHeaderFile = function() {
  46. String.prototype.replaceAll = function(search, replacement) {
  47. var target = this;
  48. return target.split(search).join(replacement);
  49. };
  50. return map(function(file, cb) {
  51. var parts = file.path.split(path.sep);
  52. var filename = parts[parts.length - 1];
  53. var destination = staticFolder + filename + ".h";
  54. var safename = "webui_image";
  55. var wstream = fs.createWriteStream(destination);
  56. wstream.on('error', function (err) {
  57. console.error(err);
  58. });
  59. var data = fs.readFileSync(file.path);
  60. wstream.write('#define ' + safename + '_len ' + data.length + '\n');
  61. wstream.write('const uint8_t ' + safename + '[] PROGMEM = {');
  62. for (var i=0; i<data.length; i++) {
  63. if (0 === (i % 20)) {
  64. wstream.write('\n');
  65. }
  66. wstream.write('0x' + ('00' + data[i].toString(16)).slice(-2));
  67. if (i < (data.length - 1)) {
  68. wstream.write(',');
  69. }
  70. }
  71. wstream.write('\n};');
  72. wstream.end();
  73. var fstat = fs.statSync(file.path);
  74. console.log("Created '" + filename + "' size: " + fstat.size + " bytes");
  75. cb(0, destination);
  76. });
  77. }
  78. var htmllintReporter = function(filepath, issues) {
  79. if (issues.length > 0) {
  80. issues.forEach(function (issue) {
  81. console.info(
  82. '[gulp-htmllint] ' +
  83. filepath + ' [' +
  84. issue.line + ',' +
  85. issue.column + ']: ' +
  86. '(' + issue.code + ') ' +
  87. issue.msg
  88. );
  89. });
  90. process.exitCode = 1;
  91. }
  92. };
  93. var buildWebUI = function(module) {
  94. var modules = {"light": false, "sensor": false, "rfbridge": false, "rfm69": false};
  95. if ("all" == module) {
  96. modules["light"] = true;
  97. modules["sensor"] = true;
  98. modules["rfbridge"] = false; // we will never be adding this except when building RFBRIDGE
  99. modules["rfm69"] = false; // we will never be adding this except when building RFM69GW
  100. } else if ("small" != module) {
  101. modules[module] = true;
  102. }
  103. return gulp.src('html/*.html').
  104. pipe(htmllint({
  105. 'failOnError': true,
  106. 'rules': {
  107. 'id-class-style': false,
  108. 'label-req-for': false,
  109. }
  110. }, htmllintReporter)).
  111. pipe(favicon()).
  112. pipe(inline({
  113. base: 'html/',
  114. js: [],
  115. css: [crass, inlineImages],
  116. disabledTypes: ['svg', 'img']
  117. })).
  118. pipe(remover(modules)).
  119. pipe(htmlmin({
  120. collapseWhitespace: true,
  121. removeComments: true,
  122. minifyCSS: true,
  123. minifyJS: true
  124. })).
  125. pipe(replace('pure-', 'p-')).
  126. pipe(gzip()).
  127. pipe(rename("index." + module + ".html.gz")).
  128. pipe(gulp.dest(dataFolder));
  129. };
  130. // -----------------------------------------------------------------------------
  131. // Tasks
  132. // -----------------------------------------------------------------------------
  133. gulp.task('build_certs', function() {
  134. gulp.src(dataFolder + 'server.*').
  135. pipe(buildHeaderFile());
  136. });
  137. gulp.task('csslint', function() {
  138. gulp.src('html/*.css').
  139. pipe(csslint({ids: false})).
  140. pipe(csslint.formatter());
  141. });
  142. gulp.task('build_webui_small', function() {
  143. return buildWebUI("small");
  144. })
  145. gulp.task('build_webui_sensor', function() {
  146. return buildWebUI("sensor");
  147. })
  148. gulp.task('build_webui_light', function() {
  149. return buildWebUI("light");
  150. })
  151. gulp.task('build_webui_rfbridge', function() {
  152. return buildWebUI("rfbridge");
  153. })
  154. gulp.task('build_webui_rfm69', function() {
  155. return buildWebUI("rfm69");
  156. })
  157. gulp.task('build_webui_all', function() {
  158. return buildWebUI("all");
  159. })
  160. gulp.task('buildfs_inline', function(cb) {
  161. runSequence([
  162. 'build_webui_small',
  163. 'build_webui_sensor',
  164. 'build_webui_light',
  165. 'build_webui_rfbridge',
  166. 'build_webui_rfm69',
  167. 'build_webui_all'
  168. ], cb);
  169. });
  170. gulp.task('buildfs_embeded', ['buildfs_inline'], function() {
  171. gulp.src(dataFolder + 'index.*').
  172. pipe(buildHeaderFile());
  173. });
  174. gulp.task('default', ['buildfs_embeded']);