From e860e186b830e4e72effd11192f46c9522ce14f3 Mon Sep 17 00:00:00 2001 From: theapache64 Date: Sun, 23 May 2021 18:29:45 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9E=96=20remove=20kinquire?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +- jdeploy-bundle/jdeploy.js | 163 ++++++++++++++++++ package.json | 25 +++ .../gpm/commands/base/BaseCommand.kt | 2 +- 4 files changed, 192 insertions(+), 2 deletions(-) create mode 100755 jdeploy-bundle/jdeploy.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore index 7ea9177..60356ce 100644 --- a/.gitignore +++ b/.gitignore @@ -138,4 +138,6 @@ gradle-app.setting SecretConstants.kt src/test/resources/temp.build.gradle x.html -src/test/resources/temp.gpm.json \ No newline at end of file +src/test/resources/temp.gpm.json +node_modules +package-lock.json \ No newline at end of file diff --git a/jdeploy-bundle/jdeploy.js b/jdeploy-bundle/jdeploy.js new file mode 100755 index 0000000..cefe5a2 --- /dev/null +++ b/jdeploy-bundle/jdeploy.js @@ -0,0 +1,163 @@ +#! /usr/bin/env node +var fail = reason => { + console.error(reason); + process.exit(1); +}; +function getJavaVersion() { + try { + var javaVersionProc = exec('java -version', {silent:true}); + if (javaVersionProc.code !== 0) { + return false; + } + var stdout = javaVersionProc.stderr; + //console.log(javaVersionProc); + //console.log("stdout is "+stdout); + var regexp = /version "(.*?)"/; + var match = regexp.exec(stdout); + var parts = match[1].split('.'); + var join = '.'; + var versionStr = ''; + parts.forEach(function(v) { + versionStr += v; + if (join !== null) { + versionStr += join; + join = null; + } + }); + versionStr = versionStr.replace('_', ''); + //console.log("Java version string "+versionStr) + return parseFloat(versionStr); + } catch (e) { + return false; + } +} + +var getDirectories = dirPath => fs.readdirSync(dirPath).filter( + file => fs.statSync(path.join(dirPath, file)).isDirectory() + ); + +function getEmbeddedJavaDir() { + var _platform = os.platform(); + var _driver; + switch (_platform) { + case 'darwin': _platform = 'macosx'; _driver = 'Contents' + path.sep + 'Home' + path.sep + 'bin'; break; + case 'win32': _platform = 'windows'; _driver = 'bin'; break; + case 'linux': _driver = 'bin'; break; + default: + fail('unsupported platform: ' + _platform); + } + + var jreDir = getJdeploySupportDir() + path.sep + 'node_modules' + path.sep + 'node-jre' + path.sep + 'jre'; + + try { + return jreDir + path.sep + getDirectories(jreDir)[0] + path.sep + _driver; + } catch (e) { + //console.log(e); + return jreDir; + } +} + + + +function getJdeploySupportDir() { + return os.homedir() + path.sep + ".jdeploy"; +} +var fs = require('fs'); +var os = require('os'); +var path = require('path'); +var jarName = "gpm.main.jar"; +var mainClass = "{{MAIN_CLASS}}"; +var classPath = "{{CLASSPATH}}"; +var port = "0"; +var warPath = ""; +classPath = classPath.split(':'); +var classPathStr = ''; +var first = true; +classPath.forEach(function(part) { + if (!first) classPathStr += path.delimiter; + first = false; + classPathStr += __dirname + '/' + part; +}); +classPath = classPathStr; +var shell = require("shelljs/global"); +var userArgs = process.argv.slice(2); +var javaArgs = []; +javaArgs.push('-Djdeploy.base='+__dirname); +javaArgs.push('-Djdeploy.port='+port); +javaArgs.push('-Djdeploy.war.path='+warPath); +var programArgs = []; +userArgs.forEach(function(arg) { + if (arg.startsWith('-D') || arg.startsWith('-X')) { + javaArgs.push(arg); + } else { + programArgs.push(arg); + } +}); +var cmd = 'java'; + +env['PATH'] = getEmbeddedJavaDir() + path.delimiter + env['PATH']; +if (env['JAVA_HOME']) { + env['PATH'] = env['JAVA_HOME'] + path.sep + 'bin' + path.delimiter + env['PATH']; +} + +var javaVersion = getJavaVersion(); +if (javaVersion === false || javaVersion < 1.8 || env['JDEPLOY_USE_NODE_JRE']) { + if (!test('-e', getJdeploySupportDir())) { + mkdir(getJdeploySupportDir()); + } + var packageJson = getJdeploySupportDir() + path.sep + 'package.json'; + if (!test('-e', packageJson)) { + fs.writeFileSync(packageJson, JSON.stringify({'name' : 'jdeploy-support', 'version': '1.0.0'}), 'utf8'); + } + if (!test('-e', getEmbeddedJavaDir())) { + var currDir = pwd(); + cd(getJdeploySupportDir()); + console.log("Installing/Updating JRE in "+getJdeploySupportDir()+"..."); + exec('npm install node-jre --save'); + cd(currDir); + } + env['PATH'] = getEmbeddedJavaDir() + path.delimiter + env['PATH']; + /* + // System java either not on path or too old + if (!test('-e', getEmbeddedJavaDir())) { + // Could not find embedded java dir + // We need to install it. + fail("Could not find embedded java at "+getEmbeddedJavaDir()); + + } else { + // Found the embedded version. Add it to the PATH + env['PATH'] = getEmbeddedJavaDir() + path.delimiter + env['PATH'] + console.log("Now java version is "+getJavaVersion()); + fail("Path is now "+env['PATH']); + } + */ + +} +//console.log("Java version is "+getJavaVersion()); + +javaArgs.forEach(function(arg) { + cmd += ' "'+arg+'"'; +}); +if (jarName !== '{'+'{JAR_NAME}}') { + cmd += ' -jar "'+__dirname+'/'+jarName+'" '; +} else { + cmd += ' -cp "'+classPath+'" '+mainClass+' '; +} + +programArgs.forEach(function(arg) { + cmd += ' "'+arg+'"'; +}); +var child = exec(cmd, {async: true}); +process.stdin.setEncoding('utf8'); + +process.stdin.on('readable', function() { + var chunk = null; + while (null !== (chunk = process.stdin.read())) { + try { + child.stdin.write(chunk); + } catch(e){} + } +}); +child.on('close', function(code) { + process.exit(code); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..c85b247 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "bin": { + "gpm": "jdeploy-bundle/jdeploy.js" + }, + "author": "theapache64", + "description": "GPM is a package manager for Gradle projects", + "main": "index.js", + "preferGlobal": true, + "repository": "https://github.com/theapache64/gpm", + "version": "1.0.2", + "jdeploy": { + "jar": "gpm.main.jar" + }, + "dependencies": { + "shelljs": "0.8.4" + }, + "license": "ISC", + "name": "gpm-cli", + "files": [ + "jdeploy-bundle" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + } +} diff --git a/src/main/kotlin/com/theapache64/gpm/commands/base/BaseCommand.kt b/src/main/kotlin/com/theapache64/gpm/commands/base/BaseCommand.kt index 77cf1a6..898de60 100644 --- a/src/main/kotlin/com/theapache64/gpm/commands/base/BaseCommand.kt +++ b/src/main/kotlin/com/theapache64/gpm/commands/base/BaseCommand.kt @@ -21,7 +21,7 @@ abstract class BaseCommand( delay(1000) 0 } else { - InputUtils.getInt("Choose one", 1, items.size) - 1 + InputUtils.getInt("Choose #", 1, items.size) - 1 } } } \ No newline at end of file