-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
146 lines (126 loc) · 3.92 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
def requiredVersionNodeJS = "^18.0.0"
def requiredVersionTsc = "^5.0.0"
allprojects {
ext {
if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
nodeExecutable = Arrays.asList("node")
npmExecutable = Arrays.asList("cmd", "/c", "npm")
tscExecutable = Arrays.asList("cmd", "/c", "tsc")
} else {
nodeExecutable = Arrays.asList("node")
npmExecutable = Arrays.asList("npm")
tscExecutable = Arrays.asList("tsc")
}
}
}
static def isSemVerMatching(String actual, String expected) {
def beginIndex
def equalParts
if (expected.startsWith("^")) {
beginIndex = 1
equalParts = 1
} else if (expected.startsWith("~")) {
beginIndex = 1
equalParts = 2
} else {
beginIndex = 0
equalParts = 3
}
def expectedParts = expected.substring(beginIndex).split(/\./).collect { it as int }
def actualParts = actual.split(/\./).collect { it as int }
for (def i = 0; i < expectedParts.size(); i++) {
if (i < equalParts) {
if (actualParts[i] != expectedParts[i]) {
return false
}
} else {
if (actualParts[i] > expectedParts[i]) {
return true
}
if (actualParts[i] < expectedParts[i]) {
return false
}
}
}
return true
}
def getVersionNodeJS() {
def stdout = new ByteArrayOutputStream()
def stderr = new ByteArrayOutputStream()
try {
exec {
commandLine nodeExecutable
args "--version"
standardOutput stdout
errorOutput stderr
}
} catch (Exception ignored) {
}
return stdout.toString().trim().replace("v", "")
}
def getVersionTsc() {
def stdout = new ByteArrayOutputStream()
def stderr = new ByteArrayOutputStream()
try {
exec {
commandLine tscExecutable
args "--v"
standardOutput stdout
errorOutput stderr
}
} catch (Exception ignored) {
}
return stdout.toString().trim().replace("Version ", "")
}
task verifyNodeJS {
def actualVersion = getVersionNodeJS()
inputs.property("VERSION_NODE_JS", requiredVersionNodeJS)
outputs.upToDateWhen { isSemVerMatching(actualVersion, requiredVersionNodeJS) }
doLast {
if (!isSemVerMatching(actualVersion, requiredVersionNodeJS)) {
throw new Exception("Required Node version " + requiredVersionNodeJS + " not found. Detected version " + actualVersion)
}
}
}
task verifyTsc {
def actualVersion = getVersionTsc()
inputs.property("VERSION_TSC", requiredVersionTsc)
outputs.upToDateWhen { isSemVerMatching(actualVersion, requiredVersionTsc) }
doLast {
if (!isSemVerMatching(actualVersion, requiredVersionTsc)) {
throw new Exception("Required tsc version " + requiredVersionTsc + " not found. Detected version " + actualVersion)
}
}
}
task clean {}
task setNpmRegistryToLocal(type: Exec) {
commandLine npmExecutable
args "set", "registry", "http://localhost:4873/"
}
task setNpmRegistryToPublic(type: Exec) {
commandLine npmExecutable
args "set", "registry", "https://registry.npmjs.org/"
}
afterEvaluate {
// avoiding simultaneous connections to npmjs.com
def prevTask = null
project.subprojects.each {
def task = it.tasks.find { task -> task.name.contains("npmUpdate") }
if (task != null) {
if (prevTask != null) {
task.mustRunAfter(prevTask)
}
prevTask = task
}
}
prevTask = null
project.subprojects.each {
def task = it.tasks.find { task -> task.name.contains("npmInstall") }
if (task != null) {
if (prevTask != null) {
task.mustRunAfter(prevTask)
}
prevTask = task
}
}
}