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 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
|
#include "clFileSystemWorkspace.hpp"
#include "CompileCommandsGenerator.h"
#include "JSON.h"
#include "NewFileSystemWorkspaceDialog.h"
#include "StringUtils.h"
#include "asyncprocess.h"
#include "build_settings_config.h"
#include "clConsoleBase.h"
#include "clFileSystemEvent.h"
#include "clFileSystemWorkspaceView.hpp"
#include "clFilesCollector.h"
#include "clKeyboardManager.h"
#include "clSFTPEvent.h"
#include "clShellHelper.hpp"
#include "clWorkspaceManager.h"
#include "clWorkspaceView.h"
#include "codelite_events.h"
#include "compiler_command_line_parser.h"
#include "ctags_manager.h"
#include "debuggermanager.h"
#include "editor_config.h"
#include "environmentconfig.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "fileutils.h"
#include "globals.h"
#include "imanager.h"
#include "macromanager.h"
#include "macros.h"
#include "processreaderthread.h"
#include "shell_command.h"
#include "wxStringHash.h"
#include <thread>
#include <wx/msgdlg.h>
#include <wx/tokenzr.h>
#include <wx/xrc/xmlres.h>
#define CHECK_ACTIVE_CONFIG() \
if(!GetSettings().GetSelectedConfig()) { \
return; \
}
#define CHECK_EVENT(e) \
{ \
if(!IsOpen()) { \
e.Skip(); \
return; \
} \
event.Skip(false); \
}
wxDEFINE_EVENT(wxEVT_FS_SCAN_COMPLETED, clFileSystemEvent);
wxDEFINE_EVENT(wxEVT_FS_NEW_WORKSPACE_FILE_CREATED, clFileSystemEvent);
clFileSystemWorkspace::clFileSystemWorkspace(bool dummy)
: m_dummy(dummy)
{
SetWorkspaceType("File System Workspace");
if(!dummy) {
EventNotifier::Get()->Bind(wxEVT_CMD_CLOSE_WORKSPACE, &clFileSystemWorkspace::OnCloseWorkspace, this);
EventNotifier::Get()->Bind(wxEVT_CMD_OPEN_WORKSPACE, &clFileSystemWorkspace::OnOpenWorkspace, this);
EventNotifier::Get()->Bind(wxEVT_CMD_RELOAD_WORKSPACE, &clFileSystemWorkspace::OnReloadWorkspace, this);
EventNotifier::Get()->Bind(wxEVT_CMD_CREATE_NEW_WORKSPACE, &clFileSystemWorkspace::OnNewWorkspace, this);
EventNotifier::Get()->Bind(wxEVT_ALL_EDITORS_CLOSED, &clFileSystemWorkspace::OnAllEditorsClosed, this);
EventNotifier::Get()->Bind(wxEVT_FS_SCAN_COMPLETED, &clFileSystemWorkspace::OnScanCompleted, this);
EventNotifier::Get()->Bind(wxEVT_CMD_RETAG_WORKSPACE, &clFileSystemWorkspace::OnParseWorkspace, this);
EventNotifier::Get()->Bind(wxEVT_CMD_RETAG_WORKSPACE_FULL, &clFileSystemWorkspace::OnParseWorkspace, this);
EventNotifier::Get()->Bind(wxEVT_SAVE_SESSION_NEEDED, &clFileSystemWorkspace::OnSaveSession, this);
EventNotifier::Get()->Bind(wxEVT_SOURCE_CONTROL_PULLED, &clFileSystemWorkspace::OnSourceControlPulled, this);
// Build events
EventNotifier::Get()->Bind(wxEVT_BUILD_STARTING, &clFileSystemWorkspace::OnBuildStarting, this);
EventNotifier::Get()->Bind(wxEVT_STOP_BUILD, &clFileSystemWorkspace::OnStopBuild, this);
EventNotifier::Get()->Bind(wxEVT_GET_IS_BUILD_IN_PROGRESS, &clFileSystemWorkspace::OnIsBuildInProgress, this);
EventNotifier::Get()->Bind(wxEVT_BUILD_CUSTOM_TARGETS_MENU_SHOWING, &clFileSystemWorkspace::OnCustomTargetMenu,
this);
Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &clFileSystemWorkspace::OnBuildProcessTerminated, this);
Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &clFileSystemWorkspace::OnBuildProcessOutput, this);
Bind(wxEVT_TERMINAL_EXIT, &clFileSystemWorkspace::OnExecProcessTerminated, this);
// Exec events
EventNotifier::Get()->Bind(wxEVT_CMD_EXECUTE_ACTIVE_PROJECT, &clFileSystemWorkspace::OnExecute, this);
EventNotifier::Get()->Bind(wxEVT_CMD_IS_PROGRAM_RUNNING, &clFileSystemWorkspace::OnIsProgramRunning, this);
EventNotifier::Get()->Bind(wxEVT_CMD_STOP_EXECUTED_PROGRAM, &clFileSystemWorkspace::OnStopExecute, this);
// Debug events
EventNotifier::Get()->Bind(wxEVT_QUICK_DEBUG_DLG_SHOWING, &clFileSystemWorkspace::OnQuickDebugDlgShowing, this);
EventNotifier::Get()->Bind(wxEVT_QUICK_DEBUG_DLG_DISMISSED_OK, &clFileSystemWorkspace::OnQuickDebugDlgDismissed,
this);
EventNotifier::Get()->Bind(wxEVT_FILE_SAVED, &clFileSystemWorkspace::OnFileSaved, this);
EventNotifier::Get()->Bind(wxEVT_DBG_UI_START, &clFileSystemWorkspace::OnDebug, this);
EventNotifier::Get()->Bind(wxEVT_FILE_CREATED, &clFileSystemWorkspace::OnFileSystemUpdated, this);
}
}
clFileSystemWorkspace::~clFileSystemWorkspace()
{
if(!m_dummy) {
EventNotifier::Get()->Unbind(wxEVT_CMD_CLOSE_WORKSPACE, &clFileSystemWorkspace::OnCloseWorkspace, this);
EventNotifier::Get()->Unbind(wxEVT_CMD_OPEN_WORKSPACE, &clFileSystemWorkspace::OnOpenWorkspace, this);
EventNotifier::Get()->Unbind(wxEVT_CMD_RELOAD_WORKSPACE, &clFileSystemWorkspace::OnReloadWorkspace, this);
EventNotifier::Get()->Unbind(wxEVT_CMD_CREATE_NEW_WORKSPACE, &clFileSystemWorkspace::OnNewWorkspace, this);
EventNotifier::Get()->Unbind(wxEVT_ALL_EDITORS_CLOSED, &clFileSystemWorkspace::OnAllEditorsClosed, this);
EventNotifier::Get()->Unbind(wxEVT_FS_SCAN_COMPLETED, &clFileSystemWorkspace::OnScanCompleted, this);
EventNotifier::Get()->Unbind(wxEVT_SAVE_SESSION_NEEDED, &clFileSystemWorkspace::OnSaveSession, this);
// parsing event
EventNotifier::Get()->Unbind(wxEVT_CMD_RETAG_WORKSPACE, &clFileSystemWorkspace::OnParseWorkspace, this);
EventNotifier::Get()->Unbind(wxEVT_CMD_RETAG_WORKSPACE_FULL, &clFileSystemWorkspace::OnParseWorkspace, this);
EventNotifier::Get()->Unbind(wxEVT_SOURCE_CONTROL_PULLED, &clFileSystemWorkspace::OnSourceControlPulled, this);
// Build events
EventNotifier::Get()->Unbind(wxEVT_BUILD_STARTING, &clFileSystemWorkspace::OnBuildStarting, this);
EventNotifier::Get()->Unbind(wxEVT_GET_IS_BUILD_IN_PROGRESS, &clFileSystemWorkspace::OnIsBuildInProgress, this);
EventNotifier::Get()->Unbind(wxEVT_STOP_BUILD, &clFileSystemWorkspace::OnStopBuild, this);
EventNotifier::Get()->Unbind(wxEVT_BUILD_CUSTOM_TARGETS_MENU_SHOWING,
&clFileSystemWorkspace::OnCustomTargetMenu, this);
Unbind(wxEVT_ASYNC_PROCESS_TERMINATED, &clFileSystemWorkspace::OnBuildProcessTerminated, this);
Unbind(wxEVT_ASYNC_PROCESS_OUTPUT, &clFileSystemWorkspace::OnBuildProcessOutput, this);
Unbind(wxEVT_TERMINAL_EXIT, &clFileSystemWorkspace::OnExecProcessTerminated, this);
// Exec events
EventNotifier::Get()->Unbind(wxEVT_CMD_EXECUTE_ACTIVE_PROJECT, &clFileSystemWorkspace::OnExecute, this);
EventNotifier::Get()->Unbind(wxEVT_CMD_IS_PROGRAM_RUNNING, &clFileSystemWorkspace::OnIsProgramRunning, this);
EventNotifier::Get()->Unbind(wxEVT_CMD_STOP_EXECUTED_PROGRAM, &clFileSystemWorkspace::OnStopExecute, this);
// Debug events
EventNotifier::Get()->Unbind(wxEVT_QUICK_DEBUG_DLG_SHOWING, &clFileSystemWorkspace::OnQuickDebugDlgShowing,
this);
EventNotifier::Get()->Unbind(wxEVT_QUICK_DEBUG_DLG_DISMISSED_OK,
&clFileSystemWorkspace::OnQuickDebugDlgDismissed, this);
EventNotifier::Get()->Unbind(wxEVT_FILE_SAVED, &clFileSystemWorkspace::OnFileSaved, this);
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_START, &clFileSystemWorkspace::OnDebug, this);
EventNotifier::Get()->Unbind(wxEVT_FILE_CREATED, &clFileSystemWorkspace::OnFileSystemUpdated, this);
}
}
wxString clFileSystemWorkspace::GetActiveProjectName() const { return ""; }
wxString clFileSystemWorkspace::GetFileName() const { return m_filename.GetFullPath(); }
wxString clFileSystemWorkspace::GetDir() const { return m_filename.GetPath(); }
wxString clFileSystemWorkspace::GetFilesMask() const
{
clFileSystemWorkspaceConfig::Ptr_t conf = m_settings.GetSelectedConfig();
if(conf) {
return conf->GetFileExtensions();
}
return wxEmptyString;
}
wxFileName clFileSystemWorkspace::GetProjectFileName(const wxString& projectName) const
{
wxUnusedVar(projectName);
return wxFileName();
}
void clFileSystemWorkspace::GetProjectFiles(const wxString& projectName, wxArrayString& files) const {}
wxString clFileSystemWorkspace::GetProjectFromFile(const wxFileName& filename) const { return ""; }
void clFileSystemWorkspace::GetWorkspaceFiles(wxArrayString& files) const
{
files.clear();
files.Alloc(m_files.GetSize());
for(const wxFileName& file : m_files) {
files.Add(file.GetFullPath());
}
}
wxArrayString clFileSystemWorkspace::GetWorkspaceProjects() const { return {}; }
bool clFileSystemWorkspace::IsBuildSupported() const { return true; }
bool clFileSystemWorkspace::IsProjectSupported() const { return false; }
void clFileSystemWorkspace::CacheFiles(bool force)
{
// should we force a rescan of the files?
if(force) {
m_files.Clear();
}
// sanity
if(!m_files.IsEmpty()) {
m_files.Clear();
}
std::thread thr(
[=](const wxString& rootFolder) {
clFilesScanner fs;
std::vector<wxString> files;
wxStringSet_t excludeFolders = { ".git", ".svn", ".codelite", ".ctagsd" };
wxString excludePaths = GetExcludeFolders();
wxArrayString paths = StringUtils::BuildArgv(excludePaths);
if(!paths.IsEmpty()) {
for(wxString& excludePath : paths) {
excludePath.Trim().Trim(false);
if(excludePath.EndsWith("/") || excludePath.EndsWith("\\")) {
excludePath.RemoveLast();
}
if(excludePath.IsEmpty()) {
continue;
}
wxFileName fnpath(excludePath, "");
excludeFolders.insert(fnpath.GetPath());
}
}
fs.Scan(rootFolder, files, GetFilesMask(), "", excludeFolders);
clFileSystemEvent event(wxEVT_FS_SCAN_COMPLETED);
wxArrayString arrfiles;
arrfiles.Alloc(files.size());
for(const wxString& s : files) {
wxFileName f(s);
arrfiles.Add(f.GetFullPath());
}
event.SetPaths(arrfiles);
EventNotifier::Get()->QueueEvent(event.Clone());
},
GetDir());
thr.detach();
}
void clFileSystemWorkspace::OnBuildStarting(clBuildEvent& event)
{
event.Skip();
if(IsOpen()) {
event.Skip(false);
// before we start the build, save all modified files
clGetManager()->SaveAll(false);
DoBuild(event.GetKind());
}
}
void clFileSystemWorkspace::OnBuildEnded(clBuildEvent& event) { event.Skip(); }
void clFileSystemWorkspace::OnOpenWorkspace(clCommandEvent& event)
{
event.Skip();
if(OpenWorkspace(event.GetFileName())) {
event.Skip(false);
} else {
m_filename.Clear();
}
}
void clFileSystemWorkspace::OnCloseWorkspace(clCommandEvent& event)
{
event.Skip();
if(CloseWorkspace()) {
event.Skip(false);
}
}
bool clFileSystemWorkspace::Load(const wxFileName& file)
{
if(m_isLoaded) {
return true;
}
m_filename = file;
bool loadOk = m_settings.Load(m_filename);
if(loadOk && m_settings.GetName().empty()) {
m_settings.SetName(m_filename.GetName());
}
return loadOk;
}
void clFileSystemWorkspace::Save(bool parse)
{
if(!m_filename.IsOk()) {
return;
}
m_settings.Save(m_filename);
// Fire 'saved' event
clCommandEvent eventFileSave(wxEVT_FILE_SAVED);
eventFileSave.SetFileName(m_filename.GetFullPath());
eventFileSave.SetString(m_filename.GetFullPath());
EventNotifier::Get()->AddPendingEvent(eventFileSave);
GetView()->SetExcludeFilePatterns(
GetSettings().GetSelectedConfig() ? GetSettings().GetSelectedConfig()->GetExcludeFilesPattern() : "");
GetView()->UpdateConfigs(GetSettings().GetConfigs(), GetConfig() ? GetConfig()->GetName() : wxString());
// trigger a file scan
if(parse) {
CacheFiles();
}
}
void clFileSystemWorkspace::RestoreSession()
{
if(IsOpen()) {
clGetManager()->LoadWorkspaceSession(m_filename);
}
}
void clFileSystemWorkspace::DoOpen()
{
// Close any opened workspace
auto frame = EventNotifier::Get()->TopFrame();
wxCommandEvent eventCloseWsp(wxEVT_COMMAND_MENU_SELECTED, XRCID("close_workspace"));
eventCloseWsp.SetEventObject(frame);
frame->GetEventHandler()->ProcessEvent(eventCloseWsp);
// set the working directory to the workspace view
::wxSetWorkingDirectory(GetDir());
// Create the symbols db file
wxFileName fnFolder(GetFileName());
fnFolder.SetExt("db");
fnFolder.AppendDir(".codelite");
fnFolder.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
// Load the backticks cache file, this needs to be done early as we can
// since it is used
if(m_backtickCache) {
m_backtickCache.reset(nullptr);
}
// load the new cache
m_backtickCache.reset(new clBacktickCache(GetDir()));
// Init the view
GetView()->Clear();
// Update the view exclude patterns
auto selectedConf = GetSettings().GetSelectedConfig();
if(selectedConf) {
GetView()->SetExcludeFilePatterns(selectedConf->GetExcludeFilesPattern());
}
// And now load the main folder
GetView()->AddFolder(GetDir());
// Notify CodeLite that this workspace is opened
clGetManager()->GetWorkspaceView()->SelectPage(GetWorkspaceType());
clWorkspaceManager::Get().SetWorkspace(this);
// Notify that the a new workspace is loaded
clWorkspaceEvent event(wxEVT_WORKSPACE_LOADED);
event.SetString(GetFileName());
event.SetFileName(GetFileName());
EventNotifier::Get()->AddPendingEvent(event);
// Update the build configurations button
GetView()->UpdateConfigs(GetSettings().GetConfigs(), GetConfig() ? GetConfig()->GetName() : wxString());
// and finally, request codelite to keep this workspace in the recently opened workspace list
clGetManager()->AddWorkspaceToRecentlyUsedList(m_filename);
// Cache the source files from the workspace directories
CacheFiles();
// mark the workspace as loaded before restoring the session
m_isLoaded = true;
// Load the workspace session (if any)
CallAfter(&clFileSystemWorkspace::RestoreSession);
}
void clFileSystemWorkspace::DoClose()
{
if(!m_isLoaded) {
return;
}
// Store the session
clGetManager()->StoreWorkspaceSession(m_filename);
// avoid any file re-cache, we are closing
Save(false);
DoClear();
// Clear the UI
GetView()->Clear();
// Notify codelite to close the currently opened workspace
wxCommandEvent eventClose(wxEVT_MENU, wxID_CLOSE_ALL);
eventClose.SetEventObject(EventNotifier::Get()->TopFrame());
EventNotifier::Get()->TopFrame()->GetEventHandler()->ProcessEvent(eventClose);
// Notify workspace closed event
clWorkspaceEvent event_closed(wxEVT_WORKSPACE_CLOSED);
EventNotifier::Get()->ProcessEvent(event_closed);
// Free the database
TagsManagerST::Get()->CloseDatabase();
m_isLoaded = false;
m_showWelcomePage = true;
if(m_backtickCache) {
m_backtickCache->Save();
m_backtickCache.reset(nullptr);
}
wxDELETE(m_buildProcess);
GetView()->UpdateConfigs({}, wxString());
}
void clFileSystemWorkspace::DoClear()
{
m_filename.Clear();
m_settings.Clear();
}
void clFileSystemWorkspace::OnAllEditorsClosed(wxCommandEvent& event)
{
event.Skip();
if(m_showWelcomePage) {
m_showWelcomePage = false;
// Show the 'Welcome Page'
wxFrame* frame = EventNotifier::Get()->TopFrame();
wxCommandEvent eventShowWelcomePage(wxEVT_MENU, XRCID("view_welcome_page"));
eventShowWelcomePage.SetEventObject(frame);
frame->GetEventHandler()->AddPendingEvent(eventShowWelcomePage);
}
}
clFileSystemWorkspace& clFileSystemWorkspace::Get()
{
static clFileSystemWorkspace wsp(false);
return wsp;
}
void clFileSystemWorkspace::New(const wxString& folder, const wxString& name) { DoCreate(name, folder, true); }
void clFileSystemWorkspace::OnScanCompleted(clFileSystemEvent& event)
{
clDEBUG() << "FSW: CacheFiles completed. Found" << event.GetPaths().size() << "files";
m_files.Clear();
m_files.Alloc(event.GetPaths().size());
for(const wxString& filename : event.GetPaths()) {
m_files.Add(filename);
}
clGetManager()->SetStatusMessage(_("File system scan completed"));
// Trigger a non full reparse
Parse(false);
clDEBUG() << "Sending wxEVT_WORKSPACE_FILES_SCANNED event..." << endl;
clWorkspaceEvent event_scan{ wxEVT_WORKSPACE_FILES_SCANNED };
EventNotifier::Get()->ProcessEvent(event_scan);
}
void clFileSystemWorkspace::OnParseWorkspace(wxCommandEvent& event)
{
if(!m_isLoaded) {
event.Skip();
return;
}
Parse(event.GetInt() == (event.GetEventType() == wxEVT_CMD_RETAG_WORKSPACE));
}
void clFileSystemWorkspace::Parse(bool fullParse)
{
if(m_files.IsEmpty()) {
return;
}
if(fullParse) {
TagsManagerST::Get()->ParseWorkspaceFull(GetDir());
} else {
TagsManagerST::Get()->ParseWorkspaceIncremental();
}
}
void clFileSystemWorkspace::Close() { DoClose(); }
wxString clFileSystemWorkspace::CompileFlagsAsString(const wxArrayString& arr) const
{
wxString s;
for(const wxString& l : arr) {
if(!l.IsEmpty()) {
s << l << "\n";
}
}
return s.Trim();
}
wxString clFileSystemWorkspace::GetTargetCommand(const wxString& target) const
{
if(!GetConfig()) {
return wxEmptyString;
}
const auto& M = m_settings.GetSelectedConfig()->GetBuildTargets();
if(M.count(target)) {
wxString cmd = M.find(target)->second;
return cmd;
}
return wxEmptyString;
}
void clFileSystemWorkspace::OnBuildProcessTerminated(clProcessEvent& event)
{
if(event.GetProcess() == m_buildProcess) {
wxDELETE(m_buildProcess);
DoPrintBuildMessage(event.GetOutput());
clBuildEvent e(wxEVT_BUILD_PROCESS_ENDED);
EventNotifier::Get()->AddPendingEvent(e);
// Notify about build process ended to plugins
clBuildEvent eventStopped(wxEVT_BUILD_ENDED);
EventNotifier::Get()->AddPendingEvent(eventStopped);
// remove any temporary scripts generated by our helper
m_shell_helper.Cleanup();
}
}
void clFileSystemWorkspace::OnBuildProcessOutput(clProcessEvent& event)
{
if(event.GetProcess() == m_buildProcess) {
DoPrintBuildMessage(event.GetOutput());
}
}
void clFileSystemWorkspace::DoPrintBuildMessage(const wxString& message)
{
clBuildEvent e(wxEVT_BUILD_PROCESS_ADDLINE);
e.SetString(message);
EventNotifier::Get()->AddPendingEvent(e);
}
void clFileSystemWorkspace::OnSaveSession(clCommandEvent& event)
{
event.Skip();
if(IsOpen()) {
event.Skip(false);
clGetManager()->StoreWorkspaceSession(m_filename);
}
}
void clFileSystemWorkspace::Initialise()
{
if(m_initialized) {
return;
}
m_view = new clFileSystemWorkspaceView(clGetManager()->GetWorkspaceView()->GetBook(), GetWorkspaceType());
clGetManager()->GetWorkspaceView()->AddPage(m_view, GetWorkspaceType());
auto accel_manager = clKeyboardManager::Get();
// register global accelerators entries
accel_manager->AddAccelerator(_("File System Workspace"), { { "fsw_refresh_current_folder", _("Refresh") } });
}
void clFileSystemWorkspace::OnExecute(clExecuteEvent& event)
{
CHECK_EVENT(event);
CHECK_ACTIVE_CONFIG();
if(m_execPID != wxNOT_FOUND) {
return;
}
wxString exe, args, wd;
GetExecutable(exe, args, wd);
clEnvList_t envList = GetEnvList();
clConsoleBase::Ptr_t console = clConsoleBase::GetTerminal();
console->SetAutoTerminate(true);
console->SetCommand(exe, args);
console->SetWorkingDirectory(wd);
console->SetWaitWhenDone(true);
console->SetSink(this);
console->SetEnvironment(envList);
console->SetTerminalNeeded(true);
if(console->Start()) {
m_execPID = console->GetPid();
}
// Notify about program execution
clExecuteEvent startEvent(wxEVT_PROGRAM_STARTED);
EventNotifier::Get()->AddPendingEvent(startEvent);
}
clEnvList_t clFileSystemWorkspace::GetEnvList()
{
clEnvList_t envList;
if(!GetConfig()) {
return envList;
}
wxString envstr;
EvnVarList env = EnvironmentConfig::Instance()->GetSettings();
EnvMap envMap = env.GetVariables(env.GetActiveSet(), false, "", "");
// Add the global variables
envstr += envMap.String();
envstr += "\n";
// include special environment variable with the default compiler paths
const wxString& compiler_name = GetConfig()->GetCompiler();
if(!compiler_name.empty()) {
auto cmp = BuildSettingsConfigST::Get()->GetCompiler(compiler_name);
auto paths = cmp->GetDefaultIncludePaths();
if(!paths.empty()) {
wxString env_value;
for(const auto& path : paths) {
if(!env_value.empty()) {
env_value << ";";
}
env_value << path;
}
envstr += "CXX_INCLUDE_PATHS=" + env_value + "\n";
}
}
// and finally, include the user environment variables
envstr += GetConfig()->GetEnvironment();
// Append the workspace environment
envstr = MacroManager::Instance()->Expand(envstr, nullptr, wxEmptyString);
envList = FileUtils::CreateEnvironment(envstr);
return envList;
}
void clFileSystemWorkspace::OnIsBuildInProgress(clBuildEvent& event)
{
CHECK_EVENT(event);
CHECK_ACTIVE_CONFIG();
event.SetIsRunning(m_buildProcess != nullptr || (m_remoteBuilder && m_remoteBuilder->IsRunning()));
}
void clFileSystemWorkspace::OnIsProgramRunning(clExecuteEvent& event)
{
CHECK_EVENT(event);
CHECK_ACTIVE_CONFIG();
event.SetAnswer(m_execPID != wxNOT_FOUND);
}
void clFileSystemWorkspace::OnStopExecute(clExecuteEvent& event)
{
CHECK_EVENT(event);
if(m_execPID != wxNOT_FOUND) {
::clKill(m_execPID, wxSIGTERM, true);
m_execPID = wxNOT_FOUND;
}
}
void clFileSystemWorkspace::OnStopBuild(clBuildEvent& event)
{
CHECK_EVENT(event);
if(m_buildProcess) {
m_buildProcess->Terminate();
} else if(m_remoteBuilder && m_remoteBuilder->IsRunning()) {
m_remoteBuilder->Stop();
}
}
void clFileSystemWorkspace::OnQuickDebugDlgShowing(clDebugEvent& event)
{
CHECK_EVENT(event);
CHECK_ACTIVE_CONFIG();
wxString args = MacroManager::Instance()->Expand(GetConfig()->GetArgs(), NULL, "", "");
wxString exec = MacroManager::Instance()->Expand(GetConfig()->GetExecutable(), NULL, "", "");
event.SetArguments(args);
event.SetExecutableName(exec);
}
void clFileSystemWorkspace::OnQuickDebugDlgDismissed(clDebugEvent& event)
{
CHECK_EVENT(event);
CHECK_ACTIVE_CONFIG();
}
clFileSystemWorkspaceConfig::Ptr_t clFileSystemWorkspace::GetConfig() const
{
return GetSettings().GetSelectedConfig();
}
void clFileSystemWorkspace::OnMenuCustomTarget(wxCommandEvent& event)
{
if(m_buildTargetMenuIdToName.count(event.GetId()) == 0) {
return;
}
const wxString& target = m_buildTargetMenuIdToName.find(event.GetId())->second;
if(GetConfig()->GetBuildTargets().count(target) == 0) {
return;
}
DoBuild(target);
m_buildTargetMenuIdToName.clear();
}
void clFileSystemWorkspace::OnCustomTargetMenu(clContextMenuEvent& event)
{
CHECK_EVENT(event);
CHECK_ACTIVE_CONFIG();
wxMenu* menu = event.GetMenu();
wxArrayString arrTargets;
const auto& targets = GetConfig()->GetBuildTargets();
// Copy the targets to std::map to get a sorted results
std::map<wxString, wxString> M;
M.insert(targets.begin(), targets.end());
m_buildTargetMenuIdToName.clear();
for(const auto& vt : M) {
const wxString& name = vt.first;
int menuId = wxXmlResource::GetXRCID(vt.first);
menu->Append(menuId, name, name, wxITEM_NORMAL);
menu->Bind(wxEVT_MENU, &clFileSystemWorkspace::OnMenuCustomTarget, this, menuId);
m_buildTargetMenuIdToName.insert({ menuId, name });
}
}
void clFileSystemWorkspace::DoBuild(const wxString& target)
{
if(!GetConfig()) {
::wxMessageBox(_("You should have at least one workspace configuration.\n0 found\nOpen the project "
"settings and add one"),
"CodeLite", wxICON_WARNING | wxCENTER);
return;
}
wxString cmd = GetTargetCommand(target);
if(cmd.IsEmpty()) {
::wxMessageBox(_("Don't know how to run '") + target + "'", "CodeLite", wxICON_WARNING | wxCENTER);
return;
}
if(m_buildProcess) {
return;
}
m_shell_helper.Cleanup(); // clear any scripts generated earlier
size_t flags = IProcessCreateDefault | IProcessCreateWithHiddenConsole | IProcessRawOutput;
// Check that the remote development is enabled AND remote build
if(GetConfig()->IsRemoteEnabled() && GetConfig()->IsRemoteBuild()) {
flags |= IProcessCreateSSH;
} else {
flags |= IProcessWrapInShell;
}
// Replace all workspace macros from the command
cmd = MacroManager::Instance()->Expand(cmd, nullptr, wxEmptyString);
// Build the environment to use
clEnvList_t envList = GetEnvList();
// Start the process with the environemt
wxString ssh_account;
wxString wd = GetDir();
// make changes if SSH is enabled here
if(flags & IProcessCreateSSH) {
ssh_account = GetConfig()->GetRemoteAccount();
wd = GetConfig()->GetRemoteFolder();
}
// See if the command requires modifications
if(!(flags & IProcessCreateSSH) && m_shell_helper.ProcessCommand(cmd)) {
cmd = m_shell_helper.GetCommand();
flags |= m_shell_helper.GetProcessCreateFlags();
}
m_buildProcess = ::CreateAsyncProcess(this, cmd, flags, wd, &envList, ssh_account);
if(!m_buildProcess) {
clBuildEvent e(wxEVT_BUILD_PROCESS_ENDED);
EventNotifier::Get()->AddPendingEvent(e);
} else {
// notify about starting build process.
// we pass the selected compiler in the event
clBuildEvent e(wxEVT_BUILD_PROCESS_STARTED);
e.SetToolchain(GetConfig()->GetCompiler());
EventNotifier::Get()->AddPendingEvent(e);
// Notify about build process started
clBuildEvent eventStart(wxEVT_BUILD_STARTED);
eventStart.SetConfigurationName(GetConfig()->GetName());
eventStart.SetProjectName(wxEmptyString);
EventNotifier::Get()->AddPendingEvent(eventStart);
}
}
void clFileSystemWorkspace::OnNewWorkspace(clCommandEvent& event)
{
event.Skip();
if(event.GetString() == GetWorkspaceType()) {
event.Skip(false);
// Prompt the user for folder and name
NewFileSystemWorkspaceDialog dlg(EventNotifier::Get()->TopFrame());
if(dlg.ShowModal() == wxID_OK) {
DoCreate(dlg.GetWorkspaceName(), dlg.GetWorkspacePath(), false);
}
}
}
void clFileSystemWorkspace::DoCreate(const wxString& name, const wxString& path, bool loadIfExists)
{
wxFileName fn(path, "");
if(fn.GetDirCount() == 0) {
::wxMessageBox(_("Unable to create a workspace on the root folder"), "CodeLite", wxICON_ERROR | wxCENTER);
return;
}
if(loadIfExists) {
// Check to see if any workspace already exists in this folder
clFilesScanner fs;
clFilesScanner::EntryData::Vec_t results;
fs.ScanNoRecurse(path, results, "*.workspace");
for(const auto& f : results) {
if(clFileSystemWorkspaceSettings::IsOk(f.fullpath)) {
fn = f.fullpath;
break;
}
}
}
// If an workspace is opened and it is the same one as this, dont do nothing
if(m_isLoaded && (GetFileName() == fn.GetFullPath())) {
return;
}
// Call close here, it does nothing if a workspace is not opened
DoClose();
DoClear();
if(!name.IsEmpty()) {
fn.SetName(name);
} else if(fn.GetFullName().IsEmpty()) {
wxString name = ::clGetTextFromUser(_("Workspace Name"), _("Name"), fn.GetDirs().Last());
if(name.IsEmpty()) {
return;
}
fn.SetName(name);
}
fn.SetExt("workspace");
SetName(fn.GetName());
// Creates an empty workspace file
m_filename = fn;
if(!fn.FileExists()) {
Save(false);
// let the plugins know that we just created a new workspace file and we
// are about to load it
clFileSystemEvent createEvent(wxEVT_FS_NEW_WORKSPACE_FILE_CREATED);
createEvent.SetPath(m_filename.GetFullPath());
EventNotifier::Get()->ProcessEvent(createEvent);
}
// and load it
if(Load(m_filename)) {
// Now load it
DoOpen();
} else {
m_filename.Clear();
}
}
void clFileSystemWorkspace::OnFileSaved(clCommandEvent& event)
{
event.Skip();
CHECK_ACTIVE_CONFIG();
if(GetConfig()->IsRemoteEnabled()) {
const wxString& filename = event.GetFileName();
// There are 2 cases where we don't want to trigger remote save:
// 1. if the file was opened by the sftp plugin, don't attempt to save it remotely
// it will be done by the sftp plugin. These files are marked with client data
// set with the "sftp" key
// 2. if the file is not located under our root folder -> don't attempt to save it
bool managedBySftp = false;
IEditor* editor = clGetManager()->FindEditor(filename);
if(editor && editor->GetClientData("sftp")) {
managedBySftp = true;
}
wxString rootPath = GetDir();
wxString filePath = wxFileName(filename).GetPath();
bool doRemoteSave = filePath.StartsWith(rootPath) && !managedBySftp;
if(doRemoteSave) {
wxString remoteFilePath;
const wxString& account = GetConfig()->GetRemoteAccount();
const wxString& remotePath = GetConfig()->GetRemoteFolder();
// Make the local file path relative to the workspace location
wxFileName fnLocalFile(event.GetFileName());
fnLocalFile.MakeRelativeTo(GetDir());
remoteFilePath = fnLocalFile.GetFullPath(wxPATH_UNIX);
remoteFilePath.Prepend(remotePath + "/");
wxFileName fnRemoteFile(remoteFilePath);
// Build the remote filename
clSFTPEvent eventSave(wxEVT_SFTP_SAVE_FILE);
eventSave.SetAccount(account);
eventSave.SetLocalFile(filename);
eventSave.SetRemoteFile(fnRemoteFile.GetFullPath(wxPATH_UNIX));
EventNotifier::Get()->QueueEvent(eventSave.Clone());
}
}
}
void clFileSystemWorkspace::OnSourceControlPulled(clSourceControlEvent& event)
{
event.Skip();
clDEBUG() << "Source control '" << event.GetSourceControlName() << "' pulled.";
clDEBUG() << "Refreshing tree + re-parsing";
GetView()->RefreshTree();
// Re-Cache the files and trigger a workspace parse
CacheFiles(true);
}
void clFileSystemWorkspace::TriggerQuickParse()
{
wxCommandEvent eventParse(wxEVT_MENU, XRCID("retag_workspace"));
EventNotifier::Get()->TopFrame()->GetEventHandler()->QueueEvent(eventParse.Clone());
}
void clFileSystemWorkspace::FileSystemUpdated() { CacheFiles(true); }
void clFileSystemWorkspace::OnDebug(clDebugEvent& event)
{
if(!IsOpen() || (event.GetDebuggerName() != "GNU gdb debugger")) {
event.Skip();
return;
}
// This is ours to handle. Stop processing it here
event.Skip(false);
DebuggerMgr::Get().SetActiveDebugger(GetConfig()->GetDebugger());
IDebugger* dbgr = DebuggerMgr::Get().GetActiveDebugger();
if(!dbgr) {
return;
}
// if already running, skip this
// the default behaviour is to "continue"
if(dbgr->IsRunning()) {
event.Skip();
return;
}
// Update the debugger information
DebuggerInformation dinfo = dbgr->GetDebuggerInformation();
dinfo.breakAtWinMain = false;
dinfo.consoleCommand = EditorConfigST::Get()->GetOptions()->GetProgramConsoleCommand();
dbgr->SetDebuggerInformation(dinfo);
dbgr->SetIsRemoteDebugging(false);
// Setup the debug session
wxString exe, args, wd;
GetExecutable(exe, args, wd);
const wxString& user_debugger = GetConfig()->GetDebuggerPath();
// Start the debugger
DebugSessionInfo session_info;
clDebuggerBreakpoint::Vec_t bpList;
session_info.exeName = exe;
session_info.cwd = wd;
session_info.init_file_content = GetConfig()->GetDebuggerCommands();
clGetManager()->GetBreakpoints(bpList);
session_info.bpList = bpList;
// Start terminal (doesn't do anything under MSW)
m_debuggerTerminal.Clear();
m_debuggerTerminal.Launch(dbgr->GetName());
session_info.ttyName = m_debuggerTerminal.GetTty();
session_info.enablePrettyPrinting = dinfo.enableGDBPrettyPrinting;
// Fire "starting" event
clDebugEvent eventStarting(wxEVT_DEBUG_STARTING);
eventStarting.SetDebuggerName(GetConfig()->GetDebugger());
eventStarting.SetClientData(&session_info);
if(EventNotifier::Get()->ProcessEvent(eventStarting)) {
return;
}
// Check if any plugins provided us with new breakpoints
if(!eventStarting.GetBreakpoints().empty()) {
session_info.bpList.swap(eventStarting.GetBreakpoints());
}
// Set the gdb executable
if(!user_debugger.empty()) {
session_info.debuggerPath = user_debugger;
} else {
CompilerPtr cmp = GetCompiler();
if(cmp && !cmp->GetTool("Debugger").empty()) {
session_info.debuggerPath = cmp->GetTool("Debugger");
}
}
// convert the envlist into map
auto envlist = FileUtils::CreateEnvironment(GetConfig()->GetEnvironment());
// wxStringMap_t envmap;
// envmap.reserve(envlist.size());
// envmap.insert(envlist.begin(), envlist.end());
dbgr->Start(session_info, &envlist);
// Notify that debug session started
// this will ensure that the debug layout is loaded
clDebugEvent eventStarted(wxEVT_DEBUG_STARTED);
eventStarted.SetClientData(&session_info);
EventNotifier::Get()->ProcessEvent(eventStarted);
// Now run the debuggee
dbgr->Run(args, "");
}
void clFileSystemWorkspace::GetExecutable(wxString& exe, wxString& args, wxString& wd)
{
exe = GetConfig()->GetExecutable();
args = GetConfig()->GetArgs();
wd = GetConfig()->GetWorkingDirectory().IsEmpty() ? GetDir() : GetConfig()->GetWorkingDirectory();
// build the arguments
args.Replace("\r", wxEmptyString);
args.Replace("\n", " ");
auto args_arr = StringUtils::BuildArgv(args);
args.clear();
for(auto& arg : args_arr) {
if(!args.empty()) {
args << " ";
}
arg.Trim().Trim(false);
// wrap with quotes if required
::WrapWithQuotes(arg);
args << arg;
}
exe = MacroManager::Instance()->Expand(exe, nullptr, wxEmptyString);
args = MacroManager::Instance()->Expand(args, nullptr, wxEmptyString);
wd = MacroManager::Instance()->Expand(wd, nullptr, wxEmptyString);
}
CompilerPtr clFileSystemWorkspace::GetCompiler()
{
return BuildSettingsConfigST::Get()->GetCompiler(GetConfig()->GetCompiler());
}
void clFileSystemWorkspace::OnExecProcessTerminated(clProcessEvent& event)
{
event.Skip();
m_execPID = wxNOT_FOUND;
// Notify about program stopped execution
clExecuteEvent startEvent(wxEVT_PROGRAM_TERMINATED);
EventNotifier::Get()->AddPendingEvent(startEvent);
}
void clFileSystemWorkspace::OnFileSystemUpdated(clFileSystemEvent& event)
{
event.Skip();
if(IsOpen()) {
const wxArrayString& paths = event.GetPaths();
if(paths.empty()) {
return;
}
for(const wxString& path : paths) {
m_files.Add(path);
}
// Parse the newly added files
Parse(false);
}
}
void clFileSystemWorkspace::CreateCompileFlagsFile()
{
wxBusyCursor bc;
const wxFileName& filename = clFileSystemWorkspace::Get().GetFileName();
auto backticks_cache = clFileSystemWorkspace::Get().GetBackticksCache();
auto selectedConfig = m_settings.GetSelectedConfig();
// Create a compile flags from the following info:
// - Get list of folders from the current workspace
// - Any command that is displayed in the text box, expand it
// - Selected compiler paths
auto compilerOptions = selectedConfig->GetCompilerOptions(backticks_cache);
auto includes = selectedConfig->ExpandUserCompletionFlags(filename.GetPath(), backticks_cache, true);
wxString compile_flags_txt;
// Include the workspace path by default
wxString workspacePath = filename.GetPath();
::WrapWithQuotes(workspacePath);
compile_flags_txt << "-I" << workspacePath << "\n";
for(const auto& s : includes) {
compile_flags_txt << s << "\n";
}
for(const auto& s : compilerOptions) {
compile_flags_txt << s << "\n";
}
if(!compile_flags_txt.empty()) {
// replace any placeholders
compile_flags_txt = MacroManager::Instance()->Expand(compile_flags_txt, nullptr, "", "");
wxFileName fnCompileFlags(filename);
fnCompileFlags.SetFullName("compile_flags.txt");
FileUtils::WriteFileContent(fnCompileFlags, compile_flags_txt, wxConvUTF8);
// Fire JSON Generated event
clCommandEvent eventCompileCommandsGenerated(wxEVT_COMPILE_COMMANDS_JSON_GENERATED);
EventNotifier::Get()->QueueEvent(eventCompileCommandsGenerated.Clone());
clDEBUG() << "File:" << fnCompileFlags << "generated" << clEndl;
wxString msg;
msg << _("Successfully generated file:\n") << fnCompileFlags.GetFullPath();
::wxMessageBox(msg, "CodeLite");
}
}
wxString clFileSystemWorkspace::GetExcludeFolders() const
{
clFileSystemWorkspaceConfig::Ptr_t conf = m_settings.GetSelectedConfig();
if(conf) {
return conf->GetExecludePaths();
}
return wxEmptyString;
}
void clFileSystemWorkspace::SetProjectActive(const wxString& project)
{
// no projects in a file system workspace
wxUnusedVar(project);
}
bool clFileSystemWorkspace::OpenWorkspace(const wxString& filepath)
{
// Test that this is our workspace
if(m_settings.IsOk(filepath) && Load(filepath)) {
DoOpen();
return true;
} else {
return false;
}
}
bool clFileSystemWorkspace::CloseWorkspace()
{
if(!IsOpen()) {
return false;
}
DoClose();
return true;
}
void clFileSystemWorkspace::OnReloadWorkspace(clCommandEvent& event)
{
if(!IsOpen()) {
event.Skip();
return;
}
event.Skip(false);
wxString current_file = m_filename.GetFullPath();
if(CloseWorkspace()) {
OpenWorkspace(current_file);
}
}
wxString clFileSystemWorkspace::GetDebuggerName() const
{
if(GetConfig()) {
return GetConfig()->GetDebugger();
} else {
return wxEmptyString;
}
}
clEnvList_t clFileSystemWorkspace::GetEnvironment() const
{
clEnvList_t env_list;
auto config = clFileSystemWorkspace::Get().GetSettings().GetSelectedConfig();
if(config) {
const wxString& envstr = config->GetEnvironment();
env_list = StringUtils::BuildEnvFromString(envstr);
}
return env_list;
}
|