Kotlin and general Gradle support. (#745)

This commit is contained in:
Michael Pardo 2017-07-10 18:03:36 -04:00 committed by w0rp
parent 29d0a20dc3
commit d787050fa8
19 changed files with 202 additions and 4 deletions

View File

@ -12,17 +12,21 @@ let g:ale_kotlin_kotlinc_module_filename = get(g:, 'ale_kotlin_kotlinc_module_fi
let s:classpath_sep = has('unix') ? ':' : ';' let s:classpath_sep = has('unix') ? ':' : ';'
function! ale_linters#kotlin#kotlinc#GetImportPaths(buffer) abort function! ale_linters#kotlin#kotlinc#GetImportPaths(buffer) abort
" exec maven only if classpath is not set " exec maven/gradle only if classpath is not set
if ale#Var(a:buffer, 'kotlin_kotlinc_classpath') !=# '' if ale#Var(a:buffer, 'kotlin_kotlinc_classpath') !=# ''
return '' return ''
else else
let l:pom_path = ale#path#FindNearestFile(a:buffer, 'pom.xml') let l:pom_path = ale#path#FindNearestFile(a:buffer, 'pom.xml')
if !empty(l:pom_path) && executable('mvn') if !empty(l:pom_path) && executable('mvn')
return ale#path#CdString(fnamemodify(l:pom_path, ':h')) return ale#path#CdString(fnamemodify(l:pom_path, ':h'))
\ . 'mvn dependency:build-classpath' \ . 'mvn dependency:build-classpath'
endif endif
let l:classpath_command = ale#gradle#BuildClasspathCommand(a:buffer)
if !empty(l:classpath_command)
return l:classpath_command
endif
return '' return ''
endif endif
endfunction endfunction
@ -69,7 +73,7 @@ function! ale_linters#kotlin#kotlinc#GetCommand(buffer, import_paths) abort
if ale#Var(a:buffer, 'kotlin_kotlinc_classpath') !=# '' if ale#Var(a:buffer, 'kotlin_kotlinc_classpath') !=# ''
let l:kotlinc_opts .= ' -cp ' . ale#Var(a:buffer, 'kotlin_kotlinc_classpath') let l:kotlinc_opts .= ' -cp ' . ale#Var(a:buffer, 'kotlin_kotlinc_classpath')
else else
" get classpath from maven " get classpath from maven/gradle
let l:kotlinc_opts .= s:BuildClassPathOption(a:buffer, a:import_paths) let l:kotlinc_opts .= s:BuildClassPathOption(a:buffer, a:import_paths)
endif endif
@ -78,7 +82,15 @@ function! ale_linters#kotlin#kotlinc#GetCommand(buffer, import_paths) abort
let l:fname .= expand(ale#Var(a:buffer, 'kotlin_kotlinc_sourcepath'), 1) . ' ' let l:fname .= expand(ale#Var(a:buffer, 'kotlin_kotlinc_sourcepath'), 1) . ' '
else else
" Find the src directory for files in this project. " Find the src directory for files in this project.
let l:src_dir = ale#path#FindNearestDirectory(a:buffer, 'src/main/java')
let l:project_root = ale#gradle#FindProjectRoot(a:buffer)
if !empty(l:project_root)
let l:src_dir = l:project_root
else
let l:src_dir = ale#path#FindNearestDirectory(a:buffer, 'src/main/java')
\ . ' ' . ale#path#FindNearestDirectory(a:buffer, 'src/main/kotlin')
endif
let l:fname .= expand(l:src_dir, 1) . ' ' let l:fname .= expand(l:src_dir, 1) . ' '
endif endif
let l:fname .= ale#Escape(expand('#' . a:buffer . ':p')) let l:fname .= ale#Escape(expand('#' . a:buffer . ':p'))
@ -155,3 +167,4 @@ call ale#linter#Define('kotlin', {
\ 'callback': 'ale_linters#kotlin#kotlinc#Handle', \ 'callback': 'ale_linters#kotlin#kotlinc#Handle',
\ 'lint_file': 1, \ 'lint_file': 1,
\}) \})

54
autoload/ale/gradle.vim Normal file
View File

@ -0,0 +1,54 @@
" Author: Michael Pardo <michael@michaelpardo.com>
" Description: Functions for working with Gradle projects.
let s:script_path = fnamemodify(resolve(expand('<sfile>:p')), ':h')
" Given a buffer number, find a Gradle project root.
function! ale#gradle#FindProjectRoot(buffer) abort
let l:gradlew_path = ale#path#FindNearestFile(a:buffer, 'gradlew')
if !empty(l:gradlew_path)
return fnamemodify(l:gradlew_path, ':h')
endif
let l:settings_path = ale#path#FindNearestFile(a:buffer, 'settings.gradle')
if !empty(l:settings_path)
return fnamemodify(l:settings_path, ':h')
endif
let l:build_path = ale#path#FindNearestFile(a:buffer, 'build.gradle')
if !empty(l:build_path)
return fnamemodify(l:build_path, ':h')
endif
return ''
endfunction
" Given a buffer number, find the path to the executable.
" First search on the path for 'gradlew', if nothing is found, try the global
" command. Returns an empty string if cannot find the executable.
function! ale#gradle#FindExecutable(buffer) abort
let l:gradlew_path = ale#path#FindNearestFile(a:buffer, 'gradlew')
if !empty(l:gradlew_path)
return l:gradlew_path
endif
if executable('gradle')
return 'gradle'
endif
return ''
endfunction
" Given a buffer number, build a command to print the classpath of the root
" project. Returns an empty string if cannot build the command.
function! ale#gradle#BuildClasspathCommand(buffer) abort
let l:executable = ale#gradle#FindExecutable(a:buffer)
let l:project_root = ale#gradle#FindProjectRoot(a:buffer)
if !empty(l:executable) && !empty(l:project_root)
return ale#path#CdString(l:project_root)
\ . l:executable . ' -I ' . s:script_path . '/gradle/init.gradle -q printClasspath'
endif
return ''
endfunction

View File

@ -0,0 +1,23 @@
class ClasspathPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('printClasspath') {
doLast {
project
.rootProject
.allprojects
.configurations
.flatten()
.findAll { it.name.endsWith('Classpath') }
.collect { it.resolve() }
.flatten()
.unique()
.findAll { it.exists() }
.each { println it }
}
}
}
}
rootProject {
apply plugin: ClasspathPlugin
}

0
test/gradle-test-files/gradle Executable file
View File

View File

View File

@ -0,0 +1,42 @@
Before:
call ale#test#SetDirectory('/testplugin/test')
runtime ale_linters/kotlin/kotlinc.vim
let g:ale_gradle_path = $PATH
After:
call ale#test#RestoreDirectory()
call ale#linter#Reset()
let $PATH = g:ale_gradle_path
Execute(Should return 'gradlew' command if project includes gradle wapper):
call ale#test#SetFilename('gradle-test-files/wrapped-project/src/main/kotlin/dummy.kt')
let g:project_root = '/testplugin/test/gradle-test-files/wrapped-project'
let g:gradle_executable = '/testplugin/test/gradle-test-files/wrapped-project/gradlew'
let g:gradle_init_path = '/testplugin/autoload/ale/gradle/init.gradle'
let g:gradle_options = '-I ' . g:gradle_init_path . ' -q printClasspath'
AssertEqual
\ "cd '" . g:project_root . "' && " . g:gradle_executable . " " . g:gradle_options,
\ ale#gradle#BuildClasspathCommand(bufnr(''))
Execute(Should return 'gradle' command if project does not include gradle wapper):
call ale#test#SetFilename('gradle-test-files/unwrapped-project/src/main/kotlin/dummy.kt')
let $PATH .= ':' . g:dir . '/gradle-test-files'
let g:project_root = '/testplugin/test/gradle-test-files/unwrapped-project'
let g:gradle_executable = 'gradle'
let g:gradle_init_path = '/testplugin/autoload/ale/gradle/init.gradle'
let g:gradle_options = '-I ' . g:gradle_init_path . ' -q printClasspath'
AssertEqual
\ "cd '" . g:project_root . "' && " . g:gradle_executable . " " . g:gradle_options,
\ ale#gradle#BuildClasspathCommand(bufnr(''))
Execute(Should return empty string if gradle cannot be executed):
call ale#test#SetFilename('gradle-test-files/non-gradle-project/src/main/kotlin/dummy.kt')
AssertEqual
\ '',
\ ale#gradle#BuildClasspathCommand(bufnr(''))

View File

@ -0,0 +1,31 @@
Before:
call ale#test#SetDirectory('/testplugin/test')
runtime ale_linters/kotlin/kotlinc.vim
let g:ale_gradle_path = $PATH
After:
call ale#test#RestoreDirectory()
call ale#linter#Reset()
let $PATH = g:ale_gradle_path
Execute(Should return 'gradlew' if found in parent directory):
call ale#test#SetFilename('gradle-test-files/wrapped-project/src/main/kotlin/dummy.kt')
AssertEqual
\ g:dir . '/gradle-test-files/wrapped-project/gradlew',
\ ale#gradle#FindExecutable(bufnr(''))
Execute(Should return 'gradle' if 'gradlew' not found in parent directory):
call ale#test#SetFilename('gradle-test-files/unwrapped-project/src/main/kotlin/dummy.kt')
let $PATH .= ':' . g:dir . '/gradle-test-files'
AssertEqual
\ 'gradle',
\ ale#gradle#FindExecutable(bufnr(''))
Execute(Should return empty string if 'gradlew' not in parent directory and gradle not in path):
call ale#test#SetFilename('gradle-test-files/unwrapped-project/src/main/kotlin/dummy.kt')
AssertEqual
\ '',
\ ale#gradle#FindExecutable(bufnr(''))

View File

@ -0,0 +1,35 @@
Before:
call ale#test#SetDirectory('/testplugin/test')
runtime ale_linters/kotlin/kotlinc.vim
After:
call ale#test#RestoreDirectory()
call ale#linter#Reset()
Execute(Should return directory for 'gradlew' if found in parent directory):
call ale#test#SetFilename('gradle-test-files/wrapped-project/src/main/kotlin/dummy.kt')
AssertEqual
\ g:dir . '/gradle-test-files/wrapped-project',
\ ale#gradle#FindProjectRoot(bufnr(''))
Execute(Should return directory for 'settings.gradle' if found in parent directory):
call ale#test#SetFilename('gradle-test-files/settings-gradle-project/src/main/kotlin/dummy.kt')
AssertEqual
\ g:dir . '/gradle-test-files/settings-gradle-project',
\ ale#gradle#FindProjectRoot(bufnr(''))
Execute(Should return directory for 'build.gradle' if found in parent directory):
call ale#test#SetFilename('gradle-test-files/build-gradle-project/src/main/kotlin/dummy.kt')
AssertEqual
\ g:dir . '/gradle-test-files/build-gradle-project',
\ ale#gradle#FindProjectRoot(bufnr(''))
Execute(Should return empty string if gradle files are not found in parent directory):
call ale#test#SetFilename('gradle-test-files/non-gradle-project/src/main/kotlin/dummy.kt')
AssertEqual
\ '',
\ ale#gradle#FindProjectRoot(bufnr(''))