Skip to content

Commit

Permalink
Add get/remove managed dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wyluda committed Sep 20, 2013
1 parent b78eaac commit e89de09
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static String removeDependency(String source,
.setPackaging(!Strings.isNullOrEmpty(packaging) ? packaging : "jar");

SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);
for (InvocationWithClosure deps : parser.allInvocationsAtPath("dependencies"))
for (InvocationWithClosure deps : allDependencyInvocations(parser))
{
// Search in string invocations
for (InvocationWithString invocation : deps.getInvocationsWithString())
Expand Down Expand Up @@ -151,7 +151,7 @@ public static List<GradleDependency> getDependencies(String source)
List<GradleDependency> result = Lists.newArrayList();

SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);
for (InvocationWithClosure deps : parser.allInvocationsAtPath("dependencies"))
for (InvocationWithClosure deps : allDependencyInvocations(parser))
{
// Search in string invocations
for (InvocationWithString invocation : deps.getInvocationsWithString())
Expand Down Expand Up @@ -222,7 +222,7 @@ public static List<GradleDependency> getDirectDependencies(String source)
List<GradleDependency> deps = Lists.newArrayList();

SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);
for (InvocationWithClosure invocation : parser.allInvocationsAtPath("dependencies"))
for (InvocationWithClosure invocation : allDependencyInvocations(parser))
{
for (InvocationWithMap mapInvocation : invocation.getInvocationsWithMap())
{
Expand Down Expand Up @@ -254,25 +254,9 @@ public static String insertManagedDependency(String source, String group, String
}

public static String removeManagedDependency(String source, String group, String name, String version,
String configuration) throws UnremovableElementException
String configuration, String classifier, String packaging) throws UnremovableElementException
{
SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);
for (InvocationWithClosure deps : parser.allInvocationsAtPath("allprojects", "dependencies"))
{
for (InvocationWithMap invocation : deps.getInvocationsWithMap())
{
Map<String, String> params = invocation.getParameters();
if (invocation.getMethodName().equals(MANAGED_CONFIG) &&
params.get("group").equals(group) &&
params.get("name").equals(name) &&
params.get("version").equals(version))
{
return SourceUtil.removeSourceFragmentWithLine(source, invocation);
}
}
}

throw new UnremovableElementException();
return removeDependency(source, MANAGED_CONFIG, group, name, version, classifier, packaging);
}

/**
Expand All @@ -283,19 +267,13 @@ public static List<GradleDependency> getManagedDependencies(String source)
List<GradleDependency> deps = Lists.newArrayList();

SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);
for (InvocationWithClosure invocation : parser.allInvocationsAtPath("allprojects", "dependencies"))
for (InvocationWithClosure invocation : allDependencyInvocations(parser))
{
for (InvocationWithMap mapInvocation : invocation.getInvocationsWithMap())
{
if (mapInvocation.getMethodName().equals(MANAGED_CONFIG))
{
Map<String, String> params = mapInvocation.getParameters();
GradleDependencyBuilder dep = GradleDependencyBuilder.create()
.setConfigurationName(params.get("configuration"))
.setGroup(params.get("group"))
.setName(params.get("name"))
.setVersion(params.get("version"));
deps.add(dep);
deps.add(managedDependencyFromInvocation(mapInvocation));
}
}
}
Expand Down Expand Up @@ -547,11 +525,25 @@ public static Map<String, String> getDirectProperties(String source)
}
return properties;
}

private static List<InvocationWithClosure> allDependencyInvocations(SimpleGroovyParser parser) {
List<InvocationWithClosure> depsInvocations = Lists.newArrayList();
depsInvocations.addAll(parser.allInvocationsAtPath("dependencies"));
depsInvocations.addAll(parser.allInvocationsAtPath("allprojects", "dependencies"));
return depsInvocations;
}

private static GradleDependency dependencyFromInvocation(InvocationWithString invocation)
{
return dependencyFromString(invocation.getMethodName(), invocation.getString());
}

private static GradleDependency managedDependencyFromInvocation(InvocationWithMap invocation)
{
Map<String, String> map = Maps.newHashMap(invocation.getParameters());
String config = map.remove("configuration");
return dependencyFromMap(config, map);
}

private static GradleDependency dependencyFromInvocation(InvocationWithMap invocation)
{
Expand Down Expand Up @@ -640,6 +632,12 @@ private static boolean isGradleDependencyConfiguration(String configName)
return true;
}
}

if (MANAGED_CONFIG.equals(configName))
{
return true;
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private static String removeManagedDependencies(String source, List<GradleDepend
for (GradleDependency dep : deps)
{
source = GradleSourceUtil.removeManagedDependency(source, dep.getGroup(), dep.getName(),
dep.getVersion(), dep.getConfigurationName());
dep.getVersion(), dep.getConfigurationName(), dep.getClassifier(), dep.getConfigurationName());
}
return source;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,32 @@ public void testRemoveManagedDependency()
" dependencies {\n" +
" }\n" +
"}\n";
String result = GradleSourceUtil.removeManagedDependency(source, "xx", "yy", "vv", "compile");
String result = GradleSourceUtil.removeManagedDependency(source, "xx", "yy", "vv", "compile", "", "");
assertEquals(expected, result);
}

@Test
public void testRemoveManagedDependencyWithClassifierAndPackaging()
{
String source = "" +
"dependencies {\n" +
" compile 'a:b:1.0'\n" +
"}\n" +
"allprojects {\n" +
" dependencies {\n" +
" managed configuration: \"compile\", group: 'xx', name: 'yy', version: 'vv'" +
", classifier: \"clas\", ext: \"pom\"\n" +
" }\n" +
"}\n";
String expected = "" +
"dependencies {\n" +
" compile 'a:b:1.0'\n" +
"}\n" +
"allprojects {\n" +
" dependencies {\n" +
" }\n" +
"}\n";
String result = GradleSourceUtil.removeManagedDependency(source, "xx", "yy", "vv", "compile", "clas", "pom");
assertEquals(expected, result);
}

Expand All @@ -403,6 +428,28 @@ public void testGetManagedDependencies()
.setConfigurationName("compile").setGroup("xx").setName("yy").setVersion("vv"));
}

@Test
public void testGetManagedDependenciesWithClassifierAndPackaging()
{
String source = "" +
"dependencies {\n" +
" compile 'a:b:1.0'\n" +
"}\n" +
"allprojects {\n" +
" dependencies {\n" +
" managed configuration: 'compile', group: \"xx\", name: 'yy', version: 'vv'" +
", classifier: 'clas', ext: 'pom'\n" +
" }\n" +
"}\n";

List<GradleDependency> deps = GradleSourceUtil.getManagedDependencies(source);

assertEquals(1, deps.size());
assertContainsDependency(deps, GradleDependencyBuilder.create()
.setConfigurationName("compile").setGroup("xx").setName("yy").setVersion("vv")
.setClassifier("clas").setPackaging("pom"));
}

@Test
public void testGetPlugins()
{
Expand Down

0 comments on commit e89de09

Please sign in to comment.