Browse Source

Support Structure101 License ID

Closes gh-10443
Josh Cummings 3 năm trước cách đây
mục cha
commit
d779cd1d48

+ 2 - 1
.github/workflows/continuous-integration-workflow.yml

@@ -13,6 +13,7 @@ env:
   GRADLE_ENTERPRISE_SECRET_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_SECRET_ACCESS_KEY }}
   COMMIT_OWNER: ${{ github.event.pusher.name }}
   COMMIT_SHA: ${{ github.sha }}
+  STRUCTURE101_LICENSEID: ${{ secrets.STRUCTURE101_LICENSEID }}
   ARTIFACTORY_USERNAME: ${{ secrets.ARTIFACTORY_USERNAME }}
   ARTIFACTORY_PASSWORD: ${{ secrets.ARTIFACTORY_PASSWORD }}
   RUN_JOBS: ${{ github.repository == 'spring-projects/spring-security' }}
@@ -119,7 +120,7 @@ jobs:
           export GRADLE_ENTERPRISE_CACHE_USERNAME="$GRADLE_ENTERPRISE_CACHE_USER"
           export GRADLE_ENTERPRISE_CACHE_PASSWORD="$GRADLE_ENTERPRISE_CACHE_PASSWORD"
           export GRADLE_ENTERPRISE_ACCESS_KEY="$GRADLE_ENTERPRISE_SECRET_ACCESS_KEY"
-          ./gradlew check s101 --stacktrace
+          ./gradlew check s101 -Ps101.licenseId="$STRUCTURE101_LICENSEID" --stacktrace
   deploy_artifacts:
     name: Deploy Artifacts
     needs: [build_jdk_11, snapshot_tests, check_samples, check_tangles]

+ 36 - 0
buildSrc/src/main/java/s101/S101Configurer.java

@@ -24,8 +24,11 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
 import java.io.UncheckedIOException;
 import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -66,6 +69,8 @@ public class S101Configurer {
 	private final Mustache hspTemplate;
 	private final Mustache repositoryTemplate;
 
+	private final Path licenseDirectory;
+
 	private final Project project;
 	private final Logger logger;
 
@@ -84,6 +89,37 @@ public class S101Configurer {
 		} catch (IOException ex) {
 			throw new UncheckedIOException(ex);
 		}
+		this.licenseDirectory = new File(System.getProperty("user.home") + "/.Structure101/java").toPath();
+	}
+
+	public void license(String licenseId) {
+		Path licenseFile = this.licenseDirectory.resolve(".structure101license.properties");
+		if (needsLicense(licenseFile, licenseId)) {
+			writeLicense(licenseFile, licenseId);
+		}
+	}
+
+	private boolean needsLicense(Path licenseFile, String licenseId) {
+		if (!licenseFile.toFile().exists()) {
+			return true;
+		}
+		try {
+			String license = new String(Files.readAllBytes(licenseFile));
+			return !license.contains(licenseId);
+		} catch (IOException ex) {
+			throw new RuntimeException(ex);
+		}
+	}
+
+	private void writeLicense(Path licenseFile, String licenseId) {
+		if (!this.licenseDirectory.toFile().mkdirs()) {
+			this.licenseDirectory.forEach((path) -> path.toFile().delete());
+		}
+		try (PrintWriter pw = new PrintWriter(licenseFile.toFile())) {
+			pw.println("licensecode=" + licenseId);
+		} catch (IOException ex) {
+			throw new RuntimeException(ex);
+		}
 	}
 
 	public void install(File installationDirectory, File configurationDirectory) {

+ 4 - 1
buildSrc/src/main/java/s101/S101Plugin.java

@@ -57,7 +57,6 @@ public class S101Plugin implements Plugin<Project> {
 				.workingDir(extension.getInstallationDirectory())
 				.classpath(new File(extension.getInstallationDirectory().get(), "structure101-java-build.jar"))
 				.args(new File(new File(project.getBuildDir(), "s101"), "config.xml"))
-				.args("-licensedirectory=" + extension.getLicenseDirectory().get())
 				.systemProperty("s101.label", computeLabel(extension).get())
 				.doFirst((task) -> {
 					installAndConfigureIfNeeded(project);
@@ -80,6 +79,10 @@ public class S101Plugin implements Plugin<Project> {
 	private void installAndConfigureIfNeeded(Project project) {
 		S101Configurer configurer = new S101Configurer(project);
 		S101PluginExtension extension = project.getExtensions().getByType(S101PluginExtension.class);
+		String licenseId = extension.getLicenseId().getOrNull();
+		if (licenseId != null) {
+			configurer.license(licenseId);
+		}
 		File installationDirectory = extension.getInstallationDirectory().get();
 		File configurationDirectory = extension.getConfigurationDirectory().get();
 		if (!installationDirectory.exists()) {

+ 10 - 8
buildSrc/src/main/java/s101/S101PluginExtension.java

@@ -24,18 +24,18 @@ import org.gradle.api.tasks.Input;
 import org.gradle.api.tasks.InputDirectory;
 
 public class S101PluginExtension {
-	private final Property<File> licenseDirectory;
+	private final Property<String> licenseId;
 	private final Property<File> installationDirectory;
 	private final Property<File> configurationDirectory;
 	private final Property<String> label;
 
-	@InputDirectory
-	public Property<File> getLicenseDirectory() {
-		return this.licenseDirectory;
+	@Input
+	public Property<String> getLicenseId() {
+		return this.licenseId;
 	}
 
-	public void setLicenseDirectory(String licenseDirectory) {
-		this.licenseDirectory.set(new File(licenseDirectory));
+	public void setLicenseId(String licenseId) {
+		this.licenseId.set(licenseId);
 	}
 
 	@InputDirectory
@@ -66,8 +66,10 @@ public class S101PluginExtension {
 	}
 
 	public S101PluginExtension(Project project) {
-		this.licenseDirectory = project.getObjects().property(File.class)
-				.convention(new File(System.getProperty("user.home") + "/.Structure101/java"));
+		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)