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.

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