-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallerWindow.cpp
More file actions
337 lines (283 loc) · 10.1 KB
/
Copy pathInstallerWindow.cpp
File metadata and controls
337 lines (283 loc) · 10.1 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
/* InstallerWindow.cpp – HaiClaude Installer */
#include "InstallerWindow.h"
#include <Application.h>
#include <LayoutBuilder.h>
#include <Message.h>
#include <Messenger.h>
#include <Rect.h>
#include <String.h>
#include <pthread.h>
#include <cstdio>
#include <cstring>
// ---------------------------------------------------------------------------
// Command thread data
// ---------------------------------------------------------------------------
struct CommandData {
BMessenger messenger;
BString command;
uint32 doneMsg;
};
static void*
command_thread(void* arg)
{
CommandData* data = (CommandData*)arg;
// Send log messages back
BMessage logMsg(MSG_LOG_APPEND);
FILE* fp = popen(data->command.String(), "r");
if (!fp) {
BMessage resultMsg(data->doneMsg);
resultMsg.AddBool("success", false);
resultMsg.AddString("output", "Failed to run command");
data->messenger.SendMessage(&resultMsg);
delete data;
return nullptr;
}
BString output;
char buf[256];
while (fgets(buf, sizeof(buf), fp)) {
output << buf;
// Send each line to log view
BMessage lineMsg(MSG_LOG_APPEND);
lineMsg.AddString("text", buf);
data->messenger.SendMessage(&lineMsg);
}
int exitCode = pclose(fp);
BMessage resultMsg(data->doneMsg);
resultMsg.AddBool("success", exitCode == 0);
resultMsg.AddString("output", output);
data->messenger.SendMessage(&resultMsg);
delete data;
return nullptr;
}
// ---------------------------------------------------------------------------
// InstallerWindow
// ---------------------------------------------------------------------------
InstallerWindow::InstallerWindow()
: BWindow(BRect(100, 100, 550, 400),
"Claude Code Installer",
B_TITLED_WINDOW,
B_QUIT_ON_WINDOW_CLOSE | B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS)
, fState(StateIdle)
, fClaudeInstalled(false)
, fNpmInstalled(false)
{
fStatusLabel = new BStringView("status", "Ready to install Claude Code");
fStatusLabel->SetFontSize(14);
fDetailLabel = new BStringView("detail", "Click Install to begin.");
BStringView* logLabel = new BStringView("logLabel", "Installation log:");
fLogView = new BTextView("log");
fLogView->MakeEditable(false);
fLogScroll = new BScrollView("logScroll", fLogView, B_FRAME_EVENTS | B_WILL_DRAW,
false, true);
fInstallBtn = new BButton("install", "Install", new BMessage(MSG_INSTALL_CLICKED));
fInstallBtn->MakeDefault(true);
fCloseBtn = new BButton("close", "Close", new BMessage(MSG_CLOSE_CLICKED));
BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
.SetInsets(B_USE_DEFAULT_SPACING)
.Add(fStatusLabel)
.Add(fDetailLabel)
.Add(logLabel)
.Add(fLogScroll, 1.0f)
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.AddGlue()
.Add(fInstallBtn)
.Add(fCloseBtn)
.End()
.End();
InvalidateLayout(true);
BSize preferred = GetLayout()->PreferredSize();
ResizeTo(preferred.width, preferred.height);
CenterOnScreen();
}
void
InstallerWindow::MessageReceived(BMessage* msg)
{
switch (msg->what) {
case MSG_INSTALL_CLICKED:
_StartInstallation();
break;
case MSG_CLOSE_CLICKED:
be_app->PostMessage(B_QUIT_REQUESTED);
break;
case MSG_LOG_APPEND: {
BString text;
if (msg->FindString("text", &text) == B_OK) {
_LogAppend(text);
}
break;
}
case MSG_CHECK_CLAUDE_DONE: {
bool success = false;
BString output;
msg->FindBool("success", &success);
msg->FindString("output", &output);
// Check if the claude binary exists and is executable
fClaudeInstalled = output.FindFirst("found") >= 0;
if (fClaudeInstalled) {
_LogAppend("Claude Code 2.1.112 is already installed!\n");
_SetState(StateSuccess);
} else {
_LogAppend("Claude Code 2.1.112 not found (or wrong version). Installing...\n");
_SetState(StateInstallingClaude);
_InstallClaude();
}
break;
}
case MSG_CHECK_NPM_DONE: {
bool success = false;
BString output;
msg->FindBool("success", &success);
msg->FindString("output", &output);
// which returns 0 if found, non-zero if not
fNpmInstalled = success;
if (fNpmInstalled) {
_LogAppend("npm is installed. Checking for Claude Code...\n");
_SetState(StateCheckingClaude);
_CheckClaudeInstalled();
} else {
_LogAppend("npm not found. Installing npm via pkgman...\n");
_SetState(StateInstallingNpm);
_InstallNpm();
}
break;
}
case MSG_INSTALL_NPM_DONE: {
bool success = false;
msg->FindBool("success", &success);
if (success) {
_LogAppend("npm installed successfully. Checking for Claude Code...\n");
_SetState(StateCheckingClaude);
_CheckClaudeInstalled();
} else {
_LogAppend("Failed to install npm. Please check your internet connection.\n");
_SetState(StateError);
}
break;
}
case MSG_INSTALL_CLAUDE_DONE: {
bool success = false;
msg->FindBool("success", &success);
if (success) {
_LogAppend("\nClaude Code installed successfully!\n");
_LogAppend("You can now close this window and use HaiClaude launcher.\n");
_SetState(StateSuccess);
} else {
_LogAppend("\nFailed to install Claude Code. Check the log for details.\n");
_SetState(StateError);
}
break;
}
default:
BWindow::MessageReceived(msg);
}
}
void
InstallerWindow::_StartInstallation()
{
fInstallBtn->SetEnabled(false);
fLogView->SetText("");
_LogAppend("Starting installation...\n");
_LogAppend("Checking for npm...\n");
_SetState(StateCheckingNpm);
_CheckNpmInstalled();
}
void
InstallerWindow::_CheckClaudeInstalled()
{
// 2.1.112 ships as a pure-JS package (bin -> cli.js); just run --version.
_SpawnCommand("/boot/home/.npm-global/bin/claude --version 2>&1 | grep -qF '2.1.112' && echo found 2>&1",
MSG_CHECK_CLAUDE_DONE);
}
void
InstallerWindow::_CheckNpmInstalled()
{
_SpawnCommand("which npm 2>&1", MSG_CHECK_NPM_DONE);
}
void
InstallerWindow::_InstallNpm()
{
_SpawnCommand("pkgman install -y npm nodejs20 2>&1", MSG_INSTALL_NPM_DONE);
}
void
InstallerWindow::_InstallClaude()
{
// Use the exact invocation that works on Haiku: plain npm install without
// --save-exact or --force. The 2.1.112 package on npm is a pure-JS build
// (bin -> cli.js) that runs under Node.js with no native binary required.
// Also drop a profile.d snippet so every new terminal has the bin dir on PATH.
_SpawnCommand("mkdir -p /boot/home/.npm-global && "
"npm config set prefix /boot/home/.npm-global && "
"npm install -g @anthropic-ai/claude-code@2.1.112 2>&1 && "
"if [ ! -f /etc/profile.d/npm-global.sh ]; then "
"printf '# Added by HaiClaude installer\\n"
"export PATH=\"$HOME/.npm-global/bin:$PATH\"\\n' "
"> /etc/profile.d/npm-global.sh && "
"echo 'PATH configured: /etc/profile.d/npm-global.sh created.' ; "
"else "
"echo 'PATH already configured.' ; "
"fi 2>&1",
MSG_INSTALL_CLAUDE_DONE);
}
void
InstallerWindow::_LogAppend(const BString& text)
{
fLogView->Insert(fLogView->TextLength(), text.String(), text.Length());
// Scroll to bottom
fLogView->Select(fLogView->TextLength(), fLogView->TextLength());
fLogView->ScrollToSelection();
}
void
InstallerWindow::_SetState(InstallState state)
{
fState = state;
_UpdateUI();
}
void
InstallerWindow::_UpdateUI()
{
switch (fState) {
case StateIdle:
fStatusLabel->SetText("Ready to install Claude Code");
fDetailLabel->SetText("Click Install to begin.");
fInstallBtn->SetEnabled(true);
break;
case StateCheckingClaude:
fStatusLabel->SetText("Checking Claude Code...");
fDetailLabel->SetText("Verifying existing installation.");
fInstallBtn->SetEnabled(false);
break;
case StateCheckingNpm:
fStatusLabel->SetText("Checking npm...");
fDetailLabel->SetText("Looking for npm package manager.");
break;
case StateInstallingNpm:
fStatusLabel->SetText("Installing npm...");
fDetailLabel->SetText("This may take a few minutes.");
break;
case StateInstallingClaude:
fStatusLabel->SetText("Installing Claude Code...");
fDetailLabel->SetText("Downloading and installing via npm.");
break;
case StateSuccess:
fStatusLabel->SetText("Installation Complete!");
fDetailLabel->SetText("Claude Code is ready to use.");
fInstallBtn->SetEnabled(false);
break;
case StateError:
fStatusLabel->SetText("Installation Failed");
fDetailLabel->SetText("Check the log for error details.");
fInstallBtn->SetEnabled(true);
break;
}
}
void
InstallerWindow::_SpawnCommand(const char* cmd, uint32 doneMsg)
{
CommandData* data = new CommandData;
data->messenger = BMessenger(this);
data->command = cmd;
data->doneMsg = doneMsg;
pthread_t thread;
pthread_create(&thread, nullptr, command_thread, data);
pthread_detach(thread);
}