/
Brew.java
262 lines (223 loc) · 7.63 KB
/
Brew.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2021 The JReleaser 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 org.jreleaser.model;
import org.jreleaser.util.FileType;
import org.jreleaser.util.PlatformUtils;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import static org.jreleaser.util.MustacheUtils.applyTemplate;
import static org.jreleaser.util.StringUtils.getClassNameForLowerCaseHyphenSeparatedName;
import static org.jreleaser.util.StringUtils.isBlank;
import static org.jreleaser.util.StringUtils.isNotBlank;
/**
* @author Andres Almiray
* @since 0.1.0
*/
public class Brew extends AbstractRepositoryTool {
public static final String NAME = "brew";
private final List<Dependency> dependencies = new ArrayList<>();
private final List<String> livecheck = new ArrayList<>();
private final HomebrewTap tap = new HomebrewTap();
private final Cask cask = new Cask();
private String formulaName;
private String cachedFormulaName;
private Boolean multiPlatform;
public Brew() {
super(NAME);
}
void setAll(Brew brew) {
super.setAll(brew);
this.formulaName = brew.formulaName;
this.multiPlatform = brew.multiPlatform;
setTap(brew.tap);
setDependenciesAsList(brew.dependencies);
setLivecheck(brew.livecheck);
setCask(brew.cask);
}
public String getResolvedFormulaName(JReleaserContext context) {
if (isBlank(cachedFormulaName)) {
if (formulaName.contains("{{")) {
cachedFormulaName = applyTemplate(formulaName, context.props());
} else {
cachedFormulaName = formulaName;
}
cachedFormulaName = getClassNameForLowerCaseHyphenSeparatedName(cachedFormulaName);
}
return cachedFormulaName;
}
public String getResolvedFormulaName(Map<String, Object> props) {
if (isBlank(cachedFormulaName)) {
if (formulaName.contains("{{")) {
cachedFormulaName = applyTemplate(formulaName, props);
} else {
cachedFormulaName = formulaName;
}
cachedFormulaName = getClassNameForLowerCaseHyphenSeparatedName(cachedFormulaName);
} else if (cachedFormulaName.contains("{{")) {
cachedFormulaName = applyTemplate(cachedFormulaName, props);
cachedFormulaName = getClassNameForLowerCaseHyphenSeparatedName(cachedFormulaName);
}
return cachedFormulaName;
}
public String getFormulaName() {
return formulaName;
}
public void setFormulaName(String formulaName) {
this.formulaName = formulaName;
}
public boolean isMultiPlatform() {
return multiPlatform != null && multiPlatform;
}
public void setMultiPlatform(Boolean multiPlatform) {
this.multiPlatform = multiPlatform;
}
public boolean isMultiPlatformSet() {
return multiPlatform != null;
}
public HomebrewTap getTap() {
return tap;
}
public void setTap(HomebrewTap tap) {
this.tap.setAll(tap);
}
public Cask getCask() {
return cask;
}
public void setCask(Cask cask) {
this.cask.setAll(cask);
}
public void setDependencies(Map<String, String> dependencies) {
if (null == dependencies || dependencies.isEmpty()) {
return;
}
this.dependencies.clear();
dependencies.forEach(this::addDependency);
}
public List<Dependency> getDependenciesAsList() {
return dependencies;
}
public void setDependenciesAsList(List<Dependency> dependencies) {
if (null == dependencies || dependencies.isEmpty()) {
return;
}
this.dependencies.clear();
this.dependencies.addAll(dependencies);
}
public void addDependencies(Map<String, String> dependencies) {
if (null == dependencies || dependencies.isEmpty()) {
return;
}
dependencies.forEach(this::addDependency);
}
public void addDependency(String key, String value) {
dependencies.add(new Dependency(key, value));
}
public void addDependency(String key) {
dependencies.add(new Dependency(key));
}
public List<String> getLivecheck() {
return livecheck;
}
public void setLivecheck(List<String> livecheck) {
this.livecheck.clear();
this.livecheck.addAll(livecheck);
}
public boolean hasLivecheck() {
return !livecheck.isEmpty();
}
@Override
protected void asMap(boolean full, Map<String, Object> props) {
super.asMap(full, props);
props.put("formulaName", formulaName);
props.put("multiPlatform", isMultiPlatform());
props.put("tap", tap.asMap(full));
props.put("dependencies", dependencies);
props.put("livecheck", livecheck);
props.put("cask", cask.asMap(full));
}
@Override
public RepositoryTap getRepositoryTap() {
return tap;
}
@Override
public boolean supportsPlatform(String platform) {
if (isMultiPlatform()) {
return (isBlank(platform) || PlatformUtils.isMac(platform) || PlatformUtils.isLinux(platform)) &&
!PlatformUtils.isAlpineLinux(platform);
}
return isBlank(platform) || PlatformUtils.isMac(platform);
}
@Override
public Set<String> getSupportedExtensions() {
Set<String> set = new LinkedHashSet<>();
set.add(FileType.DMG.extension());
set.add(FileType.PKG.extension());
set.add(FileType.ZIP.extension());
set.add(FileType.JAR.extension());
return set;
}
public static class Dependency {
private final String key;
private final String value;
private Dependency(String key) {
this(key, null);
}
private Dependency(String key, String value) {
this.key = key;
this.value = isBlank(value) || "null".equalsIgnoreCase(value) ? null : value;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
@Override
public String toString() {
StringBuilder formatted = new StringBuilder();
if (key.startsWith(":")) {
formatted.append(key);
} else {
formatted.append("\"")
.append(key)
.append("\"");
}
if (isNotBlank(value)) {
formatted.append(" => \"")
.append(value)
.append("\"");
}
return formatted.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dependency that = (Dependency) o;
return key.equals(that.key);
}
@Override
public int hashCode() {
return Objects.hash(key);
}
}
}