|
2 | 2 | var fs = require('fs'); |
3 | 3 | var path = require('path'); |
4 | 4 | var zlib = require('zlib'); |
| 5 | +var exec = require('child_process').exec; |
5 | 6 |
|
6 | 7 | // Third-party modules. |
7 | 8 | var Q = require('q'); |
8 | 9 | var request = require('request'); |
9 | 10 | var tar = require('tar'); |
10 | 11 | var which = require('which'); |
| 12 | +var rimraf = require('rimraf'); |
11 | 13 |
|
12 | | -// Build options. |
13 | | -var options = { |
14 | | - // Normalize the libgit2 build directory. |
15 | | - libgit2build: path.join(__dirname, 'vendor/libgit2/build') |
16 | | -}; |
| 14 | +// This will take in an object and find any matching keys in the environment |
| 15 | +// to use as overrides. |
| 16 | +// |
| 17 | +// ENV variables: |
| 18 | +// |
| 19 | +// PKG: Location of `package.json` sans `.json`. |
| 20 | +// LIBGIT2: Location of libgit2 source. |
| 21 | +// BUILD: Location of nodegit build directory. |
| 22 | +function envOverride(obj) { |
| 23 | + // Look through all keys. |
| 24 | + return Object.keys(obj).reduce(function(obj, key) { |
| 25 | + var normalize = key.toUpperCase(); |
17 | 26 |
|
18 | | -function shpassthru() { |
19 | | - passthru.apply(null, ['/bin/sh', '-c'].concat(Array.prototype.slice.call(arguments))); |
| 27 | + // Check for process environment existence. |
| 28 | + if (normalize in process.env) { |
| 29 | + obj[key] = process.env[normalize]; |
| 30 | + } |
| 31 | + |
| 32 | + return obj; |
| 33 | + }, obj); |
20 | 34 | } |
21 | 35 |
|
22 | | -function envpassthru() { |
23 | | - passthru.apply(null, ['/usr/bin/env'].concat(Array.prototype.slice.call(arguments))); |
| 36 | +// Convert to the correct system path. |
| 37 | +function systemPath(parts) { |
| 38 | + return parts.join(path.sep); |
24 | 39 | } |
25 | 40 |
|
26 | | -var updateSubmodules = function(mainCallback) { |
27 | | - console.log('[nodegit] Downloading libgit2 dependency.'); |
28 | | - async.series([26 |
29 | | - function(callback) { |
30 | | - envpassthru('git', 'submodule', 'init', callback); |
31 | | - }, function(callback) { |
32 | | - envpassthru('git', 'submodule', 'update', callback); |
33 | | - } |
34 | | - ], function(error) { |
35 | | - if (error) process.exit(error); |
36 | | - mainCallback(); |
37 | | - }); |
38 | | -}; |
39 | | - |
40 | | -var checkoutDependencies = function(mainCallback) { |
41 | | - console.log('[nodegit] Downloading libgit2 dependency.'); |
42 | | - var commit = 'e953c1606d0d7aea680c9b19db0b955b34ae63c2'; |
43 | | - |
44 | | - var url = 'https://github.com/libgit2/libgit2/tarball/'+ commit; |
45 | | - var path = __dirname + '/vendor/libgit2/'; |
46 | | - request({ |
47 | | - url: url |
48 | | - }).pipe(zlib.createUnzip()).pipe(tar.Extract({ |
49 | | - path: path, |
50 | | - strip: true |
51 | | - })).on('end', function() { |
52 | | - mainCallback(); |
53 | | - }); |
54 | | -}; |
55 | | - |
56 | | -Q.ninvoke(which, "python2"). |
57 | | - |
58 | | -async.series([ |
59 | | - function checkPython2Exists(callback) { |
60 | | - exec('which python2', |
61 | | - function (error) { |
62 | | - if (!error) { |
63 | | - pythonExecutable = 'python2'; |
64 | | - callback(); |
65 | | - return; |
66 | | - } |
67 | | - // python2 is not available, check for python |
68 | | - exec('which python', function(error) { |
69 | | - if (error) { |
70 | | - throw new Error('Python is required to build libgit2'); |
71 | | - } |
72 | | - callback(); |
73 | | - }); |
74 | | - }); |
75 | | - |
76 | | - }, |
77 | | - function prepareLibgit2Repository(callback) { |
78 | | - // Check for presence of .git folder |
79 | | - fs.exists(__dirname + '/.git', function(exists) { |
80 | | - if (exists) { |
81 | | - updateSubmodules(callback); |
82 | | - } else { |
83 | | - checkoutDependencies(callback); |
84 | | - } |
85 | | - }); |
86 | | - }, |
87 | | - function deleteExistingLibgit2BuildFolder(callback) { |
88 | | - // fs.exists(libgit2BuildDirectory, function(exists) { |
89 | | - // if (exists) { |
90 | | - // fs.remove(libgit2BuildDirectory, callback); |
91 | | - // } else { |
92 | | - callback(); |
93 | | - // } |
94 | | - // }); |
95 | | - }, |
96 | | - function createLibgit2BuildDirectory(callback) { |
97 | | - console.log('[nodegit] Building libgit2 dependency.'); |
98 | | - fs.mkdirs(libgit2BuildDirectory, callback); |
99 | | - }, |
100 | | - function configureLibgit2(callback) { |
101 | | - envpassthru('cmake', '-DTHREADSAFE=1', '-DBUILD_CLAR=0', '..', { |
102 | | - cwd: libgit2BuildDirectory |
103 | | - }, callback); |
104 | | - }, |
105 | | - function buildLibgit2(callback) { |
106 | | - envpassthru('cmake', '--build', '.', { |
107 | | - cwd: libgit2BuildDirectory |
108 | | - }, callback); |
109 | | - }, |
110 | | - function configureNodegit(callback) { |
111 | | - console.log('[nodegit] Building native module.'); |
112 | | - // shpassthru('node-gyp configure --python python2 --debug', callback); |
113 | | - shpassthru('node-gyp configure --python ' + pythonExecutable, callback); |
114 | | - }, |
115 | | - function buildNodegit(callback) { |
116 | | - shpassthru('node-gyp build', callback); |
| 41 | +// Will be used near the end to configure `node-gyp`. |
| 42 | +var python, cmake; |
| 43 | + |
| 44 | +// Common reusable paths that can be overwritten by environment variables. |
| 45 | +var paths = envOverride({ |
| 46 | + pkg: __dirname + '/package', |
| 47 | + libgit2: __dirname + '/vendor/libgit2/', |
| 48 | + build: __dirname + '/vendor/libgit2/build/', |
| 49 | +}); |
| 50 | + |
| 51 | +// Load the package.json. |
| 52 | +var pkg = require(paths.pkg); |
| 53 | + |
| 54 | +// Ensure all dependencies are available. |
| 55 | +var dependencies = Q.allSettled([ |
| 56 | + // This will prioritize `python2` over `python`, because we always want to |
| 57 | + // work with Python 2.* if it's available. |
| 58 | + Q.nfcall(which, 'python2'), |
| 59 | + Q.nfcall(which, 'python'), |
| 60 | + |
| 61 | + // Check for any version of CMake. |
| 62 | + Q.nfcall(which, 'cmake'), |
| 63 | +]) |
| 64 | + |
| 65 | +// Determine if all the dependency requirements are met. |
| 66 | +.then(function(results) { |
| 67 | + console.info('[nodegit] Determining dependencies.'); |
| 68 | + |
| 69 | + // Assign to reusable variables. |
| 70 | + python = results[0].value || results[1].value; |
| 71 | + cmake = results[2].value; |
| 72 | + |
| 73 | + // Missing Python. |
| 74 | + if (!python) { |
| 75 | + throw new Error('Python is required to build libgit2.'); |
| 76 | + } |
| 77 | + |
| 78 | + // Missing CMake. |
| 79 | + if (!cmake) { |
| 80 | + throw new Error('CMake is required to build libgit2.'); |
| 81 | + } |
| 82 | + |
| 83 | + // Now lets check the Python version to ensure it's < 3. |
| 84 | + return Q.nfcall(exec, python + ' --version').then(function(version) { |
| 85 | + if (version[1].indexOf('Python 3') === 0) { |
| 86 | + throw new Error('Incorrect version of Python, gyp requires < 3.'); |
117 | 87 | } |
118 | | -], function handleError(error) { |
119 | | - if(error) process.exit(error); |
| 88 | + }); |
| 89 | +}) |
| 90 | + |
| 91 | +// Display a warning message about missing dependencies. |
| 92 | +.fail(function(message) { |
| 93 | + console.info('[nodegit] Failed to build nodegit.'); |
| 94 | + console.info(message); |
| 95 | + |
| 96 | + throw new Error(message); |
| 97 | +}) |
| 98 | + |
| 99 | +// Successfully found all dependencies. First step is to clean the vendor |
| 100 | +// directory. |
| 101 | +.then(function() { |
| 102 | + console.info('[nodegit] Removing vendor/libgit2.'); |
| 103 | + |
| 104 | + return Q.ninvoke(rimraf, null, paths.libgit2); |
| 105 | +}) |
| 106 | + |
| 107 | +// Now fetch the libgit2 source from GitHub. |
| 108 | +.then(function() { |
| 109 | + console.info('[nodegit] Fetching vendor/libgit2.'); |
| 110 | + |
| 111 | + var url = 'https://github.com/libgit2/libgit2/tarball/'+ pkg.libgit2; |
| 112 | + |
| 113 | + var extract = tar.Extract({ |
| 114 | + path: paths.libgit2, |
| 115 | + strip: true |
| 116 | + }); |
| 117 | + |
| 118 | + // First extract from Zlib and then extract from Tar. |
| 119 | + var expand = request.get(url).pipe(zlib.createUnzip()).pipe(extract); |
| 120 | + |
| 121 | + return Q.ninvoke(expand, 'on', 'end'); |
| 122 | +}) |
| 123 | + |
| 124 | +// Fetch completed successfully. |
| 125 | +.then(function() { |
| 126 | + console.info('[nodegit] Creating vendor/libgit2/build.'); |
| 127 | + |
| 128 | + return Q.ninvoke(fs, 'mkdir', paths.build); |
| 129 | +}) |
| 130 | + |
| 131 | +// Configure libgit2. |
| 132 | +.then(function() { |
| 133 | + console.info('[nodegit] Configuring libgit2.'); |
| 134 | + |
| 135 | + return Q.nfcall(exec, 'cmake -DTHREADSAFE=1 -DBUILD_CLAR=0 ..', { |
| 136 | + cwd: paths.build |
| 137 | + }); |
| 138 | +}).fail(function(err) { |
| 139 | + console.error(err); |
| 140 | +}) |
| 141 | + |
| 142 | +// Build libgit2. |
| 143 | +.then(function() { |
| 144 | + console.info('[nodegit] Building libgit2.'); |
| 145 | + |
| 146 | + return Q.nfcall(exec, 'cmake --build .', { |
| 147 | + cwd: paths.build |
| 148 | + }); |
| 149 | +}) |
| 150 | + |
| 151 | +.then(function() { |
| 152 | + console.info('[nodegit] Configuring native node module.'); |
| 153 | + |
| 154 | + return Q.nfcall(exec, systemPath([ |
| 155 | + '.', 'node_modules', '.bin', 'node-gyp configure --python ' + python |
| 156 | + ]), { |
| 157 | + cwd: '.' |
| 158 | + }); |
| 159 | +}) |
| 160 | + |
| 161 | +.then(function() { |
| 162 | + console.info('[nodegit] Building native node module.'); |
| 163 | + |
| 164 | + return Q.nfcall(exec, systemPath([ |
| 165 | + '.', 'node_modules', '.bin', 'node-gyp build ' + python |
| 166 | + ]), { |
| 167 | + cwd: '.' |
| 168 | + }); |
| 169 | +}) |
| 170 | + |
| 171 | +// Display a warning message about failing to build native node module. |
| 172 | +.fail(function(message) { |
| 173 | + console.info('[nodegit] Failed to build nodegit.'); |
| 174 | + console.info(message); |
| 175 | + |
| 176 | + throw new Error(message); |
| 177 | +}) |
| 178 | + |
| 179 | +.then(function() { |
| 180 | + console.info('[nodegit] Completed installation successfully.'); |
120 | 181 | }); |
0 commit comments