Skip to content

Commit

Permalink
Implemented insert methods in GradleUtil.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wyluda committed Jul 12, 2013
1 parent 168e4ad commit a1e1d05
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
public enum GradlePluginType
{
// TODO short names for plugin types
JAVA("org.gradle.api.plugins.JavaPlugin"), GROOVY("org.gradle.api.plugins.GroovyPlugin"),
SCALA("org.gradle.api.plugins.scala.ScalaPlugin"), WAR("org.gradle.api.plugins.WarPlugin"),
EAR("org.gradle.plugins.EarPlugin"), JETTY("org.gradle.api.plugins.jetty.JettyPlugin"),
Expand Down Expand Up @@ -45,18 +46,30 @@ private static class TypeContainer
}

private final String clazz;
private final String shortName;

private GradlePluginType(String clazz)
{
this(clazz, "");
}

private GradlePluginType(String clazz, String shortName)
{
this.clazz = clazz;
TypeContainer.TYPE_MAP.put(clazz, this);
this.shortName = shortName;
}

public String getClazz()
{
return clazz;
}


public String getShortName()
{
return shortName;
}

/**
* @return Plugin type for given class. If there is no such type then it returns {@link #OTHER}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

import java.util.List;

import org.gradle.jarjar.com.google.common.base.Preconditions;
import org.gradle.jarjar.com.google.common.base.Joiner;
import org.gradle.jarjar.com.google.common.collect.Lists;
import org.jboss.forge.addon.gradle.projects.exceptions.UnremovableElementException;
import org.jboss.forge.furnace.util.Strings;

/**
* @author Adam Wyłuda
Expand All @@ -18,6 +20,8 @@ public class GradleUtil
{
public static String insertDependency(String source, String name, String group, String version, String configuration)
{
String depString = String.format("%s '%s:%s:%s'", configuration, group, name, version);
source = SourceUtil.insertIntoInvocationAtPath(source, depString, "dependencies");
return source;
}

Expand All @@ -28,9 +32,24 @@ public static String removeDependency(String source, String name, String group,
return source;
}

/**
* Adds {@code apply plugin: 'clazz'} after the last plugin application, if there are no other applied plugins then
* it adds it at the end of source.
*/
public static String insertPlugin(String source, String clazz)
{
// TODO
String pluginString = String.format("\napply plugin: '%s'", clazz);

SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);
if (parser.getInvocationsWithMap().size() > 0)
{
InvocationWithMap lastInvocation = parser.getInvocationsWithMap().get(parser.getInvocationsWithMap().size() - 1);
source = SourceUtil.insertString(source, pluginString, lastInvocation.getLastLineNumber(), lastInvocation.getLastColumnNumber());
}
else
{
source += pluginString;
}
return source;
}

Expand All @@ -43,7 +62,8 @@ public static String removePlugin(String source, String clazz)

public static String insertRepository(String source, String name, String url)
{
// TODO
String repoString = String.format("url '%s'", url);
source = SourceUtil.insertIntoInvocationAtPath(source, repoString, "repositories", "maven");
return source;
}

Expand All @@ -58,9 +78,46 @@ public static String removeRepository(String source, String name, String url)
// so we can only insert new tasks
public static String insertTask(String source, String name, List<String> dependsOn, String type, String code)
{
// TODO
String taskDeclarationString = taskDeclarationString(name, dependsOn, type);
String taskString = String.format("\ntask %s << {\n%s\n}\n", taskDeclarationString, code);
source += taskString;
return source;
}

private static String taskDeclarationString(String name, List<String> dependsOn, String type)
{
if (dependsOn.isEmpty() && Strings.isNullOrEmpty(type))
{
return name;
}
else
{
String dependsOnString = dependsOnString(dependsOn);
String typeString = Strings.isNullOrEmpty(type) ? "" : ", type: " + type;
return String.format("(name: '%s'%s%s)", name, dependsOnString, typeString);
}
}

private static String dependsOnString(List<String> dependsOn)
{
if (dependsOn.size() == 0)
{
return "";
}
else if (dependsOn.size() == 1)
{
return ", dependsOn: '" + dependsOn.get(0) + "'";
}
else
{
List<String> dependsOnInQuotes = Lists.newArrayList();
for (String dep : dependsOn)
{
dependsOnInQuotes.add("'" + dep + "'");
}
return ", dependsOn: [" + Joiner.on(", ").join(dependsOnInQuotes) + "]";
}
}

/**
* Checks if source includes forge library and if not then adds it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void testInsertRepository()
String expected = "" +
"repositories {\n" +
" maven {\n" +
" url 'http://repo.com\n" +
" url 'http://repo.com'\n" +
" }\n" +
"}";
String result = GradleUtil.insertRepository(source, "", "http://repo.com");
Expand Down Expand Up @@ -166,9 +166,52 @@ public void testInsertTask()
" println variable\n" +
"}\n";
String result = GradleUtil.insertTask(source, "efgh", Lists.<String> newArrayList(), "", "" +
" def variable = 10\n" +
" println variable");
assertEquals(expected, result);
}

@Test
public void testInsertTaskWithDependenciesAndType()
{
String source = "" +
"task abcd << {\n" +
" println 'ABCD!!'\n" +
"}\n";
String expected = "" +
"task abcd << {\n" +
" println 'ABCD!!'\n" +
"}\n" +
"\n" +
"task (name: 'efgh', dependsOn: ['x', 'y', 'z'], type: Copy) << {\n" +
" def variable = 10\n" +
" println variable\n" +
"}\n";
String result = GradleUtil.insertTask(source, "efgh", Lists.<String> newArrayList("x", "y", "z"), "Copy", "" +
" def variable = 10\n" +
" println variable");
assertEquals(expected, result);
}

@Test
public void testInsertTaskWithOneDependency()
{
String source = "" +
"task abcd << {\n" +
" println 'ABCD!!'\n" +
"}\n";
String expected = "" +
"task abcd << {\n" +
" println 'ABCD!!'\n" +
"}\n" +
"\n" +
"task (name: 'efgh', dependsOn: 'x', type: Copy) << {\n" +
" def variable = 10\n" +
" println variable\n" +
"}\n";
String result = GradleUtil.insertTask(source, "efgh", Lists.<String> newArrayList("x"), "Copy", "" +
" def variable = 10\n" +
" println variable\n");
" println variable");
assertEquals(expected, result);
}

Expand Down

0 comments on commit a1e1d05

Please sign in to comment.