-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
358 lines (320 loc) · 15.8 KB
/
Copy pathmain.js
File metadata and controls
358 lines (320 loc) · 15.8 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
const { spawn } = require('child_process');
const { DefaultArtifactClient } = require('@actions/artifact');
const core = require('@actions/core');
const fs = require('fs');
const os = require('os');
// Function to check the platform the Node.js application is running on
function checkPlatform() {
// Returns a Promise, which is an asynchronous operation
return new Promise((resolve, reject) => {
// Gets the current platform using the 'os' module
const platform = os.platform();
// Checks if the platform is one of the supported platforms: Linux, macOS, or Windows
if (!['linux', 'darwin', 'win32'].includes(platform)) {
// If the platform is not supported, logs an error message to the console
console.error(`Unsupported Platform: ${platform}.`);
// Rejects the Promise with an error message indicating the unsupported platform
reject(`Unsupported Platform: ${platform}`);
} else {
// If the platform is supported, resolves the Promise
resolve();
}
});
}
// Function to check the version of Python installed on the system
function checkPythonVersion() {
// Returns a Promise, which is an asynchronous operation
return new Promise((resolve, reject) => {
// Logs a message indicating that Python version is being checked
console.log('Checking Python version.');
// Spawns a child process to execute the 'python --version' command
const pythonProcess = spawn('python', ['--version']);
let output = '';
// Event listener for capturing standard output from the Python process
pythonProcess.stdout.on('data', (data) => {
// Appends the output from the Python process to the 'output' variable
output += data.toString();
});
// Event listener for capturing standard error from the Python process
pythonProcess.stderr.on('data', (data) => {
// Logs an error message to the console
console.error(`Error occurred: ${data.toString()}`);
// Rejects the Promise with the error message
reject(data.toString());
});
// Event listener for handling the close event of the Python process
pythonProcess.on('close', (code) => {
// Checks if the Python process exited with a non-zero exit code
if (code !== 0) {
// Logs an error message indicating failure to check Python version
console.error(`Failed to check Python version with code ${code}.`);
// Rejects the Promise with the exit code
reject(code);
} else {
// Logs a success message indicating that Python version was checked successfully
console.log('Python version checked successfully.');
// Resolves the Promise with the trimmed output from the Python process
resolve(output.trim());
}
});
// Event listener for handling errors that occur during spawning of the Python process
pythonProcess.on('error', (error) => {
// Logs an error message to the console
console.error(`Error occurred: ${error.message}.`);
// Rejects the Promise with the error object
reject(error);
});
});
}
// Function to upgrade pip (Python package manager)
function upgradePip() {
// Returns a Promise for asynchronous operation
return new Promise((resolve, reject) => {
// Executes a function to check Python version asynchronously
checkPythonVersion().then((output) => {
// Logs the output of Python version to the console
console.log('Python version: ', output);
// Logs a message indicating pip upgrade process initiation
console.log('Upgrading pip.');
let pipCommand;
// Gets the current platform using the 'os' module
const platform = os.platform();
// Determines the appropriate pip upgrade command based on platform and Python version
if (platform === 'darwin' && output.startsWith('Python 3.10')) {
pipCommand = 'python3.10 -m pip install --upgrade pip';
} else {
pipCommand = 'pip install --upgrade pip';
}
// Spawns a new process to execute the pip upgrade command
const pipProcess = spawn('sh', ['-c', pipCommand], { stdio: 'inherit' });
// Listens for the 'close' event emitted when the pip process terminates
pipProcess.on('close', (code) => {
// Checks if the pip process terminated with a non-zero exit code
if (code !== 0) {
// Logs an error message if pip upgrade failed
console.error(`Failed to upgrade pip with code ${code}.`);
// Rejects the Promise with the exit code
reject(code);
} else {
// Resolves the Promise if pip upgrade succeeds
resolve();
}
});
// Listens for the 'error' event emitted if an error occurs during pip process execution
pipProcess.on('error', (error) => {
// Logs an error message if an error occurs
console.error(`Error occurred: ${error.message}.`);
// Rejects the Promise with the error object
reject(error);
});
}).catch((error) => {
// Logs an error message if there's an error in checking Python version
console.error(`Error occurred: ${error.message}.`);
// Rejects the Promise with the error object
reject(error);
});
});
}
// Function to install a tool using pip
function installTool(tool) {
// Returns a Promise, which is an asynchronous operation
return new Promise((resolve, reject) => {
// Logs a message indicating the tool being installed
console.log(`Installing ${tool}.`);
// Spawns a child process to execute the pip install command for the specified tool
const pipProcess = spawn('pip', ['install', tool], { stdio: 'inherit' });
// Listens for the 'close' event emitted when the pip process exits
pipProcess.on('close', (code) => {
// Checks if the pip process exited with a non-zero exit code
if (code !== 0) {
// Logs an error message indicating the failure to install the tool
console.error(`Failed to install ${tool} with code ${code}.`);
// Rejects the Promise with the exit code
reject(code);
} else {
// If the pip process exited successfully (with exit code 0), logs a success message
console.log(`${tool} installed successfully.`);
// Resolves the Promise
resolve();
}
});
// Listens for the 'error' event emitted if an error occurs with the pip process
pipProcess.on('error', (error) => {
// Logs an error message indicating the error that occurred
console.error(`Error occurred: ${error.message}.`);
// Rejects the Promise with the error object
reject(error);
});
});
}
// Function to run linting using different tools
function runLinting(tool, path, artifactName, verbose, color, statistics, arguments) {
// Returns a Promise, which is an asynchronous operation
return new Promise((resolve, reject) => {
// Logs a message indicating which linting tool is being run
console.log(`Running ${tool} linting.`);
let command; // Variable to store the linting command
let lintProcess; // Variable to store the child process for running the linting command
// Switch statement to determine the linting command based on the specified tool
switch (tool) {
case 'flake8': // For Python (flake8) linting
command = `flake8 ${path}`;
if (verbose) command += ' --verbose';
if (color) command += ' --color auto';
if (statistics) command += ' --count --statistics';
if (arguments.trim() !== '') command += ` ${arguments.trim()}`;
command += ` --format=default --output-file=${artifactName}`;
break;
case 'pylint': // For Python (pylint) linting
command = `pylint ${path}`;
if (verbose) command += ' -v';
if (color) command += ' --output-format=colorized';
if (statistics) command += " --msg-template='{path}:{line}:{column}: {msg_id} {msg} [{symbol}]'";
if (arguments.trim() !== '') command += ` ${arguments.trim()}`;
command += ` --reports=y --exit-zero > ${artifactName}`;
break;
case 'pycodestyle': // For Python (pycodestyle) linting
command = `pycodestyle ${path}`;
if (verbose) command += ' --verbose';
if (statistics) command += " --count --statistics";
if (arguments.trim() !== '') command += ` ${arguments.trim()}`;
command += ` --format=default > ${artifactName}`;
break;
case 'pyflakes': // For Python (pyflakes) linting
if (arguments.trim() !== '') command += ` ${arguments.trim()}`;
command = `pyflakes ${path} > ${artifactName}`;
break;
case 'black': // For Python (black) linting
command = `black ${path}`;
if (verbose) command += ' --verbose';
if (color) command += ' --color';
if (arguments.trim() !== '') command += ` ${arguments.trim()}`;
command += ` > ${artifactName}`;
break;
case 'mypy': // For Python (mypy) linting
command = `mypy ${path}`;
if (verbose) command += ' --verbose';
if (color) command += ' --color-output';
if (arguments.trim() !== '') command += ` ${arguments.trim()}`;
command += ` --show-error-codes --html-report .`;
break;
default:
// If an unsupported tool is specified, log an error and reject the Promise
console.error(`Unsupported tool: ${tool}`);
reject(`Unsupported tool: ${tool}`);
return;
}
// Logs the linting command that will be executed
console.log(command);
// Spawns a child process to execute the linting command
lintProcess = spawn('sh', ['-c', command], { stdio: 'inherit' });
// Event listener for when the linting process is closed
lintProcess.on('close', (code) => {
// Checks if the lint process exited with a non-zero exit code
if (code !== 0) {
// If the linting process exits with a non-zero code, log an error and reject the Promise
console.error(`Failed to run ${tool} with code ${code}.`);
reject(code);
} else {
// If the linting process exits successfully (code 0), log success message and resolve the Promise
console.log(`${tool} linting completed.`);
resolve();
}
});
// Event listener for errors that occur during the linting process
lintProcess.on('error', (error) => {
// Logs the error message and rejects the Promise
console.error(`Error occurred: ${error.message}.`);
reject(error);
});
});
}
// Function to upload an artifact
function uploadArtifact(artifactName) {
// Returns a Promise, which is an asynchronous operation
return new Promise((resolve, reject) => {
let content; // Variable to store the content of the artifact
try {
// Reads the content of the artifact synchronously
content = fs.readFileSync(artifactName, 'utf-8');
// Creates a new instance of the DefaultArtifactClient class
const artifactClient = new DefaultArtifactClient();
// Specifies the list of files to upload
const files = [artifactName];
// Uploads the artifact using the artifact client
const { id, size } = artifactClient.uploadArtifact(artifactName, files, '.');
// If the upload is successful, resolves the Promise
resolve();
} catch (error) {
// If an error occurs during the process, logs an error message to the console
console.error(`Error occurred: ${error.message}.`);
// Rejects the Promise with the error object
reject(error);
}
});
}
// Function to rename a file asynchronously
function renameFile(sourceName, targetName) {
// Returns a Promise, which is an asynchronous operation
return new Promise((resolve, reject) => {
// Uses the 'fs' module to rename the file from 'sourceName' to 'targetName'
fs.rename(sourceName, targetName, (error) => {
// Checks if an error occurred during the renaming process
if (error) {
// If an error occurred, logs an error message to the console
console.error(`Failed to rename file: ${error}`);
// Rejects the Promise with the error object
reject(error);
} else {
// If renaming is successful, logs a success message to the console
console.log(`File renamed successfully from ${sourceName} to ${targetName}`);
// Resolves the Promise
resolve();
}
});
});
}
// Asynchronous function that serves as the main entry point for the script
async function main() {
try {
// Waits for the platform check to complete before proceeding
await checkPlatform();
// Retrieves input values from GitHub Actions workflow
const tool = core.getInput('tool');
const path = core.getInput('path');
const artifactName = core.getInput('artifact-name');
const verbose = ['true', 'True', 'TRUE'].includes(core.getInput('verbose'));
const color = ['true', 'True', 'TRUE'].includes(core.getInput('color'));
const statistics = ['true', 'True', 'TRUE'].includes(core.getInput('statistics'));
const arguments = core.getInput('arguments');
// Checks if the specified linting tool is supported
if (!['flake8', 'pylint', 'pycodestyle', 'pyflakes', 'black', 'mypy'].includes(tool)) {
// Outputs a warning message if the linting tool is unsupported
core.warning(`Unsupported Linting Tool: ${tool}`);
// Exits the process with a success status code
process.exit(0);
}
// Ensures pip is upgraded before installing any Python packages
await upgradePip();
// Installs the specified linting tool and its dependencies
await installTool(tool);
// Special case for mypy: installs lxml as a dependency
if (tool == 'mypy') {
await installTool('lxml');
}
// Runs linting using the specified tool and parameters
await runLinting(tool, path, artifactName, verbose, color, statistics, arguments);
// Special case for mypy: renames the generated HTML report
if (tool == 'mypy') {
await renameFile('index.html', artifactName);
}
// Uploads the linting report artifact to GitHub Actions
await uploadArtifact(artifactName);
} catch (error) {
// Handles any errors that occur during the execution of the script
core.error(error);
// Exits the process with a failure status code
process.exit(1);
}
}
main();