/* * Copyright 2004-present the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package s101; import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlAnchor; import com.gargoylesoftware.htmlunit.html.HtmlPage; import org.gradle.api.Project; import org.gradle.api.provider.Property; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.InputDirectory; public class S101PluginExtension { private final Property licenseId; private final Property repository; private final Property version; private final Property installationDirectory; private final Property configurationDirectory; private final Property label; @Input public Property getLicenseId() { return this.licenseId; } public void setLicenseId(String licenseId) { this.licenseId.set(licenseId); } @InputDirectory public Property getInstallationDirectory() { return this.installationDirectory; } public void setInstallationDirectory(String installationDirectory) { this.installationDirectory.set(new File(installationDirectory)); } @InputDirectory public Property getConfigurationDirectory() { return this.configurationDirectory; } public void setConfigurationDirectory(String configurationDirectory) { this.configurationDirectory.set(new File(configurationDirectory)); } @Input public Property getLabel() { return this.label; } public void setLabel(String label) { this.label.set(label); } @Input public Property getRepository() { return repository; } public void setRepository(String repository) { this.repository.set(repository); } @Input public Property getVersion() { return this.version; } public void setVersion(String version) { this.version.set(version); } public S101PluginExtension(Project project) { this.licenseId = project.getObjects().property(String.class); if (project.hasProperty("s101.licenseId")) { setLicenseId((String) project.findProperty("s101.licenseId")); } this.installationDirectory = project.getObjects().property(File.class) .convention(new File(project.getBuildDir(), "s101")); this.configurationDirectory = project.getObjects().property(File.class) .convention(new File(project.getProjectDir(), "s101")); this.label = project.getObjects().property(String.class); if (project.hasProperty("s101.label")) { setLabel((String) project.findProperty("s101.label")); } this.repository = project.getObjects().property(String.class); if (project.hasProperty("s101.repository")) { setRepository((String) project.findProperty("s101.repository")); } else { setRepository("https://structure101.com/binaries/v6"); } this.version = project.getObjects().property(String.class); if (project.hasProperty("s101.version")) { setVersion((String) project.findProperty("s101.version")); } else { try (final WebClient webClient = new WebClient()) { HtmlPage page = webClient.getPage(getRepository().get()); Matcher matcher = null; for (HtmlAnchor anchor : page.getAnchors()) { Matcher candidate = Pattern.compile("(structure101-build-java-all-)(.*).zip").matcher(anchor.getHrefAttribute()); if (candidate.find()) { matcher = candidate; } } if (matcher != null) { setVersion(matcher.group(2)); } } catch (Exception ex) { throw new RuntimeException(ex); } } } }