|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2019, R. Tyler Croy <[email protected]>, |
| 3 | + * Schalk Cronje <[email protected]>, Christian Meier, Lookout, Inc. |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | + * a copy of this software and associated documentation files (the |
| 7 | + * "Software"), to deal in the Software without restriction, including |
| 8 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | + * permit persons to whom the Software is furnished to do so, subject to |
| 11 | + * the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be |
| 14 | + * included in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | + */ |
| 24 | +package com.github.jrubygradle.api.gems |
| 25 | + |
| 26 | +import com.github.jrubygradle.api.core.RepositoryHandlerExtension |
| 27 | +import groovy.transform.CompileStatic |
| 28 | +import groovy.transform.EqualsAndHashCode |
| 29 | +import org.gradle.api.artifacts.Configuration |
| 30 | +import org.gradle.api.artifacts.ModuleVersionSelector |
| 31 | + |
| 32 | +import java.util.regex.Pattern |
| 33 | + |
| 34 | +/** Defines groups which contains GEMs and controls GEM resolving rules. |
| 35 | + * |
| 36 | + * @author Schalk W. Cronjé |
| 37 | + * |
| 38 | + * @since 2.0 |
| 39 | + */ |
| 40 | +@CompileStatic |
| 41 | +class GemResolverStrategy { |
| 42 | + |
| 43 | + public static final String NAME = 'gemResolverStrategy' |
| 44 | + |
| 45 | + /** Is this group/organisation a GEM group ? |
| 46 | + * |
| 47 | + * @param groupName Name of group/organisation. |
| 48 | + * @return {@code true} is group is a GEM group. |
| 49 | + */ |
| 50 | + boolean isGemGroup(final String groupName) { |
| 51 | + groups.contains(groupName) |
| 52 | + } |
| 53 | + |
| 54 | + /** Add a new group for GEMs. |
| 55 | + * |
| 56 | + * @param groupName Name of group to add. |
| 57 | + */ |
| 58 | + void addGemGroup(final String groupName) { |
| 59 | + groups.add(groupName) |
| 60 | + } |
| 61 | + |
| 62 | + /** Exclude a configuration from being resolved using the GEM |
| 63 | + * version resolver strategy. |
| 64 | + * |
| 65 | + * @param configs Configurations to be excluded |
| 66 | + */ |
| 67 | + void excludeConfigurations(Configuration... configs) { |
| 68 | + this.excludedConfigurations.addAll(configs*.name) |
| 69 | + } |
| 70 | + |
| 71 | + /** Exclude a configuration from being resolved using the GEM |
| 72 | + * version resolver strategy. |
| 73 | + * |
| 74 | + * @param configs Configurations to be excluded |
| 75 | + */ |
| 76 | + void excludeConfigurations(String... configs) { |
| 77 | + this.excludedConfigurations.addAll(configs) |
| 78 | + } |
| 79 | + |
| 80 | + /** Exclude a module from being resolved using the GEM version resolver |
| 81 | + * strategy. |
| 82 | + * |
| 83 | + * @param name Module name. Never {@code null}. |
| 84 | + * @param version Version. Can be {@code null}. |
| 85 | + */ |
| 86 | + void excludeModule(String name, String version = null) { |
| 87 | + excludedModules.add(new Matcher( |
| 88 | + module: Pattern.compile(Pattern.quote(name)), |
| 89 | + version: version ? Pattern.compile(Pattern.quote(version)) : null |
| 90 | + )) |
| 91 | + } |
| 92 | + |
| 93 | + /** Exclude a module from being resolved using the GEM version resolver |
| 94 | + * strategy. |
| 95 | + * |
| 96 | + * @param namePattern Pattern for name. Never {@code null}. |
| 97 | + * @param versionPattern Pattern for version. Can be {@code null} |
| 98 | + */ |
| 99 | + void excludeModule(Pattern namePattern, Pattern versionPattern = null) { |
| 100 | + excludedModules.add(new Matcher(module: namePattern, version: versionPattern)) |
| 101 | + } |
| 102 | + |
| 103 | + /** Whether the GEM version resolving strategy should be applied for a specific module. |
| 104 | + * |
| 105 | + * In most cases this will alsways be {@code true} unless a specific rule excludes it. |
| 106 | + * |
| 107 | + * @param mvs Module version selector |
| 108 | + * @return Whether the Bundler-like version selector atregty may be applied |
| 109 | + */ |
| 110 | + boolean useGemVersionResolver(ModuleVersionSelector mvs) { |
| 111 | + isGemGroup(mvs.group) && excludedModules.find { it.match(mvs.name, mvs.version) } |
| 112 | + } |
| 113 | + |
| 114 | + /** Whether the GEM version resolving strategy should be applied to a specific configuration. |
| 115 | + * |
| 116 | + * In most cases this will always be {@code true} unless a specific rule excludes it. |
| 117 | + * |
| 118 | + * @param configurationName Name fo configuration |
| 119 | + * @return Whether the Bundler-like version selector strategy may be applied |
| 120 | + */ |
| 121 | + boolean useGemVersionResolver(String configurationName) { |
| 122 | + configurationName in excludedConfigurations |
| 123 | + } |
| 124 | + |
| 125 | + @EqualsAndHashCode |
| 126 | + private class Matcher { |
| 127 | + Pattern module |
| 128 | + Pattern version |
| 129 | + |
| 130 | + boolean match(String name, String ver) { |
| 131 | + name =~ module && (this.version == null || ver ==~ this.version) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private final Set<Matcher> excludedModules = [].toSet() |
| 136 | + private final Set<String> excludedConfigurations = [].toSet() |
| 137 | + private final Set<String> groups = [RepositoryHandlerExtension.DEFAULT_GROUP_NAME].toSet() |
| 138 | +} |
0 commit comments