-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.js
172 lines (141 loc) · 4.86 KB
/
install.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var os = require('os');
var fs = require('fs');
var path = require('path');
var zlib = require('zlib');
var exec = require('child_process').exec;
var Q = require('q');
var request = require('request');
var tar = require('tar');
var which = require('which');
var rimraf = require('rimraf');
// This will take in an object and find any matching keys in the environment
// to use as overrides.
//
// ENV variables:
//
// PKG: Location of `package.json` sans `.json`.
// LIBGIT2: Location of libgit2 source.
// BUILD: Location of nodegit build directory.
function envOverride(obj) {
// Look through all keys.
return Object.keys(obj).reduce(function(obj, key) {
var normalize = key.toUpperCase();
// Check for process environment existence.
if (normalize in process.env) {
obj[key] = process.env[normalize];
}
return obj;
}, obj);
}
// Convert to the correct system path.
function systemPath(parts) {
return parts.join(path.sep);
}
// Will be used near the end to configure `node-gyp`.
var python;
var local = path.join.bind(path, __dirname);
// Common reusable paths that can be overwritten by environment variables.
var paths = envOverride({
pkg: local('package'),
libgit2: local('vendor/libgit2/'),
sys: {
include: local('include/sys/'),
src: local('src/sys/'),
build: local('build/Release/obj.target/src/sys/')
},
release: local('build/Release/')
});
// Load the package.json.
var pkg = require(paths.pkg);
// Ensure all dependencies are available.
var dependencies = Q.allSettled([
// This will prioritize `python2` over `python`, because we always want to
// work with Python 2.* if it's available.
Q.nfcall(which, 'python2'),
Q.nfcall(which, 'python')
])
// Determine if all the dependency requirements are met.
.then(function(results) {
console.info('[nodegit] Determining dependencies.');
// Assign to reusable variables.
python = results[0].value || results[1].value;
// Missing Python.
if (!python) {
throw new Error('Python is required to build libgit2.');
}
// Now lets check the Python version to ensure it's < 3.
return Q.nfcall(exec, python + ' --version').then(function(version) {
if (version[1].indexOf('Python 3') === 0) {
throw new Error('Incorrect version of Python, gyp requires < 3.');
}
});
})
// Successfully found all dependencies. First step is to detect the vendor
// directory.
.then(function() {
console.info('[nodegit] Detecting vendor/libgit2.');
return Q.ninvoke(fs, 'stat', paths.libgit2).then(function() {
return Q.ninvoke(fs, 'stat', paths.libgit2 + pkg.libgit2.sha);
}).fail(function() {
console.info('[nodegit] Removing outdated vendor/libgit2.');
// This directory is outdated, remove.
throw Q.ninvoke(rimraf, null, paths.libgit2);
});
})
// If the directory already exists, no need to refetch.
.fail(function() {
// Otherwise fetch the libgit2 source from GitHub.
console.info('[nodegit] Fetching vendor/libgit2.');
var url = 'https://github.com/libgit2/libgit2/tarball/' + pkg.libgit2.sha;
var extract = tar.Extract({
path: paths.libgit2,
strip: true
});
// First extract from Zlib and then extract from Tar.
var expand = request.get(url).pipe(zlib.createUnzip()).pipe(extract);
return Q.ninvoke(expand, 'on', 'end').then(function() {
// Write out a sha file for testing in the future.
return Q.ninvoke(fs, 'writeFile', paths.libgit2 + pkg.libgit2.sha, '');
});
})
// Build the native module using node-gyp.
.then(function() {
console.info('[nodegit] Building native node module.');
var pythonFlag = ' --python ' + python;
return Q.nfcall(exec, systemPath([
'.', 'node_modules', '.bin', 'node-gyp clean configure build' + pythonFlag
]), {
cwd: '.',
maxBuffer: Number.MAX_VALUE
});
})
// Attempt to fallback on a prebuilt binary.
.fail(function(message) {
console.info('[nodegit] Failed to build nodegit.');
console.info('[nodegit] Attempting to fallback on a prebuilt binary.');
console.log(message.stack);
function fetchPrebuilt() {
console.info('[nodegit] Fetching binary from S3.');
// Using the node-pre-gyp module, attempt to fetch a compatible build.
return Q.nfcall(exec, 'node-pre-gyp install');
}
// Attempt to fetch prebuilt binary.
return Q.ninvoke(fs, 'mkdir', paths.release)
.then(fetchPrebuilt, fetchPrebuilt);
})
// Display a warning message about failing to build native node module.
.fail(function(message) {
console.info('[nodegit] Failed to build and install nodegit.');
console.info(message.message);
console.info(message.stack);
})
// Display a success message.
.then(function() {
console.info('[nodegit] Completed installation successfully.');
})
// Display a warning message about failing to build native node module.
.fail(function(message) {
console.info('[nodegit] Failed to build nodegit.');
console.info(message.message);
console.info(message.stack);
});