|
1 |
| -// scripts/run.js |
2 | 1 | const core = require('@actions/core');
|
3 | 2 | const { execSync } = require('child_process');
|
4 | 3 | const path = require('path');
|
5 | 4 |
|
6 | 5 | async function main() {
|
7 |
| - try { |
8 |
| - const out = execSync('ls out/src/Day*.class 2>/dev/null | sort -V', { |
9 |
| - encoding: 'utf-8', |
10 |
| - }).trim(); |
11 |
| - |
12 |
| - const classFiles = out ? out.split('\n') : []; |
13 |
| - if (classFiles.length === 0) { |
14 |
| - console.log('No compiled classes named "Day*.class" found in out/src.'); |
15 |
| - return; |
| 6 | + try { |
| 7 | + const classFiles = findDayClassFiles(); |
| 8 | + if (classFiles.length === 0) { |
| 9 | + console.log('No compiled classes named "Day*.class" found in out/src.'); |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + const resultsByDay = {}; |
| 14 | + for (const filePath of classFiles) { |
| 15 | + const { dayNum, result } = runDayClass(filePath); |
| 16 | + if (!dayNum) |
| 17 | + continue; |
| 18 | + if (!resultsByDay[dayNum]) resultsByDay[dayNum] = []; |
| 19 | + resultsByDay[dayNum].push(result); |
| 20 | + } |
| 21 | + |
| 22 | + buildSummary(resultsByDay); |
| 23 | + } catch (err) { |
| 24 | + core.setFailed(`Script error: ${err.message}`); |
16 | 25 | }
|
| 26 | +} |
17 | 27 |
|
18 |
| - // 2) We'll store results in a map from day => array of results |
19 |
| - // For each day, we keep { className, status, durationMs } |
20 |
| - const resultsByDay = {}; |
21 |
| - |
22 |
| - // 3) Run each class, measure time |
23 |
| - for (const filePath of classFiles) { |
24 |
| - const className = path.basename(filePath, '.class'); // e.g. "Day10Part2" |
25 |
| - const match = className.match(/^Day(\d+)/); |
26 |
| - if (!match) continue; |
27 |
| - |
28 |
| - const dayNum = match[1]; |
| 28 | +function findDayClassFiles() { |
| 29 | + let listing = ''; |
| 30 | + try { |
| 31 | + listing = execSync('ls out/src/Day*.class 2>/dev/null | sort -V', { |
| 32 | + encoding: 'utf-8', |
| 33 | + }).trim(); |
| 34 | + } catch { } |
| 35 | + return listing ? listing.split('\n') : []; |
| 36 | +} |
29 | 37 |
|
30 |
| - // Ensure there's a bucket for this day |
31 |
| - if (!resultsByDay[dayNum]) { |
32 |
| - resultsByDay[dayNum] = []; |
33 |
| - } |
| 38 | +function runDayClass(filePath) { |
| 39 | + const className = path.basename(filePath, '.class'); |
| 40 | + const match = className.match(/^Day(\d+)/); |
| 41 | + if (!match) return { dayNum: null, result: null }; |
34 | 42 |
|
35 |
| - console.log(`Running ${className} (Day ${dayNum})...`); |
36 |
| - const start = Date.now(); |
| 43 | + const dayNum = match[1]; |
| 44 | + console.log(`Running ${className} (Day ${dayNum})...`); |
37 | 45 |
|
38 |
| - let exitCode = 0; |
39 |
| - let stdout = ''; |
40 |
| - try { |
| 46 | + const start = Date.now(); |
| 47 | + let exitCode = 0; |
| 48 | + let stdout = ''; |
| 49 | + try { |
41 | 50 | stdout = execSync(`java -cp out/src ${className}`, { encoding: 'utf-8' });
|
42 |
| - } catch (error) { |
| 51 | + } catch (error) { |
43 | 52 | exitCode = error.status || 1;
|
44 | 53 | stdout = error.stdout?.toString() || '';
|
45 |
| - } |
46 |
| - const end = Date.now(); |
47 |
| - const durationMs = end - start; |
| 54 | + } |
| 55 | + const duration = Date.now() - start; |
48 | 56 |
|
49 |
| - console.log(stdout); |
50 |
| - const statusMsg = exitCode === 0 ? 'Success' : 'Failed'; |
51 |
| - console.log(`${statusMsg} [${durationMs} ms]\n`); |
| 57 | + console.log(stdout); |
| 58 | + const status = exitCode === 0 ? 'Success' : 'Failed'; |
| 59 | + console.log(`${status} [${duration} ms]\n`); |
52 | 60 |
|
53 |
| - // Store the info for this day |
54 |
| - resultsByDay[dayNum].push({ |
55 |
| - className, |
56 |
| - status: statusMsg, |
57 |
| - duration: durationMs, |
58 |
| - }); |
59 |
| - } |
| 61 | + return { |
| 62 | + dayNum, |
| 63 | + result: { className, status, duration }, |
| 64 | + }; |
| 65 | +} |
60 | 66 |
|
61 |
| - // 4) Now build the Summary content day by day |
62 |
| - // Instead of one big table, we create one table *per* day |
| 67 | +function buildSummary(resultsByDay) { |
63 | 68 | core.summary.addHeading('Build & Run Java Files - Summary', 2);
|
64 | 69 |
|
65 |
| - // Sort the days numerically |
66 |
| - const sortedDays = Object.keys(resultsByDay).sort((a, b) => Number(a) - Number(b)); |
67 |
| - |
| 70 | + const sortedDays = Object.keys(resultsByDay).sort((a, b) => +a - +b); |
68 | 71 | for (const day of sortedDays) {
|
69 |
| - core.summary.addHeading(`Day ${day}`, 3); |
70 |
| - |
71 |
| - // Build a small table for this day |
72 |
| - const tableData = [ |
73 |
| - [ |
74 |
| - { data: 'Class Name', header: true }, |
75 |
| - { data: 'Status', header: true }, |
76 |
| - { data: 'Duration (ms)', header: true }, |
77 |
| - ], |
78 |
| - ]; |
79 |
| - |
80 |
| - for (const row of resultsByDay[day]) { |
81 |
| - tableData.push([ |
82 |
| - `\`${row.className}\``, |
83 |
| - row.status, |
84 |
| - row.duration.toString(), |
85 |
| - ]); |
86 |
| - } |
87 |
| - |
88 |
| - core.summary.addTable(tableData); |
| 72 | + core.summary.addHeading(`Day ${day}`, 3); |
| 73 | + const tableData = [ |
| 74 | + [ |
| 75 | + { data: 'Class Name', header: true }, |
| 76 | + { data: 'Status', header: true }, |
| 77 | + { data: 'Duration (ms)', header: true }, |
| 78 | + ], |
| 79 | + ]; |
| 80 | + for (const row of resultsByDay[day]) { |
| 81 | + tableData.push([`\`${row.className}\``, row.status, row.duration.toString()]); |
| 82 | + } |
| 83 | + core.summary.addTable(tableData); |
89 | 84 | }
|
90 | 85 |
|
91 |
| - await core.summary.write(); |
92 |
| - |
93 |
| - } catch (err) { |
94 |
| - core.setFailed(`Script error: ${err.message}`); |
95 |
| - } |
| 86 | + return core.summary.write(); |
96 | 87 | }
|
97 | 88 |
|
98 | 89 | main();
|
0 commit comments