This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.gradle
160 lines (133 loc) · 6.37 KB
/
build.gradle
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
import com.synopsys.integration.log.IntLogger
import com.synopsys.integration.log.Slf4jIntLogger
import com.synopsys.integration.rest.HttpMethod
import com.synopsys.integration.rest.body.FileBodyContent
import com.synopsys.integration.rest.client.IntHttpClient
import com.synopsys.integration.rest.proxy.ProxyInfo
import com.synopsys.integration.rest.request.Request
import com.synopsys.integration.rest.request.Response
import org.apache.commons.lang3.StringUtils
import org.apache.http.auth.AuthScope
import org.apache.http.auth.Credentials
import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.client.CredentialsProvider
import org.slf4j.LoggerFactory
import java.nio.charset.StandardCharsets
import java.time.Instant
buildscript {
ext {
springBootVersion = '2.0.3.RELEASE'
}
repositories {
jcenter()
mavenCentral()
maven { url 'https://plugins.gradle.org/m2/' }
}
dependencies {
classpath 'com.blackducksoftware.integration:common-gradle-plugin:0.0.+'
classpath 'com.blackducksoftware.integration:integration-rest:0.8.3'
classpath 'com.blackducksoftware.integration:integration-common:15.4.0'
}
}
group = 'com.synopsys.integration'
version = '1.0.2-SNAPSHOT'
apply plugin: 'com.blackducksoftware.integration.simple'
final String RELEASE_REPO = "scripts-release"
final String SNAPSHOT_REPO = "scripts-snapshot"
final File shellScriptFile = new File("${buildDir}/detect.sh")
final File shellScriptVersionedFile = new File("${buildDir}/detect-${version}.sh")
final File powershellScriptFile = new File("${buildDir}/detect.ps1")
final File powershellScriptVersionedFile = new File("${buildDir}/detect-${version}.ps1")
build {
doLast {
final File shellScriptTemplateFile = new File("${projectDir}/script-templates/detect-sh")
buildScript(shellScriptTemplateFile, shellScriptFile)
buildScript(shellScriptTemplateFile, shellScriptVersionedFile)
final File powershellScriptTemplateFile = new File("${projectDir}/script-templates/detect-ps")
buildScript(powershellScriptTemplateFile, powershellScriptFile)
buildScript(powershellScriptTemplateFile, powershellScriptVersionedFile)
}
}
tasks.register("deploy") {
doLast {
if (version.contains("SNAPSHOT")) {
// Don't deploy un-versioned files to snapshot because those are production scripts
deployToArtifactory(shellScriptVersionedFile, SNAPSHOT_REPO)
deployToArtifactory(powershellScriptVersionedFile, SNAPSHOT_REPO)
} else {
deployToArtifactory(shellScriptFile, SNAPSHOT_REPO)
deployToArtifactory(powershellScriptFile, SNAPSHOT_REPO)
deployToArtifactory(shellScriptVersionedFile, RELEASE_REPO)
deployToArtifactory(powershellScriptVersionedFile, RELEASE_REPO)
}
}
}
private void deployToArtifactory(final File file, final String repoPath) {
final IntLogger intLogger = new Slf4jIntLogger(LoggerFactory.getLogger("deployToArtifactory"))
final IntHttpClient intHttpClient = new IntHttpClient(intLogger, 200, true, ProxyInfo.NO_PROXY_INFO)
final CredentialsProvider credentialsProvider = intHttpClient.getCredentialsProvider()
final Credentials credentials = new UsernamePasswordCredentials(project.ext.artifactoryDeployerUsername, project.ext.artifactoryDeployerPassword)
credentialsProvider.setCredentials(AuthScope.ANY, credentials)
final String artifactoryUrl = "${project.ext.artifactoryUrl}/artifactory/$repoPath/${file.getName()}"
final Request request = new Request.Builder()
.uri(artifactoryUrl)
.method(HttpMethod.PUT)
.bodyContent(new FileBodyContent(file))
.build()
final Response response = intHttpClient.execute(request)
println response.getContentString(StandardCharsets.UTF_8)
response.close()
}
private void buildScript(final File scriptTemplateFile, final File outputFile) {
final String VERSION_TOKEN = '//SCRIPT_VERSION//'
final String BUILD_DATE_TOKEN = '//BUILD_DATE//'
final String MAJOR_VERSIONS_TOKEN = '//DETECT_MAJOR_VERSIONS//'
String scriptContents = scriptTemplateFile.getText('UTF-8')
scriptContents = scriptContents.replaceAll(VERSION_TOKEN, version.toString())
final Date date = Date.from(Instant.now())
scriptContents = scriptContents.replaceAll(BUILD_DATE_TOKEN, date.format("YYYY-MM-dd"))
final String majorVersionsCommentBlock = formatDetectPropertyTags(fetchDetectPropertyTags())
scriptContents = scriptContents.replace(MAJOR_VERSIONS_TOKEN, majorVersionsCommentBlock)
outputFile.delete()
outputFile << scriptContents
outputFile.setExecutable(true)
}
private String formatDetectPropertyTags(final List<String> detectPropertyTags) {
final int MAX_COMMENT_CHARACTERS = 55
final StringBuilder result = new StringBuilder()
String line = "#"
for (int i = 0; i < detectPropertyTags.size(); i++) {
final String tag = detectPropertyTags.get(i)
String newText = " $tag"
final boolean nextTagExists = i + 1 < detectPropertyTags.size()
if (nextTagExists) {
newText += ","
}
if (line.size() + newText.size() < MAX_COMMENT_CHARACTERS) {
line = line + newText
} else {
result.append(line)
result.append(System.lineSeparator())
line = "#" + newText
}
}
if (StringUtils.isNotBlank(line)) {
result.append(line)
}
return result.toString()
}
private List<String> fetchDetectPropertyTags() {
final String artifactoryUrl = "https://repo.blackducksoftware.com/artifactory/api/storage/bds-integrations-release/com/synopsys/integration/synopsys-detect?properties"
final IntLogger intLogger = new Slf4jIntLogger(LoggerFactory.getLogger("fetchDetectPropertyTagLines"))
final IntHttpClient intHttpClient = new IntHttpClient(intLogger, 200, true, ProxyInfo.NO_PROXY_INFO)
final Request request = new Request.Builder().uri(artifactoryUrl).build()
final List<String> propertyTags = new ArrayList<>()
intHttpClient.execute(request).withCloseable { response ->
final String responseContent = response.getContentString(StandardCharsets.UTF_8)
final List<String> tags = responseContent.findAll("\"DETECT_LATEST.*?\"")
for (final String tag : tags) {
propertyTags.add(tag.replace("\"", ""))
}
}
return propertyTags
}