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.

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