Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #30 from daw42/classpath_cson
Browse files Browse the repository at this point in the history
Support classpath configuration via .classpath files.
  • Loading branch information
keplersj committed Aug 11, 2015
2 parents aaa9198 + 84cf882 commit 6339b5f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,28 @@ This package will lint your `.java` opened files in Atom through [javac](http://
You can configure linter-javac by editing ~/.atom/config.cson (choose Open Your Config in Atom menu):

'linter-javac':
'javaExecutablePath': null # java path. run 'which javac' to find the path
# The path to javac. The default (javac) should work as long as you have it
# in your system PATH.
'javaExecutablePath': "javac"
# Extra classpath. This will be appended to the classpath when executing javac.
'classpath': ''

## Classpath

It is strongly recommended that you configure your classpath via a `.classpath`
file within your project (typically at the root). Simply create a file
named `.classpath` somewhere within your project (ideally at the root of
the project). The linter will search
for this file by starting at the directory of the file being compiled and then
searching all parent directories within the project. If you have more than one
of these configuration files, it will use the one that is "closest" to the file
being compiled. Within `.classpath` place only the classpath to be used for the
project (nothing else). For example:

.:./lib/junit.jar

This linter will execute `javac` within the directory of the `.classpath`
file, so relative paths can be considered to be relative to that file.

## Other available linters
There are other linters available - take a look at the linters [mainpage](https://github.com/AtomLinter/Linter).

## Donation
[![Share the love!](https://chewbacco-stuff.s3.amazonaws.com/donate.png)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=KXUYS4ARNHCN8)
47 changes: 41 additions & 6 deletions lib/init.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{BufferedProcess, CompositeDisposable} = require 'atom'
path = require 'path'
helpers = require 'atom-linter'
fs = require 'fs'
cpConfigFileName = '.classpath'

module.exports =
config:
Expand Down Expand Up @@ -32,14 +34,31 @@ module.exports =
lint: (textEditor) =>
filePath = textEditor.getPath()
wd = path.dirname filePath
# Use the text editor's working directory as the classpath, and add user's
# classpath (if it exists) and/or environment variable
cp = wd

# Classpath
cp = null

# Find project config file if it exists.
cpConfig = @findClasspathConfig(wd)
if cpConfig?
# Use the location of the config file as the working directory
wd = cpConfig.cfgDir
# Use configured classpath
cp = cpConfig.cfgCp

# Add extra classpath if provided
cp += path.delimiter + @classpath if @classpath

# Add environment variable if it exists
cp += path.delimiter + process.env.CLASSPATH if process.env.CLASSPATH
args = ['-Xlint:all', '-cp', cp, filePath]

helpers.exec(@javaExecutablePath, args, {stream: 'stderr'})

# Arguments to javac
args = ['-Xlint:all']
args = args.concat(['-cp', cp]) if cp?
args.push filePath

# Execute javac
helpers.exec(@javaExecutablePath, args, {stream: 'stderr', cwd: wd})
.then (val) => return @parse(val, textEditor)

parse: (javacOutput, textEditor) ->
Expand All @@ -65,3 +84,19 @@ module.exports =
messages[messages.length - 1].range[0][1] = column
messages[messages.length - 1].range[1][1] = column + 1
return messages

findClasspathConfig: (d) ->
# Search for the .classpath file starting in the given directory
# and searching parent directories until it is found, or we go outside the
# project base directory.
while atom.project.contains(d) or (d in atom.project.getPaths())
try
result =
cfgCp: fs.readFileSync( path.join(d, cpConfigFileName), { encoding: 'utf-8' } )
cfgDir: d
result.cfgCp = result.cfgCp.trim()
return result
catch e
d = path.dirname(d)

return null
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"atom": ">0.50.0"
},
"dependencies": {
"atom-linter": ">=3.0.0"
"atom-linter": "^3.1.1"
},
"providedServices": {
"linter": {
Expand Down

0 comments on commit 6339b5f

Please sign in to comment.