The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

900 lines
29KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "../Application/jucer_Application.h"
  19. #include "../Project Saving/jucer_ProjectExporter.h"
  20. #include "projucer_MessageIDs.h"
  21. #include "projucer_CppHelpers.h"
  22. #include "projucer_SourceCodeRange.h"
  23. #include "projucer_ClassDatabase.h"
  24. #include "projucer_DiagnosticMessage.h"
  25. #include "projucer_ProjectBuildInfo.h"
  26. #include "projucer_ClientServerMessages.h"
  27. #include "projucer_CompileEngineClient.h"
  28. #include "../LiveBuildEngine/projucer_CompileEngineServer.h"
  29. #ifndef RUN_CLANG_IN_CHILD_PROCESS
  30. #error
  31. #endif
  32. //==============================================================================
  33. namespace ProjectProperties
  34. {
  35. const Identifier liveSettingsType ("LIVE_SETTINGS");
  36. #if JUCE_MAC
  37. const Identifier liveSettingsSubtype ("OSX");
  38. #elif JUCE_WINDOWS
  39. const Identifier liveSettingsSubtype ("WINDOWS");
  40. #elif JUCE_LINUX
  41. const Identifier liveSettingsSubtype ("LINUX");
  42. #endif
  43. static ValueTree getLiveSettings (Project& project)
  44. {
  45. return project.getProjectRoot().getOrCreateChildWithName (liveSettingsType, nullptr)
  46. .getOrCreateChildWithName (liveSettingsSubtype, nullptr);
  47. }
  48. static const ValueTree getLiveSettingsConst (Project& project)
  49. {
  50. return project.getProjectRoot().getChildWithName (liveSettingsType)
  51. .getChildWithName (liveSettingsSubtype);
  52. }
  53. static Value getLiveSetting (Project& p, const Identifier& i) { return getLiveSettings (p).getPropertyAsValue (i, p.getUndoManagerFor (getLiveSettings (p))); }
  54. static var getLiveSettingVar (Project& p, const Identifier& i) { return getLiveSettingsConst (p) [i]; }
  55. static Value getUserHeaderPathValue (Project& p) { return getLiveSetting (p, Ids::headerPath); }
  56. static String getUserHeaderPathString (Project& p) { return getLiveSettingVar (p, Ids::headerPath); }
  57. static Value getSystemHeaderPathValue (Project& p) { return getLiveSetting (p, Ids::systemHeaderPath); }
  58. static String getSystemHeaderPathString (Project& p) { return getLiveSettingVar (p, Ids::systemHeaderPath); }
  59. static Value getExtraDLLsValue (Project& p) { return getLiveSetting (p, Ids::extraDLLs); }
  60. static String getExtraDLLsString (Project& p) { return getLiveSettingVar (p, Ids::extraDLLs); }
  61. static Value getExtraCompilerFlagsValue (Project& p) { return getLiveSetting (p, Ids::extraCompilerFlags); }
  62. static String getExtraCompilerFlagsString (Project& p) { return getLiveSettingVar (p, Ids::extraCompilerFlags); }
  63. static Value getExtraPreprocessorDefsValue (Project& p) { return getLiveSetting (p, Ids::defines); }
  64. static String getExtraPreprocessorDefsString (Project& p) { return getLiveSettingVar (p, Ids::defines); }
  65. static File getProjucerTempFolder()
  66. {
  67. #if JUCE_MAC
  68. return File ("~/Library/Caches/com.juce.projucer");
  69. #else
  70. return File::getSpecialLocation (File::tempDirectory).getChildFile ("com.juce.projucer");
  71. #endif
  72. }
  73. static File getCacheLocation (Project& project)
  74. {
  75. return getProjucerTempFolder()
  76. .getChildFile ("Intermediate Files")
  77. .getChildFile (project.getProjectFilenameRoot() + "_" + project.getProjectUID());
  78. }
  79. }
  80. //==============================================================================
  81. void LiveBuildProjectSettings::getLiveSettings (Project& project, PropertyListBuilder& props)
  82. {
  83. using namespace ProjectProperties;
  84. props.addSearchPathProperty (getUserHeaderPathValue (project), "User header paths", "User header search paths.");
  85. props.addSearchPathProperty (getSystemHeaderPathValue (project), "System header paths", "System header search paths.");
  86. props.add (new TextPropertyComponent (getExtraPreprocessorDefsValue (project), "Preprocessor Definitions", 32768, true),
  87. "Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace or commas "
  88. "to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  89. props.add (new TextPropertyComponent (getExtraCompilerFlagsValue (project), "Extra compiler flags", 2048, true),
  90. "Extra command-line flags to be passed to the compiler. This string can contain references to preprocessor"
  91. " definitions in the form ${NAME_OF_DEFINITION}, which will be replaced with their values.");
  92. props.add (new TextPropertyComponent (getExtraDLLsValue (project), "Extra dynamic libraries", 2048, true),
  93. "Extra dynamic libs that the running code may require. Use new-lines or commas to separate the items");
  94. }
  95. void LiveBuildProjectSettings::updateNewlyOpenedProject (Project&) { /* placeholder */ }
  96. bool LiveBuildProjectSettings::isBuildDisabled (Project& p)
  97. {
  98. const bool defaultBuildDisabled = true;
  99. return p.getStoredProperties().getBoolValue ("buildDisabled", defaultBuildDisabled);
  100. }
  101. void LiveBuildProjectSettings::setBuildDisabled (Project& p, bool b) { p.getStoredProperties().setValue ("buildDisabled", b); }
  102. bool LiveBuildProjectSettings::areWarningsDisabled (Project& p) { return p.getStoredProperties().getBoolValue ("warningsDisabled"); }
  103. void LiveBuildProjectSettings::setWarningsDisabled (Project& p, bool b) { p.getStoredProperties().setValue ("warningsDisabled", b); }
  104. //==============================================================================
  105. class ClientIPC : public MessageHandler,
  106. private InterprocessConnection,
  107. private Timer
  108. {
  109. public:
  110. ClientIPC (CompileEngineChildProcess& cp)
  111. : InterprocessConnection (true), owner (cp)
  112. {
  113. launchServer();
  114. }
  115. ~ClientIPC()
  116. {
  117. #if RUN_CLANG_IN_CHILD_PROCESS
  118. if (childProcess.isRunning())
  119. killServerPolitely();
  120. #endif
  121. }
  122. void launchServer()
  123. {
  124. DBG ("Client: Launching Server...");
  125. const String pipeName ("ipc_" + String::toHexString (Random().nextInt64()));
  126. const String command (createCommandLineForLaunchingServer (pipeName,
  127. owner.project.getProjectUID(),
  128. ProjectProperties::getCacheLocation (owner.project)));
  129. #if RUN_CLANG_IN_CHILD_PROCESS
  130. if (! childProcess.start (command))
  131. {
  132. jassertfalse;
  133. }
  134. #else
  135. server = createClangServer (command);
  136. #endif
  137. bool ok = connectToPipe (pipeName, 10000);
  138. jassert (ok);
  139. if (ok)
  140. MessageTypes::sendPing (*this);
  141. startTimer (serverKeepAliveTimeout);
  142. }
  143. void killServerPolitely()
  144. {
  145. DBG ("Client: Killing Server...");
  146. MessageTypes::sendQuit (*this);
  147. disconnect();
  148. stopTimer();
  149. #if RUN_CLANG_IN_CHILD_PROCESS
  150. childProcess.waitForProcessToFinish (5000);
  151. #endif
  152. killServerWithoutMercy();
  153. }
  154. void killServerWithoutMercy()
  155. {
  156. disconnect();
  157. stopTimer();
  158. #if RUN_CLANG_IN_CHILD_PROCESS
  159. childProcess.kill();
  160. #else
  161. destroyClangServer (server);
  162. server = nullptr;
  163. #endif
  164. }
  165. void connectionMade()
  166. {
  167. DBG ("Client: connected");
  168. stopTimer();
  169. }
  170. void connectionLost()
  171. {
  172. DBG ("Client: disconnected");
  173. startTimer (100);
  174. }
  175. bool sendMessage (const ValueTree& m)
  176. {
  177. return InterprocessConnection::sendMessage (MessageHandler::convertMessage (m));
  178. }
  179. void messageReceived (const MemoryBlock& message)
  180. {
  181. #if RUN_CLANG_IN_CHILD_PROCESS
  182. startTimer (serverKeepAliveTimeout);
  183. #else
  184. stopTimer();
  185. #endif
  186. MessageTypes::dispatchToClient (owner, MessageHandler::convertMessage (message));
  187. }
  188. enum { serverKeepAliveTimeout = 10000 };
  189. private:
  190. CompileEngineChildProcess& owner;
  191. #if RUN_CLANG_IN_CHILD_PROCESS
  192. ChildProcess childProcess;
  193. #else
  194. void* server;
  195. #endif
  196. void timerCallback() { owner.handleCrash (String()); }
  197. };
  198. //==============================================================================
  199. class CompileEngineChildProcess::ChildProcess : private ValueTree::Listener,
  200. private Timer
  201. {
  202. public:
  203. ChildProcess (CompileEngineChildProcess& proc, Project& p)
  204. : owner (proc), project (p)
  205. {
  206. projectRoot = project.getProjectRoot();
  207. restartServer();
  208. projectRoot.addListener (this);
  209. openedOk = true;
  210. }
  211. ~ChildProcess()
  212. {
  213. projectRoot.removeListener (this);
  214. if (isRunningApp && server != nullptr)
  215. server->killServerWithoutMercy();
  216. server = nullptr;
  217. }
  218. void restartServer()
  219. {
  220. server = nullptr;
  221. server = new ClientIPC (owner);
  222. sendRebuild();
  223. }
  224. void sendRebuild()
  225. {
  226. stopTimer();
  227. ProjectBuildInfo build;
  228. if (! doesProjectMatchSavedHeaderState (project))
  229. {
  230. MessageTypes::sendNewBuild (*server, build);
  231. owner.errorList.resetToError ("Project structure does not match the saved headers! "
  232. "Please re-save your project to enable compilation");
  233. return;
  234. }
  235. build.setSystemIncludes (getSystemIncludePaths());
  236. build.setUserIncludes (getUserIncludes());
  237. build.setGlobalDefs (getGlobalDefs (project));
  238. build.setCompileFlags (ProjectProperties::getExtraCompilerFlagsString (project).trim());
  239. build.setExtraDLLs (getExtraDLLs());
  240. build.setJuceModulesFolder (EnabledModuleList::findDefaultModulesFolder (project).getFullPathName());
  241. build.setUtilsCppInclude (project.getAppIncludeFile().getFullPathName());
  242. scanForProjectFiles (project, build);
  243. owner.updateAllEditors();
  244. MessageTypes::sendNewBuild (*server, build);
  245. }
  246. void cleanAll()
  247. {
  248. MessageTypes::sendCleanAll (*server);
  249. sendRebuild();
  250. }
  251. void reinstantiatePreviews()
  252. {
  253. MessageTypes::sendReinstantiate (*server);
  254. }
  255. bool launchApp()
  256. {
  257. MessageTypes::sendLaunchApp (*server);
  258. return true;
  259. }
  260. ScopedPointer<ClientIPC> server;
  261. bool openedOk = false;
  262. bool isRunningApp = false;
  263. private:
  264. CompileEngineChildProcess& owner;
  265. Project& project;
  266. ValueTree projectRoot;
  267. void projectStructureChanged()
  268. {
  269. startTimer (100);
  270. }
  271. void timerCallback() override
  272. {
  273. sendRebuild();
  274. }
  275. void valueTreePropertyChanged (ValueTree&, const Identifier&) override { projectStructureChanged(); }
  276. void valueTreeChildAdded (ValueTree&, ValueTree&) override { projectStructureChanged(); }
  277. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override { projectStructureChanged(); }
  278. void valueTreeParentChanged (ValueTree&) override { projectStructureChanged(); }
  279. void valueTreeChildOrderChanged (ValueTree&, int, int) override {}
  280. static String getGlobalDefs (Project& proj)
  281. {
  282. String defs (ProjectProperties::getExtraPreprocessorDefsString (proj));
  283. for (Project::ExporterIterator exporter (proj); exporter.next();)
  284. if (exporter->canLaunchProject())
  285. defs << " " << exporter->getExporterIdentifierMacro() << "=1";
  286. return defs;
  287. }
  288. static void scanProjectItem (const Project::Item& projectItem, Array<File>& compileUnits, Array<File>& userFiles)
  289. {
  290. if (projectItem.isGroup())
  291. {
  292. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  293. scanProjectItem (projectItem.getChild(i), compileUnits, userFiles);
  294. return;
  295. }
  296. if (projectItem.shouldBeCompiled())
  297. {
  298. const File f (projectItem.getFile());
  299. if (f.exists())
  300. compileUnits.add (f);
  301. }
  302. if (projectItem.shouldBeAddedToTargetProject() && ! projectItem.shouldBeAddedToBinaryResources())
  303. {
  304. const File f (projectItem.getFile());
  305. if (f.exists())
  306. userFiles.add (f);
  307. }
  308. }
  309. void scanForProjectFiles (Project& proj, ProjectBuildInfo& build)
  310. {
  311. Array<File> compileUnits, userFiles;
  312. scanProjectItem (proj.getMainGroup(), compileUnits, userFiles);
  313. {
  314. OwnedArray<LibraryModule> modules;
  315. proj.getModules().createRequiredModules (modules);
  316. for (Project::ExporterIterator exporter (proj); exporter.next();)
  317. {
  318. if (exporter->canLaunchProject())
  319. {
  320. for (const LibraryModule* m : modules)
  321. {
  322. const File localModuleFolder = proj.getModules().shouldCopyModuleFilesLocally (m->moduleInfo.getID()).getValue()
  323. ? proj.getLocalModuleFolder (m->moduleInfo.getID())
  324. : m->moduleInfo.getFolder();
  325. m->findAndAddCompiledUnits (*exporter, nullptr, compileUnits);
  326. }
  327. break;
  328. }
  329. }
  330. }
  331. for (int i = 0; ; ++i)
  332. {
  333. const File binaryDataCpp (proj.getBinaryDataCppFile (i));
  334. if (! binaryDataCpp.exists())
  335. break;
  336. compileUnits.add (binaryDataCpp);
  337. }
  338. for (int i = compileUnits.size(); --i >= 0;)
  339. if (compileUnits.getReference(i).hasFileExtension (".r"))
  340. compileUnits.remove (i);
  341. build.setFiles (compileUnits, userFiles);
  342. }
  343. static bool doesProjectMatchSavedHeaderState (Project& project)
  344. {
  345. ValueTree liveModules (project.getProjectRoot().getChildWithName (Ids::MODULES));
  346. ScopedPointer<XmlElement> xml (XmlDocument::parse (project.getFile()));
  347. if (xml == nullptr || ! xml->hasTagName (Ids::JUCERPROJECT.toString()))
  348. return false;
  349. ValueTree diskModules (ValueTree::fromXml (*xml).getChildWithName (Ids::MODULES));
  350. return liveModules.isEquivalentTo (diskModules);
  351. }
  352. StringArray getUserIncludes()
  353. {
  354. StringArray paths;
  355. paths.add (project.getGeneratedCodeFolder().getFullPathName());
  356. paths.addArray (getSearchPathsFromString (ProjectProperties::getUserHeaderPathString (project)));
  357. return convertSearchPathsToAbsolute (paths);
  358. }
  359. StringArray getSystemIncludePaths()
  360. {
  361. StringArray paths;
  362. paths.addArray (getSearchPathsFromString (ProjectProperties::getSystemHeaderPathString (project)));
  363. if (project.getProjectType().isAudioPlugin())
  364. {
  365. paths.add (getAppSettings().getGlobalPath (Ids::vst2Path, TargetOS::getThisOS()).toString());
  366. paths.add (getAppSettings().getGlobalPath (Ids::vst3Path, TargetOS::getThisOS()).toString());
  367. }
  368. OwnedArray<LibraryModule> modules;
  369. project.getModules().createRequiredModules (modules);
  370. for (auto* module : modules)
  371. paths.addIfNotAlreadyThere (module->getFolder().getParentDirectory().getFullPathName());
  372. return convertSearchPathsToAbsolute (paths);
  373. }
  374. StringArray convertSearchPathsToAbsolute (const StringArray& paths) const
  375. {
  376. StringArray s;
  377. const File root (project.getProjectFolder());
  378. for (String p : paths)
  379. s.add (root.getChildFile (p).getFullPathName());
  380. return s;
  381. }
  382. StringArray getExtraDLLs()
  383. {
  384. StringArray dlls;
  385. dlls.addTokens (ProjectProperties::getExtraDLLsString (project), "\n\r,", StringRef());
  386. dlls.trim();
  387. dlls.removeEmptyStrings();
  388. return dlls;
  389. }
  390. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcess)
  391. };
  392. //==============================================================================
  393. CompileEngineChildProcess::CompileEngineChildProcess (Project& p)
  394. : project (p),
  395. continuousRebuild (false)
  396. {
  397. ProjucerApplication::getApp().openDocumentManager.addListener (this);
  398. createProcess();
  399. errorList.setWarningsEnabled (! LiveBuildProjectSettings::areWarningsDisabled (project));
  400. }
  401. CompileEngineChildProcess::~CompileEngineChildProcess()
  402. {
  403. ProjucerApplication::getApp().openDocumentManager.removeListener (this);
  404. process = nullptr;
  405. lastComponentList.clear();
  406. }
  407. void CompileEngineChildProcess::createProcess()
  408. {
  409. jassert (process == nullptr);
  410. process = new ChildProcess (*this, project);
  411. if (! process->openedOk)
  412. process = nullptr;
  413. updateAllEditors();
  414. }
  415. void CompileEngineChildProcess::cleanAll()
  416. {
  417. if (process != nullptr)
  418. process->cleanAll();
  419. }
  420. void CompileEngineChildProcess::openPreview (const ClassDatabase::Class& comp)
  421. {
  422. if (process != nullptr)
  423. {
  424. MainWindow* projectWindow = nullptr;
  425. OwnedArray<MainWindow>& windows = ProjucerApplication::getApp().mainWindowList.windows;
  426. for (int i = 0; i < windows.size(); ++i)
  427. {
  428. if (MainWindow* w = windows[i])
  429. {
  430. if (w->getProject() == &project)
  431. {
  432. projectWindow = w;
  433. break;
  434. }
  435. }
  436. }
  437. Rectangle<int> mainWindowRect;
  438. if (projectWindow != nullptr)
  439. mainWindowRect = projectWindow->getBounds();
  440. MessageTypes::sendOpenPreview (*process->server, comp, mainWindowRect);
  441. }
  442. }
  443. void CompileEngineChildProcess::reinstantiatePreviews()
  444. {
  445. if (process != nullptr)
  446. process->reinstantiatePreviews();
  447. }
  448. void CompileEngineChildProcess::processActivationChanged (bool isForeground)
  449. {
  450. if (process != nullptr)
  451. MessageTypes::sendProcessActivationState (*process->server, isForeground);
  452. }
  453. //==============================================================================
  454. bool CompileEngineChildProcess::canLaunchApp() const
  455. {
  456. return process != nullptr
  457. && runningAppProcess == nullptr
  458. && activityList.getNumActivities() == 0
  459. && errorList.getNumErrors() == 0;
  460. }
  461. void CompileEngineChildProcess::launchApp()
  462. {
  463. if (process != nullptr)
  464. process->launchApp();
  465. }
  466. bool CompileEngineChildProcess::canKillApp() const
  467. {
  468. return runningAppProcess != nullptr;
  469. }
  470. void CompileEngineChildProcess::killApp()
  471. {
  472. runningAppProcess = nullptr;
  473. }
  474. void CompileEngineChildProcess::handleAppLaunched()
  475. {
  476. runningAppProcess = process;
  477. runningAppProcess->isRunningApp = true;
  478. createProcess();
  479. }
  480. void CompileEngineChildProcess::handleAppQuit()
  481. {
  482. DBG ("handleAppQuit");
  483. runningAppProcess = nullptr;
  484. }
  485. //==============================================================================
  486. struct CompileEngineChildProcess::Editor : private CodeDocument::Listener,
  487. private Timer
  488. {
  489. Editor (CompileEngineChildProcess& ccp, const File& f, CodeDocument& doc)
  490. : owner (ccp), file (f), document (doc), transactionTimer (doc)
  491. {
  492. sendFullUpdate();
  493. document.addListener (this);
  494. }
  495. ~Editor()
  496. {
  497. document.removeListener (this);
  498. }
  499. void codeDocumentTextInserted (const String& newText, int insertIndex) override
  500. {
  501. CodeChange (Range<int> (insertIndex, insertIndex), newText).addToList (pendingChanges);
  502. startEditorChangeTimer();
  503. transactionTimer.stopTimer();
  504. owner.lastComponentList.globalNamespace
  505. .nudgeAllCodeRanges (file.getFullPathName(), insertIndex, newText.length());
  506. }
  507. void codeDocumentTextDeleted (int start, int end) override
  508. {
  509. CodeChange (Range<int> (start, end), String()).addToList (pendingChanges);
  510. startEditorChangeTimer();
  511. transactionTimer.stopTimer();
  512. owner.lastComponentList.globalNamespace
  513. .nudgeAllCodeRanges (file.getFullPathName(), start, start - end);
  514. }
  515. void sendFullUpdate()
  516. {
  517. reset();
  518. if (owner.process != nullptr)
  519. MessageTypes::sendFileContentFullUpdate (*owner.process->server, file, document.getAllContent());
  520. }
  521. bool flushEditorChanges()
  522. {
  523. if (pendingChanges.size() > 0)
  524. {
  525. if (owner.process != nullptr && owner.process->server != nullptr)
  526. MessageTypes::sendFileChanges (*owner.process->server, pendingChanges, file);
  527. reset();
  528. return true;
  529. }
  530. stopTimer();
  531. return false;
  532. }
  533. void reset()
  534. {
  535. stopTimer();
  536. pendingChanges.clear();
  537. }
  538. void startTransactionTimer()
  539. {
  540. transactionTimer.startTimer (1000);
  541. }
  542. void startEditorChangeTimer()
  543. {
  544. startTimer (200);
  545. }
  546. CompileEngineChildProcess& owner;
  547. File file;
  548. CodeDocument& document;
  549. private:
  550. Array<CodeChange> pendingChanges;
  551. void timerCallback() override
  552. {
  553. if (owner.continuousRebuild)
  554. flushEditorChanges();
  555. else
  556. stopTimer();
  557. }
  558. struct TransactionTimer : public Timer
  559. {
  560. TransactionTimer (CodeDocument& doc) : document (doc) {}
  561. void timerCallback() override
  562. {
  563. stopTimer();
  564. document.newTransaction();
  565. }
  566. CodeDocument& document;
  567. };
  568. TransactionTimer transactionTimer;
  569. };
  570. void CompileEngineChildProcess::editorOpened (const File& file, CodeDocument& document)
  571. {
  572. editors.add (new Editor (*this, file, document));
  573. }
  574. bool CompileEngineChildProcess::documentAboutToClose (OpenDocumentManager::Document* document)
  575. {
  576. for (int i = editors.size(); --i >= 0;)
  577. {
  578. if (document->getFile() == editors.getUnchecked(i)->file)
  579. {
  580. const File f (editors.getUnchecked(i)->file);
  581. editors.remove (i);
  582. if (process != nullptr)
  583. MessageTypes::sendHandleFileReset (*process->server, f);
  584. }
  585. }
  586. return true;
  587. }
  588. void CompileEngineChildProcess::updateAllEditors()
  589. {
  590. for (int i = editors.size(); --i >= 0;)
  591. editors.getUnchecked(i)->sendFullUpdate();
  592. }
  593. //==============================================================================
  594. void CompileEngineChildProcess::handleCrash (const String& message)
  595. {
  596. Logger::writeToLog ("*** Child process crashed: " + message);
  597. if (crashHandler != nullptr)
  598. crashHandler (message);
  599. }
  600. void CompileEngineChildProcess::handleNewDiagnosticList (const ValueTree& l) { errorList.setList (l); }
  601. void CompileEngineChildProcess::handleActivityListChanged (const StringArray& l) { activityList.setList (l); }
  602. void CompileEngineChildProcess::handleCloseIDE()
  603. {
  604. if (JUCEApplication* app = JUCEApplication::getInstance())
  605. app->systemRequestedQuit();
  606. }
  607. void CompileEngineChildProcess::handleMissingSystemHeaders()
  608. {
  609. if (ProjectContentComponent* p = findProjectContentComponent())
  610. p->handleMissingSystemHeaders();
  611. }
  612. void CompileEngineChildProcess::handleKeyPress (const String& className, const KeyPress& key)
  613. {
  614. ApplicationCommandManager& commandManager = ProjucerApplication::getCommandManager();
  615. CommandID command = commandManager.getKeyMappings()->findCommandForKeyPress (key);
  616. if (command == StandardApplicationCommandIDs::undo)
  617. {
  618. handleUndoInEditor (className);
  619. }
  620. else if (command == StandardApplicationCommandIDs::redo)
  621. {
  622. handleRedoInEditor (className);
  623. }
  624. else if (ApplicationCommandTarget* const target = ApplicationCommandManager::findTargetForComponent (findProjectContentComponent()))
  625. {
  626. commandManager.setFirstCommandTarget (target);
  627. commandManager.getKeyMappings()->keyPressed (key, findProjectContentComponent());
  628. commandManager.setFirstCommandTarget (nullptr);
  629. }
  630. }
  631. void CompileEngineChildProcess::handleUndoInEditor (const String& /*className*/)
  632. {
  633. }
  634. void CompileEngineChildProcess::handleRedoInEditor (const String& /*className*/)
  635. {
  636. }
  637. void CompileEngineChildProcess::handleClassListChanged (const ValueTree& newList)
  638. {
  639. lastComponentList = ClassDatabase::ClassList::fromValueTree (newList);
  640. activityList.sendClassListChangedMessage (lastComponentList);
  641. }
  642. void CompileEngineChildProcess::handleBuildFailed()
  643. {
  644. if (errorList.getNumErrors() > 0)
  645. ProjucerApplication::getCommandManager().invokeDirectly (CommandIDs::showBuildTab, true);
  646. ProjucerApplication::getCommandManager().commandStatusChanged();
  647. }
  648. void CompileEngineChildProcess::handleChangeCode (const SourceCodeRange& location, const String& newText)
  649. {
  650. if (Editor* ed = getOrOpenEditorFor (location.file))
  651. {
  652. if (ed->flushEditorChanges())
  653. return; // client-side editor changes were pending, so deal with them first, and discard
  654. // the incoming change, whose position may now be wrong.
  655. ed->document.deleteSection (location.range.getStart(), location.range.getEnd());
  656. ed->document.insertText (location.range.getStart(), newText);
  657. // deliberately clear the messages that we just added, to avoid these changes being
  658. // sent to the server (which will already have processed the same ones locally)
  659. ed->reset();
  660. ed->startTransactionTimer();
  661. }
  662. }
  663. void CompileEngineChildProcess::handlePing()
  664. {
  665. }
  666. //==============================================================================
  667. void CompileEngineChildProcess::setContinuousRebuild (bool b)
  668. {
  669. continuousRebuild = b;
  670. }
  671. void CompileEngineChildProcess::flushEditorChanges()
  672. {
  673. for (Editor* ed : editors)
  674. ed->flushEditorChanges();
  675. }
  676. ProjectContentComponent* CompileEngineChildProcess::findProjectContentComponent() const
  677. {
  678. for (MainWindow* mw : ProjucerApplication::getApp().mainWindowList.windows)
  679. if (mw->getProject() == &project)
  680. return mw->getProjectContentComponent();
  681. return nullptr;
  682. }
  683. CompileEngineChildProcess::Editor* CompileEngineChildProcess::getOrOpenEditorFor (const File& file)
  684. {
  685. for (Editor* ed : editors)
  686. if (ed->file == file)
  687. return ed;
  688. if (ProjectContentComponent* pcc = findProjectContentComponent())
  689. if (pcc->showEditorForFile (file, false))
  690. return getOrOpenEditorFor (file);
  691. return nullptr;
  692. }
  693. void CompileEngineChildProcess::handleHighlightCode (const SourceCodeRange& location)
  694. {
  695. ProjectContentComponent* pcc = findProjectContentComponent();
  696. if (pcc != nullptr && pcc->showEditorForFile (location.file, false))
  697. {
  698. SourceCodeEditor* sce = dynamic_cast <SourceCodeEditor*> (pcc->getEditorComponent());
  699. if (sce != nullptr && sce->editor != nullptr)
  700. {
  701. sce->highlight (location.range, true);
  702. Process::makeForegroundProcess();
  703. CodeEditorComponent& ed = *sce->editor;
  704. ed.getTopLevelComponent()->toFront (false);
  705. ed.grabKeyboardFocus();
  706. }
  707. }
  708. }
  709. void CompileEngineChildProcess::cleanAllCachedFilesForProject (Project& p)
  710. {
  711. File cacheFolder (ProjectProperties::getCacheLocation (p));
  712. if (cacheFolder.isDirectory())
  713. cacheFolder.deleteRecursively();
  714. }