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.

239 lines
7.1 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
  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 gulp = require('gulp');
  21. const runSequence = require('run-sequence');
  22. const through = require('through2');
  23. const htmlmin = require('gulp-htmlmin');
  24. const inline = require('gulp-inline');
  25. const inlineImages = require('gulp-css-base64');
  26. const favicon = require('gulp-base64-favicon');
  27. const crass = require('gulp-crass');
  28. const htmllint = require('gulp-htmllint');
  29. const csslint = require('gulp-csslint');
  30. const jsonlint = require('gulp-jsonlint');
  31. const concat = require('gulp-concat');
  32. const gap = require('gulp-append-prepend');
  33. const rename = require('gulp-rename');
  34. const replace = require('gulp-replace');
  35. const remover = require('gulp-remove-code');
  36. const gzip = require('gulp-gzip');
  37. const path = require('path');
  38. // -----------------------------------------------------------------------------
  39. // Configuration
  40. // -----------------------------------------------------------------------------
  41. const htmlFolder = 'html/';
  42. const configFolder = 'espurna/config/';
  43. const dataFolder = 'espurna/data/';
  44. const staticFolder = 'espurna/static/';
  45. const devicesFolder = 'devices/';
  46. // -----------------------------------------------------------------------------
  47. // Methods
  48. // -----------------------------------------------------------------------------
  49. var toHeader = function(name, debug) {
  50. return through.obj(function (source, encoding, callback) {
  51. var parts = source.path.split(path.sep);
  52. var filename = parts[parts.length - 1];
  53. var safename = name || filename.split('.').join('_');
  54. // Generate output
  55. var output = '';
  56. output += '#define ' + safename + '_len ' + source.contents.length + '\n';
  57. output += 'const uint8_t ' + safename + '[] PROGMEM = {';
  58. for (var i=0; i<source.contents.length; i++) {
  59. if (i > 0) { output += ','; }
  60. if (0 === (i % 20)) { output += '\n'; }
  61. output += '0x' + ('00' + source.contents[i].toString(16)).slice(-2);
  62. }
  63. output += '\n};';
  64. // clone the contents
  65. var destination = source.clone();
  66. destination.path = source.path + '.h';
  67. destination.contents = Buffer.from(output);
  68. if (debug) {
  69. console.info('Image "' + filename + '" \tsize: ' + source.contents.length + ' bytes');
  70. }
  71. callback(null, destination);
  72. });
  73. };
  74. var htmllintReporter = function(filepath, issues) {
  75. if (issues.length > 0) {
  76. issues.forEach(function (issue) {
  77. console.info(
  78. '[gulp-htmllint] ' +
  79. filepath + ' [' +
  80. issue.line + ',' +
  81. issue.column + ']: ' +
  82. '(' + issue.code + ') ' +
  83. issue.msg
  84. );
  85. });
  86. process.exitCode = 1;
  87. }
  88. };
  89. var buildWebUI = function(module) {
  90. var modules = {'light': false, 'sensor': false, 'rfbridge': false, 'rfm69': false};
  91. if ('all' === module) {
  92. modules['light'] = true;
  93. modules['sensor'] = true;
  94. modules['rfbridge'] = true;
  95. modules['rfm69'] = false; // we will never be adding this except when building RFM69GW
  96. } else if ('small' !== module) {
  97. modules[module] = true;
  98. }
  99. return gulp.src(htmlFolder + '*.html').
  100. pipe(htmllint({
  101. 'failOnError': true,
  102. 'rules': {
  103. 'id-class-style': false,
  104. 'label-req-for': false,
  105. }
  106. }, htmllintReporter)).
  107. pipe(favicon()).
  108. pipe(inline({
  109. base: htmlFolder,
  110. js: [],
  111. css: [crass, inlineImages],
  112. disabledTypes: ['svg', 'img']
  113. })).
  114. pipe(remover(modules)).
  115. pipe(htmlmin({
  116. collapseWhitespace: true,
  117. removeComments: true,
  118. minifyCSS: true,
  119. minifyJS: true
  120. })).
  121. pipe(replace('pure-', 'p-')).
  122. pipe(gzip()).
  123. pipe(rename('index.' + module + '.html.gz')).
  124. pipe(gulp.dest(dataFolder)).
  125. pipe(toHeader('webui_image', true)).
  126. pipe(gulp.dest(staticFolder));
  127. };
  128. // -----------------------------------------------------------------------------
  129. // Tasks
  130. // -----------------------------------------------------------------------------
  131. gulp.task('merge_devices', function() {
  132. gulp.src(devicesFolder + '*.json').
  133. pipe(concat('devices.js', { newLine: ',' })).
  134. pipe(gap.prependText('{ "devices": [')).
  135. pipe(gap.appendText(']}')).
  136. pipe(replace(' ', '')).
  137. pipe(replace('\n', '')).
  138. pipe(replace('[', '[\n')).
  139. pipe(replace('}', '}\n')).
  140. pipe(replace('}\n,', '},\n')).
  141. pipe(jsonlint()).
  142. pipe(jsonlint.reporter(function (file) {
  143. console.error('File ' + file.path + ' is not valid JSON.');
  144. })).
  145. pipe(gulp.dest(htmlFolder));
  146. });
  147. gulp.task('devices', function() {
  148. gulp.src(devicesFolder + '*.json').
  149. pipe(replace(' ', '')).
  150. pipe(replace('\n', '')).
  151. pipe(jsonlint()).
  152. pipe(jsonlint.reporter(function (file) {
  153. console.error('File ' + file.path + ' is not valid JSON.');
  154. })).
  155. pipe(toHeader("device_config", false)).
  156. pipe(gulp.dest(configFolder + "devices/"));
  157. });
  158. gulp.task('certs', function() {
  159. gulp.src(dataFolder + 'server.*').
  160. pipe(toHeader(debug=false)).
  161. pipe(gulp.dest(staticFolder));
  162. });
  163. gulp.task('csslint', function() {
  164. gulp.src(htmlFolder + '*.css').
  165. pipe(csslint({ids: false})).
  166. pipe(csslint.formatter());
  167. });
  168. gulp.task('webui_small', function() {
  169. return buildWebUI('small');
  170. });
  171. gulp.task('webui_sensor', function() {
  172. return buildWebUI('sensor');
  173. });
  174. gulp.task('webui_light', function() {
  175. return buildWebUI('light');
  176. });
  177. gulp.task('webui_rfbridge', function() {
  178. return buildWebUI('rfbridge');
  179. });
  180. gulp.task('webui_rfm69', function() {
  181. return buildWebUI('rfm69');
  182. });
  183. gulp.task('webui_all', function() {
  184. return buildWebUI('all');
  185. });
  186. gulp.task('webui', ['devices'], function(cb) {
  187. runSequence([
  188. 'webui_small',
  189. 'webui_sensor',
  190. 'webui_light',
  191. 'webui_rfbridge',
  192. 'webui_rfm69',
  193. 'webui_all'
  194. ], cb);
  195. });
  196. gulp.task('default', ['webui']);