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.

794 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 (int i = 0; i < numElementsInArray (filesToKeep); ++i)
  248. if (filename == filesToKeep[i])
  249. return true;
  250. return false;
  251. }
  252. void writeMainProjectFile()
  253. {
  254. std::unique_ptr<XmlElement> xml (project.getProjectRoot().createXml());
  255. jassert (xml != nullptr);
  256. if (xml != nullptr)
  257. {
  258. MemoryOutputStream mo;
  259. mo.setNewLineString (projectLineFeed);
  260. xml->writeToStream (mo, String());
  261. replaceFileIfDifferent (projectFile, mo);
  262. }
  263. }
  264. static int findLongestModuleName (const OwnedArray<LibraryModule>& modules)
  265. {
  266. int longest = 0;
  267. for (int i = modules.size(); --i >= 0;)
  268. longest = jmax (longest, modules.getUnchecked(i)->getID().length());
  269. return longest;
  270. }
  271. File getAppConfigFile() const { return generatedCodeFolder.getChildFile (project.getAppConfigFilename()); }
  272. String loadUserContentFromAppConfig() const
  273. {
  274. StringArray userContent;
  275. bool foundCodeSection = false;
  276. auto lines = StringArray::fromLines (getAppConfigFile().loadFileAsString());
  277. for (int i = 0; i < lines.size(); ++i)
  278. {
  279. if (lines[i].contains ("[BEGIN_USER_CODE_SECTION]"))
  280. {
  281. for (int j = i + 1; j < lines.size() && ! lines[j].contains ("[END_USER_CODE_SECTION]"); ++j)
  282. userContent.add (lines[j]);
  283. foundCodeSection = true;
  284. break;
  285. }
  286. }
  287. if (! foundCodeSection)
  288. {
  289. userContent.add ({});
  290. userContent.add ("// (You can add your own code in this section, and the Projucer will not overwrite it)");
  291. userContent.add ({});
  292. }
  293. return userContent.joinIntoString (projectLineFeed) + projectLineFeed;
  294. }
  295. void checkModuleValidity (OwnedArray<LibraryModule>& modules)
  296. {
  297. if (project.getNumExporters() == 0)
  298. {
  299. addError ("No exporters found!\n"
  300. "Please add an exporter before saving.");
  301. return;
  302. }
  303. for (LibraryModule** moduleIter = modules.begin(); moduleIter != modules.end(); ++moduleIter)
  304. {
  305. if (auto* module = *moduleIter)
  306. {
  307. if (! module->isValid())
  308. {
  309. addError ("At least one of your JUCE module paths is invalid!\n"
  310. "Please go to the Modules settings page and ensure each path points to the correct JUCE modules folder.");
  311. return;
  312. }
  313. if (project.getEnabledModules().getExtraDependenciesNeeded (module->getID()).size() > 0)
  314. {
  315. addError ("At least one of your modules has missing dependencies!\n"
  316. "Please go to the settings page of the highlighted modules and add the required dependencies.");
  317. return;
  318. }
  319. }
  320. else
  321. {
  322. // this should never happen!
  323. jassertfalse;
  324. }
  325. }
  326. }
  327. void writeAppConfig (MemoryOutputStream& out, const OwnedArray<LibraryModule>& modules, const String& userContent)
  328. {
  329. writeAutoGenWarningComment (out);
  330. out << " There's a section below where you can add your own custom code safely, and the" << newLine
  331. << " Projucer will preserve the contents of that block, but the best way to change" << newLine
  332. << " any of these definitions is by using the Projucer's project settings." << newLine
  333. << newLine
  334. << " Any commented-out settings will assume their default values." << newLine
  335. << newLine
  336. << "*/" << newLine
  337. << newLine;
  338. out << "#pragma once" << newLine
  339. << newLine
  340. << "//==============================================================================" << newLine
  341. << "// [BEGIN_USER_CODE_SECTION]" << newLine
  342. << userContent
  343. << "// [END_USER_CODE_SECTION]" << newLine;
  344. out << newLine
  345. << "/*" << newLine
  346. << " ==============================================================================" << newLine
  347. << newLine
  348. << " In accordance with the terms of the JUCE 5 End-Use License Agreement, the" << newLine
  349. << " JUCE Code in SECTION A cannot be removed, changed or otherwise rendered" << newLine
  350. << " ineffective unless you have a JUCE Indie or Pro license, or are using JUCE" << newLine
  351. << " under the GPL v3 license." << newLine
  352. << newLine
  353. << " End User License Agreement: www.juce.com/juce-5-licence" << newLine
  354. << newLine
  355. << " ==============================================================================" << newLine
  356. << "*/" << newLine
  357. << newLine
  358. << "// BEGIN SECTION A" << newLine
  359. << newLine
  360. << "#ifndef JUCE_DISPLAY_SPLASH_SCREEN" << newLine
  361. << " #define JUCE_DISPLAY_SPLASH_SCREEN " << (project.shouldDisplaySplashScreen() ? "1" : "0") << newLine
  362. << "#endif" << newLine << newLine
  363. << "#ifndef JUCE_REPORT_APP_USAGE" << newLine
  364. << " #define JUCE_REPORT_APP_USAGE " << (project.shouldReportAppUsage() ? "1" : "0") << newLine
  365. << "#endif" << newLine
  366. << newLine
  367. << "// END SECTION A" << newLine
  368. << newLine
  369. << "#define JUCE_USE_DARK_SPLASH_SCREEN " << (project.getSplashScreenColourString() == "Dark" ? "1" : "0") << newLine;
  370. out << newLine
  371. << "//==============================================================================" << newLine;
  372. auto longestName = findLongestModuleName (modules);
  373. for (int k = 0; k < modules.size(); ++k)
  374. {
  375. auto* m = modules.getUnchecked(k);
  376. out << "#define JUCE_MODULE_AVAILABLE_" << m->getID()
  377. << String::repeatedString (" ", longestName + 5 - m->getID().length()) << " 1" << newLine;
  378. }
  379. out << newLine << "#define JUCE_GLOBAL_MODULE_SETTINGS_INCLUDED 1" << newLine;
  380. for (int j = 0; j < modules.size(); ++j)
  381. {
  382. auto* m = modules.getUnchecked(j);
  383. OwnedArray<Project::ConfigFlag> flags;
  384. m->getConfigFlags (project, flags);
  385. if (flags.size() > 0)
  386. {
  387. out << newLine
  388. << "//==============================================================================" << newLine
  389. << "// " << m->getID() << " flags:" << newLine;
  390. for (auto* flag : flags)
  391. {
  392. out << newLine
  393. << "#ifndef " << flag->symbol
  394. << newLine
  395. << (flag->value.isUsingDefault() ? " //#define " : " #define ") << flag->symbol << " " << (flag->value.get() ? "1" : "0")
  396. << newLine
  397. << "#endif"
  398. << newLine;
  399. }
  400. }
  401. }
  402. if (extraAppConfigContent.isNotEmpty())
  403. out << newLine << extraAppConfigContent.trimEnd() << newLine;
  404. {
  405. auto& type = project.getProjectType();
  406. auto isStandaloneApplication = (! type.isAudioPlugin() && ! type.isDynamicLibrary());
  407. out << newLine
  408. << "//==============================================================================" << newLine
  409. << "#ifndef JUCE_STANDALONE_APPLICATION" << newLine
  410. << " #if defined(JucePlugin_Name) && defined(JucePlugin_Build_Standalone)" << newLine
  411. << " #define JUCE_STANDALONE_APPLICATION JucePlugin_Build_Standalone" << newLine
  412. << " #else" << newLine
  413. << " #define JUCE_STANDALONE_APPLICATION " << (isStandaloneApplication ? "1" : "0") << newLine
  414. << " #endif" << newLine
  415. << "#endif" << newLine;
  416. }
  417. }
  418. void writeAppConfigFile (const OwnedArray<LibraryModule>& modules, const String& userContent)
  419. {
  420. appConfigFile = getAppConfigFile();
  421. MemoryOutputStream mem;
  422. mem.setNewLineString (projectLineFeed);
  423. writeAppConfig (mem, modules, userContent);
  424. saveGeneratedFile (project.getAppConfigFilename(), mem);
  425. }
  426. void writeAppHeader (MemoryOutputStream& out, const OwnedArray<LibraryModule>& modules)
  427. {
  428. writeAutoGenWarningComment (out);
  429. out << " This is the header file that your files should include in order to get all the" << newLine
  430. << " JUCE library headers. You should avoid including the JUCE headers directly in" << newLine
  431. << " your own source files, because that wouldn't pick up the correct configuration" << newLine
  432. << " options for your app." << newLine
  433. << newLine
  434. << "*/" << newLine << newLine;
  435. out << "#pragma once" << newLine << newLine;
  436. if (appConfigFile.exists())
  437. out << CodeHelpers::createIncludeStatement (project.getAppConfigFilename()) << newLine;
  438. if (modules.size() > 0)
  439. {
  440. out << newLine;
  441. for (int i = 0; i < modules.size(); ++i)
  442. modules.getUnchecked(i)->writeIncludes (*this, out);
  443. out << newLine;
  444. }
  445. if (hasBinaryData && project.shouldIncludeBinaryInJuceHeader())
  446. out << CodeHelpers::createIncludeStatement (project.getBinaryDataHeaderFile(), appConfigFile) << newLine;
  447. out << newLine
  448. << "#if ! DONT_SET_USING_JUCE_NAMESPACE" << newLine
  449. << " // If your code uses a lot of JUCE classes, then this will obviously save you" << newLine
  450. << " // a lot of typing, but can be disabled by setting DONT_SET_USING_JUCE_NAMESPACE." << newLine
  451. << " using namespace juce;" << newLine
  452. << "#endif" << newLine
  453. << newLine
  454. << "#if ! JUCE_DONT_DECLARE_PROJECTINFO" << newLine
  455. << "namespace ProjectInfo" << newLine
  456. << "{" << newLine
  457. << " const char* const projectName = " << CppTokeniserFunctions::addEscapeChars (project.getProjectNameString()).quoted() << ";" << newLine
  458. << " const char* const companyName = " << CppTokeniserFunctions::addEscapeChars (project.getCompanyNameString()).quoted() << ";" << newLine
  459. << " const char* const versionString = " << CppTokeniserFunctions::addEscapeChars (project.getVersionString()).quoted() << ";" << newLine
  460. << " const int versionNumber = " << project.getVersionAsHex() << ";" << newLine
  461. << "}" << newLine
  462. << "#endif" << newLine;
  463. }
  464. void writeAppHeader (const OwnedArray<LibraryModule>& modules)
  465. {
  466. MemoryOutputStream mem;
  467. mem.setNewLineString (projectLineFeed);
  468. writeAppHeader (mem, modules);
  469. saveGeneratedFile (project.getJuceSourceHFilename(), mem);
  470. }
  471. void writeModuleCppWrappers (const OwnedArray<LibraryModule>& modules)
  472. {
  473. for (auto* module : modules)
  474. {
  475. for (auto& cu : module->getAllCompileUnits())
  476. {
  477. MemoryOutputStream mem;
  478. mem.setNewLineString (projectLineFeed);
  479. writeAutoGenWarningComment (mem);
  480. mem << "*/" << newLine
  481. << newLine
  482. << "#include " << project.getAppConfigFilename().quoted() << newLine
  483. << "#include <";
  484. if (cu.file.getFileExtension() != ".r") // .r files are included without the path
  485. mem << module->getID() << "/";
  486. mem << cu.file.getFileName() << ">" << newLine;
  487. replaceFileIfDifferent (generatedCodeFolder.getChildFile (cu.getFilenameForProxyFile()), mem);
  488. }
  489. }
  490. }
  491. void writeBinaryDataFiles()
  492. {
  493. auto binaryDataH = project.getBinaryDataHeaderFile();
  494. ResourceFile resourceFile (project);
  495. if (resourceFile.getNumFiles() > 0)
  496. {
  497. auto dataNamespace = project.getBinaryDataNamespaceString().trim();
  498. if (dataNamespace.isEmpty())
  499. dataNamespace = "BinaryData";
  500. resourceFile.setClassName (dataNamespace);
  501. Array<File> binaryDataFiles;
  502. auto maxSize = project.getMaxBinaryFileSize();
  503. if (maxSize <= 0)
  504. maxSize = 10 * 1024 * 1024;
  505. auto r = resourceFile.write (binaryDataFiles, maxSize);
  506. if (r.wasOk())
  507. {
  508. hasBinaryData = true;
  509. for (int i = 0; i < binaryDataFiles.size(); ++i)
  510. {
  511. auto& f = binaryDataFiles.getReference(i);
  512. filesCreated.add (f);
  513. generatedFilesGroup.addFileRetainingSortOrder (f, ! f.hasFileExtension (".h"));
  514. }
  515. }
  516. else
  517. {
  518. addError (r.getErrorMessage());
  519. }
  520. }
  521. else
  522. {
  523. for (int i = 20; --i >= 0;)
  524. project.getBinaryDataCppFile (i).deleteFile();
  525. binaryDataH.deleteFile();
  526. }
  527. }
  528. void writeReadmeFile()
  529. {
  530. MemoryOutputStream out;
  531. out.setNewLineString (projectLineFeed);
  532. out << newLine
  533. << " Important Note!!" << newLine
  534. << " ================" << newLine
  535. << newLine
  536. << "The purpose of this folder is to contain files that are auto-generated by the Projucer," << newLine
  537. << "and ALL files in this folder will be mercilessly DELETED and completely re-written whenever" << newLine
  538. << "the Projucer saves your project." << newLine
  539. << newLine
  540. << "Therefore, it's a bad idea to make any manual changes to the files in here, or to" << newLine
  541. << "put any of your own files in here if you don't want to lose them. (Of course you may choose" << newLine
  542. << "to add the folder's contents to your version-control system so that you can re-merge your own" << newLine
  543. << "modifications after the Projucer has saved its changes)." << newLine;
  544. replaceFileIfDifferent (generatedCodeFolder.getChildFile ("ReadMe.txt"), out);
  545. }
  546. void addError (const String& message)
  547. {
  548. const ScopedLock sl (errorLock);
  549. errors.add (message);
  550. }
  551. void writePluginCharacteristicsFile();
  552. void writeUnityScriptFile()
  553. {
  554. auto unityScriptContents = replaceLineFeeds (BinaryData::jucer_UnityPluginGUIScript_cs,
  555. projectLineFeed);
  556. auto projectName = Project::addUnityPluginPrefixIfNecessary (project.getProjectNameString());
  557. unityScriptContents = unityScriptContents.replace ("%%plugin_class_name%%", projectName.replace (" ", "_"))
  558. .replace ("%%plugin_name%%", projectName)
  559. .replace ("%%plugin_vendor%%", project.getPluginManufacturerString())
  560. .replace ("%%plugin_description%%", project.getPluginDescriptionString());
  561. auto f = getGeneratedCodeFolder().getChildFile (project.getUnityScriptName());
  562. MemoryOutputStream out;
  563. out << unityScriptContents;
  564. replaceFileIfDifferent (f, out);
  565. }
  566. void writeProjects (const OwnedArray<LibraryModule>&, const String&, bool);
  567. void runPostExportScript()
  568. {
  569. #if JUCE_WINDOWS
  570. auto cmdString = project.getPostExportShellCommandWinString();
  571. #else
  572. auto cmdString = project.getPostExportShellCommandPosixString();
  573. #endif
  574. auto shellCommand = cmdString.replace ("%%1%%", project.getProjectFolder().getFullPathName());
  575. if (shellCommand.isNotEmpty())
  576. {
  577. #if JUCE_WINDOWS
  578. StringArray argList ("cmd.exe", "/c");
  579. #else
  580. StringArray argList ("/bin/sh", "-c");
  581. #endif
  582. argList.add (shellCommand);
  583. ChildProcess shellProcess;
  584. if (! shellProcess.start (argList))
  585. {
  586. addError ("Failed to run shell command: " + argList.joinIntoString (" "));
  587. return;
  588. }
  589. if (! shellProcess.waitForProcessToFinish (10000))
  590. {
  591. addError ("Timeout running shell command: " + argList.joinIntoString (" "));
  592. return;
  593. }
  594. auto exitCode = shellProcess.getExitCode();
  595. if (exitCode != 0)
  596. addError ("Shell command: " + argList.joinIntoString (" ") + " failed with exit code: " + String (exitCode));
  597. }
  598. }
  599. void saveExporter (ProjectExporter* exporter, const OwnedArray<LibraryModule>& modules)
  600. {
  601. try
  602. {
  603. exporter->create (modules);
  604. if (! exporter->isCLion())
  605. std::cout << "Finished saving: " << exporter->getName() << std::endl;
  606. }
  607. catch (ProjectExporter::SaveError& error)
  608. {
  609. addError (error.message);
  610. }
  611. }
  612. class ExporterJob : public ThreadPoolJob
  613. {
  614. public:
  615. ExporterJob (ProjectSaver& ps, ProjectExporter* pe,
  616. const OwnedArray<LibraryModule>& moduleList)
  617. : ThreadPoolJob ("export"),
  618. owner (ps), exporter (pe), modules (moduleList)
  619. {
  620. }
  621. JobStatus runJob() override
  622. {
  623. owner.saveExporter (exporter.get(), modules);
  624. return jobHasFinished;
  625. }
  626. private:
  627. ProjectSaver& owner;
  628. std::unique_ptr<ProjectExporter> exporter;
  629. const OwnedArray<LibraryModule>& modules;
  630. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ExporterJob)
  631. };
  632. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProjectSaver)
  633. };