Browse Source

add continuous build

Gregg Tavares 6 years ago
parent
commit
8ef676c18f
2 changed files with 90 additions and 21 deletions
  1. 54 9
      Gruntfile.js
  2. 36 12
      build/js/build.js

+ 54 - 9
Gruntfile.js

@@ -15,6 +15,11 @@ module.exports = function(grunt) {
     return !s_ignoreRE.test(filename);
   }
 
+  const s_isMdRE = /\.md$/i;
+  function mdsOnly(filename) {
+    return s_isMdRE.test(filename);
+  }
+
   function notFolder(filename) {
     return !fs.statSync(filename).isDirectory();
   }
@@ -58,6 +63,11 @@ module.exports = function(grunt) {
     clean: [
       'out/**/*',
     ],
+    buildlesson: {
+      main: {
+        files: [],
+      },
+    },
     watch: {
       main: {
         files: [
@@ -69,6 +79,15 @@ module.exports = function(grunt) {
           spawn: false,
         },
       },
+      lessons: {
+        files: [
+          'threejs/lessons/**/threejs*.md',
+        ],
+        tasks: ['buildlesson'],
+        options: {
+          spawn: false,
+        },
+      },
     },
   });
 
@@ -80,6 +99,11 @@ module.exports = function(grunt) {
         dest: 'out/',
       };
     }));
+    grunt.config('buildlesson.main.files', Object.keys(changedFiles).filter(mdsOnly).map((file) => {
+      return {
+        src: file,
+      };
+    }));
     changedFiles = {};
   }, 200);
   grunt.event.on('watch', function(action, filepath) {
@@ -87,18 +111,39 @@ module.exports = function(grunt) {
     onChange();
   });
 
+  const buildSettings = {
+    outDir: 'out',
+    baseUrl: 'http://threejsfundamentals.org',
+    rootFolder: 'threejs',
+    lessonGrep: 'threejs*.md',
+    siteName: 'ThreeJSFundamentals',
+    siteThumbnail: 'threejsfundamentals.jpg',  // in rootFolder/lessons/resources
+    templatePath: 'build/templates',
+  };
+
+  // just the hackiest way to get this working.
+  grunt.registerMultiTask('buildlesson', 'build a lesson', function() {
+    const filenames = new Set();
+    this.files.forEach((files) => {
+      files.src.forEach((filename) => {
+        filenames.add(filename);
+      });
+    });
+    const buildStuff = require('./build/js/build');
+    const settings = Object.assign({}, buildSettings, {
+      filenames,
+    });
+    const finish = this.async();
+    buildStuff(settings).then(function() {
+      finish();
+    }).done();
+  });
+
   grunt.registerTask('buildlessons', function() {
     const buildStuff = require('./build/js/build');
     const finish = this.async();
-    buildStuff({
-      outDir: 'out',
-      baseUrl: 'http://threejsfundamentals.org',
-      rootFolder: 'threejs',
-      lessonGrep: 'threejs*.md',
-      siteName: 'ThreeJSFundamentals',
-      siteThumbnail: 'threejsfundamentals.jpg',  // in rootFolder/lessons/resources
-    }).then(function() {
-        finish();
+    buildStuff(buildSettings).then(function() {
+      finish();
     }).done();
   });
 

+ 36 - 12
build/js/build.js

@@ -11,6 +11,8 @@ if (parseInt((/^v(\d+)\./).exec(process.version)[1]) < requiredNodeVersion) {
 
 module.exports = function(settings) { // wrapper in case we're in module_context mode
 
+const hackyProcessSelectFiles = settings.filenames !== undefined;
+
 const cache      = new (require('inmemfilecache'))();
 const Feed       = require('feed').Feed;
 const fs         = require('fs');
@@ -201,6 +203,11 @@ function slashify(s) {
 }
 
 function articleFilter(f) {
+  if (hackyProcessSelectFiles) {
+    if (!settings.filenames.has(f)) {
+      return false;
+    }
+  }
   return !process.env['ARTICLE_FILTER'] || f.indexOf(process.env['ARTICLE_FILTER']) >= 0;
 }
 
@@ -537,6 +544,10 @@ const Builder = function(outBaseDir, options) {
       });
     }
 
+    if (hackyProcessSelectFiles) {
+      return Promise.resolve();
+    }
+
     // generate place holders for non-translated files
     const missing = g_origArticles.filter(name => articlesFilenames.indexOf(name) < 0);
     missing.forEach(name => {
@@ -704,6 +715,18 @@ const Builder = function(outBaseDir, options) {
       table_of_contents: '',
       templateOptions: '',
     });
+
+    {
+      const filename = path.join(settings.outDir, 'link-check.html');
+      const html = `
+      <html>
+      <body>
+      ${langs.map(lang => `<a href="${lang.home}">${lang.lang}</a>`).join('\n')}
+      </body>
+      </html>
+      `;
+      writeFileIfChanged(filename, html);
+    }
   };
 
 
@@ -766,16 +789,14 @@ langs = langs.concat(readdirs(`${settings.rootFolder}/lessons`)
 
 b.preProcess(langs);
 
-{
-  const filename = path.join(settings.outDir, 'link-check.html');
-  const html = `
-  <html>
-  <body>
-  ${langs.map(lang => `<a href="${lang.home}">${lang.lang}</a>`).join('\n')}
-  </body>
-  </html>
-  `;
-  writeFileIfChanged(filename, html);
+if (hackyProcessSelectFiles) {
+  const langsInFilenames = new Set();
+  [...settings.filenames].forEach((filename) => {
+    const m = /lessons\/(\w{2}|\w{5})\//.exec(filename);
+    const lang = m ? m[1] : 'en';
+    langsInFilenames.add(lang);
+  });
+  langs = langs.filter(lang => langsInFilenames.has(lang.lang));
 }
 
 const tasks = langs.map(function(lang) {
@@ -787,9 +808,12 @@ const tasks = langs.map(function(lang) {
 return tasks.reduce(function(cur, next) {
   return cur.then(next);
 }, Promise.resolve()).then(function() {
-  b.writeGlobalFiles();
-  cache.clear();
+  if (!hackyProcessSelectFiles) {
+    b.writeGlobalFiles(langs);
+  }
   return numErrors ? Promise.reject(new Error(`${numErrors} errors`)) : Promise.resolve();
+}).finally(() => {
+  cache.clear();
 });
 
 };