diff options
author | Miroslav Šulc <fordfrog@fordfrog.com> | 2015-10-26 22:43:31 +0100 |
---|---|---|
committer | Miroslav Šulc <fordfrog@fordfrog.com> | 2015-10-26 22:43:31 +0100 |
commit | da4487f8b130526bffc6dff8b8e5ad400d6d4bc2 (patch) | |
tree | 8cd81947d4ca2bc63eea7c03714ca33940167fcd | |
parent | Matches jar and zip but non-greedily. (diff) | |
download | java-ebuilder-da4487f8b130526bffc6dff8b8e5ad400d6d4bc2.tar.gz java-ebuilder-da4487f8b130526bffc6dff8b8e5ad400d6d4bc2.tar.bz2 java-ebuilder-da4487f8b130526bffc6dff8b8e5ad400d6d4bc2.zip |
enhanced processing of SLOT value
-rw-r--r-- | src/main/java/org/gentoo/java/ebuilder/portage/PortageParser.java | 90 |
1 files changed, 89 insertions, 1 deletions
diff --git a/src/main/java/org/gentoo/java/ebuilder/portage/PortageParser.java b/src/main/java/org/gentoo/java/ebuilder/portage/PortageParser.java index 6ce566f..c3122a8 100644 --- a/src/main/java/org/gentoo/java/ebuilder/portage/PortageParser.java +++ b/src/main/java/org/gentoo/java/ebuilder/portage/PortageParser.java @@ -11,7 +11,11 @@ import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.gentoo.java.ebuilder.Config; /** @@ -42,6 +46,24 @@ public class PortageParser { */ private static final String ECLASS_JAVA_UTILS = "java-utils-2"; /** + * Pattern for parsing SLOT with bash substring. + */ + private static final Pattern PATTERN_SLOT_SUBSTRING = Pattern.compile( + "^\\$\\{PV:(\\d+):(\\d+)\\}$"); + /** + * Pattern for parsing version component range in SLOT. + */ + private static final Pattern PATTERN_SLOT_VERSION_COMPOPONENT_RANGE + = Pattern.compile( + "^\\$\\(get_version_component_range (\\d+)-(\\d+)\\)$"); + /** + * Pattern for checking whether the line contains variable declaration. It + * does not handle correctly variables spread across several lines but we + * most probably do not care about these. + */ + private static final Pattern PATTERN_VARIABLE = Pattern.compile( + "^(\\S+?)=(.*)$"); + /** * List of cache items. This list is populated during parsing the tree. */ private final List<CacheItem> cacheItems = new ArrayList<>(40_000); @@ -149,6 +171,7 @@ public class PortageParser { final String category = ebuild.getParentFile().getParentFile().getName(); final String pkg = ebuild.getParentFile().getName(); final String version = filename.substring(pkg.length() + 1); + final Map<String, String> variables = new HashMap<>(20); String eclass = null; String slot = "0"; String useFlag = null; @@ -174,6 +197,13 @@ public class PortageParser { } if (!line.isEmpty()) { + final Matcher matcher = PATTERN_VARIABLE.matcher(line); + + if (matcher.matches()) { + variables.put(matcher.group(1), matcher.group(2). + replaceAll("(^\"|\"$)", "")); + } + if (line.startsWith("inherit ")) { eclass = getJavaInheritEclass(line); @@ -215,7 +245,7 @@ public class PortageParser { pv = version.substring(0, pos); } - slot = slot.replaceAll("\\$(\\{PV\\}|PV)", pv); + slot = processSlot(slot, pv, variables); if (mavenId != null) { mavenId = mavenId.replaceAll("\\$(\\{PN\\}|PN)", pkg). @@ -263,6 +293,64 @@ public class PortageParser { } /** + * Processes various instructions in SLOT string. + * + * @param slot SLOT string + * @param pv PV variable + * @param variables map of collected variables and their values + * + * @return processed SLOT string + */ + private String processSlot(final String slot, final String pv, + final Map<String, String> variables) { + String result = slot.replaceAll("\\$(\\{PV\\}|PV)", pv); + + if (result.indexOf('$') != -1) { + for (final Map.Entry<String, String> variable + : variables.entrySet()) { + result = result. + replace("$" + variable.getKey(), variable.getValue()). + replace("${" + variable.getKey() + '}', + variable.getValue()); + } + } + + if (result.indexOf('$') != -1) { + final Matcher matcher = PATTERN_SLOT_SUBSTRING.matcher(result); + + if (matcher.matches()) { + final int start = Integer.parseInt(matcher.group(1), 10); + final int length = Integer.parseInt(matcher.group(2), 10); + result = pv.substring(start, start + length); + } + } + + if (result.indexOf('$') != -1) { + final Matcher matcher = PATTERN_SLOT_VERSION_COMPOPONENT_RANGE. + matcher(result); + + if (matcher.matches()) { + final int start = Integer.parseInt(matcher.group(1), 10); + final int end = Integer.parseInt(matcher.group(2), 10); + final String[] parts = pv.split("\\."); + final StringBuilder sbResult = new StringBuilder(10); + + for (int i = start; i <= end; i++) { + if (sbResult.length() > 0) { + sbResult.append('.'); + } + + sbResult.append(i <= parts.length ? parts[i - 1] : '0'); + } + + result = sbResult.toString(); + } + } + + return result; + } + + /** * Writes cache items to the cache file. * * @param config application configuration |