1/* Copyright 2004-2005 the original author or authors.
2 *
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
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15package org.codehaus.groovy.grails.resolve
16
17import java.lang.reflect.Field
18
19import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor
20import org.apache.ivy.core.module.descriptor.DefaultExcludeRule
21import org.apache.ivy.core.module.descriptor.ModuleDescriptor
22import org.apache.ivy.core.module.id.ArtifactId
23import org.apache.ivy.core.module.id.ModuleId
24import org.apache.ivy.core.module.id.ModuleRevisionId
25import org.apache.ivy.plugins.matcher.ExactPatternMatcher
26
27/**
28 * Adds new methods to make access to this class Groovier
29 *
30 * @author Graeme Rocher
31 * @since 1.2
32 */
33class EnhancedDefaultDependencyDescriptor extends DefaultDependencyDescriptor {
34
35    static final String WILDCARD = '*'
36
37    /**
38     * Configuration scope of the plugin 'runtime', 'build', 'test' etc.
39     */
40    String scope
41
42    /**
43     * Plugin that the dependency relates to, null if it is a framework or application dependency
44     */
45    String plugin
46
47    /**
48     * Whether the dependency is inherted from a plugin or framework and not an application dependency
49     */
50    boolean inherited
51
52    /**
53     * Whether a plugin dependencies is 'exported' to the application
54     */
55    boolean exported = true
56
57    private confList
58
59    /**
60     * Whether the dependency should be exposed to the application
61     */
62    boolean isExportedToApplication() {
63        if (plugin && !exported) return false
64        return true
65    }
66
67    EnhancedDefaultDependencyDescriptor(ModuleRevisionId mrid, boolean force, String scope) {
68        super(mrid, force)
69        this.scope = scope
70        confList = getModuleConfigurations().toList()
71    }
72
73    EnhancedDefaultDependencyDescriptor(ModuleRevisionId mrid, boolean force, boolean transitive, String scope) {
74        this(mrid, force, scope)
75        setTransitive(transitive)
76    }
77
78    void setExport(boolean b) {
79        this.exported = b
80    }
81
82    void excludes(Object... args) {
83        for (arg in args) {
84            exclude(arg)
85        }
86    }
87
88    void exclude(def exclude) {
89        if (exclude instanceof String) {
90            excludeForString(exclude)
91        }
92        else if (exclude instanceof Map) {
93            excludeForMap(exclude)
94        }
95    }
96
97    private excludeForString (String dep) {
98        def mid = ModuleId.newInstance(WILDCARD, dep)
99        addRuleForModuleId(mid, scope, WILDCARD, WILDCARD)
100    }
101
102    private excludeForMap (Map args) {
103        def mid = ModuleId.newInstance(args?.group ?: WILDCARD, args?.name ?: WILDCARD)
104        addRuleForModuleId(mid, scope, args?.type ?: WILDCARD, args?.ext ?: WILDCARD)
105    }
106
107    void dependencyConfiguration(String config) {
108        addDependencyConfiguration(scope, config)
109    }
110
111    void setTransitive(boolean b) {
112        // nasty hack since the isTransitive Ivy field is not public
113        Field field = getClass().getSuperclass().getDeclaredField("isTransitive")
114        field.accessible = true
115        field.set(this, b)
116    }
117
118    void setChanging(boolean b) {
119        // nasty hack since the isChanging Ivy field is not public
120        Field field = getClass().getSuperclass().getDeclaredField("isChanging")
121        field.accessible = true
122        field.set(this, b)
123    }
124
125    void addRuleForModuleId(ModuleId mid, String scope, String type, String ext) {
126        def id = new ArtifactId(mid, WILDCARD, type, ext)
127        def rule = new DefaultExcludeRule(id, ExactPatternMatcher.INSTANCE, null)
128        addExcludeRule scope, rule
129    }
130
131    boolean isSupportedInConfiguration(String conf) {
132        scope == conf
133    }
134}
135