admin
2025-07-17 6cd92a169cbc0db35042f243a09d976fd3e1445c
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// CProjectBuilderDlg.cpp: 实现文件
//
 
#include "common/pch.h"
#include "CProjectBuilderDlg.h"
#include "afxdialogex.h"
#include "resource.h"
#include <regex>
#include "../common/StringUtil.h"
#include "../common/ProjectBuildNetworkApi.h"
#include "../common/JsonUtil.h"
#include "SSHUtil.h"
#include <iostream>
#include <filesystem>
#include <thread>
 
 
 
 
// CProjectBuilderDlg 对话框
 
IMPLEMENT_DYNAMIC(CProjectBuilderDlg, CDialogEx)
int IDS[MAX_PROJECT_COUNT][2] = {
    {IDC_BUTTON_SAVE_1,IDC_BUTTON_BUILD_1},
    {IDC_BUTTON_SAVE_2,IDC_BUTTON_BUILD_2},
    {IDC_BUTTON_SAVE_3,IDC_BUTTON_BUILD_3},
    {IDC_BUTTON_SAVE_4,IDC_BUTTON_BUILD_4},
    {IDC_BUTTON_SAVE_5,IDC_BUTTON_BUILD_5},
    {IDC_BUTTON_SAVE_6,IDC_BUTTON_BUILD_6},
    {IDC_BUTTON_SAVE_7,IDC_BUTTON_BUILD_7},
    {IDC_BUTTON_SAVE_8,IDC_BUTTON_BUILD_8}
};
 
 
CProjectBuilderDlg::CProjectBuilderDlg(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_DIALOG_PROJECT_BUILDER, pParent)
{
 
}
 
CProjectBuilderDlg::~CProjectBuilderDlg()
{
}
 
void CProjectBuilderDlg::saveProjectBuildInfo(int index)
{
    ProjectBuildInfo* buildInfo = getProjectBuildInfo(index);
    if (buildInfo != nullptr) {
        ConfigUtil::setProjectBuildInfo(buildInfo, index);
        MessageBox(L"保存成功", L"提示");
    }
}
 
void CProjectBuilderDlg::createLabel(CStatic* label, CString text, CRect rect)
{
    label->Create(
        text,                 // 文本
        WS_CHILD | WS_VISIBLE | SS_LEFT,  // 样式
        rect,
        this);
    label->SetFont(GetFont());
 
}
 
void CProjectBuilderDlg::createBtn(CButton* btn, CString text, CRect rect, UINT id)
{
    btn->Create(
        text,                 // 文本
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,  // 样式
        rect,
        this, 
        id);
    btn->SetFont(GetFont());
}
 
void CProjectBuilderDlg::createEdit(CEdit* edit, CRect rect, UINT id)
{
    edit->Create(
        WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
        rect,  // 位置和大小
        this,
        id
    );
    edit->SetFont(GetFont());
}
 
void CProjectBuilderDlg::createProjectView(ProjectControls& controls,int index, int start_x,int start_y)
{
    UINT start_id = 10000;
    CStatic *nameLabel=new CStatic(), *dirLabel = new CStatic(), *scriptFileLabel = new CStatic(), *outputDirLabel = new CStatic(), *outputFileLabel = new CStatic(), *targetServerIPLabel = new CStatic(), *targetServerPortLabel = new CStatic(), *targetServerUserNameLabel = new CStatic(), *targetServerPwdLabel = new CStatic(), *targetServerDirLabel = new CStatic(), * targetAppFileLabel = new CStatic(), * targetAppRestartFileLabel = new CStatic();
    CEdit *nameEdit=new CEdit(), *dirEdit = new CEdit(), *scriptFileEdit = new CEdit(), *outputDirEdit = new CEdit(), *outputFileEdit = new CEdit(), *targetServerIPEdit = new CEdit(), *targetServerPortEdit = new CEdit(), *targetServerUserNameEdit = new CEdit(), *targetServerPwdEdit = new CEdit(), *targetServerDirEdit = new CEdit(), * targetAppFileEdit = new CEdit(), *targetAppRestartFileEdit = new CEdit();
    CButton* saveBtn=new CButton(), * buildBtn = new CButton();
    CButton*groupBox=new CButton();
    CStatic *msgLabel = new CStatic();
    INT EDIT_HEIGHT = 23, LABEL_HEIGHT = 14, BUTTON_HEIGHT = 25;
    INT LINE_SPACE = 8;
    CString title = CString("项目");
    title.Append(to_wstring(index + 1).c_str());
    groupBox->Create(
        title,                     // 标题文本
        WS_CHILD | WS_VISIBLE | BS_GROUPBOX, // 样式
        CRect(CPoint(start_x - 10, start_y-15), CSize(550,  210)),            // 位置和大小 (left, top, right, bottom)
        this,
        start_id++
    );
 
    groupBox->SetFont(GetFont());
    int x = start_x, y = start_y;
    createLabel(nameLabel, L"项目名称:", CRect(CPoint(x,y+(EDIT_HEIGHT - LABEL_HEIGHT)/2), CSize(65, LABEL_HEIGHT)));
    createEdit(nameEdit, CRect(CPoint(x + 65, y), CSize(100, EDIT_HEIGHT)), start_id++);
    x = start_x + 180;
    createLabel(dirLabel, L"项目目录:", CRect(CPoint(x, y + (EDIT_HEIGHT - LABEL_HEIGHT) / 2), CSize(65, LABEL_HEIGHT)));
    createEdit(dirEdit, CRect(CPoint(x + 65, y), CSize(280, EDIT_HEIGHT)), start_id++);
 
    x = start_x;
    y += EDIT_HEIGHT + LINE_SPACE;
    createLabel(scriptFileLabel, L"脚本文件:", CRect(CPoint(x, y + (EDIT_HEIGHT - LABEL_HEIGHT) / 2), CSize(65, LABEL_HEIGHT)));
    createEdit(scriptFileEdit, CRect(CPoint(x + 65, y), CSize(100, EDIT_HEIGHT)), start_id++);
    
    x +=  180;
    createLabel(outputDirLabel, L"输出目录:", CRect(CPoint(x, y + (EDIT_HEIGHT - LABEL_HEIGHT) / 2), CSize(65, LABEL_HEIGHT)));
    createEdit(outputDirEdit, CRect(CPoint(x + 65, y), CSize(100, EDIT_HEIGHT)), start_id++);
 
    x += 180;
    createLabel(outputFileLabel, L"输出文件:", CRect(CPoint(x, y + (EDIT_HEIGHT - LABEL_HEIGHT) / 2), CSize(65, LABEL_HEIGHT)));
    createEdit(outputFileEdit, CRect(CPoint(x + 65, y), CSize(100, EDIT_HEIGHT)), start_id++);
 
    x = start_x;
    y += EDIT_HEIGHT + LINE_SPACE;
    createLabel(targetServerIPLabel, L"目标服务器IP:", CRect(CPoint(x, y + (EDIT_HEIGHT - LABEL_HEIGHT) / 2), CSize(85, LABEL_HEIGHT)));
    createEdit(targetServerIPEdit, CRect(CPoint(x + 85, y), CSize(85, EDIT_HEIGHT)), start_id++);
 
    x += 90 + 80 + 5;
    createLabel(targetServerPortLabel, L"目标服务器PORT:", CRect(CPoint(x, y + (EDIT_HEIGHT - LABEL_HEIGHT) / 2), CSize(100, LABEL_HEIGHT)));
    createEdit(targetServerPortEdit, CRect(CPoint(x + 100, y), CSize(50, EDIT_HEIGHT)), start_id++);
    x += 100 + 50 + 5;
    createLabel(targetServerUserNameLabel, L"目标服务器用户名:", CRect(CPoint(x, y + (EDIT_HEIGHT - LABEL_HEIGHT) / 2), CSize(110, LABEL_HEIGHT)));
    createEdit(targetServerUserNameEdit, CRect(CPoint(x + 110, y), CSize(85, EDIT_HEIGHT)), start_id++);
 
    x = start_x;
    y += EDIT_HEIGHT + LINE_SPACE;
    createLabel(targetServerPwdLabel, L"目标服务器密码:", CRect(CPoint(x, y + (EDIT_HEIGHT - LABEL_HEIGHT) / 2), CSize(100, LABEL_HEIGHT)));
    createEdit(targetServerPwdEdit, CRect(CPoint(x + 100, y), CSize(100, EDIT_HEIGHT)), start_id++);
    targetServerPwdEdit->SetPasswordChar('*');
    x += 100 + 100 + 5;
    createLabel(targetServerDirLabel, L"目标服务器目录:", CRect(CPoint(x, y + (EDIT_HEIGHT - LABEL_HEIGHT) / 2), CSize(100, LABEL_HEIGHT)));
    createEdit(targetServerDirEdit, CRect(CPoint(x + 100, y), CSize(220, EDIT_HEIGHT)), start_id++);
    controls.editName = nameEdit;
 
    y += EDIT_HEIGHT + LINE_SPACE;
    x = start_x;
 
    createLabel(targetAppFileLabel, L"应用文件:", CRect(CPoint(x, y + (EDIT_HEIGHT - LABEL_HEIGHT) / 2), CSize(100, LABEL_HEIGHT)));
    createEdit(targetAppFileEdit, CRect(CPoint(x + 100, y), CSize(150, EDIT_HEIGHT)), start_id++);
    targetServerPwdEdit->SetPasswordChar('*');
    x += 100 + 150 + 5;
    createLabel(targetAppRestartFileLabel, L"应用重启脚本:", CRect(CPoint(x, y + (EDIT_HEIGHT - LABEL_HEIGHT) / 2), CSize(100, LABEL_HEIGHT)));
    createEdit(targetAppRestartFileEdit, CRect(CPoint(x + 100, y), CSize(170, EDIT_HEIGHT)), start_id++);
 
 
 
    x = start_x;
    y += EDIT_HEIGHT + LINE_SPACE;
    createLabel(msgLabel, L"", CRect(CPoint(x, y+ (EDIT_HEIGHT - LABEL_HEIGHT) / 2), CSize(360, BUTTON_HEIGHT)));
 
    x += 525 - 80;
    createBtn(buildBtn, L"编译并上传", CRect(CPoint(x, y), CSize(80, BUTTON_HEIGHT)), IDS[index][1]);
 
    x -= 80;
 
    createBtn(saveBtn, L"保存", CRect(CPoint(x, y), CSize(60, BUTTON_HEIGHT)), IDS[index][0]);
 
    controls.labels.push_back(nameLabel);
    controls.labels.push_back(dirLabel);
    controls.labels.push_back(outputDirLabel);
    controls.labels.push_back(outputFileLabel);
    controls.labels.push_back(scriptFileLabel);
    controls.labels.push_back(targetServerDirLabel);
    controls.labels.push_back(targetServerIPLabel);
    controls.labels.push_back(targetServerPortLabel);
    controls.labels.push_back(targetServerPwdLabel);
    controls.labels.push_back(targetServerUserNameLabel);
 
    controls.editName = nameEdit;
    controls.editDir = dirEdit;
    controls.editOutputDir = outputDirEdit;
    controls.editOutputFile = outputFileEdit;
    controls.editScriptFile = scriptFileEdit;
    controls.editTargetServerDir = targetServerDirEdit;
    controls.editTargetServerIP = targetServerIPEdit;
    controls.editTargetServerPort = targetServerPortEdit;
    controls.editTargetServerPwd = targetServerPwdEdit;
    controls.editTargetServerUserName = targetServerUserNameEdit;
    controls.editTargetAppFile = targetAppFileEdit;
    controls.editTargetAppRestartFile = targetAppRestartFileEdit;
    controls.btnBuild = buildBtn;
    controls.btnSave = saveBtn;
    controls.labelMsg = msgLabel;
}
 
void CProjectBuilderDlg::buildProject(ServerInfo* buildServer, ProjectBuildInfo* info, ProjectControls controls,string cacheDir, CProjectBuilderDlg* context)
{
    string cmd = "cd ";
    cmd.append(info->dir).append(" && ").append("rm -rf ").append(info->outputDir).append(" && ").append(info->scriptFile);
    try {
        controls.labelMsg->SetWindowText(_T("准备编译..."));
        SSHServer buildSSHServer = SSHServer({ buildServer->ip, buildServer->port, buildServer->userName, buildServer->pwd });
        SSHUtil::excuteCmd(cmd, buildSSHServer);
        controls.labelMsg->SetWindowText(_T("编译完成,准备下载文件..."));
        string cacheFile = string(cacheDir.c_str()).append("/").append(info->outputFile);
        // 删除本地文件
        DeleteFileA(cacheFile.c_str());
 
        Sleep(100);
        string remotePath = string(info->dir.c_str()).append("/").append(info->outputDir.c_str()).append("/").append(info->outputFile.c_str());
        SSHUtil::downloadFile(remotePath, cacheFile, buildSSHServer);
        controls.labelMsg->SetWindowText(_T("文件下载完成!准备上传目标服务器"));
 
 
        SSHServer targetSSHServer = SSHServer({ info->targetServer.ip,info->targetServer.port, info->targetServer.userName, info->targetServer.pwd });
        // 杀死原有应用
        cmd = string("pkill -f ").append(info->outputFile);
        SSHUtil::excuteCmd(cmd, targetSSHServer);
        controls.labelMsg->SetWindowText(_T("结束目标进程成功!准备删除应用"));
        // 上传应用
 
        string remoteAppFile = string(info->targetDir).append("/").append(info->targetAppFile);
 
        cmd = string("rm -rf ").append(remoteAppFile);
        SSHUtil::excuteCmd(cmd, targetSSHServer);
        controls.labelMsg->SetWindowText(_T("删除应用成功!准备上传文件"));
 
        SSHUtil::uploadFile(remoteAppFile, cacheFile, targetSSHServer, [controls](uint64_t transferred, uint64_t total) {
            double percent = (double)transferred / total * 100;
            CString progressText = L"上传进度:";
            progressText.Append(CString(StringUtil::to_string(percent).c_str()));
            progressText.Append(L"%");
            controls.labelMsg->SetWindowText(progressText);
            });
 
        controls.labelMsg->SetWindowText(L"上传完成,准备授权应用!");
        // 应用授权
        cmd = string("chmod 777 ").append(remoteAppFile);
        SSHUtil::excuteCmd(cmd, targetSSHServer);
 
        controls.labelMsg->SetWindowText(L"应用授权完成,准备重启应用!");
        // TODO 重启进程
        cmd = string("cd ").append(info->targetDir).append(" && ").append(info->targetAppRestartFile);
        SSHUtil::excuteCmd(cmd, targetSSHServer);
        controls.labelMsg->SetWindowText(L"应用重启完成!");
        context->MessageBox(L"任务完成", L"提示");
    }
    catch (string msg) {
        context->MessageBox(CString(msg.c_str()), _T("错误"), MB_OK | MB_ICONERROR);
    }
}
 
void CProjectBuilderDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}
 
 
BEGIN_MESSAGE_MAP(CProjectBuilderDlg, CDialogEx)
    ON_BN_CLICKED(IDC_BUTTON_BUILD_SERVER_SAVE, &CProjectBuilderDlg::OnBnClickedButtonBuildServerSave)
    ON_BN_CLICKED(IDC_BUTTON_BUILD_1, &CProjectBuilderDlg::OnBnClickedButtonBuild)
    ON_BN_CLICKED(IDC_BUTTON_BUILD_2, &CProjectBuilderDlg::OnBnClickedButtonBuild)
    ON_BN_CLICKED(IDC_BUTTON_BUILD_3, &CProjectBuilderDlg::OnBnClickedButtonBuild)
    ON_BN_CLICKED(IDC_BUTTON_BUILD_4, &CProjectBuilderDlg::OnBnClickedButtonBuild)
    ON_BN_CLICKED(IDC_BUTTON_BUILD_5, &CProjectBuilderDlg::OnBnClickedButtonBuild)
    ON_BN_CLICKED(IDC_BUTTON_BUILD_6, &CProjectBuilderDlg::OnBnClickedButtonBuild)
    ON_BN_CLICKED(IDC_BUTTON_BUILD_7, &CProjectBuilderDlg::OnBnClickedButtonBuild)
    ON_BN_CLICKED(IDC_BUTTON_BUILD_8, &CProjectBuilderDlg::OnBnClickedButtonBuild)
    ON_BN_CLICKED(IDC_BUTTON_SAVE_1, &CProjectBuilderDlg::OnBnClickedButtonSave)
    ON_BN_CLICKED(IDC_BUTTON_SAVE_2, &CProjectBuilderDlg::OnBnClickedButtonSave)
    ON_BN_CLICKED(IDC_BUTTON_SAVE_3, &CProjectBuilderDlg::OnBnClickedButtonSave)
    ON_BN_CLICKED(IDC_BUTTON_SAVE_4, &CProjectBuilderDlg::OnBnClickedButtonSave)
    ON_BN_CLICKED(IDC_BUTTON_SAVE_5, &CProjectBuilderDlg::OnBnClickedButtonSave)
    ON_BN_CLICKED(IDC_BUTTON_SAVE_6, &CProjectBuilderDlg::OnBnClickedButtonSave)
    ON_BN_CLICKED(IDC_BUTTON_SAVE_7, &CProjectBuilderDlg::OnBnClickedButtonSave)
    ON_BN_CLICKED(IDC_BUTTON_SAVE_8, &CProjectBuilderDlg::OnBnClickedButtonSave)
    ON_BN_CLICKED(IDC_BUTTON_SAVE_LOCAL_SETTINGS, &CProjectBuilderDlg::OnBnClickedButtonSaveLocalSettings)
    ON_BN_CLICKED(IDC_BUTTON_TEST1, &CProjectBuilderDlg::OnBnClickedTest1)
    
    
END_MESSAGE_MAP()
 
 
// CProjectBuilderDlg 消息处理程序
 
bool CProjectBuilderDlg::validateIPv4(const std::string& ip) {
    std::regex ipv4_regex("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
    return std::regex_match(ip, ipv4_regex);
}
 
ProjectBuildInfo* CProjectBuilderDlg::getProjectBuildInfo(int index)
{
    list<ProjectControls>::iterator it = projectControlsList.begin();
    advance(it, index);
    ProjectControls projectControls = *it;
    CString name, dir, scriptFile, outputDir, outputFile, targetServerIP, targetServerPort, targetServerUserName, targetServerPwd, targetServerDir, targetAppFile, targetAppRestartFile;
    projectControls.editName->GetWindowText(name);
    projectControls.editDir->GetWindowText(dir);
    projectControls.editScriptFile->GetWindowText(scriptFile);
    projectControls.editOutputFile->GetWindowText(outputFile);
    projectControls.editOutputDir->GetWindowText(outputDir);
    projectControls.editTargetServerIP->GetWindowText(targetServerIP);
    projectControls.editTargetServerPort->GetWindowText(targetServerPort);
    projectControls.editTargetServerUserName->GetWindowText(targetServerUserName);
    projectControls.editTargetServerPwd->GetWindowText(targetServerPwd);
    projectControls.editTargetServerDir->GetWindowText(targetServerDir);
    projectControls.editTargetAppFile->GetWindowText(targetAppFile);
    projectControls.editTargetAppRestartFile->GetWindowText(targetAppRestartFile);
 
    if (!validateIPv4(StringUtil::cstring2String(targetServerIP))) {
        AfxMessageBox(L"IP格式错误");
        return nullptr;
    }
 
    std::regex pattern("^[1-9]\\d*$");
    if (!std::regex_match(StringUtil::cstring2String(targetServerPort), std::regex("^[1-9]\\d*$"))) {
        AfxMessageBox(L"PORT格式错误");
        return nullptr;
    }
 
 
    ProjectBuildInfo* buildInfo = new ProjectBuildInfo();
    buildInfo->name = StringUtil::cstring2String(name);
    buildInfo->dir = StringUtil::cstring2String(dir);
    buildInfo->scriptFile = StringUtil::cstring2String(scriptFile);
    buildInfo->outputDir = StringUtil::cstring2String(outputDir);
    buildInfo->outputFile = StringUtil::cstring2String(outputFile);
    buildInfo->targetServer.ip = StringUtil::cstring2String(targetServerIP);
    buildInfo->targetServer.port = _ttoi(targetServerPort);
    buildInfo->targetServer.userName = StringUtil::cstring2String(targetServerUserName);
    buildInfo->targetServer.pwd = StringUtil::cstring2String(targetServerPwd);
    buildInfo->targetDir = StringUtil::cstring2String(targetServerDir);
    buildInfo->targetAppFile = StringUtil::cstring2String(targetAppFile);
    buildInfo->targetAppRestartFile = StringUtil::cstring2String(targetAppRestartFile);
 
 
    return buildInfo;
}
 
 
void CProjectBuilderDlg::OnBnClickedButtonBuildServerSave()
{
    CString ip, port, username, password;
    ((CEdit*)GetDlgItem(IDC_EDIT_BUILD_SERVER_IP))->GetWindowText(ip);
    ((CEdit*)GetDlgItem(IDC_EDIT_BUILD_SERVER_PORT))->GetWindowText(port);
    ((CEdit*)GetDlgItem(IDC_EDIT_BUILD_SERVER_USERNAME))->GetWindowText(username);
    ((CEdit*)GetDlgItem(IDC_EDIT_BUILD_SERVER_PASSWORD))->GetWindowText(password);
 
    if (!validateIPv4(StringUtil::cstring2String(ip))) {
        AfxMessageBox(L"IP格式错误");
        return;
    }
 
    std::regex pattern("^[1-9]\\d*$");
    if (!std::regex_match(StringUtil::cstring2String(port), pattern)) {
        AfxMessageBox(L"PORT格式错误");
        return;
    }
    ServerInfo* buildServer = new ServerInfo();
    buildServer->ip = StringUtil::cstring2String(ip);
    buildServer->port = _ttoi(port);
    buildServer->userName = StringUtil::cstring2String(username);
    buildServer->pwd = StringUtil::cstring2String(password);
    ConfigUtil::setBuildServer(buildServer);
    MessageBox(L"保存成功", L"提示");
}
 
 
void CProjectBuilderDlg::OnBnClickedButtonBuild()
{
    UINT nID = (UINT)GetCurrentMessage()->wParam;
    ProjectControls controls;
    int index = 0;
    for (list<ProjectControls>::iterator e = projectControlsList.begin(); e != projectControlsList.end(); e++) {
        if ((*e).btnBuild->GetDlgCtrlID() == nID) {
            controls = *e;
            break;
        }
        index++;
    }
    ServerInfo* buildServer = ConfigUtil::getBuildServer();
    if (buildServer == nullptr) {
        AfxMessageBox(_T("编译服务器为空值"));
        return;
    }
 
    string cacheDir = ConfigUtil::getLocalCacheDir();
    if (cacheDir.empty()) {
        AfxMessageBox(_T("本地缓存文件夹为空"));
        return;
    }
 
    ProjectBuildInfo *info = getProjectBuildInfo(index);
    
    std::thread t1(buildProject, buildServer, info, controls, cacheDir, this);
    t1.detach();
}
 
 
void CProjectBuilderDlg::OnBnClickedButtonSave()
{
    UINT nID = (UINT)GetCurrentMessage()->wParam;
    int index = 0;
    for (list<ProjectControls>::iterator e = projectControlsList.begin(); e != projectControlsList.end(); e++) {
        if ((*e).btnSave->GetDlgCtrlID() == nID) {
            break;
        }
        index++;
    }
    
    saveProjectBuildInfo(index);
}
 
void CProjectBuilderDlg::OnBnClickedButtonSaveLocalSettings()
{
    //保存本地设置
    CString localCacheDir;
    ((CEdit *) GetDlgItem(IDC_EDIT_LOCAL_CACHE_DIR))->GetWindowText(localCacheDir);
    ConfigUtil::setLocalCacheDir(StringUtil::cstring2String( localCacheDir));
    MessageBox(_T("保存成功"));
}
 
void CProjectBuilderDlg::OnBnClickedTest1()
{
// 测试
    try {
        SSHUtil::downloadFile("/home/userzjj/testFile", "D:/文件传输/交易/cache/gp_server_test", SSHServer({ "192.168.84.126",22, "root","Yeshi2016@" }));
    }
    catch (string msg) {
        AfxMessageBox(CString(msg.c_str()));
    }
}
 
 
BOOL CProjectBuilderDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
    ((CEdit*)GetDlgItem(IDC_EDIT_BUILD_SERVER_PASSWORD))->SetPasswordChar('*');
 
    ServerInfo* buildServer = ConfigUtil::getBuildServer();
    if (buildServer != nullptr) {
        ((CEdit*)GetDlgItem(IDC_EDIT_BUILD_SERVER_IP))->SetWindowText(CString(buildServer->ip.c_str()));
        ((CEdit*)GetDlgItem(IDC_EDIT_BUILD_SERVER_PORT))->SetWindowText(CString(to_wstring(buildServer->port).c_str()));
        ((CEdit*)GetDlgItem(IDC_EDIT_BUILD_SERVER_USERNAME))->SetWindowText(CString(buildServer->userName.c_str()));
        ((CEdit*)GetDlgItem(IDC_EDIT_BUILD_SERVER_PASSWORD))->SetWindowText(CString(buildServer->pwd.c_str()));
        delete buildServer;
    }
 
    string localCacheDir = ConfigUtil::getLocalCacheDir();
 
    ((CEdit*)GetDlgItem(IDC_EDIT_LOCAL_CACHE_DIR))->SetWindowTextW(CString(localCacheDir.c_str()));
    
 
    INT x[] = { 20, 600 };
    int start_y = 190;
 
    for (int i = 0;i < 4;i++) {
        if (i % 2 == 0) {
            start_y += (i / 2) * 200 + 20;
        }
        ProjectControls controls = {};
        createProjectView(controls,i, x[i%2], start_y);
        projectControlsList.push_back(controls);
    }
 
    for (int i = 0;i < MAX_PROJECT_COUNT;i++) {
        ProjectBuildInfo* buildServer = ConfigUtil::getProjectBuildInfo(i);
        if (buildServer == nullptr) {
            continue;
        }
        if (i + 1 >= projectControlsList.size()) {
            break;
        }
 
        list<ProjectControls>::iterator e =    projectControlsList.begin();
        advance(e, i);
        ProjectControls controls = *e;
        controls.editName->SetWindowTextW(CString(buildServer->name.c_str()));
        controls.editDir->SetWindowTextW(CString(buildServer->dir.c_str()));
        controls.editOutputDir->SetWindowTextW(CString(buildServer->outputDir.c_str()));
        controls.editOutputFile->SetWindowTextW(CString(buildServer->outputFile.c_str()));
        controls.editScriptFile->SetWindowTextW(CString(buildServer->scriptFile.c_str()));
        controls.editTargetServerIP->SetWindowTextW(CString(buildServer->targetServer.ip.c_str()));
        controls.editTargetServerPort->SetWindowTextW(CString(to_wstring(buildServer->targetServer.port).c_str()));
        controls.editTargetServerPwd->SetWindowTextW(CString(buildServer->targetServer.pwd.c_str()));
        controls.editTargetServerUserName->SetWindowTextW(CString(buildServer->targetServer.userName.c_str()));
        controls.editTargetServerDir->SetWindowTextW(CString(buildServer->targetDir.c_str()));
        controls.editTargetAppFile->SetWindowTextW(CString(buildServer->targetAppFile.c_str()));
        controls.editTargetAppRestartFile->SetWindowTextW(CString(buildServer->targetAppRestartFile.c_str()));
    }
    return TRUE;  // return TRUE unless you set the focus to a control
                  // 异常: OCX 属性页应返回 FALSE
}