Skip to content

Commit

Permalink
Search for the main class relative to the URI of the compiled class
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Mar 4, 2015
1 parent 429da67 commit 1898739
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import groovyjarjarasm.asm.MethodVisitor
import groovyjarjarasm.asm.Opcodes
import groovyjarjarasm.asm.Type

import java.nio.file.Paths

/**
* @author Graeme Rocher
* @since 3.0
Expand All @@ -23,26 +25,30 @@ class MainClassFinder {

static String mainClassName = null

static String searchMainClass() {
/**
* Searches for the main class relative to the give path that is within the project tree
*
* @param path The path as a URI
* @return The name of the main class
*/
static String searchMainClass(URI path) {
if(mainClassName) return mainClassName

File file = path ? Paths.get(path).toFile() : null
def rootDir = findRootDirectory(file)

def classesDir = BuildSettings.CLASSES_DIR
Collection<File> searchDirs
if (classesDir == null) {
def tokens = System.getProperty('java.class.path').split(System.getProperty('path.separator'))
def dirs = tokens.findAll() { String str -> !str.endsWith('.jar') }.collect() { String str -> new File(str) }
searchDirs = dirs.findAll() { File f -> f.isDirectory() }
searchDirs = []
} else {
searchDirs = [classesDir]
}
def userDir = new File(System.getProperty('user.dir'))
if(new File(userDir, 'settings.gradle').exists()) {
userDir.eachDir { File dir ->
// looking for subproject build directories...
def mainClassesDir = new File(dir, 'build/classes/main')
if(mainClassesDir.exists()) {
searchDirs << mainClassesDir
}

if(rootDir) {
def rootClassesDir = new File(rootDir, "build/classes/main")
if(rootClassesDir.exists()) {
searchDirs << rootClassesDir
}
}

Expand All @@ -55,6 +61,22 @@ class MainClassFinder {
mainClass
}

private static File findRootDirectory(File file) {
if(file) {
def parent = file.parentFile

while(parent != null) {
if(new File(parent, "build.gradle").exists() || new File(parent, "grails-app").exists()) {
return parent
}
else {
parent = parent.parentFile
}
}
}
return null
}

static String findMainClass(File rootFolder = BuildSettings.CLASSES_DIR) {
if(mainClassName) return mainClassName

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class IntegrationTestMixinTransformation implements ASTTransformation {
GrailsASTUtils.error(source, applicationClassExpression, "Invalid applicationClass attribute value [${applicationClassNode.getName()}]. The applicationClass attribute must specify a class which extends grails.boot.config.GrailsAutoConfiguration.", true)
}
} else {
String mainClass = MainClassFinder.searchMainClass()
String mainClass = MainClassFinder.searchMainClass(source.source.URI)
if(mainClass) {
applicationClassNode = ClassHelper.make(mainClass)
}
Expand Down

0 comments on commit 1898739

Please sign in to comment.