diff --git a/.gitignore b/.gitignore index 1e0b2068..7cfebd8a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ node-java.cbp *.iml *.kdev4 */.kdev_include_paths +lib/jvm_dll_path.json diff --git a/binding.gyp b/binding.gyp index 95706a5a..94dbc9a4 100644 --- a/binding.gyp +++ b/binding.gyp @@ -162,6 +162,17 @@ }, ], ] + }, + { + "target_name": "action_after_build", + "type": "none", + "dependencies": [ "<(module_name)" ], + "copies": [ + { + "files": [ "<(PRODUCT_DIR)/<(module_name).node" ], + "destination": "<(module_path)" + } + ] } ] } diff --git a/install.js b/install.js new file mode 100644 index 00000000..cfa91f46 --- /dev/null +++ b/install.js @@ -0,0 +1,89 @@ +var pregyp = require('node-pre-gyp'); +var glob = require('glob'); +var fs = require('fs'); +var path = require('path'); +var os = require('os'); + +require('find-java-home')(function(err, home){ + var dll; + var dylib; + var so,soFiles; + var binary; + + if (!home) { + console.log('Java home not found. Can\'t continue'); + } + + if(home){ + console.log('Java home found. Searching for applicable DLLs'); + + dll = glob.sync('**/jvm.dll', {cwd: home})[0]; + dylib = glob.sync('**/libjvm.dylib', {cwd: home})[0]; + soFiles = glob.sync('**/libjvm.so', {cwd: home}); + + if(soFiles.length>0) + so = getCorrectSoForPlatform(soFiles); + + binary = dll || dylib || so; + + var jvmDllPath = path.resolve(__dirname, './lib/jvm_dll_path.json'); + var jvmDllContent = path.delimiter + path.dirname(path.resolve(home, binary)); + + console.log('Creating ' + jvmDllPath); + + fs.writeFileSync( + jvmDllPath, + binary ? JSON.stringify(jvmDllContent) : '""' + ); + + if (binary) { + console.log('Setting environment path for JVM DLL'); + process.env.PATH += jvmDllContent; + } + + var pregypRun = new pregyp.Run(); + pregypRun.parseArgv([ '--fallback-to-build' ]); + + pregypRun.commands.install(pregypRun, function() { + console.log('Done installing with node-pre-gyp'); + }); + } +}); + +function getCorrectSoForPlatform(soFiles){ + var so = _getCorrectSoForPlatform(soFiles); + if (so) { + so = removeDuplicateJre(so); + } + return so; +} + +function removeDuplicateJre(filePath){ + while(filePath.indexOf('jre/jre')>=0){ + filePath = filePath.replace('jre/jre','jre'); + } + return filePath; +} + +function _getCorrectSoForPlatform(soFiles){ + + var architectureFolderNames = { + 'ia32': 'i386', + 'x64': 'amd64' + }; + + if(os.platform() != 'sunos') + return soFiles[0]; + + var requiredFolderName = architectureFolderNames[os.arch()]; + + for (var i = 0; i < soFiles.length; i++) { + var so = soFiles[i]; + + if(so.indexOf('server')>0) + if(so.indexOf(requiredFolderName)>0) + return so; + } + + return soFiles[0]; +} \ No newline at end of file diff --git a/lib/nodeJavaBridge.js b/lib/nodeJavaBridge.js index bb67c71d..0a2f3ad5 100644 --- a/lib/nodeJavaBridge.js +++ b/lib/nodeJavaBridge.js @@ -1,12 +1,13 @@ 'use strict'; -process.env.PATH += require('../build/jvm_dll_path.json'); - var _ = require('lodash'); var async = require('async'); var path = require('path'); var fs = require('fs'); -var binaryPath = path.resolve(path.join(__dirname, "../build/Release/nodejavabridge_bindings.node")); + +process.env.PATH += require('..' + path.sep + 'lib' + path.sep + 'jvm_dll_path.json'); + +var binaryPath = path.resolve(path.join(__dirname, "../lib/binding/nodejavabridge_bindings.node")); var bindings = require(binaryPath); var java = module.exports = new bindings.Java(); diff --git a/package.json b/package.json index 5d6703aa..ae6e0248 100644 --- a/package.json +++ b/package.json @@ -30,16 +30,24 @@ "find-java-home": "0.1.2", "glob": "5.0.5", "lodash": "3.7.0", - "nan": "2.0.9" + "nan": "2.0.9", + "node-pre-gyp": "0.5.x" }, "devDependencies": { "chalk": "1.0.0", "nodeunit": "0.9.1", - "when": "3.7.2" + "when": "3.7.2", + "aws-sdk": "~2.0.0-rc.15" }, + "bundledDependencies":["node-pre-gyp"], "scripts": { - "test": "node testRunner.js", - "postinstall": "node postInstall.js" + "install": "node install.js", + "test": "node testRunner.js" }, - "main": "./index.js" + "main": "./index.js", + "binary": { + "module_name": "nodejavabridge_bindings", + "module_path": "./lib/binding/", + "host": "https://node-java.s3.amazonaws.com/" + } } diff --git a/testRunner.js b/testRunner.js index a1e63869..a4193375 100644 --- a/testRunner.js +++ b/testRunner.js @@ -14,7 +14,7 @@ var tests = glob.sync(path.join('testAsyncOptions', '*.js')); tests.unshift('test test8'); // Arrange to run the primary tests first, in a single process function runTest(testArgs, done) { - var cmd = 'node_modules/.bin/nodeunit ' + testArgs; + var cmd = 'node_modules' + path.sep + '.bin' + path.sep + 'nodeunit ' + testArgs; childProcess.exec(cmd, function (error, stdout, stderr) { // It appears that nodeunit merges error output into the stdout // so these three lines are probably useless.