Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
SLATOM-7 check java (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Gyerik authored Jul 5, 2017
1 parent 01b4827 commit b456c62
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 13 deletions.
10 changes: 0 additions & 10 deletions lib/java-utils.js

This file was deleted.

91 changes: 91 additions & 0 deletions lib/requirements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* --------------------------------------------------------------------------------------------
* SonarLint for Atom
* Copyright (C) 2017 SonarSource SA
* [email protected]
* Licensed under the LGPLv3 License. See LICENSE.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';

const cp = require('child_process')
const path = require('path')
const fs = require('fs')
const pathExists = require('path-exists');
const expandHomeDir = require('expand-home-dir');
const findJavaHome = require('find-java-home');

const JAVA_BINARY = 'java' + ((process.platform.indexOf('win') === 0) ? '.exe' : '');

exports.java = JAVA_BINARY;

exports.resolve = () =>
checkJavaRuntime().then(javaHome =>
checkJavaVersion(javaHome).then(javaVersion =>
Promise.resolve({ javaHome: javaHome, javaVersion: javaVersion })))

function checkJavaRuntime() {
return new Promise((resolve, reject) => {
let source;
let javaHome = readJavaConfig();
if (javaHome) {
source = "The 'sonarlint.ls.javaHome' variable defined in settings";
} else {
javaHome = process.env['JDK_HOME'];
if (javaHome) {
source = 'The JDK_HOME environment variable';
} else {
javaHome = process.env['JAVA_HOME'];
source = 'The JAVA_HOME environment variable';
}
}
if (javaHome) {
javaHome = expandHomeDir(javaHome);
if (!pathExists.sync(javaHome)) {
openJREDownload(reject, source + ' points to a missing folder');
}
if (!pathExists.sync(path.resolve(javaHome, 'bin', JAVA_BINARY))){
openJREDownload(reject, source + ' does not point to a JRE.');
}
return resolve(javaHome);
}
//No settings, let's try to detect as last resort.
findJavaHome(function (err, home) {
if (err) {
openJREDownload(reject, "Java runtime could not be located. Install it and set its location using 'sonarlint.ls.javaHome' variable in settings.");
} else {
resolve(home);
}
});
});
}

function readJavaConfig() {
return atom.config.get('sonarlint.ls.javaHome');
}

function checkJavaVersion(javaHome) {
return new Promise((resolve, reject) => {
const prog = path.resolve(javaHome, 'bin', JAVA_BINARY);
if ((fs.statSync(prog).mode & 1) == 0) {
openJREDownload(reject, "Java binary not an executable file: " + prog);
return;
}

cp.execFile(prog, ['-version'], {}, (error, stdout, stderr) => {
if (stderr.indexOf('version "9') > -1) {
resolve(9);
} if (stderr.indexOf('1.8') < 0) {
openJREDownload(reject, 'Java 8 is required to run. Please download and install a JRE 8.');
} else {
resolve(8);
}
});
});
}

function openJREDownload(reject, message) {
const jreUrl = 'http://www.oracle.com/technetwork/java/javase/downloads/index.html';
reject({
message: message,
description: 'You can get the Java Runtime Environment from ' + jreUrl
});
}
15 changes: 13 additions & 2 deletions lib/sonarlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const path = require('path')
const {AutoLanguageClient, LinterPushV2Adapter} = require('atom-languageclient')

const files = require('./files');
const javaUtils = require('./java-utils');
const requirements = require('./requirements');

const ruleDetailsBaseUrl = 'http://www.sonarlint.org/atom/rules/index.html#version=1.0.0'

Expand All @@ -21,8 +21,19 @@ class SonarLintLanguageServer extends AutoLanguageClient {
getServerName () { return 'SonarLint' }

startServerProcess () {
return requirements.resolve()
.catch(error => {
atom.notifications.addError(error.message, {dismissable: true, description: error.description})
throw error
})
.then(() => {
return this._startServerProcess()
})
}

_startServerProcess () {
const basedir = path.resolve(__dirname, "..")
const command = javaUtils.java
const command = requirements.java
const args = ['-jar', files.languageServerJar(basedir)]
let process

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
"files"
],
"dependencies": {
"atom-languageclient": "SonarSource/atom-languageclient#f1a0372e163191213edeefa13c8d3f8e7cf7a367",
"atom-linter": "^10.0.0",
"atom-languageclient": "SonarSource/atom-languageclient#f1a0372e163191213edeefa13c8d3f8e7cf7a367"
"expand-home-dir": "0.0.3",
"find-java-home": "^0.1.4"
},
"bundledDependencies": [
"atom-languageclient"
Expand Down

0 comments on commit b456c62

Please sign in to comment.