|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.build.cli;
|
18 | 18 |
|
| 19 | +import java.io.File; |
| 20 | +import java.security.MessageDigest; |
19 | 21 | import java.util.Collections;
|
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
20 | 24 |
|
| 25 | +import org.apache.commons.codec.digest.DigestUtils; |
| 26 | +import org.gradle.api.DefaultTask; |
| 27 | +import org.gradle.api.Project; |
21 | 28 | import org.gradle.api.Task;
|
| 29 | +import org.gradle.api.file.RegularFile; |
| 30 | +import org.gradle.api.provider.Provider; |
| 31 | +import org.gradle.api.tasks.InputFile; |
| 32 | +import org.gradle.api.tasks.OutputDirectory; |
| 33 | +import org.gradle.api.tasks.PathSensitive; |
| 34 | +import org.gradle.api.tasks.PathSensitivity; |
22 | 35 | import org.gradle.api.tasks.TaskAction;
|
| 36 | +import org.gradle.api.tasks.TaskExecutionException; |
| 37 | + |
| 38 | +import org.springframework.boot.build.artifactory.ArtifactoryRepository; |
23 | 39 |
|
24 | 40 | /**
|
25 | 41 | * A {@link Task} for creating a Homebrew formula manifest.
|
26 | 42 | *
|
27 | 43 | * @author Andy Wilkinson
|
28 | 44 | */
|
29 |
| -public class HomebrewFormula extends AbstractPackageManagerDefinitionTask { |
| 45 | +public class HomebrewFormula extends DefaultTask { |
| 46 | + |
| 47 | + private static final String SPRING_REPO = "https://repo.spring.io/%s"; |
| 48 | + |
| 49 | + private static final String MAVEN_REPO = "https://repo1.maven.org/maven2"; |
| 50 | + |
| 51 | + private Provider<RegularFile> archive; |
| 52 | + |
| 53 | + private File template; |
| 54 | + |
| 55 | + private File outputDir; |
| 56 | + |
| 57 | + public HomebrewFormula() { |
| 58 | + getInputs().property("version", getProject().provider(getProject()::getVersion)); |
| 59 | + } |
| 60 | + |
| 61 | + @InputFile |
| 62 | + @PathSensitive(PathSensitivity.RELATIVE) |
| 63 | + public RegularFile getArchive() { |
| 64 | + return this.archive.get(); |
| 65 | + } |
| 66 | + |
| 67 | + public void setArchive(Provider<RegularFile> archive) { |
| 68 | + this.archive = archive; |
| 69 | + } |
| 70 | + |
| 71 | + @InputFile |
| 72 | + @PathSensitive(PathSensitivity.RELATIVE) |
| 73 | + public File getTemplate() { |
| 74 | + return this.template; |
| 75 | + } |
| 76 | + |
| 77 | + public void setTemplate(File template) { |
| 78 | + this.template = template; |
| 79 | + } |
| 80 | + |
| 81 | + @OutputDirectory |
| 82 | + public File getOutputDir() { |
| 83 | + return this.outputDir; |
| 84 | + } |
| 85 | + |
| 86 | + public void setOutputDir(File outputDir) { |
| 87 | + this.outputDir = outputDir; |
| 88 | + } |
| 89 | + |
| 90 | + protected void createDescriptor(Map<String, Object> additionalProperties) { |
| 91 | + getProject().copy((copy) -> { |
| 92 | + copy.from(this.template); |
| 93 | + copy.into(this.outputDir); |
| 94 | + copy.expand(getProperties(additionalProperties)); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + private Map<String, Object> getProperties(Map<String, Object> additionalProperties) { |
| 99 | + Map<String, Object> properties = new HashMap<>(additionalProperties); |
| 100 | + Project project = getProject(); |
| 101 | + properties.put("hash", sha256(this.archive.get().getAsFile())); |
| 102 | + properties.put("repo", getRepo(project)); |
| 103 | + properties.put("project", project); |
| 104 | + return properties; |
| 105 | + } |
| 106 | + |
| 107 | + private String sha256(File file) { |
| 108 | + try { |
| 109 | + MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 110 | + return new DigestUtils(digest).digestAsHex(file); |
| 111 | + } |
| 112 | + catch (Exception ex) { |
| 113 | + throw new TaskExecutionException(this, ex); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private String getRepo(Project project) { |
| 118 | + ArtifactoryRepository artifactoryRepo = ArtifactoryRepository.forProject(project); |
| 119 | + return (!artifactoryRepo.isRelease()) ? String.format(SPRING_REPO, artifactoryRepo.getName()) : MAVEN_REPO; |
| 120 | + } |
30 | 121 |
|
31 | 122 | @TaskAction
|
32 | 123 | void createFormula() {
|
|
0 commit comments