/
AbstractUploader.java
265 lines (224 loc) · 7.39 KB
/
AbstractUploader.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
263
264
265
/*
* 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 java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.jreleaser.util.Constants.KEY_ARTIFACT_ARCHIVE_FORMAT;
import static org.jreleaser.util.Constants.KEY_ARTIFACT_FILE_NAME;
import static org.jreleaser.util.Constants.KEY_ARTIFACT_NAME;
import static org.jreleaser.util.Constants.KEY_ARTIFACT_PLATFORM;
import static org.jreleaser.util.StringUtils.capitalize;
import static org.jreleaser.util.StringUtils.getClassNameForLowerCaseHyphenSeparatedName;
import static org.jreleaser.util.StringUtils.getFilename;
import static org.jreleaser.util.StringUtils.isNotBlank;
/**
* @author Andres Almiray
* @since 0.3.0
*/
abstract class AbstractUploader implements Uploader {
protected final String type;
private final Map<String, Object> extraProperties = new LinkedHashMap<>();
protected String name;
protected boolean enabled;
protected Active active;
private int connectTimeout;
private int readTimeout;
private Boolean artifacts;
private Boolean files;
private Boolean signatures;
protected AbstractUploader(String type) {
this.type = type;
}
void setAll(AbstractUploader uploader) {
this.active = uploader.active;
this.enabled = uploader.enabled;
this.name = uploader.name;
this.connectTimeout = uploader.connectTimeout;
this.readTimeout = uploader.readTimeout;
this.artifacts = uploader.artifacts;
this.files = uploader.files;
this.signatures = uploader.signatures;
setExtraProperties(uploader.extraProperties);
}
@Override
public String getPrefix() {
return name;
}
@Override
public boolean isEnabled() {
return enabled;
}
public void disable() {
active = Active.NEVER;
enabled = false;
}
public boolean resolveEnabled(Project project) {
if (null == active) {
active = Active.NEVER;
}
enabled = active.check(project);
if (project.isSnapshot() && !isSnapshotSupported()) {
enabled = false;
}
return enabled;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public Active getActive() {
return active;
}
@Override
public void setActive(Active active) {
this.active = active;
}
@Override
public void setActive(String str) {
this.active = Active.of(str);
}
@Override
public boolean isActiveSet() {
return active != null;
}
@Override
public boolean isSnapshotSupported() {
return true;
}
@Override
public String getType() {
return type;
}
@Override
public int getConnectTimeout() {
return connectTimeout;
}
@Override
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
@Override
public int getReadTimeout() {
return readTimeout;
}
@Override
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
@Override
public Map<String, Object> getExtraProperties() {
return extraProperties;
}
@Override
public void setExtraProperties(Map<String, Object> extraProperties) {
this.extraProperties.clear();
this.extraProperties.putAll(extraProperties);
}
@Override
public void addExtraProperties(Map<String, Object> extraProperties) {
this.extraProperties.putAll(extraProperties);
}
@Override
public Boolean isArtifacts() {
return artifacts == null || artifacts;
}
@Override
public void setArtifacts(Boolean artifacts) {
this.artifacts = artifacts;
}
@Override
public boolean isArtifactsSet() {
return artifacts != null;
}
@Override
public Boolean isFiles() {
return files == null || files;
}
@Override
public void setFiles(Boolean files) {
this.files = files;
}
@Override
public boolean isFilesSet() {
return files != null;
}
@Override
public Boolean isSignatures() {
return signatures == null || signatures;
}
@Override
public void setSignatures(Boolean signatures) {
this.signatures = signatures;
}
@Override
public boolean isSignaturesSet() {
return signatures != null;
}
@Override
public Map<String, Object> asMap(boolean full) {
if (!full && !isEnabled()) return Collections.emptyMap();
Map<String, Object> props = new LinkedHashMap<>();
props.put("enabled", isEnabled());
props.put("active", active);
props.put("connectTimeout", connectTimeout);
props.put("readTimeout", readTimeout);
props.put("artifacts", isArtifacts());
props.put("files", isFiles());
props.put("signatures", isSignatures());
asMap(props, full);
props.put("extraProperties", getResolvedExtraProperties());
Map<String, Object> map = new LinkedHashMap<>();
map.put(this.getName(), props);
return map;
}
protected abstract void asMap(Map<String, Object> props, boolean full);
@Override
public List<String> resolveSkipKeys() {
String skipUpload = "skipUpload";
String skipUploadByType = skipUpload + capitalize(type);
String skipUploadByName = skipUploadByType + getClassNameForLowerCaseHyphenSeparatedName(name);
return Arrays.asList(skipUpload, skipUploadByType, skipUploadByName);
}
@Override
public Map<String, Object> artifactProps(JReleaserContext context, Artifact artifact) {
Map<String, Object> props = context.props();
String platform = isNotBlank(artifact.getPlatform()) ? artifact.getPlatform() : "";
// add extra properties without clobbering existing keys
Map<String, Object> artifactProps = artifact.getResolvedExtraProperties("artifact");
artifactProps.keySet().stream()
.filter(k -> !props.containsKey(k))
.filter(k -> !k.startsWith("artifactSkip"))
.forEach(k -> props.put(k, artifactProps.get(k)));
String artifactFileName = artifact.getEffectivePath(context).getFileName().toString();
String filename = getFilename(artifactFileName, FileType.getSupportedExtensions());
props.put(KEY_ARTIFACT_PLATFORM, platform);
props.put(KEY_ARTIFACT_FILE_NAME, artifactFileName);
props.put(KEY_ARTIFACT_NAME, filename);
props.put(KEY_ARTIFACT_ARCHIVE_FORMAT, artifactFileName.substring(filename.length()) + 1);
return props;
}
}