-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.gradle
138 lines (122 loc) · 4.48 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
import org.apache.http.entity.mime.MultipartEntity
import org.apache.http.entity.mime.HttpMultipartMode
import org.apache.http.entity.mime.content.FileBody
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.*
import static groovyx.net.http.ContentType.*
import com.eriwen.gradle.js.tasks.MinifyJsTask
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.apache.httpcomponents:httpmime:4.5.2'
classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
}
}
plugins {
id "com.eriwen.gradle.js" version "2.14.1"
}
// set the properties accordingly
project.ext {
baseDir = projectDir
uiDir = "${baseDir}/ui"
libDir = "${baseDir}/lib"
configDir = "${baseDir}/metadata.xml"
entitiesDir = "${baseDir}/Entities"
zipDir = "${baseDir}/zip"
packageVendor = "ThingWorx Customer Service"
thingworxServerRoot = "http://localhost:8016"
thingworxUser = "Administrator"
thingworxPass = "trUf6yuz2?_Gub"
skipJsFiles = ["**/libthingload.js", "**/three.js"]
}
task increaseVersionNumber() {
def file = "${baseDir}/metadata.xml"
def parser = new groovy.util.XmlParser(false,true);
def xml = parser.parse(file)
def currentVersion = xml.ExtensionPackages.ExtensionPackage.@packageVersion[0];
def versionComponents = currentVersion.split('\\.');
def minorVersion = ++Integer.parseInt(versionComponents[versionComponents.length - 1]);
versionComponents[versionComponents.length - 1] = String.valueOf(minorVersion);
version = String.join('.', versionComponents)
xml.ExtensionPackages.ExtensionPackage.@packageVersion = version
println 'Updated to version ' + project.version
PrintWriter pw = new PrintWriter(new File(file))
pw.write(groovy.xml.XmlUtil.serialize(xml))
pw.close()
}
task copyUiFiles {
doLast {
copy {
from uiDir
into "${buildDir}/zip/ui/"
}
if (project.hasProperty('buildRelease')) {
fileTree("${buildDir}/zip/ui/") {include '**/*.js' exclude project.ext.skipJsFiles } .eachWithIndex { jsFile, index ->
def dynamicTask = "minify$index"
task "$dynamicTask" (type: MinifyJsTask) {
source = jsFile
dest = jsFile
closure {
compilationLevel = 'SIMPLE_OPTIMIZATIONS'
warningLevel = 'QUIET'
compilerOptions = [
languageIn: 'ECMASCRIPT6',
languageOut: 'ECMASCRIPT5'
]
}
}
tasks."$dynamicTask".execute()
}
}
}
}
task prepPackage(dependsOn: ['increaseVersionNumber', 'copyUiFiles']) {
delete project.ext.zipDir
delete "${buildDir}/zip/"
doLast {
// add the configuration
copy {
from "${project.ext.configDir}"
into "${buildDir}/zip/"
}
// add the entities
copy {
from entitiesDir
into "${buildDir}/zip/Entities/"
}
}
}
task packageExtension(type: Zip, dependsOn: prepPackage, overwrite: true) {
version = project.version
baseName = project.name
extension = "zip"
appendix = project.hasProperty('buildRelease') ? "release" : "dev"
destinationDir = new File(project.ext.zipDir)
from "${buildDir}/zip/"
}
task upload(dependsOn: packageExtension) {
doLast{
def http = new HTTPBuilder("${thingworxServerRoot}/Thingworx/")
def basicAuth = "${thingworxUser}:${thingworxPass}".bytes.encodeBase64().toString()
http.setHeaders ([Authorization: "Basic ${basicAuth}"])
def extZip = file("${baseDir}/zip/${project.name}-${project.hasProperty('buildRelease') ? "release": "dev" }-${version}.zip")
http.request(POST) { req ->
uri.path = 'ExtensionPackageUploader'
uri.query = ['purpose': 'import']
headers."X-XSRF-TOKEN" = "TWX-XSRF-TOKEN-VALUE"
requestContentType = 'multipart/form-data'
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
entity.addPart('file', new FileBody(extZip))
req.setEntity(entity)
response.success = {resp ->
println("Upload successful!")
}
response.failure = {resp ->
println(resp.statusLine)
throw new StopExecutionException("Thingworx upload failed! See server response above")
}
}
}
}