Skip to content

Commit

Permalink
Add getDirectProperties() to GradleSourceUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wyluda committed Aug 10, 2013
1 parent 983192a commit 4d44285
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class GradleSourceUtil
public static final String DIRECT_CONFIG = GradleDependencyConfiguration.DIRECT.getName();

public static final String ARCHIVE_NAME_METHOD = "archiveName";

public static final String PROJECT_PROPERTY_PREFIX = "ext.";

/**
* Sets the project name in Gradle project settings script.
Expand Down Expand Up @@ -365,4 +367,23 @@ public static String checkForIncludeForgeLibraryAndInsert(String source)
// If statement including forge library was not found then we add it
return INCLUDE_FORGE_LIBRARY + source;
}

/**
* Returns a map of properties which are clearly declared in a given build script (i.e. they can be
* modified/removed). Also, they must be project properties, declared in project.ext namespace.
*/
public static Map<String, String> getDirectProperties(String source)
{
SimpleGroovyParser parser = SimpleGroovyParser.fromSource(source);
Map<String, String> properties = Maps.newHashMap();
for (VariableAssignment assignment : parser.getVariableAssignments())
{
if (assignment.getVariable().startsWith(PROJECT_PROPERTY_PREFIX))
{
properties.put(assignment.getVariable().substring(PROJECT_PROPERTY_PREFIX.length()),
assignment.getValue());
}
}
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Map;
import java.util.Set;

import org.gradle.jarjar.com.google.common.collect.ImmutableList;
import org.gradle.jarjar.com.google.common.collect.Lists;
import org.gradle.jarjar.com.google.common.collect.Maps;
import org.gradle.jarjar.com.google.common.collect.Sets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import static org.junit.Assert.assertEquals;

import java.util.Map;

import org.gradle.jarjar.com.google.common.collect.Lists;
import org.jboss.forge.addon.gradle.projects.exceptions.UnremovableElementException;
import org.junit.Test;
Expand All @@ -30,26 +32,26 @@ public void testSetArchiveName()
String result = GradleSourceUtil.setArchiveName(source, "XYZ");
assertEquals(expected, result);
}

@Test
public void testSetArchiveNameExisting()
{
String source = "" +
"dependencies {\n" +
"dependencies {\n" +
"}\n" +
"archiveName 'oldArchive'\n" +
"repositories {\n" +
"}\n";
String expected = "" +
"dependencies {\n" +
"dependencies {\n" +
"}\n" +
"archiveName 'newArchive'\n" +
"repositories {\n" +
"}\n";
String result = GradleSourceUtil.setArchiveName(source, "newArchive");
assertEquals(expected, result);
}

@Test
public void testInsertDependency()
{
Expand Down Expand Up @@ -108,7 +110,7 @@ public void testRemoveDependencyForException() throws UnremovableElementExceptio
"}";
GradleSourceUtil.removeDependency(source, "a", "b", "1.0", "compile");
}

@Test
public void testInsertDirectDependency()
{
Expand All @@ -124,7 +126,7 @@ public void testInsertDirectDependency()
String result = GradleSourceUtil.insertDirectDependency(source, "x", "y");
assertEquals(expected, result);
}

@Test
public void testRemoveDirectDependency() throws UnremovableElementException
{
Expand All @@ -140,7 +142,7 @@ public void testRemoveDirectDependency() throws UnremovableElementException
String result = GradleSourceUtil.removeDirectDependency(source, "x", "y");
assertEquals(expected, result);
}

@Test(expected = UnremovableElementException.class)
public void testRemoveDirectDependencyForException() throws UnremovableElementException
{
Expand Down Expand Up @@ -464,4 +466,26 @@ public void testCheckForIncludeForgeLibraryAndInsertExisting()
String result = GradleSourceUtil.checkForIncludeForgeLibraryAndInsert(source);
assertEquals(expected, result);
}

@Test
public void testGetDirectProperties()
{
// Assuming that direct properties are those which can be obtained by analyzing source
String source = "" +
"dependencies {\n" +
" ext.dependencyProperty = 'xxxx'\n" +
"}\n" +
"badProperty = 'yyy'\n" +
"ext.goodProperty = 'zzz'\n" +
"def propertyMaker = {\n" +
" project.ext.makerProperty = it\n" +
"}\n" +
"propertyMaker '7'\n" +
"task myTask << {}\n" +
"myTask.ext.taskProperty = 'majtaski'\n";
Map<String, String> result = GradleSourceUtil.getDirectProperties(source);

assertEquals(1, result.size());
assertEquals("zzz", result.get("goodProperty"));
}
}

0 comments on commit 4d44285

Please sign in to comment.