| [9af201e] | 1 | let fs = require('fs');
|
|---|
| 2 | let Task = require('./task').Task;
|
|---|
| 3 |
|
|---|
| 4 | function isFileOrDirectory(t) {
|
|---|
| 5 | return (t instanceof FileTask ||
|
|---|
| 6 | t instanceof DirectoryTask);
|
|---|
| 7 | }
|
|---|
| 8 |
|
|---|
| 9 | function isFile(t) {
|
|---|
| 10 | return (t instanceof FileTask && !(t instanceof DirectoryTask));
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | @name jake
|
|---|
| 15 | @namespace jake
|
|---|
| 16 | */
|
|---|
| 17 | /**
|
|---|
| 18 | @name jake.FileTask
|
|---|
| 19 | @class`
|
|---|
| 20 | @extentds Task
|
|---|
| 21 | @description A Jake FileTask
|
|---|
| 22 |
|
|---|
| 23 | @param {String} name The name of the Task
|
|---|
| 24 | @param {Array} [prereqs] Prerequisites to be run before this task
|
|---|
| 25 | @param {Function} [action] The action to perform to create this file
|
|---|
| 26 | @param {Object} [opts]
|
|---|
| 27 | @param {Array} [opts.asyc=false] Perform this task asynchronously.
|
|---|
| 28 | If you flag a task with this option, you must call the global
|
|---|
| 29 | `complete` method inside the task's action, for execution to proceed
|
|---|
| 30 | to the next task.
|
|---|
| 31 | */
|
|---|
| 32 | class FileTask extends Task {
|
|---|
| 33 | constructor(...args) {
|
|---|
| 34 | super(...args);
|
|---|
| 35 | this.dummy = false;
|
|---|
| 36 | if (fs.existsSync(this.name)) {
|
|---|
| 37 | this.updateModTime();
|
|---|
| 38 | }
|
|---|
| 39 | else {
|
|---|
| 40 | this.modTime = null;
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | isNeeded() {
|
|---|
| 45 | let prereqs = this.prereqs;
|
|---|
| 46 | let prereqName;
|
|---|
| 47 | let prereqTask;
|
|---|
| 48 |
|
|---|
| 49 | // No repeatsies
|
|---|
| 50 | if (this.taskStatus == Task.runStatuses.DONE) {
|
|---|
| 51 | return false;
|
|---|
| 52 | }
|
|---|
| 53 | // The always-make override
|
|---|
| 54 | else if (jake.program.opts['always-make']) {
|
|---|
| 55 | return true;
|
|---|
| 56 | }
|
|---|
| 57 | // Default case
|
|---|
| 58 | else {
|
|---|
| 59 |
|
|---|
| 60 | // We need either an existing file, or an action to create one.
|
|---|
| 61 | // First try grabbing the actual mod-time of the file
|
|---|
| 62 | try {
|
|---|
| 63 | this.updateModTime();
|
|---|
| 64 | }
|
|---|
| 65 | // Then fall back to looking for an action
|
|---|
| 66 | catch(e) {
|
|---|
| 67 | if (typeof this.action == 'function') {
|
|---|
| 68 | return true;
|
|---|
| 69 | }
|
|---|
| 70 | else {
|
|---|
| 71 | throw new Error('File-task ' + this.fullName + ' has no ' +
|
|---|
| 72 | 'existing file, and no action to create one.');
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | // Compare mod-time of all the prereqs with its mod-time
|
|---|
| 77 | // If any prereqs are newer, need to run the action to update
|
|---|
| 78 | if (prereqs && prereqs.length) {
|
|---|
| 79 | for (let i = 0, ii = prereqs.length; i < ii; i++) {
|
|---|
| 80 | prereqName = prereqs[i];
|
|---|
| 81 | prereqTask = this.namespace.resolveTask(prereqName) ||
|
|---|
| 82 | jake.createPlaceholderFileTask(prereqName, this.namespace);
|
|---|
| 83 | // Run the action if:
|
|---|
| 84 | // 1. The prereq is a normal task (not file/dir)
|
|---|
| 85 | // 2. The prereq is a file-task with a mod-date more recent than
|
|---|
| 86 | // the one for this file/dir
|
|---|
| 87 | if (prereqTask) {
|
|---|
| 88 | if (!isFileOrDirectory(prereqTask) ||
|
|---|
| 89 | (isFile(prereqTask) && prereqTask.modTime > this.modTime)) {
|
|---|
| 90 | return true;
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 | this.taskStatus = Task.runStatuses.DONE;
|
|---|
| 95 | return false;
|
|---|
| 96 | }
|
|---|
| 97 | // File/dir has no prereqs, and exists -- no need to run
|
|---|
| 98 | else {
|
|---|
| 99 | // Effectively done
|
|---|
| 100 | this.taskStatus = Task.runStatuses.DONE;
|
|---|
| 101 | return false;
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | updateModTime() {
|
|---|
| 107 | let stats = fs.statSync(this.name);
|
|---|
| 108 | this.modTime = stats.mtime;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | complete() {
|
|---|
| 112 | if (!this.dummy) {
|
|---|
| 113 | this.updateModTime();
|
|---|
| 114 | }
|
|---|
| 115 | // Hackity hack
|
|---|
| 116 | Task.prototype.complete.apply(this, arguments);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | exports.FileTask = FileTask;
|
|---|
| 122 |
|
|---|
| 123 | // DirectoryTask is a subclass of FileTask, depends on it
|
|---|
| 124 | // being defined
|
|---|
| 125 | let DirectoryTask = require('./directory_task').DirectoryTask;
|
|---|
| 126 |
|
|---|