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.

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