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.

215 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. // -----------------------------------------------------------------------------
  37. // Configuration
  38. // -----------------------------------------------------------------------------
  39. const dataFolder = 'espurna/data/';
  40. const staticFolder = 'espurna/static/';
  41. // -----------------------------------------------------------------------------
  42. // Methods
  43. // -----------------------------------------------------------------------------
  44. var buildHeaderFile = function() {
  45. String.prototype.replaceAll = function(search, replacement) {
  46. var target = this;
  47. return target.split(search).join(replacement);
  48. };
  49. return map(function(file, cb) {
  50. var parts = file.path.split("/");
  51. var filename = parts[parts.length - 1];
  52. var destination = staticFolder + filename + ".h";
  53. var safename = "webui_image";
  54. var wstream = fs.createWriteStream(destination);
  55. wstream.on('error', function (err) {
  56. console.error(err);
  57. });
  58. var data = fs.readFileSync(file.path);
  59. wstream.write('#define ' + safename + '_len ' + data.length + '\n');
  60. wstream.write('const uint8_t ' + safename + '[] PROGMEM = {');
  61. for (var i=0; i<data.length; i++) {
  62. if (0 === (i % 20)) {
  63. wstream.write('\n');
  64. }
  65. wstream.write('0x' + ('00' + data[i].toString(16)).slice(-2));
  66. if (i < (data.length - 1)) {
  67. wstream.write(',');
  68. }
  69. }
  70. wstream.write('\n};');
  71. wstream.end();
  72. var fstat = fs.statSync(file.path);
  73. console.log("Created '" + filename + "' size: " + fstat.size + " bytes");
  74. cb(0, destination);
  75. });
  76. }
  77. var htmllintReporter = function(filepath, issues) {
  78. if (issues.length > 0) {
  79. issues.forEach(function (issue) {
  80. console.info(
  81. '[gulp-htmllint] ' +
  82. filepath + ' [' +
  83. issue.line + ',' +
  84. issue.column + ']: ' +
  85. '(' + issue.code + ') ' +
  86. issue.msg
  87. );
  88. });
  89. process.exitCode = 1;
  90. }
  91. };
  92. var buildWebUI = function(module) {
  93. var modules = {"light": false, "sensor": false, "rfbridge": false, "rfm69": false};
  94. if ("all" == module) {
  95. modules["light"] = true;
  96. modules["sensor"] = true;
  97. modules["rfbridge"] = false; // we will never be adding this except when building RFBRIDGE
  98. modules["rfm69"] = false; // we will never be adding this except when building RFM69GW
  99. } else if ("small" != module) {
  100. modules[module] = true;
  101. }
  102. return gulp.src('html/*.html').
  103. pipe(htmllint({
  104. 'failOnError': true,
  105. 'rules': {
  106. 'id-class-style': false,
  107. 'label-req-for': false,
  108. }
  109. }, htmllintReporter)).
  110. pipe(favicon()).
  111. pipe(inline({
  112. base: 'html/',
  113. js: [],
  114. css: [crass, inlineImages],
  115. disabledTypes: ['svg', 'img']
  116. })).
  117. pipe(remover(modules)).
  118. pipe(htmlmin({
  119. collapseWhitespace: true,
  120. removeComments: true,
  121. minifyCSS: true,
  122. minifyJS: true
  123. })).
  124. pipe(replace('pure-', 'p-')).
  125. pipe(gzip()).
  126. pipe(rename("index." + module + ".html.gz")).
  127. pipe(gulp.dest(dataFolder));
  128. };
  129. // -----------------------------------------------------------------------------
  130. // Tasks
  131. // -----------------------------------------------------------------------------
  132. gulp.task('build_certs', function() {
  133. gulp.src(dataFolder + 'server.*').
  134. pipe(buildHeaderFile());
  135. });
  136. gulp.task('csslint', function() {
  137. gulp.src('html/*.css').
  138. pipe(csslint({ids: false})).
  139. pipe(csslint.formatter());
  140. });
  141. gulp.task('build_webui_small', function() {
  142. return buildWebUI("small");
  143. })
  144. gulp.task('build_webui_sensor', function() {
  145. return buildWebUI("sensor");
  146. })
  147. gulp.task('build_webui_light', function() {
  148. return buildWebUI("light");
  149. })
  150. gulp.task('build_webui_rfbridge', function() {
  151. return buildWebUI("rfbridge");
  152. })
  153. gulp.task('build_webui_rfm69', function() {
  154. return buildWebUI("rfm69");
  155. })
  156. gulp.task('build_webui_all', function() {
  157. return buildWebUI("all");
  158. })
  159. gulp.task('buildfs_inline', function(cb) {
  160. runSequence([
  161. 'build_webui_small',
  162. 'build_webui_sensor',
  163. 'build_webui_light',
  164. 'build_webui_rfbridge',
  165. 'build_webui_rfm69',
  166. 'build_webui_all'
  167. ], cb);
  168. });
  169. gulp.task('buildfs_embeded', ['buildfs_inline'], function() {
  170. gulp.src(dataFolder + 'index.*').
  171. pipe(buildHeaderFile());
  172. });
  173. gulp.task('default', ['buildfs_embeded']);