S101PluginExtension.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2004-present 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. package s101;
  17. import java.io.File;
  18. import java.util.regex.Matcher;
  19. import java.util.regex.Pattern;
  20. import com.gargoylesoftware.htmlunit.WebClient;
  21. import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
  22. import com.gargoylesoftware.htmlunit.html.HtmlPage;
  23. import org.gradle.api.Project;
  24. import org.gradle.api.provider.Property;
  25. import org.gradle.api.tasks.Input;
  26. import org.gradle.api.tasks.InputDirectory;
  27. public class S101PluginExtension {
  28. private final Property<String> licenseId;
  29. private final Property<String> repository;
  30. private final Property<String> version;
  31. private final Property<File> installationDirectory;
  32. private final Property<File> configurationDirectory;
  33. private final Property<String> label;
  34. @Input
  35. public Property<String> getLicenseId() {
  36. return this.licenseId;
  37. }
  38. public void setLicenseId(String licenseId) {
  39. this.licenseId.set(licenseId);
  40. }
  41. @InputDirectory
  42. public Property<File> getInstallationDirectory() {
  43. return this.installationDirectory;
  44. }
  45. public void setInstallationDirectory(String installationDirectory) {
  46. this.installationDirectory.set(new File(installationDirectory));
  47. }
  48. @InputDirectory
  49. public Property<File> getConfigurationDirectory() {
  50. return this.configurationDirectory;
  51. }
  52. public void setConfigurationDirectory(String configurationDirectory) {
  53. this.configurationDirectory.set(new File(configurationDirectory));
  54. }
  55. @Input
  56. public Property<String> getLabel() {
  57. return this.label;
  58. }
  59. public void setLabel(String label) {
  60. this.label.set(label);
  61. }
  62. @Input
  63. public Property<String> getRepository() {
  64. return repository;
  65. }
  66. public void setRepository(String repository) {
  67. this.repository.set(repository);
  68. }
  69. @Input
  70. public Property<String> getVersion() {
  71. return this.version;
  72. }
  73. public void setVersion(String version) {
  74. this.version.set(version);
  75. }
  76. public S101PluginExtension(Project project) {
  77. this.licenseId = project.getObjects().property(String.class);
  78. if (project.hasProperty("s101.licenseId")) {
  79. setLicenseId((String) project.findProperty("s101.licenseId"));
  80. }
  81. this.installationDirectory = project.getObjects().property(File.class)
  82. .convention(new File(project.getBuildDir(), "s101"));
  83. this.configurationDirectory = project.getObjects().property(File.class)
  84. .convention(new File(project.getProjectDir(), "s101"));
  85. this.label = project.getObjects().property(String.class);
  86. if (project.hasProperty("s101.label")) {
  87. setLabel((String) project.findProperty("s101.label"));
  88. }
  89. this.repository = project.getObjects().property(String.class);
  90. if (project.hasProperty("s101.repository")) {
  91. setRepository((String) project.findProperty("s101.repository"));
  92. } else {
  93. setRepository("https://structure101.com/binaries/v6");
  94. }
  95. this.version = project.getObjects().property(String.class);
  96. if (project.hasProperty("s101.version")) {
  97. setVersion((String) project.findProperty("s101.version"));
  98. } else {
  99. try (final WebClient webClient = new WebClient()) {
  100. HtmlPage page = webClient.getPage(getRepository().get());
  101. Matcher matcher = null;
  102. for (HtmlAnchor anchor : page.getAnchors()) {
  103. Matcher candidate = Pattern.compile("(structure101-build-java-all-)(.*).zip").matcher(anchor.getHrefAttribute());
  104. if (candidate.find()) {
  105. matcher = candidate;
  106. }
  107. }
  108. if (matcher != null) {
  109. setVersion(matcher.group(2));
  110. }
  111. } catch (Exception ex) {
  112. throw new RuntimeException(ex);
  113. }
  114. }
  115. }
  116. }