-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
286 lines (244 loc) · 12.2 KB
/
install.js
File metadata and controls
286 lines (244 loc) · 12.2 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
/* eslint-disable */
const {execSync} = require("child_process");
const fs = require("fs");
const path = require("path");
const os = require("os");
const readline = require("readline");
const IS_WIN = process.platform === "win32";
const HOME = os.homedir();
let INSTALL = IS_WIN ? "C:\\StaffScheduling" : `${HOME}/StaffScheduling`;
let WEB_DIR = path.join(INSTALL, "StaffSchedulingWeb");
let CLI_DIR = path.join(INSTALL, "StaffScheduling");
let CASES_DIR = path.join(CLI_DIR, "cases");
const run = (cmd, cwd) => execSync(cmd, {cwd, stdio: "inherit"});
const rl = readline.createInterface({input: process.stdin, output: process.stdout});
const ask = (q) => new Promise((resolve) => rl.question(q, resolve));
// ── Hilfsfunktionen ──────────────────────────────────────────────────────────
function checkTool(name) {
try {
execSync(`${name} --version`, {stdio: "pipe"});
return true;
} catch {
return false;
}
}
function pickFolder(title, defaultPath = "") {
try {
if (IS_WIN) {
const tmpScript = path.join(os.tmpdir(), "_pick_folder.ps1");
fs.writeFileSync(tmpScript, `
Add-Type -AssemblyName System.Windows.Forms
$d = New-Object System.Windows.Forms.FolderBrowserDialog
$d.Description = '${title}'
$d.ShowNewFolderButton = $true
$d.SelectedPath = '${defaultPath}'
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
if ($d.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
Write-Output $d.SelectedPath
}
`.trim(), "utf8");
const result = execSync(
`powershell -STA -ExecutionPolicy Bypass -File "${tmpScript}"`,
{stdio: "pipe"}
).toString().trim();
fs.unlinkSync(tmpScript);
return result || null;
} else if (process.platform === "darwin") {
// ✅ "default location" setzt den Startordner im Finder
const defaultClause = defaultPath
? `default location (POSIX file "${defaultPath}")`
: "";
return execSync(
`osascript -e 'POSIX path of (choose folder with prompt "${title}" ${defaultClause})'`,
{stdio: "pipe"}
).toString().trim().replace(/\/$/, "") || null;
} else {
if (!process.env.DISPLAY && !process.env.WAYLAND_DISPLAY) return null;
if (checkTool("zenity"))
// ✅ --filename setzt Startverzeichnis bei zenity
return execSync(
`zenity --file-selection --directory --title="${title}" --filename="${defaultPath}/"`,
{stdio: "pipe"}
).toString().trim() || null;
if (checkTool("kdialog"))
// ✅ Pfad direkt als Argument
return execSync(
`kdialog --getexistingdirectory "${defaultPath}" --title "${title}"`,
{stdio: "pipe"}
).toString().trim() || null;
}
} catch {
}
return null;
}
async function askPath(prompt, defaultVal, openFolder) {
// ✅ defaultVal wird jetzt an den Picker weitergegeben
const picked = pickFolder(prompt, openFolder);
if (picked) {
console.log(` Ausgewählt: ${picked}`);
return picked;
}
const input = await ask(` ${prompt} [${defaultVal}]: `);
return input.trim() || defaultVal;
}
function syncRepo(dir, url, branch) {
if (fs.existsSync(dir)) {
if (fs.existsSync(path.join(dir, ".git"))) {
console.log(` Aktualisiere ${dir}...`);
// Switch to the target branch before pulling updates.
if (branch) run(`git checkout ${branch}`, dir);
run(`git pull origin ${branch ?? "main"}`, dir);
} else {
// Check if Folder is empty
const files = fs.readdirSync(dir);
if (files.length === 0) {
console.log(` Klone ${url} nach ${dir}...`);
run(`git clone ${url} ${dir}`);
if (branch) run(`git checkout ${branch}`, dir);
} else {
// Ask before cloning into a non-empty directory that is not a Git repository.
console.log(` Ordner ${dir} existiert bereits und ist kein Git-Repo.`);
console.log(` Inhalt: ${files.join(", ")}`);
const doClone = ask(` Trotzdem klonen und vorhandene Dateien überschreiben? (j/n): `);
if (doClone.trim().toLowerCase() === "j") {
fs.rmSync(dir, {recursive: true, force: true});
console.log(` Klone ${url} nach ${dir}...`);
run(`git clone ${url} ${dir}`);
if (branch) run(`git checkout ${branch}`, dir);
}
}
}
} else {
console.log(` Klone ${url} nach ${dir}...`);
run(`git clone ${url} ${dir}`);
if (branch) run(`git checkout ${branch}`, dir);
}
}
function isRepo(dir, url) {
try {
const remote = execSync("git config --get remote.origin.url", {cwd: dir, stdio: "pipe"})
.toString().trim();
return remote === url;
} catch {
return false;
}
}
// ── main ─────────────────────────────────────────────────────────────────────
async function main() {
// ── 1. Prerequisites ──────────────────────────────────────────────────────
console.log("\n==> Prüfe Voraussetzungen...");
if (!checkTool("git")) throw new Error("Git fehlt. Bitte installieren: https://git-scm.com");
if (!checkTool("node")) throw new Error("Node.js fehlt. Bitte installieren: https://nodejs.org");
if (!checkTool("uv")) {
console.log(" uv nicht gefunden, installiere...");
if (IS_WIN) run(`powershell -c "irm https://astral.sh/uv/install.ps1 | iex"`);
else run(`curl -LsSf https://astral.sh/uv/install.sh | sh`);
// Refresh PATH in the current process after installing uv.
process.env.PATH += IS_WIN
? `;${HOME}\\.local\\bin`
: `:${HOME}/.local/bin`;
if (!checkTool("uv")) throw new Error("uv konnte nicht installiert werden. Bitte manuell installieren: https://docs.astral.sh/uv");
}
console.log(" ✓ Alle Voraussetzungen erfüllt");
// ── 2. Define installation paths ──────────────────────────────────────────
console.log("\n==> Installationspfade festlegen...");
console.log(" (Es öffnen sich Ordner-Dialoge. Einfach Abbrechen drücken für den Standardpfad)\n");
// Allow the user to override the default installation paths.
WEB_DIR = await askPath("Ordner für Web-GUI waehlen", WEB_DIR, INSTALL);
CLI_DIR = await askPath("Ordner für Python Solver waehlen", CLI_DIR, WEB_DIR);
CASES_DIR = path.join(CLI_DIR, "cases");
fs.mkdirSync(WEB_DIR, {recursive: true});
fs.mkdirSync(CLI_DIR, {recursive: true});
// ── 3. Web-GUI klonen / updaten ───────────────────────────────────────────
console.log("\n==> Web-GUI synchronisieren...");
syncRepo(WEB_DIR, "https://github.com/Julian466/StaffSchedulingWeb.git");
// ── 4. Solver klonen / updaten ────────────────────────────────────────────
console.log("\n==> Solver synchronisieren...");
let solverPath = CLI_DIR;
const solverAlreadyValid =
(fs.existsSync(path.join(CLI_DIR, "pyproject.toml")) ||
fs.existsSync(path.join(CLI_DIR, "uv.lock"))) &&
isRepo(CLI_DIR, "https://github.com/CombiRWTH/StaffScheduling.git");
if (solverAlreadyValid) {
// The solver is already installed, so only offer an update.
console.log(` ✓ Solver gefunden: ${CLI_DIR}`);
const doUpdate = await ask(" Auf neueste Version aktualisieren? (j/n): ");
if (doUpdate.trim().toLowerCase() === "j") {
run("git checkout feature/add_api_interface", CLI_DIR);
run("git pull origin feature/add_api_interface", CLI_DIR);
}
} else {
console.log(" Solver wird installiert...");
syncRepo(CLI_DIR, "https://github.com/CombiRWTH/StaffScheduling.git", "feature/add_api_interface");
}
rl.close();
// ── 5. Dependencies installieren ─────────────────────────────────────────
console.log("\n==> Installiere Web-Abhängigkeiten...");
run("npm ci", WEB_DIR);
console.log("\n==> Installiere Solver-Abhängigkeiten...");
run("uv sync", solverPath);
// ── 6. Build ──────────────────────────────────────────────────────────────
console.log("\n==> Baue Web-Applikation... (das kann einige Minuten dauern)");
run("npm run build", WEB_DIR);
console.log(" ✓ Build erfolgreich");
// ── 7. config.json ────────────────────────────────────────────────────────
console.log("\n==> config.json...");
const configPath = path.join(WEB_DIR, "config.json");
let writeConfig = true;
if (fs.existsSync(configPath)) {
const rl2 = readline.createInterface({input: process.stdin, output: process.stdout});
const ask2 = (q) => new Promise((resolve) => rl2.question(q, resolve));
const overwrite = await ask2(" config.json existiert bereits. Überschreiben? (j/n): ");
rl2.close();
writeConfig = overwrite.trim().toLowerCase() === "j";
}
if (writeConfig) {
const config = {
casesDirectory: CASES_DIR,
staffSchedulingProject: {
include: true,
path: solverPath,
pythonExecutable: "uv",
},
solverApi: {
url: "http://127.0.0.1:8000"
},
};
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf8");
console.log(" ✓ config.json geschrieben:", configPath);
} else {
console.log(" config.json nicht verändert");
}
// ── 8. Zusammenfassung ────────────────────────────────────────────────────
const webHash = execSync("git rev-parse --short HEAD", {cwd: WEB_DIR}).toString().trim();
let solverHash = "n/a";
if (fs.existsSync(path.join(solverPath, ".git"))) {
try {
solverHash = execSync("git rev-parse --short HEAD", {cwd: solverPath}).toString().trim();
} catch { /* not a Git repository */
}
}
// Write a compact version log for troubleshooting.
const log = [
`Installiert: ${new Date().toISOString()}`,
`Web-GUI: ${WEB_DIR} [${webHash}]`,
`Solver: ${solverPath} [${solverHash}]`,
`Cases: ${CASES_DIR}`,
`Node: ${execSync("node --version").toString().trim()}`,
`uv: ${execSync("uv --version").toString().trim()}`,
].join("\n");
fs.writeFileSync(path.join(WEB_DIR, "version.log"), log, "utf8");
console.log(`
╔══════════════════════════════════════════╗
║ Installation erfolgreich abgeschlossen ║
╚══════════════════════════════════════════╝
Web-GUI: ${WEB_DIR} [${webHash}]
Solver: ${solverPath} [${solverHash}]
Cases: ${CASES_DIR}
Support: ${path.join(WEB_DIR, "version.log")}
`);
}
main().catch((err) => {
console.error("\n[FEHLER] Installation fehlgeschlagen:", err.message);
process.exit(1);
});