-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathimport-libGD.js
More file actions
235 lines (209 loc) · 7.29 KB
/
Copy pathimport-libGD.js
File metadata and controls
235 lines (209 loc) · 7.29 KB
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
const shell = require('shelljs');
const { downloadLocalFile } = require('./lib/DownloadLocalFile');
const path = require('path');
const sourceDirectory = '../../../Binaries/embuild/GDevelop.js';
const destinationTestDirectory = '../node_modules/libGD.js-for-tests-only';
const alreadyHasLibGdJs =
shell.test('-f', '../public/libGD.js') &&
shell.test('-f', '../public/libGD.wasm') &&
shell.test('-f', destinationTestDirectory + '/index.js') &&
shell.test('-f', destinationTestDirectory + '/libGD.wasm');
if (shell.mkdir('-p', destinationTestDirectory).stderr) {
shell.echo('❌ Error while creating node_modules folder for libGD.js');
}
if (shell.test('-f', path.join(sourceDirectory, 'libGD.js'))) {
shell.echo(
'ℹ️ Copying libGD.js and associated files built locally to newIDE...'
);
const copyToNewIDEScriptPath = path.join(
__dirname,
'..',
'..',
'..',
'GDevelop.js',
'scripts',
'copy-to-newIDE.js'
);
shell.exec(`node ${copyToNewIDEScriptPath}`);
} else {
// Download a pre-built version otherwise
shell.echo(
'🌐 Downloading pre-built libGD.js from https://s3.amazonaws.com/gdevelop-gdevelop.js (be patient)...'
);
const getBranchFromGitRef = gitRef => {
const branchShellString = shell.exec(
`git rev-parse --abbrev-ref "${gitRef}"`,
{
silent: true,
}
);
if (branchShellString.stderr || branchShellString.code) {
return null;
}
let branch = (branchShellString.stdout || '').trim();
if (branch === 'HEAD') {
// We're in detached HEAD. Try to read the branch from the CI environment variables.
if (process.env.APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH) {
branch = process.env.APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH;
} else if (process.env.APPVEYOR_REPO_BRANCH) {
branch = process.env.APPVEYOR_REPO_BRANCH;
}
}
if (!branch) {
shell.echo(
`⚠️ Can't find the branch of the associated commit - if you're in detached HEAD, you need to be on a branch instead.`
);
return 'unknown-branch';
}
return branch;
};
const getHashFromGitRef = gitRef => {
const hashShellString = shell.exec(`git rev-parse "${gitRef}"`, {
silent: true,
});
const hash = (hashShellString.stdout || 'unknown-hash').trim();
if (hashShellString.stderr || hashShellString.code) {
shell.echo(`⚠️ Can't find the hash of the associated commit.`);
return null;
}
return hash;
};
// Try to download libGD.js from a specific commit on the inferred branch.
const downloadCommitLibGdJs = gitRef =>
new Promise((resolve, reject) => {
shell.echo(`ℹ️ Trying to download libGD.js for ${gitRef}.`);
const hash = getHashFromGitRef(gitRef);
const branch = getBranchFromGitRef(gitRef);
if (!hash || !branch) {
shell.echo(
`⚠️ Can't find the hash or branch of the associated commit.`
);
reject();
return;
}
resolve(
downloadLibGdJs(
`https://s3.amazonaws.com/gdevelop-gdevelop.js/${branch}/commit/${hash}`
)
);
});
const downloadMasterCommitLibGdJs = gitRef =>
new Promise((resolve, reject) => {
shell.echo(
`ℹ️ Trying to download libGD.js for ${gitRef} from master.`
);
const hash = getHashFromGitRef(gitRef);
if (!hash) {
reject();
return;
}
resolve(
downloadLibGdJs(
`https://s3.amazonaws.com/gdevelop-gdevelop.js/master/commit/${hash}`
)
);
});
// Try to download libGD.js from the latest version built for master branch.
const downloadBranchLatestLibGdJs = branchName => {
shell.echo(
`ℹ️ Trying to download libGD.js from ${branchName}, latest build.`
);
return downloadLibGdJs(
`https://s3.amazonaws.com/gdevelop-gdevelop.js/${branchName}/latest`
);
};
const downloadLibGdJs = baseUrl =>
Promise.all([
downloadLocalFile(baseUrl + '/libGD.js', '../public/libGD.js'),
downloadLocalFile(baseUrl + '/libGD.wasm', '../public/libGD.wasm'),
]).then(
responses => {},
error => {
if (error.statusCode === 403) {
shell.echo(
`ℹ️ Maybe libGD.js was not automatically built yet, try again in a few minutes.`
);
throw error;
}
if (error.statusCode === 0) {
shell.echo(
`⚠️ Can't download libGD.js (error: ${
error.statusMessage
}) (baseUrl=${baseUrl}), please check your internet connection.`
);
throw error;
}
shell.echo(
`⚠️ Can't download libGD.js (${
error.statusMessage
}) (baseUrl=${baseUrl}), try again later.`
);
throw error;
}
);
const onLibGdJsDownloaded = response => {
shell.echo('✅ libGD.js downloaded and stored in public/libGD.js');
if (
!shell.cp('../public/libGD.js', destinationTestDirectory + '/index.js')
.stderr &&
!shell.cp(
'../public/libGD.wasm',
destinationTestDirectory + '/libGD.wasm'
).stderr
) {
shell.echo('✅ Copied libGD.js to node_modules folder');
} else {
shell.echo('❌ Error while copying libGD.js to node_modules folder');
}
};
const branch = getBranchFromGitRef('HEAD');
const downloadCommitLibGdJsWithMasterFallback = gitRef =>
downloadCommitLibGdJs(gitRef).catch(() =>
downloadMasterCommitLibGdJs(gitRef)
);
const tryDownloadInOrder = downloaders =>
downloaders.reduce(
(previousDownload, download) => previousDownload.catch(() => download()),
Promise.reject()
);
// Try to download the latest libGD.js, fallback to previous or master ones
// if not found (including different parents, for handling of merge commits).
downloadCommitLibGdJsWithMasterFallback('HEAD').then(
onLibGdJsDownloaded,
() => {
// Force the exact version of GDevelop.js to be downloaded for AppVeyor - because
// this means we build the app and we don't want to risk mismatch (Core C++ not up to date
// with the IDE JavaScript).
if (process.env.APPVEYOR || process.env.REQUIRES_EXACT_LIBGD_JS_VERSION) {
shell.echo(
`❌ Can't download the exact required version of libGD.js - check it was built by CircleCI before running this CI.`
);
shell.echo(
`ℹ️ See the pipeline on https://app.circleci.com/pipelines/github/4ian/GDevelop.`
);
shell.exit(1);
}
tryDownloadInOrder([
() => downloadCommitLibGdJsWithMasterFallback('HEAD~1'),
() => downloadCommitLibGdJsWithMasterFallback('HEAD~2'),
() => downloadCommitLibGdJsWithMasterFallback('HEAD~3'),
() => downloadBranchLatestLibGdJs(branch),
() => downloadBranchLatestLibGdJs('master'),
]).then(onLibGdJsDownloaded, () => {
if (alreadyHasLibGdJs) {
shell.echo(
`ℹ️ Can't download any version of libGD.js, assuming you can go ahead with the existing one.`
);
shell.exit(0);
return;
} else {
shell.echo(
`❌ Can't download any version of libGD.js, please check your internet connection.`
);
shell.exit(1);
return;
}
});
}
);
}