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.

208 lines
5.9 KiB

8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
7 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};
  94. if ("all" == module) {
  95. modules["light"] = true;
  96. modules["rfbridge"] = true;
  97. modules["sensor"] = true;
  98. } else if ("small" != module) {
  99. modules[module] = true;
  100. }
  101. return gulp.src('html/*.html').
  102. pipe(htmllint({
  103. 'failOnError': true,
  104. 'rules': {
  105. 'id-class-style': false,
  106. 'label-req-for': false,
  107. }
  108. }, htmllintReporter)).
  109. pipe(favicon()).
  110. pipe(inline({
  111. base: 'html/',
  112. js: [],
  113. css: [crass, inlineImages],
  114. disabledTypes: ['svg', 'img']
  115. })).
  116. pipe(remover(modules)).
  117. pipe(htmlmin({
  118. collapseWhitespace: true,
  119. removeComments: true,
  120. minifyCSS: true,
  121. minifyJS: true
  122. })).
  123. pipe(replace('pure-', 'p-')).
  124. pipe(gzip()).
  125. pipe(rename("index." + module + ".html.gz")).
  126. pipe(gulp.dest(dataFolder));
  127. };
  128. // -----------------------------------------------------------------------------
  129. // Tasks
  130. // -----------------------------------------------------------------------------
  131. gulp.task('build_certs', function() {
  132. gulp.src(dataFolder + 'server.*').
  133. pipe(buildHeaderFile());
  134. });
  135. gulp.task('csslint', function() {
  136. gulp.src('html/*.css').
  137. pipe(csslint({ids: false})).
  138. pipe(csslint.formatter());
  139. });
  140. gulp.task('build_webui_small', function() {
  141. return buildWebUI("small");
  142. })
  143. gulp.task('build_webui_sensor', function() {
  144. return buildWebUI("sensor");
  145. })
  146. gulp.task('build_webui_light', function() {
  147. return buildWebUI("light");
  148. })
  149. gulp.task('build_webui_rfbridge', function() {
  150. return buildWebUI("rfbridge");
  151. })
  152. gulp.task('build_webui_all', function() {
  153. return buildWebUI("all");
  154. })
  155. gulp.task('buildfs_inline', function(cb) {
  156. runSequence([
  157. 'build_webui_small',
  158. 'build_webui_sensor',
  159. 'build_webui_light',
  160. 'build_webui_rfbridge'
  161. ], cb);
  162. });
  163. gulp.task('buildfs_embeded', ['buildfs_inline'], function() {
  164. gulp.src(dataFolder + 'index.*').
  165. pipe(buildHeaderFile());
  166. });
  167. gulp.task('default', ['buildfs_embeded']);