|
| 1 | +// Core Node.js modules. |
| 2 | +var fs = require('fs'); |
| 3 | +var path = require('path'); |
| 4 | +var zlib = require('zlib'); |
| 5 | +var exec = require('child_process').exec; |
| 6 | + |
| 7 | +// Third-party modules. |
| 8 | +var Q = require('q'); |
| 9 | +var request = require('request'); |
| 10 | +var tar = require('tar'); |
| 11 | +var which = require('which'); |
| 12 | +var rimraf = require('rimraf'); |
| 13 | + |
| 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(); |
| 26 | + |
| 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); |
| 34 | +} |
| 35 | + |
| 36 | +// Convert to the correct system path. |
| 37 | +function systemPath(parts) { |
| 38 | + return parts.join(path.sep); |
| 39 | +} |
| 40 | + |
| 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.'); |
| 87 | + } |
| 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.'); |
| 181 | +}); |
0 commit comments