GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / commons / org / codehaus / groovy / grails / plugins / GrailsPluginUtils.groovy
blob917dfd7026d068fe99b5e8f11a3ad254fae201bf
1 /* Copyright 2004-2005 Graeme Rocher
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 package org.codehaus.groovy.grails.plugins;
17 /**
18 * Utility class containing methods that aid in loading and evaluating plug-ins
20 * @author Graeme Rocher
21 * @since 1.0
22 * <p/>
23 * Created: Nov 29, 2007
25 public class GrailsPluginUtils {
27 static final String WILDCARD = "*";
28 static final COMPARATOR = [compare: { o1, o2 ->
29 def result = 0
30 if(o1 == '*') result = 1
31 else if(o2 == '*') result = -1
32 else {
33 def nums1 = o1.split(/\./).findAll { it.trim() != ''}*.toInteger()
34 def nums2 = o2.split(/\./).findAll { it.trim() != ''}*.toInteger()
35 for(i in 0..<nums1.size()) {
36 if(nums2.size() > i) {
37 result = nums1[i].compareTo(nums2[i])
38 if(result != 0)break
42 result
44 equals: { false }] as Comparator
46 /**
47 * Check if the required version is a valid for the given plugin version
49 * @param pluginVersion The plugin version
50 * @param requiredVersion The required version
51 * @return True if it is valid
53 public static boolean isValidVersion(String pluginVersion, String requiredVersion) {
55 pluginVersion = trimTag(pluginVersion);
57 if(requiredVersion.indexOf('>')>-1) {
58 def tokens = requiredVersion.split(">")*.trim()
59 def newTokens = []
60 for(t in tokens) {
61 newTokens << trimTag(t)
64 tokens << pluginVersion
65 tokens = tokens.sort(COMPARATOR)
67 if(tokens[1] == pluginVersion) return true
70 else if(pluginVersion.equals(trimTag(requiredVersion))) return true;
71 return false;
74 private static trimTag(pluginVersion) {
75 def i = pluginVersion.indexOf('-')
76 if(i>-1)
77 pluginVersion = pluginVersion[0..i-1]
78 pluginVersion