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.

250 lines
6.9 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
5 years ago
5 years ago
  1. /*
  2. ESP8266 file system builder
  3. Copyright (C) 2016-2019 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 gulp = require('gulp');
  21. const through = require('through2');
  22. const htmlmin = require('gulp-htmlmin');
  23. const inline = require('gulp-inline');
  24. const inlineImages = require('gulp-css-base64');
  25. const favicon = require('gulp-base64-favicon');
  26. const crass = require('gulp-crass');
  27. const htmllint = require('gulp-htmllint');
  28. const csslint = require('gulp-csslint');
  29. const rename = require('gulp-rename');
  30. const replace = require('gulp-replace');
  31. const remover = require('gulp-remove-code');
  32. const gzip = require('gulp-gzip');
  33. const path = require('path');
  34. // -----------------------------------------------------------------------------
  35. // Configuration
  36. // -----------------------------------------------------------------------------
  37. const htmlFolder = 'html/';
  38. const configFolder = 'espurna/config/';
  39. const dataFolder = 'espurna/data/';
  40. const staticFolder = 'espurna/static/';
  41. // -----------------------------------------------------------------------------
  42. // Methods
  43. // -----------------------------------------------------------------------------
  44. var toHeader = function(name, debug) {
  45. return through.obj(function (source, encoding, callback) {
  46. var parts = source.path.split(path.sep);
  47. var filename = parts[parts.length - 1];
  48. var safename = name || filename.split('.').join('_');
  49. // Generate output
  50. var output = '';
  51. output += '#define ' + safename + '_len ' + source.contents.length + '\n';
  52. output += 'const uint8_t ' + safename + '[] PROGMEM = {';
  53. for (var i=0; i<source.contents.length; i++) {
  54. if (i > 0) { output += ','; }
  55. if (0 === (i % 20)) { output += '\n'; }
  56. output += '0x' + ('00' + source.contents[i].toString(16)).slice(-2);
  57. }
  58. output += '\n};';
  59. // clone the contents
  60. var destination = source.clone();
  61. destination.path = source.path + '.h';
  62. destination.contents = Buffer.from(output);
  63. if (debug) {
  64. console.info('Image ' + filename + ' \tsize: ' + source.contents.length + ' bytes');
  65. }
  66. callback(null, destination);
  67. });
  68. };
  69. var htmllintReporter = function(filepath, issues) {
  70. if (issues.length > 0) {
  71. issues.forEach(function (issue) {
  72. console.info(
  73. '[gulp-htmllint] ' +
  74. filepath + ' [' +
  75. issue.line + ',' +
  76. issue.column + ']: ' +
  77. '(' + issue.code + ') ' +
  78. issue.msg
  79. );
  80. });
  81. process.exitCode = 1;
  82. }
  83. };
  84. var buildWebUI = function(module) {
  85. // Declare some modules as optional to remove with
  86. // removeIf(!name) ...code... endRemoveIf(!name) sections
  87. // (via gulp-remove-code)
  88. var modules = {
  89. 'light': false,
  90. 'sensor': false,
  91. 'rfbridge': false,
  92. 'rfm69': false,
  93. 'garland': false,
  94. 'thermostat': false,
  95. 'lightfox': false,
  96. 'curtain': false
  97. };
  98. // Note: only build these when specified as module arg
  99. var excludeAll = [
  100. 'rfm69',
  101. 'lightfox'
  102. ];
  103. // 'all' to include all *but* excludeAll
  104. // '<module>' to include a single module
  105. // 'small' is the default state (all disabled)
  106. if ('all' === module) {
  107. Object.keys(modules).
  108. filter(function(key) {
  109. return excludeAll.indexOf(key) < 0;
  110. }).
  111. forEach(function(key) {
  112. modules[key] = true;
  113. });
  114. } else if ('small' !== module) {
  115. modules[module] = true;
  116. }
  117. return gulp.src(htmlFolder + '*.html').
  118. pipe(htmllint({
  119. 'failOnError': true,
  120. 'rules': {
  121. 'id-class-style': false,
  122. 'label-req-for': false,
  123. 'line-end-style': false,
  124. }
  125. }, htmllintReporter)).
  126. pipe(favicon()).
  127. pipe(inline({
  128. base: htmlFolder,
  129. js: [],
  130. css: [crass, inlineImages],
  131. disabledTypes: ['svg', 'img']
  132. })).
  133. pipe(remover(modules)).
  134. pipe(htmlmin({
  135. collapseWhitespace: true,
  136. removeComments: true,
  137. minifyCSS: true,
  138. minifyJS: true
  139. })).
  140. pipe(replace('pure-', 'p-')).
  141. pipe(gzip({ gzipOptions: { level: 9 } })).
  142. pipe(rename('index.' + module + '.html.gz')).
  143. pipe(gulp.dest(dataFolder)).
  144. pipe(toHeader('webui_image', true)).
  145. pipe(gulp.dest(staticFolder));
  146. };
  147. // -----------------------------------------------------------------------------
  148. // Tasks
  149. // -----------------------------------------------------------------------------
  150. gulp.task('certs', function() {
  151. gulp.src(dataFolder + 'server.*').
  152. pipe(toHeader(debug=false)).
  153. pipe(gulp.dest(staticFolder));
  154. });
  155. gulp.task('csslint', function() {
  156. gulp.src(htmlFolder + '*.css').
  157. pipe(csslint({ids: false})).
  158. pipe(csslint.formatter());
  159. });
  160. gulp.task('webui_small', function() {
  161. return buildWebUI('small');
  162. });
  163. gulp.task('webui_sensor', function() {
  164. return buildWebUI('sensor');
  165. });
  166. gulp.task('webui_light', function() {
  167. return buildWebUI('light');
  168. });
  169. gulp.task('webui_rfbridge', function() {
  170. return buildWebUI('rfbridge');
  171. });
  172. gulp.task('webui_rfm69', function() {
  173. return buildWebUI('rfm69');
  174. });
  175. gulp.task('webui_lightfox', function() {
  176. return buildWebUI('lightfox');
  177. });
  178. gulp.task('webui_garland', function() {
  179. return buildWebUI('garland');
  180. });
  181. gulp.task('webui_thermostat', function() {
  182. return buildWebUI('thermostat');
  183. });
  184. gulp.task('webui_curtain', function() {
  185. return buildWebUI('curtain');
  186. });
  187. gulp.task('webui_all', function() {
  188. return buildWebUI('all');
  189. });
  190. gulp.task('webui',
  191. gulp.parallel(
  192. 'webui_small',
  193. 'webui_sensor',
  194. 'webui_light',
  195. 'webui_rfbridge',
  196. 'webui_rfm69',
  197. 'webui_lightfox',
  198. 'webui_garland',
  199. 'webui_thermostat',
  200. 'webui_curtain',
  201. 'webui_all'
  202. )
  203. );
  204. gulp.task('default', gulp.series('webui'));