Skip to content

Commit

Permalink
Profiles - fix list-profiles command
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Oct 5, 2015
1 parent 77dd98f commit 3eb08a9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 16 deletions.
Expand Up @@ -15,21 +15,10 @@
*/
package org.grails.cli.profile

import grails.util.CosineSimilarity
import grails.util.Environment
import groovy.transform.CompileStatic
import jline.console.completer.ArgumentCompleter
import jline.console.completer.Completer
import org.grails.build.parsing.CommandLine
import org.grails.build.parsing.ScriptNameResolver
import org.grails.cli.interactive.completers.StringsCompleter
import org.grails.cli.profile.commands.CommandRegistry
import org.grails.config.NavigableMap
import org.grails.io.support.PathMatchingResourcePatternResolver
import org.grails.io.support.Resource
import org.grails.io.support.StaticResourceLoader
import org.yaml.snakeyaml.Yaml;

import org.yaml.snakeyaml.Yaml
/**
* Simple disk based implementation of the {@link Profile} interface
*
Expand Down
Expand Up @@ -42,6 +42,7 @@ abstract class AbstractJarProfileRepository implements ProfileRepository {
protected final List<Profile> allProfiles = []
protected final Map<String, Profile> profilesByName = [:]

private Set<URL> registeredUrls = []
@Override
Profile getProfile(String profileName) {
return profilesByName[profileName]
Expand All @@ -65,9 +66,12 @@ abstract class AbstractJarProfileRepository implements ProfileRepository {
}

protected void registerProfile(URL url, ClassLoader parent) {
if(registeredUrls.contains(url)) return

def classLoader = new URLClassLoader([url] as URL[], parent)
def profileYml = classLoader.getResource("META-INF/grails-profile/profile.yml")
if (profileYml != null) {
registeredUrls.add(url)
def profile = new JarProfile(new ClassPathResource("META-INF/grails-profile/", classLoader), classLoader)
profile.profileRepository = this
allProfiles.add profile
Expand Down
Expand Up @@ -38,6 +38,7 @@ class MavenProfileRepository extends AbstractJarProfileRepository {
List<RepositoryConfiguration> repositoryConfigurations
AetherGrapeEngine grapeEngine
GroovyClassLoader classLoader
private boolean resolved = false

MavenProfileRepository(List<RepositoryConfiguration> repositoryConfigurations) {
this.repositoryConfigurations = repositoryConfigurations
Expand All @@ -53,19 +54,23 @@ class MavenProfileRepository extends AbstractJarProfileRepository {

@Override
Profile getProfile(String profileName) {
if(!profilesByName.containsKey(profileName)) {
if(!resolved && !profilesByName.containsKey(profileName)) {
resolveProfile(profileName)
}
return super.getProfile(profileName)
}

protected void resolveProfile(String profileName) {
if (!profileName.contains(':')) {
profileName = "org.grails.profiles:$profileName:${BuildSettings.package.implementationVersion}"
profileName = "org.grails.profiles:$profileName:LATEST"
}
def art = new DefaultArtifact(profileName)
grapeEngine.grab(group: art.groupId, module: art.artifactId, version: art.version)
grapeEngine.grab(group: art.groupId ?: 'org.grails.profiles', module: art.artifactId, version: art.version ?: 'LATEST')

processUrls()
}

protected void processUrls() {
def urls = classLoader.getURLs()
for (URL url in urls) {
registerProfile(url, new URLClassLoader([url] as URL[], Thread.currentThread().contextClassLoader))
Expand All @@ -74,6 +79,26 @@ class MavenProfileRepository extends AbstractJarProfileRepository {

@Override
List<Profile> getAllProfiles() {


if(!resolved) {
List<String> profileNames = []
for(repo in repositoryConfigurations) {

def baseUri = repo.uri
def text = new URL("${baseUri}/org/grails/profiles").text
text.eachMatch(/<a href="([a-z-]+)\/">.+/) { List<String> it ->
profileNames.add it[1]
}
}

for(name in profileNames) {
grapeEngine.grab(group: 'org.grails.profiles', module: name, version: 'LATEST')
}

processUrls()
resolved = true
}
return super.getAllProfiles()
}
}
Expand Up @@ -15,7 +15,6 @@
*/
package org.grails.cli.profile.repository
import groovy.transform.CompileStatic
import org.grails.io.support.ClassPathResource

/**
* A JAR file repository that resolves profiles from a static array of JAR file URLs
Expand Down
@@ -0,0 +1,35 @@
package org.grails.cli.profile.repository

import spock.lang.Specification



/**
* @author graemerocher
*/
class MavenRepositorySpec extends Specification {


void "Test resolve profile"() {
given:"A maven profile repository"
def repo = new MavenProfileRepository()

when:"We resolve the web profile"
def profile = repo.getProfile("web")

then:"The profile is not null"
profile != null
profile.name == 'web'
}

void "Test list all profiles"() {
given:"A maven profile repository"
def repo = new MavenProfileRepository()

when:"We resolve the web profile"
def profiles = repo.allProfiles

then:"The profiles are not null or empty"
profiles
}
}

0 comments on commit 3eb08a9

Please sign in to comment.