-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildpack.js
154 lines (126 loc) · 4.14 KB
/
buildpack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var path = require('path'),
fs = require('fs'),
spawn = require('child_process').spawn,
yaml = require('js-yaml'),
fetch = require('./fetch'),
store = require('./store');
module.exports = function () {};
module.exports.pending = 0;
module.exports.maintainStore = function () {
module.exports.storePath = path.join(store.root, 'buildpacks');
var stock = require('./conf/buildpacks.json');
module.exports.stock = [];
stock.forEach(function (entry) {
var pack = module.exports.fromStock(entry);
module.exports.stock.push(pack);
// TODO: be more intelligent, have a last checked time, update at
// most once a day or so
// TODO: replace with fs.exists() if/when the prod node.js is upgraded
fs.stat(pack.path, function (err, stats) {
if (!err) return;
if (stats) return; // TODO: handle upgrades sometimes
console.log(stats);
module.exports.pending++;
pack.ensureLatest(function (success) {
console.log('ensureLatest completed on', pack.path, '- success:', success);
module.exports.pending--;
});
});
});
module.exports.timer = setInterval(function () {
if (module.exports.pending) return;
clearInterval(module.exports.timer);
delete module.exports.timer;
console.log('-----> Buildpack store maintanence complete');
if (module.exports.readyCallback)
module.exports.readyCallback();
}, 500);
};
module.exports.whenReady = function (callback) {
if (module.exports.timer)
module.exports.readyCallback = callback;
else
callback();
};
module.exports.fromStock = function (info) {
var pack = new module.exports();
pack.name = info.name;
pack.sourceInfo = fetch.detect(info.uri);
pack.path = path.join(module.exports.storePath, pack.sourceInfo.basename);
return pack;
};
module.exports.fromStore = function (name) {
var pack = new module.exports();
pack.name = name;
pack.path = path.join(module.exports.storePath, name);
return pack;
};
module.exports.detect = function (buildPath, callback, stack) {
if (!stack) stack = module.exports.stock;
if (!stack || !stack.length) return callback(null);
var self = this;
stack[0].detect(buildPath, function (name) {
if (name) {
callback(stack[0], name);
} else {
self.detect(buildPath, callback, stack.splice(1));
};
});
};
module.exports.prototype = {
onDisk: function (callback) {
fs.stat(this.path, callback);
},
ensureLatest: function (callback) {
var self = this;
this.onDisk(function (err, stat) {
if (stat) {
if (self.sourceInfo.method != 'git') return;
// TODO: handle commit freezes
spawn('git', ['pull'], {cwd: self.path}).on('exit', function () {
callback(true);
});
} else {
fetch.fetchInto(self.sourceInfo, self.path, function (success) {
callback(success);
});
};
});
},
// TODO: sandbox it a little
runBin: function (command, args, cwd, exitCall, dataCall) {
var opts = {cwd: (cwd || this.path), env: {PATH: process.env.PATH}};
var proc = spawn(path.join(this.path, 'bin', command), args || [], opts);
proc.on('exit', exitCall);
proc.stdout.setEncoding('utf8');
proc.stdout.on('data', dataCall);
},
detect: function (buildPath, callback) {
var buffer = '';
this.runBin('detect', [buildPath], null, function (code) {
callback(!code && buffer.slice(0, buffer.length - 1));
}, function (data) {
buffer += data;
});
},
compile: function (buildPath, cachePath, exitCall, lineCall) {
var buffer = '';
this.runBin('compile', [buildPath, cachePath], buildPath, function (code) {
exitCall(!code);
}, function (data) {
buffer += data;
while (buffer.indexOf("\n") >= 0) {
lineCall(buffer.substr(0, buffer.indexOf("\n")));
buffer = buffer.substr(buffer.indexOf("\n") + 1);
};
});
},
release: function (buildPath, callback) {
var buffer = '';
this.runBin('release', [buildPath], buildPath, function (code) {
callback(yaml.load(buffer));
}, function (data) {
buffer += data;
});
},
};