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.

792 lines
28KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. #include "jucer_ResourceFile.h"
  21. #include "../Project/jucer_Module.h"
  22. #include "jucer_ProjectExporter.h"
  23. //==============================================================================
  24. class ProjectSaver
  25. {
  26. public:
  27. ProjectSaver (Project& p, const File& file)
  28. : project (p),
  29. projectFile (file),
  30. generatedCodeFolder (project.getGeneratedCodeFolder()),
  31. generatedFilesGroup (Project::Item::createGroup (project, getJuceCodeGroupName(), "__generatedcode__", true))
  32. {
  33. generatedFilesGroup.setID (getGeneratedGroupID());
  34. }
  35. struct SaveThread : public ThreadWithProgressWindow
  36. {
  37. public:
  38. SaveThread (ProjectSaver& ps, bool wait, const String& exp)
  39. : ThreadWithProgressWindow ("Saving...", true, false),
  40. saver (ps),
  41. shouldWaitAfterSaving (wait),
  42. specifiedExporterToSave (exp)
  43. {}
  44. void run() override
  45. {
  46. setProgress (-1);
  47. result = saver.save (false, shouldWaitAfterSaving, specifiedExporterToSave);
  48. }
  49. ProjectSaver& saver;
  50. Result result = Result::ok();
  51. bool shouldWaitAfterSaving;
  52. String specifiedExporterToSave;
  53. JUCE_DECLARE_NON_COPYABLE (SaveThread)
  54. };
  55. Result save (bool showProgressBox, bool waitAfterSaving, const String& specifiedExporterToSave)
  56. {
  57. if (showProgressBox)
  58. {
  59. SaveThread thread (*this, waitAfterSaving, specifiedExporterToSave);
  60. thread.runThread();
  61. return thread.result;
  62. }
  63. projectLineFeed = project.getProjectLineFeed();
  64. auto appConfigUserContent = loadUserContentFromAppConfig();
  65. auto oldFile = project.getFile();
  66. project.setFile (projectFile);
  67. OwnedArray<LibraryModule> modules;
  68. project.getEnabledModules().createRequiredModules (modules);
  69. checkModuleValidity (modules);
  70. if (errors.size() == 0)
  71. {
  72. writeMainProjectFile();
  73. project.updateModificationTime();
  74. auto projectRootHash = project.getProjectRoot().toXmlString().hashCode();
  75. if (project.getProjectType().isAudioPlugin())
  76. {
  77. writePluginCharacteristicsFile();
  78. if (project.shouldBuildUnityPlugin())
  79. writeUnityScriptFile();
  80. }
  81. writeAppConfigFile (modules, appConfigUserContent);
  82. writeBinaryDataFiles();
  83. writeAppHeader (modules);
  84. writeModuleCppWrappers (modules);
  85. writeProjects (modules, specifiedExporterToSave, ! showProgressBox);
  86. runPostExportScript();
  87. // if the project root has changed after writing the other files then re-save it
  88. if (project.getProjectRoot().toXmlString().hashCode() != projectRootHash)
  89. {
  90. writeMainProjectFile();
  91. project.updateModificationTime();
  92. }
  93. if (generatedCodeFolder.exists())
  94. {
  95. writeReadmeFile();
  96. deleteUnwantedFilesIn (generatedCodeFolder);
  97. }
  98. if (errors.size() == 0)
  99. {
  100. // Workaround for a bug where Xcode thinks the project is invalid if opened immedietely
  101. // after writing
  102. if (waitAfterSaving)
  103. Thread::sleep (2000);
  104. return Result::ok();
  105. }
  106. }
  107. project.setFile (oldFile);
  108. return Result::fail (errors[0]);
  109. }
  110. Result saveResourcesOnly()
  111. {
  112. writeBinaryDataFiles();
  113. if (errors.size() > 0)
  114. return Result::fail (errors[0]);
  115. return Result::ok();
  116. }
  117. Result saveContentNeededForLiveBuild()
  118. {
  119. OwnedArray<LibraryModule> modules;
  120. project.getEnabledModules().createRequiredModules (modules);
  121. checkModuleValidity (modules);
  122. if (errors.size() == 0)
  123. {
  124. if (project.getProjectType().isAudioPlugin())
  125. writePluginCharacteristicsFile();
  126. writeAppConfigFile (modules, loadUserContentFromAppConfig());
  127. writeBinaryDataFiles();
  128. writeAppHeader (modules);
  129. writeModuleCppWrappers (modules);
  130. return Result::ok();
  131. }
  132. return Result::fail (errors[0]);
  133. }
  134. Project::Item saveGeneratedFile (const String& filePath, const MemoryOutputStream& newData)
  135. {
  136. if (! generatedCodeFolder.createDirectory())
  137. {
  138. addError ("Couldn't create folder: " + generatedCodeFolder.getFullPathName());
  139. return Project::Item (project, ValueTree(), false);
  140. }
  141. auto file = generatedCodeFolder.getChildFile (filePath);
  142. if (replaceFileIfDifferent (file, newData))
  143. return addFileToGeneratedGroup (file);
  144. return { project, {}, true };
  145. }
  146. Project::Item addFileToGeneratedGroup (const File& file)
  147. {
  148. auto item = generatedFilesGroup.findItemForFile (file);
  149. if (item.isValid())
  150. return item;
  151. generatedFilesGroup.addFileAtIndex (file, -1, true);
  152. return generatedFilesGroup.findItemForFile (file);
  153. }
  154. void setExtraAppConfigFileContent (const String& content)
  155. {
  156. extraAppConfigContent = content;
  157. }
  158. static void writeAutoGenWarningComment (OutputStream& out)
  159. {
  160. out << "/*" << newLine << newLine
  161. << " IMPORTANT! This file is auto-generated each time you save your" << newLine
  162. << " project - if you alter its contents, your changes may be overwritten!" << newLine
  163. << newLine;
  164. }
  165. static const char* getGeneratedGroupID() noexcept { return "__jucelibfiles"; }
  166. Project::Item& getGeneratedCodeGroup() { return generatedFilesGroup; }
  167. static String getJuceCodeGroupName() { return "JUCE Library Code"; }
  168. File getGeneratedCodeFolder() const { return generatedCodeFolder; }
  169. bool replaceFileIfDifferent (const File& f, const MemoryOutputStream& newData)
  170. {
  171. filesCreated.add (f);
  172. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (f, newData))
  173. {
  174. addError ("Can't write to file: " + f.getFullPathName());
  175. return false;
  176. }
  177. return true;
  178. }
  179. static bool shouldFolderBeIgnoredWhenCopying (const File& f)
  180. {
  181. return f.getFileName() == ".git" || f.getFileName() == ".svn" || f.getFileName() == ".cvs";
  182. }
  183. bool copyFolder (const File& source, const File& dest)
  184. {
  185. if (source.isDirectory() && dest.createDirectory())
  186. {
  187. for (auto& f : source.findChildFiles (File::findFiles, false))
  188. {
  189. auto target = dest.getChildFile (f.getFileName());
  190. filesCreated.add (target);
  191. if (! f.copyFileTo (target))
  192. return false;
  193. }
  194. for (auto& f : source.findChildFiles (File::findDirectories, false))
  195. if (! shouldFolderBeIgnoredWhenCopying (f))
  196. if (! copyFolder (f, dest.getChildFile (f.getFileName())))
  197. return false;
  198. return true;
  199. }
  200. return false;
  201. }
  202. Project& project;
  203. SortedSet<File> filesCreated;
  204. private:
  205. const File projectFile, generatedCodeFolder;
  206. Project::Item generatedFilesGroup;
  207. String extraAppConfigContent;
  208. StringArray errors;
  209. CriticalSection errorLock;
  210. File appConfigFile;
  211. bool hasBinaryData = false;
  212. String projectLineFeed = "\r\n";
  213. // Recursively clears out any files in a folder that we didn't create, but avoids
  214. // any folders containing hidden files that might be used by version-control systems.
  215. bool deleteUnwantedFilesIn (const File& parent)
  216. {
  217. bool folderIsNowEmpty = true;
  218. DirectoryIterator i (parent, false, "*", File::findFilesAndDirectories);
  219. Array<File> filesToDelete;
  220. bool isFolder;
  221. while (i.next (&isFolder, nullptr, nullptr, nullptr, nullptr, nullptr))
  222. {
  223. auto f = i.getFile();
  224. if (filesCreated.contains (f) || shouldFileBeKept (f.getFileName()))
  225. {
  226. folderIsNowEmpty = false;
  227. }
  228. else if (isFolder)
  229. {
  230. if (deleteUnwantedFilesIn (f))
  231. filesToDelete.add (f);
  232. else
  233. folderIsNowEmpty = false;
  234. }
  235. else
  236. {
  237. filesToDelete.add (f);
  238. }
  239. }
  240. for (int j = filesToDelete.size(); --j >= 0;)
  241. filesToDelete.getReference(j).deleteRecursively();
  242. return folderIsNowEmpty;
  243. }
  244. static bool shouldFileBeKept (const String& filename)
  245. {
  246. static const char* filesToKeep[] = { ".svn", ".cvs", "CMakeLists.txt" };
  247. for (auto* f : filesToKeep)
  248. if (filename == f)
  249. return true;
  250. return false;
  251. }
  252. void writeMainProjectFile()
  253. {
  254. if (auto xml = project.getProjectRoot().createXml())
  255. {
  256. XmlElement::TextFormat format;
  257. format.newLineChars = projectLineFeed.toRawUTF8();
  258. MemoryOutputStream mo (8192);
  259. xml->writeTo (mo, format);
  260. replaceFileIfDifferent (projectFile, mo);
  261. }
  262. else
  263. {
  264. jassertfalse;
  265. }
  266. }
  267. static int findLongestModuleName (const OwnedArray<LibraryModule>& modules)
  268. {
  269. int longest = 0;
  270. for (auto& m : modules)
  271. longest = jmax (longest, m->getID().length());
  272. return longest;
  273. }
  274. File getAppConfigFile() const { return generatedCodeFolder.getChildFile (project.getAppConfigFilename()); }
  275. String loadUserContentFromAppConfig() const
  276. {
  277. StringArray userContent;
  278. bool foundCodeSection = false;
  279. auto lines = StringArray::fromLines (getAppConfigFile().loadFileAsString());
  280. for (int i = 0; i < lines.size(); ++i)
  281. {
  282. if (lines[i].contains ("[BEGIN_USER_CODE_SECTION]"))
  283. {
  284. for (int j = i + 1; j < lines.size() && ! lines[j].contains ("[END_USER_CODE_SECTION]"); ++j)
  285. userContent.add (lines[j]);
  286. foundCodeSection = true;
  287. break;
  288. }
  289. }
  290. if (! foundCodeSection)
  291. {
  292. userContent.add ({});
  293. userContent.add ("// (You can add your own code in this section, and the Projucer will not overwrite it)");
  294. userContent.add ({});
  295. }
  296. return userContent.joinIntoString (projectLineFeed) + projectLineFeed;
  297. }
  298. void checkModuleValidity (OwnedArray<LibraryModule>& modules)
  299. {
  300. if (project.getNumExporters() == 0)
  301. {
  302. addError ("No exporters found!\n"
  303. "Please add an exporter before saving.");
  304. return;
  305. }
  306. for (auto moduleIter = modules.begin(); moduleIter != modules.end(); ++moduleIter)
  307. {
  308. if (auto* module = *moduleIter)
  309. {
  310. if (! module->isValid())
  311. {
  312. addError ("At least one of your JUCE module paths is invalid!\n"
  313. "Please go to the Modules settings page and ensure each path points to the correct JUCE modules folder.");
  314. return;
  315. }
  316. if (project.getEnabledModules().getExtraDependenciesNeeded (module->getID()).size() > 0)
  317. {
  318. addError ("At least one of your modules has missing dependencies!\n"
  319. "Please go to the settings page of the highlighted modules and add the required dependencies.");
  320. return;
  321. }
  322. }
  323. else
  324. {
  325. // this should never happen!
  326. jassertfalse;
  327. }
  328. }
  329. }
  330. void writeAppConfig (MemoryOutputStream& out, const OwnedArray<LibraryModule>& modules, const String& userContent)
  331. {
  332. writeAutoGenWarningComment (out);
  333. out << " There's a section below where you can add your own custom code safely, and the" << newLine
  334. << " Projucer will preserve the contents of that block, but the best way to change" << newLine
  335. << " any of these definitions is by using the Projucer's project settings." << newLine
  336. << newLine
  337. << " Any commented-out settings will assume their default values." << newLine
  338. << newLine
  339. << "*/" << newLine
  340. << newLine;
  341. out << "#pragma once" << newLine
  342. << newLine
  343. << "//==============================================================================" << newLine
  344. << "// [BEGIN_USER_CODE_SECTION]" << newLine
  345. << userContent
  346. << "// [END_USER_CODE_SECTION]" << newLine;
  347. out << newLine
  348. << "/*" << newLine
  349. << " ==============================================================================" << newLine
  350. << newLine
  351. << " In accordance with the terms of the JUCE 5 End-Use License Agreement, the" << newLine
  352. << " JUCE Code in SECTION A cannot be removed, changed or otherwise rendered" << newLine
  353. << " ineffective unless you have a JUCE Indie or Pro license, or are using JUCE" << newLine
  354. << " under the GPL v3 license." << newLine
  355. << newLine
  356. << " End User License Agreement: www.juce.com/juce-5-licence" << newLine
  357. << newLine
  358. << " ==============================================================================" << newLine
  359. << "*/" << newLine
  360. << newLine
  361. << "// BEGIN SECTION A" << newLine
  362. << newLine
  363. << "#ifndef JUCE_DISPLAY_SPLASH_SCREEN" << newLine
  364. << " #define JUCE_DISPLAY_SPLASH_SCREEN " << (project.shouldDisplaySplashScreen() ? "1" : "0") << newLine
  365. << "#endif" << newLine << newLine
  366. << "#ifndef JUCE_REPORT_APP_USAGE" << newLine
  367. << " #define JUCE_REPORT_APP_USAGE " << (project.shouldReportAppUsage() ? "1" : "0") << newLine
  368. << "#endif" << newLine
  369. << newLine
  370. << "// END SECTION A" << newLine
  371. << newLine
  372. << "#define JUCE_USE_DARK_SPLASH_SCREEN " << (project.getSplashScreenColourString() == "Dark" ? "1" : "0") << newLine;
  373. out << newLine
  374. << "//==============================================================================" << newLine;
  375. auto longestName = findLongestModuleName (modules);
  376. for (auto& m : modules)
  377. out << "#define JUCE_MODULE_AVAILABLE_" << m->getID()
  378. << String::repeatedString (" ", longestName + 5 - m->getID().length()) << " 1" << newLine;
  379. out << newLine << "#define JUCE_GLOBAL_MODULE_SETTINGS_INCLUDED 1" << newLine;
  380. for (auto& m : modules)
  381. {
  382. OwnedArray<Project::ConfigFlag> flags;
  383. m->getConfigFlags (project, flags);
  384. if (flags.size() > 0)
  385. {
  386. out << newLine
  387. << "//==============================================================================" << newLine
  388. << "// " << m->getID() << " flags:" << newLine;
  389. for (auto* flag : flags)
  390. {
  391. out << newLine
  392. << "#ifndef " << flag->symbol
  393. << newLine
  394. << (flag->value.isUsingDefault() ? " //#define " : " #define ") << flag->symbol << " " << (flag->value.get() ? "1" : "0")
  395. << newLine
  396. << "#endif"
  397. << newLine;
  398. }
  399. }
  400. }
  401. if (extraAppConfigContent.isNotEmpty())
  402. out << newLine << extraAppConfigContent.trimEnd() << newLine;
  403. {
  404. auto& type = project.getProjectType();
  405. auto isStandaloneApplication = (! type.isAudioPlugin() && ! type.isDynamicLibrary());
  406. out << newLine
  407. << "//==============================================================================" << newLine
  408. << "#ifndef JUCE_STANDALONE_APPLICATION" << newLine
  409. << " #if defined(JucePlugin_Name) && defined(JucePlugin_Build_Standalone)" << newLine
  410. << " #define JUCE_STANDALONE_APPLICATION JucePlugin_Build_Standalone" << newLine
  411. << " #else" << newLine
  412. << " #define JUCE_STANDALONE_APPLICATION " << (isStandaloneApplication ? "1" : "0") << newLine
  413. << " #endif" << newLine
  414. << "#endif" << newLine;
  415. }
  416. }
  417. void writeAppConfigFile (const OwnedArray<LibraryModule>& modules, const String& userContent)
  418. {
  419. appConfigFile = getAppConfigFile();
  420. MemoryOutputStream mem;
  421. mem.setNewLineString (projectLineFeed);
  422. writeAppConfig (mem, modules, userContent);
  423. saveGeneratedFile (project.getAppConfigFilename(), mem);
  424. }
  425. void writeAppHeader (MemoryOutputStream& out, const OwnedArray<LibraryModule>& modules)
  426. {
  427. writeAutoGenWarningComment (out);
  428. out << " This is the header file that your files should include in order to get all the" << newLine
  429. << " JUCE library headers. You should avoid including the JUCE headers directly in" << newLine
  430. << " your own source files, because that wouldn't pick up the correct configuration" << newLine
  431. << " options for your app." << newLine
  432. << newLine
  433. << "*/" << newLine << newLine;
  434. out << "#pragma once" << newLine << newLine;
  435. if (appConfigFile.exists())
  436. out << CodeHelpers::createIncludeStatement (project.getAppConfigFilename()) << newLine;
  437. if (modules.size() > 0)
  438. {
  439. out << newLine;
  440. for (int i = 0; i < modules.size(); ++i)
  441. modules.getUnchecked(i)->writeIncludes (*this, out);
  442. out << newLine;
  443. }
  444. if (hasBinaryData && project.shouldIncludeBinaryInJuceHeader())
  445. out << CodeHelpers::createIncludeStatement (project.getBinaryDataHeaderFile(), appConfigFile) << newLine;
  446. out << newLine
  447. << "#if ! DONT_SET_USING_JUCE_NAMESPACE" << newLine
  448. << " // If your code uses a lot of JUCE classes, then this will obviously save you" << newLine
  449. << " // a lot of typing, but can be disabled by setting DONT_SET_USING_JUCE_NAMESPACE." << newLine
  450. << " using namespace juce;" << newLine
  451. << "#endif" << newLine
  452. << newLine
  453. << "#if ! JUCE_DONT_DECLARE_PROJECTINFO" << newLine
  454. << "namespace ProjectInfo" << newLine
  455. << "{" << newLine
  456. << " const char* const projectName = " << CppTokeniserFunctions::addEscapeChars (project.getProjectNameString()).quoted() << ";" << newLine
  457. << " const char* const companyName = " << CppTokeniserFunctions::addEscapeChars (project.getCompanyNameString()).quoted() << ";" << newLine
  458. << " const char* const versionString = " << CppTokeniserFunctions::addEscapeChars (project.getVersionString()).quoted() << ";" << newLine
  459. << " const int versionNumber = " << project.getVersionAsHex() << ";" << newLine
  460. << "}" << newLine
  461. << "#endif" << newLine;
  462. }
  463. void writeAppHeader (const OwnedArray<LibraryModule>& modules)
  464. {
  465. MemoryOutputStream mem;
  466. mem.setNewLineString (projectLineFeed);
  467. writeAppHeader (mem, modules);
  468. saveGeneratedFile (project.getJuceSourceHFilename(), mem);
  469. }
  470. void writeModuleCppWrappers (const OwnedArray<LibraryModule>& modules)
  471. {
  472. for (auto* module : modules)
  473. {
  474. for (auto& cu : module->getAllCompileUnits())
  475. {
  476. MemoryOutputStream mem;
  477. mem.setNewLineString (projectLineFeed);
  478. writeAutoGenWarningComment (mem);
  479. mem << "*/" << newLine
  480. << newLine
  481. << "#include " << project.getAppConfigFilename().quoted() << newLine
  482. << "#include <";
  483. if (cu.file.getFileExtension() != ".r") // .r files are included without the path
  484. mem << module->getID() << "/";
  485. mem << cu.file.getFileName() << ">" << newLine;
  486. replaceFileIfDifferent (generatedCodeFolder.getChildFile (cu.getFilenameForProxyFile()), mem);
  487. }
  488. }
  489. }
  490. void writeBinaryDataFiles()
  491. {
  492. auto binaryDataH = project.getBinaryDataHeaderFile();
  493. ResourceFile resourceFile (project);
  494. if (resourceFile.getNumFiles() > 0)
  495. {
  496. auto dataNamespace = project.getBinaryDataNamespaceString().trim();
  497. if (dataNamespace.isEmpty())
  498. dataNamespace = "BinaryData";
  499. resourceFile.setClassName (dataNamespace);
  500. Array<File> binaryDataFiles;
  501. auto maxSize = project.getMaxBinaryFileSize();
  502. if (maxSize <= 0)
  503. maxSize = 10 * 1024 * 1024;
  504. auto r = resourceFile.write (binaryDataFiles, maxSize);
  505. if (r.wasOk())
  506. {
  507. hasBinaryData = true;
  508. for (auto& f : binaryDataFiles)
  509. {
  510. filesCreated.add (f);
  511. generatedFilesGroup.addFileRetainingSortOrder (f, ! f.hasFileExtension (".h"));
  512. }
  513. }
  514. else
  515. {
  516. addError (r.getErrorMessage());
  517. }
  518. }
  519. else
  520. {
  521. for (int i = 20; --i >= 0;)
  522. project.getBinaryDataCppFile (i).deleteFile();
  523. binaryDataH.deleteFile();
  524. }
  525. }
  526. void writeReadmeFile()
  527. {
  528. MemoryOutputStream out;
  529. out.setNewLineString (projectLineFeed);
  530. out << newLine
  531. << " Important Note!!" << newLine
  532. << " ================" << newLine
  533. << newLine
  534. << "The purpose of this folder is to contain files that are auto-generated by the Projucer," << newLine
  535. << "and ALL files in this folder will be mercilessly DELETED and completely re-written whenever" << newLine
  536. << "the Projucer saves your project." << newLine
  537. << newLine
  538. << "Therefore, it's a bad idea to make any manual changes to the files in here, or to" << newLine
  539. << "put any of your own files in here if you don't want to lose them. (Of course you may choose" << newLine
  540. << "to add the folder's contents to your version-control system so that you can re-merge your own" << newLine
  541. << "modifications after the Projucer has saved its changes)." << newLine;
  542. replaceFileIfDifferent (generatedCodeFolder.getChildFile ("ReadMe.txt"), out);
  543. }
  544. void addError (const String& message)
  545. {
  546. const ScopedLock sl (errorLock);
  547. errors.add (message);
  548. }
  549. void writePluginCharacteristicsFile();
  550. void writeUnityScriptFile()
  551. {
  552. auto unityScriptContents = replaceLineFeeds (BinaryData::jucer_UnityPluginGUIScript_cs,
  553. projectLineFeed);
  554. auto projectName = Project::addUnityPluginPrefixIfNecessary (project.getProjectNameString());
  555. unityScriptContents = unityScriptContents.replace ("%%plugin_class_name%%", projectName.replace (" ", "_"))
  556. .replace ("%%plugin_name%%", projectName)
  557. .replace ("%%plugin_vendor%%", project.getPluginManufacturerString())
  558. .replace ("%%plugin_description%%", project.getPluginDescriptionString());
  559. auto f = getGeneratedCodeFolder().getChildFile (project.getUnityScriptName());
  560. MemoryOutputStream out;
  561. out << unityScriptContents;
  562. replaceFileIfDifferent (f, out);
  563. }
  564. void writeProjects (const OwnedArray<LibraryModule>&, const String&, bool);
  565. void runPostExportScript()
  566. {
  567. #if JUCE_WINDOWS
  568. auto cmdString = project.getPostExportShellCommandWinString();
  569. #else
  570. auto cmdString = project.getPostExportShellCommandPosixString();
  571. #endif
  572. auto shellCommand = cmdString.replace ("%%1%%", project.getProjectFolder().getFullPathName());
  573. if (shellCommand.isNotEmpty())
  574. {
  575. #if JUCE_WINDOWS
  576. StringArray argList ("cmd.exe", "/c");
  577. #else
  578. StringArray argList ("/bin/sh", "-c");
  579. #endif
  580. argList.add (shellCommand);
  581. ChildProcess shellProcess;
  582. if (! shellProcess.start (argList))
  583. {
  584. addError ("Failed to run shell command: " + argList.joinIntoString (" "));
  585. return;
  586. }
  587. if (! shellProcess.waitForProcessToFinish (10000))
  588. {
  589. addError ("Timeout running shell command: " + argList.joinIntoString (" "));
  590. return;
  591. }
  592. auto exitCode = shellProcess.getExitCode();
  593. if (exitCode != 0)
  594. addError ("Shell command: " + argList.joinIntoString (" ") + " failed with exit code: " + String (exitCode));
  595. }
  596. }
  597. void saveExporter (ProjectExporter* exporter, const OwnedArray<LibraryModule>& modules)
  598. {
  599. try
  600. {
  601. exporter->create (modules);
  602. if (! exporter->isCLion())
  603. std::cout << "Finished saving: " << exporter->getName() << std::endl;
  604. }
  605. catch (ProjectExporter::SaveError& error)
  606. {
  607. addError (error.message);
  608. }
  609. }
  610. class ExporterJob : public ThreadPoolJob
  611. {
  612. public:
  613. ExporterJob (ProjectSaver& ps, ProjectExporter* pe,
  614. const OwnedArray<LibraryModule>& moduleList)
  615. : ThreadPoolJob ("export"),
  616. owner (ps), exporter (pe), modules (moduleList)
  617. {
  618. }
  619. JobStatus runJob() override
  620. {
  621. owner.saveExporter (exporter.get(), modules);
  622. return jobHasFinished;
  623. }
  624. private:
  625. ProjectSaver& owner;
  626. std::unique_ptr<ProjectExporter> exporter;
  627. const OwnedArray<LibraryModule>& modules;
  628. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ExporterJob)
  629. };
  630. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProjectSaver)
  631. };