|
| 1 | +/* |
| 2 | + * Copyright 2019-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.gradle.xsd; |
| 18 | + |
| 19 | +import org.gradle.api.DefaultTask; |
| 20 | +import org.gradle.api.file.ConfigurableFileCollection; |
| 21 | +import org.gradle.api.file.RegularFileProperty; |
| 22 | +import org.gradle.api.tasks.*; |
| 23 | +import org.gradle.work.DisableCachingByDefault; |
| 24 | + |
| 25 | +import java.io.File; |
| 26 | +import java.io.IOException; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.nio.file.StandardCopyOption; |
| 30 | +import java.util.regex.Matcher; |
| 31 | +import java.util.regex.Pattern; |
| 32 | + |
| 33 | +/** |
| 34 | + * Creates the spring-security.xsd automatically |
| 35 | + * |
| 36 | + * @author Rob Winch |
| 37 | + */ |
| 38 | +@DisableCachingByDefault(because = "not worth it") |
| 39 | +public abstract class CreateVersionlessXsdTask extends DefaultTask { |
| 40 | + |
| 41 | + @InputFiles |
| 42 | + public abstract ConfigurableFileCollection getInputFiles(); |
| 43 | + |
| 44 | + @OutputFile |
| 45 | + abstract RegularFileProperty getVersionlessXsdFile(); |
| 46 | + |
| 47 | + @TaskAction |
| 48 | + void createVersionlessXsd() throws IOException { |
| 49 | + XsdFileMajorMinorVersion largest = null; |
| 50 | + ConfigurableFileCollection inputFiles = getInputFiles(); |
| 51 | + if (inputFiles.isEmpty()) { |
| 52 | + throw new IllegalStateException("No Inputs configured"); |
| 53 | + } |
| 54 | + for (File file : inputFiles) { |
| 55 | + XsdFileMajorMinorVersion current = XsdFileMajorMinorVersion.create(file); |
| 56 | + if (current == null) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + if (largest == null) { |
| 60 | + largest = current; |
| 61 | + } |
| 62 | + else if (current.getVersion().isGreaterThan(largest.getVersion())) { |
| 63 | + largest = current; |
| 64 | + } |
| 65 | + } |
| 66 | + if (largest == null) { |
| 67 | + throw new IllegalStateException("Could not create versionless xsd file because no files matching spring-security-<digit>.xsd were found in " + inputFiles.getFiles()); |
| 68 | + } |
| 69 | + Path to = getVersionlessXsdFile().getAsFile().get().toPath(); |
| 70 | + Path from = largest.getFile().toPath(); |
| 71 | + Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES); |
| 72 | + } |
| 73 | + |
| 74 | + static class XsdFileMajorMinorVersion { |
| 75 | + private final File file; |
| 76 | + |
| 77 | + private final MajorMinorVersion version; |
| 78 | + |
| 79 | + private XsdFileMajorMinorVersion(File file, MajorMinorVersion version) { |
| 80 | + this.file = file; |
| 81 | + this.version = version; |
| 82 | + } |
| 83 | + |
| 84 | + private static final Pattern FILE_MAJOR_MINOR_VERSION_PATTERN = Pattern.compile("^spring-security-(\\d+)\\.(\\d+)\\.xsd$"); |
| 85 | + |
| 86 | + /** |
| 87 | + * If matches xsd with major minor version (e.g. spring-security-5.1.xsd returns it, otherwise null |
| 88 | + * @param file |
| 89 | + * @return |
| 90 | + */ |
| 91 | + static XsdFileMajorMinorVersion create(File file) { |
| 92 | + String fileName = file.getName(); |
| 93 | + Matcher matcher = FILE_MAJOR_MINOR_VERSION_PATTERN.matcher(fileName); |
| 94 | + if (!matcher.find()) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + int major = Integer.parseInt(matcher.group(1)); |
| 98 | + int minor = Integer.parseInt(matcher.group(2)); |
| 99 | + MajorMinorVersion version = new MajorMinorVersion(major, minor); |
| 100 | + return new XsdFileMajorMinorVersion(file, version); |
| 101 | + } |
| 102 | + |
| 103 | + public File getFile() { |
| 104 | + return file; |
| 105 | + } |
| 106 | + |
| 107 | + public MajorMinorVersion getVersion() { |
| 108 | + return version; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + static class MajorMinorVersion { |
| 113 | + private final int major; |
| 114 | + |
| 115 | + private final int minor; |
| 116 | + |
| 117 | + MajorMinorVersion(int major, int minor) { |
| 118 | + this.major = major; |
| 119 | + this.minor = minor; |
| 120 | + } |
| 121 | + |
| 122 | + public int getMajor() { |
| 123 | + return major; |
| 124 | + } |
| 125 | + |
| 126 | + public int getMinor() { |
| 127 | + return minor; |
| 128 | + } |
| 129 | + |
| 130 | + public boolean isGreaterThan(MajorMinorVersion version) { |
| 131 | + if (getMajor() > version.getMajor()) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + if (getMajor() < version.getMajor()) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + if (getMinor() > version.getMinor()) { |
| 138 | + return true; |
| 139 | + } |
| 140 | + if (getMinor() < version.getMinor()) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + // they are equal |
| 144 | + return false; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments