-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
185 lines (130 loc) · 5.13 KB
/
index.js
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
const fetch = require('node-fetch');
const parseStringPromise = require('xml2js').parseStringPromise;
const core = require('@actions/core');
const fs = require('fs');
const { spawn } = require('child_process');
const process = require('process');
let version = core.getInput('minecraftVersion');
let loaderVersion = '';
let yarnVersion = '';
let minecraftVersion = '';
let fabricVersion = '';
let properties = [];
async function run() {
if(version === "latest") {
const yarnResp = await fetch('https://meta.fabricmc.net/v1/versions/', {
method: 'get',
credentials: 'include',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json',
}
});
const yarnJson = await yarnResp.json();
// eslint-disable-next-line require-atomic-updates
version = yarnJson.game[0].version;
}
const loaderResp = await fetch('https://meta.fabricmc.net/v1/versions/loader/' + version, {
method: 'get',
credentials: 'include',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json',
}
});
const json = await loaderResp.json();
console.log('[ACTION] Requesting data via HTTPS GET from https://meta.fabricmc.net/v1/versions/loader/' + version + '\n')
yarnVersion = json[0].mappings.version;
minecraftVersion = json[0].mappings.gameVersion;
loaderVersion = json[0].loader.version;
console.log('[ACTION] Minecraft version retrieved! Version ' + minecraftVersion + '\n');
console.log('[ACTION] Yarn version retrieved! Version ' + yarnVersion);
console.log('[ACTION] Fabric Loader version retrieved! Version ' + loaderVersion + '\n');
const mavenResp = await fetch('https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api/maven-metadata.xml', {
method: 'get',
credentials: 'include',
mode: 'no-cors'
});
const text = await mavenResp.text();
console.log('[ACTION] Requesting data via HTTPS GET from https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api/maven-metadata.xml')
await parseStringPromise(text).then((result) => {
let branch = '1.15';
if(minecraftVersion === '1.14.4'){
branch = '1.14';
} else if (minecraftVersion.startsWith('20w')) {
branch = '1.16';
}
const versions = result.metadata.versioning[0];
if(version === 'latest')
fabricVersion = versions.latest;
else {
let latestVersion = versions.release;
for(const version of versions.versions[0].version) {
if(version.endsWith(branch))
latestVersion = version;
}
fabricVersion = latestVersion;
}
console.log('[ACTION] Fabric API version retrieved! Version ' + fabricVersion);
}).catch((err) => {
core.setFailed(err.message);
})
const mod = fs.readFileSync('./src/main/resources/fabric.mod.json')
let modJson = JSON.parse(mod);
let writeJson = Object.assign({}, modJson);
writeJson.depends.fabricloader = "*";
writeJson.depends.minecraft = "*";
fs.writeFileSync('./src/main/resources/fabric.mod.json', JSON.stringify(writeJson));
properties.push("-Pminecraft_version=" + minecraftVersion);
properties.push("-Pyarn_mappings=" + yarnVersion);
properties.push("-Ploader_version=" + loaderVersion);
properties.push("-Pfabric_version=" + fabricVersion);
console.log('[ACTION] Running gradle build test.');
await runBuild(() => {
console.log('[ACTION] Running server test!');
let dir = './run/'
if (!fs.existsSync(dir))
fs.mkdirSync(dir);
fs.writeFile( './run/eula.txt', 'eula=true', { flag: 'wx' }, err => {
if(err)
core.setFailed(err.message);
runServer(() => console.log('[ACTION] All tests have passed!'));
});
})
}
async function runBuild(callback) {
if(!core.getInput('runBuildTest')) return;
let build;
if(process.platform === 'win32')
build = await spawn('cmd', ['/c', 'gradlew', 'build', '--refresh-dependencies'].concat(properties))
else
build = await spawn('./gradlew', ['build', '--refresh-dependencies'].concat(properties))
build.stdout.on('data', (data) => process.stdout.write(`${data}`));
build.stderr.on('data', (data) => process.stderr.write(`${data}`));
build.on('error', (err) => process.stderr.write(err))
// eslint-disable-next-line no-unused-vars
build.on('close', (code) => callback())
}
async function runServer(callback) {
if(!core.getInput('runServerTest')) return;
let server;
if(process.platform === 'win32')
server = await spawn('gradlew', ['runServer', '--args=“nogui”'].concat(properties), { shell: true});
else
server = await spawn('./gradlew', ['runServer', '--args=“nogui”'].concat(properties));
server.stdout.on('data', (data) => {
if (data.includes('For help, type')) {
console.log('[ACTION] Server test complete! Exiting process.')
if(process.platform === 'win32')
spawn("taskkill", ["/pid", server.pid, '/f', '/t']);
else
server.kill();
}
process.stdout.write(`${data}`)
});
server.stderr.on('data', (data) => process.stderr.write(`${data}`));
server.on('error', (err) => process.stderr.write(err))
// eslint-disable-next-line no-unused-vars
server.on('close', (code) => callback())
}
run();