Skip to content

Commit

Permalink
Implemented (and tested):
Browse files Browse the repository at this point in the history
- insert managed dependencies
- check and include Forge library
- find a list of invocations at path
- removing a line in source
  • Loading branch information
adam-wyluda committed Jul 13, 2013
1 parent a1e1d05 commit 037c02f
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
package org.jboss.forge.addon.gradle.parser;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

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

Expand All @@ -18,6 +21,8 @@
*/
public class GradleUtil
{
public static final String INCLUDE_FORGE_LIBRARY = "apply from: 'forge.gradle'\n";

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);
Expand All @@ -27,6 +32,38 @@ public static String insertDependency(String source, String name, String group,

public static String removeDependency(String source, String name, String group, String version, String configuration)
throws UnremovableElementException
{
String depString = String.format("%s '%s:%s:%s'", configuration, group, name, version);
Map<String, String> depMap = Maps.newHashMap();
depMap.put("group", group);
depMap.put("name", name);
depMap.put("version", version);

SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);
for (InvocationWithClosure deps : parser.allInvocationsAtPath("dependencies"))
{
// Search in string invocations
for (InvocationWithString invocation : deps.getInternalStringInvocations())
{
if (invocation.getMethodName().equals(configuration))
{

}
}
}

throw new UnremovableElementException();
}

public static String insertManagedDependency(String source, String name, String group, String version, String configuration)
{
String depString = String.format("managed config: '%s', group: '%s', name: '%s', version: '%s'",
configuration, group, name, version);
source = SourceUtil.insertIntoInvocationAtPath(source, depString, "allprojects", "dependencies");
return source;
}

public static String removeManagedDependency(String source, String name, String group, String version, String configuration)
{
// TODO
return source;
Expand Down Expand Up @@ -124,7 +161,23 @@ else if (dependsOn.size() == 1)
*/
public static String checkForIncludeForgeLibraryAndInsert(String source)
{
// TODO
return source;
SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);
for (InvocationWithMap invocation : parser.getInvocationsWithMap())
{
if (invocation.getMethodName().equals("apply"))
{
Map<String, String> map = invocation.getParameters();
String from = map.get("from");

// If it is already included in source then we just return the source
if ("forge.gradle".equals(from))
{
return source;
}
}
}

// If statement including forge library was not found then we add it
return INCLUDE_FORGE_LIBRARY + source;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.jboss.forge.addon.gradle.parser;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -24,6 +25,7 @@
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.control.SourceUnit;
import org.gradle.jarjar.com.google.common.base.Optional;
import org.gradle.jarjar.com.google.common.base.Preconditions;
import org.gradle.jarjar.com.google.common.collect.Lists;

/**
Expand Down Expand Up @@ -100,6 +102,31 @@ public Optional<InvocationWithMap> invocationWithMapByName(String name)
{
return root.invocationWithMapByName(name);
}

public List<InvocationWithClosure> allInvocationsAtPath(String... path)
{
Preconditions.checkArgument(path.length > 0, "Path must have at least one element");
List<InvocationWithClosure> list = Lists.newArrayList();
allInvocationsAtPath(list, root, path);
return list;
}

private void allInvocationsAtPath(List<InvocationWithClosure> list, InvocationWithClosure invocation, String... path)
{
if (path.length == 0)
{
list.add(invocation);
return;
}
String name = path[0];
for (InvocationWithClosure subinvocation : invocation.getInternalInvocations())
{
if (subinvocation.getMethodName().equals(name))
{
allInvocationsAtPath(list, subinvocation, Arrays.copyOfRange(path, 1, path.length));
}
}
}

static InvocationWithClosure createInvocationWithClosureRoot(String source)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package org.jboss.forge.addon.gradle.parser;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.gradle.jarjar.com.google.common.base.Optional;
import org.jboss.forge.addon.gradle.projects.util.Preconditions;
Expand All @@ -17,7 +19,9 @@
public class SourceUtil
{
public static final String INDENT = " ";

public static final Pattern PRECEDING_WHITESPACE_PATTERN = Pattern.compile("\\s*$");
public static final Pattern SUCCEEDING_WHITESPACE_PATTERN = Pattern.compile("^\\s*");

/**
* Inserts string at specified position in source.
*
Expand Down Expand Up @@ -75,7 +79,45 @@ public static String removeSourceFragment(String source, int lineNumber, int col
{
int begginingPosition = positionInSource(source, lineNumber, columnNumber);
int endingPosition = positionInSource(source, lastLineNumber, lastColumnNumber);
return source.substring(0, begginingPosition) + source.substring(endingPosition);
return removeSourceFragment(source, begginingPosition, endingPosition);
}

public static String removeSourceFragment(String source, int start, int end)
{
return source.substring(0, start) + source.substring(end);
}

/**
* {@link #removeSourceFragmentWithLine(String, int, int)}
*/
public static String removeSourceFragmentWithLine(String source, int lineNumber, int columnNumber,
int lastLineNumber, int lastColumnNumber)
{
int beginningPosition = positionInSource(source, lineNumber, columnNumber);
int endingPosition = positionInSource(source, lastLineNumber, lastColumnNumber);
return removeSourceFragmentWithLine(source, beginningPosition, endingPosition);
}

/**
* Replaces specified region and surrounding whitespaces with a single new line character.
*/
public static String removeSourceFragmentWithLine(String source, int start, int end)
{
String beforeCode = source.substring(0, start);
String afterCode = source.substring(end);

Matcher precedingMatcher = PRECEDING_WHITESPACE_PATTERN.matcher(beforeCode);
precedingMatcher.find();
String precedingWhitespace = precedingMatcher.group();

Matcher succeedingMatcher = SUCCEEDING_WHITESPACE_PATTERN.matcher(afterCode);
succeedingMatcher.find();
String succeedingWhitespace = succeedingMatcher.group();

start -= precedingWhitespace.length();
end += succeedingWhitespace.length();

return source.substring(0, start) + "\n" + source.substring(end);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,54 @@ public void testRemoveDependencyForException() throws UnremovableElementExceptio
"}";
GradleUtil.removeDependency(source, "b", "a", "1.0", "compile");
}

@Test
public void testInsertManagedDependency()
{
String source = "" +
"dependencies {\n" +
" compile 'a:b:1.0'\n" +
"}\n" +
"allprojects {\n" +
" dependencies {\n" +
" }\n" +
"}\n";
String expected = "" +
"dependencies {\n" +
" compile 'a:b:1.0'\n" +
"}\n" +
"allprojects {\n" +
" dependencies {\n" +
" managed config: 'compile', group: 'xx', name: 'yy', version: 'vv'\n" +
" }\n" +
"}\n";
String result = GradleUtil.insertManagedDependency(source, "yy", "xx", "vv", "compile");
assertEquals(expected, result);
}

@Test
public void testRemoveManagedDependency()
{
String source = "" +
"dependencies {\n" +
" compile 'a:b:1.0'\n" +
"}\n" +
"allprojects {\n" +
" dependencies {\n" +
" managed config: 'compile', group: 'xx', name: 'yy', version: 'vv'\n" +
" }\n" +
"}\n";
String expected = "" +
"dependencies {\n" +
" compile 'a:b:1.0'\n" +
"}\n" +
"allprojects {\n" +
" dependencies {\n" +
" }\n" +
"}\n";
String result = GradleUtil.removeManagedDependency(source, "yy", "xx", "vv", "compile");
assertEquals(expected, result);
}

@Test
public void testInsertPlugin()
Expand Down Expand Up @@ -134,7 +182,9 @@ public void testRemoveRepository() throws UnremovableElementException
" }\n" +
"}";
String expected = "" +
"repositories {\n" +
"repositories {" +
" maven {\n" +
" }\n" +
"}";
String result = GradleUtil.removeRepository(source, "", "http://repo.com");
assertEquals(expected, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*/
package org.jboss.forge.addon.gradle.parser;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.util.List;
import java.util.Map;

import org.gradle.jarjar.com.google.common.base.Optional;
Expand Down Expand Up @@ -120,4 +121,36 @@ public void testInvocationWithClosureByNameAbsent()
Optional<InvocationWithClosure> optional = SimpleGroovyParser.fromSource(sauce).invocationWithClosureByName("z");
assertFalse(optional.isPresent());
}

@Test
public void testAllInvocationAtPath()
{
String source = "// comment\n" +
"subprojects {\n" +
" dependencies {\n" +
" compile 'group:artifact:1.0.0'\n" +
" }\n" +
"}\n" +
"xyz\n" +
"subprojects {\n" +
" qwerty\n" +
" dependencies {\n" +
" testRuntime 'x:y:z'\n" +
" }\n" +
" println 'asdf'\n" +
"}\n" +
"";
SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);

List<InvocationWithClosure> list = parser.allInvocationsAtPath("subprojects", "dependencies");
assertEquals(2, list.size());

InvocationWithClosure invocation = list.get(0);
assertEquals(1, invocation.getInternalStringInvocations().size());
assertEquals("compile", invocation.getInternalStringInvocations().get(0).getMethodName());

invocation = list.get(1);
assertEquals(1, invocation.getInternalStringInvocations().size());
assertEquals("testRuntime", invocation.getInternalStringInvocations().get(0).getMethodName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void testPositionInSourceException()
}

@Test
public void testRemoveSourceCodeElement()
public void testRemoveSourceFragment()
{
String source =
"import java.util.Scanner;";
Expand All @@ -67,6 +67,22 @@ public void testRemoveSourceCodeElement()
String output = SourceUtil.removeSourceFragment(source, 1, 8, 1, 13);
assertEquals(expectedOutput, output);
}

@Test
public void testRemoveSourceFragmentWithLine()
{
String source =
"subprojects {\n" +
" println 'abcdef'\n" +
" abcdef\n" +
"}\n";
String expected =
"subprojects {\n" +
" println 'abcdef'\n" +
"}\n";
String result = SourceUtil.removeSourceFragmentWithLine(source, 3, 5, 3, 11);
assertEquals(expected, result);
}

@Test
public void testAppendLineToClosure()
Expand Down

0 comments on commit 037c02f

Please sign in to comment.