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.

753 lines
27KB

  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. // if the project root has changed after writing the other files then re-save it
  87. if (project.getProjectRoot().toXmlString().hashCode() != projectRootHash)
  88. {
  89. writeMainProjectFile();
  90. project.updateModificationTime();
  91. }
  92. if (generatedCodeFolder.exists())
  93. {
  94. writeReadmeFile();
  95. deleteUnwantedFilesIn (generatedCodeFolder);
  96. }
  97. if (errors.size() == 0)
  98. {
  99. // Workaround for a bug where Xcode thinks the project is invalid if opened immedietely
  100. // after writing
  101. if (waitAfterSaving)
  102. Thread::sleep (2000);
  103. return Result::ok();
  104. }
  105. }
  106. project.setFile (oldFile);
  107. return Result::fail (errors[0]);
  108. }
  109. Result saveResourcesOnly()
  110. {
  111. writeBinaryDataFiles();
  112. if (errors.size() > 0)
  113. return Result::fail (errors[0]);
  114. return Result::ok();
  115. }
  116. Result saveContentNeededForLiveBuild()
  117. {
  118. OwnedArray<LibraryModule> modules;
  119. project.getEnabledModules().createRequiredModules (modules);
  120. checkModuleValidity (modules);
  121. if (errors.size() == 0)
  122. {
  123. if (project.getProjectType().isAudioPlugin())
  124. writePluginCharacteristicsFile();
  125. writeAppConfigFile (modules, loadUserContentFromAppConfig());
  126. writeBinaryDataFiles();
  127. writeAppHeader (modules);
  128. writeModuleCppWrappers (modules);
  129. return Result::ok();
  130. }
  131. return Result::fail (errors[0]);
  132. }
  133. Project::Item saveGeneratedFile (const String& filePath, const MemoryOutputStream& newData)
  134. {
  135. if (! generatedCodeFolder.createDirectory())
  136. {
  137. addError ("Couldn't create folder: " + generatedCodeFolder.getFullPathName());
  138. return Project::Item (project, ValueTree(), false);
  139. }
  140. auto file = generatedCodeFolder.getChildFile (filePath);
  141. if (replaceFileIfDifferent (file, newData))
  142. return addFileToGeneratedGroup (file);
  143. return { project, {}, true };
  144. }
  145. Project::Item addFileToGeneratedGroup (const File& file)
  146. {
  147. auto item = generatedFilesGroup.findItemForFile (file);
  148. if (item.isValid())
  149. return item;
  150. generatedFilesGroup.addFileAtIndex (file, -1, true);
  151. return generatedFilesGroup.findItemForFile (file);
  152. }
  153. void setExtraAppConfigFileContent (const String& content)
  154. {
  155. extraAppConfigContent = content;
  156. }
  157. static void writeAutoGenWarningComment (OutputStream& out)
  158. {
  159. out << "/*" << newLine << newLine
  160. << " IMPORTANT! This file is auto-generated each time you save your" << newLine
  161. << " project - if you alter its contents, your changes may be overwritten!" << newLine
  162. << newLine;
  163. }
  164. static const char* getGeneratedGroupID() noexcept { return "__jucelibfiles"; }
  165. Project::Item& getGeneratedCodeGroup() { return generatedFilesGroup; }
  166. static String getJuceCodeGroupName() { return "JUCE Library Code"; }
  167. File getGeneratedCodeFolder() const { return generatedCodeFolder; }
  168. bool replaceFileIfDifferent (const File& f, const MemoryOutputStream& newData)
  169. {
  170. filesCreated.add (f);
  171. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (f, newData))
  172. {
  173. addError ("Can't write to file: " + f.getFullPathName());
  174. return false;
  175. }
  176. return true;
  177. }
  178. static bool shouldFolderBeIgnoredWhenCopying (const File& f)
  179. {
  180. return f.getFileName() == ".git" || f.getFileName() == ".svn" || f.getFileName() == ".cvs";
  181. }
  182. bool copyFolder (const File& source, const File& dest)
  183. {
  184. if (source.isDirectory() && dest.createDirectory())
  185. {
  186. for (auto& f : source.findChildFiles (File::findFiles, false))
  187. {
  188. auto target = dest.getChildFile (f.getFileName());
  189. filesCreated.add (target);
  190. if (! f.copyFileTo (target))
  191. return false;
  192. }
  193. for (auto& f : source.findChildFiles (File::findDirectories, false))
  194. if (! shouldFolderBeIgnoredWhenCopying (f))
  195. if (! copyFolder (f, dest.getChildFile (f.getFileName())))
  196. return false;
  197. return true;
  198. }
  199. return false;
  200. }
  201. Project& project;
  202. SortedSet<File> filesCreated;
  203. private:
  204. const File projectFile, generatedCodeFolder;
  205. Project::Item generatedFilesGroup;
  206. String extraAppConfigContent;
  207. StringArray errors;
  208. CriticalSection errorLock;
  209. File appConfigFile;
  210. bool hasBinaryData = false;
  211. String projectLineFeed = "\r\n";
  212. // Recursively clears out any files in a folder that we didn't create, but avoids
  213. // any folders containing hidden files that might be used by version-control systems.
  214. bool deleteUnwantedFilesIn (const File& parent)
  215. {
  216. bool folderIsNowEmpty = true;
  217. DirectoryIterator i (parent, false, "*", File::findFilesAndDirectories);
  218. Array<File> filesToDelete;
  219. bool isFolder;
  220. while (i.next (&isFolder, nullptr, nullptr, nullptr, nullptr, nullptr))
  221. {
  222. auto f = i.getFile();
  223. if (filesCreated.contains (f) || shouldFileBeKept (f.getFileName()))
  224. {
  225. folderIsNowEmpty = false;
  226. }
  227. else if (isFolder)
  228. {
  229. if (deleteUnwantedFilesIn (f))
  230. filesToDelete.add (f);
  231. else
  232. folderIsNowEmpty = false;
  233. }
  234. else
  235. {
  236. filesToDelete.add (f);
  237. }
  238. }
  239. for (int j = filesToDelete.size(); --j >= 0;)
  240. filesToDelete.getReference(j).deleteRecursively();
  241. return folderIsNowEmpty;
  242. }
  243. static bool shouldFileBeKept (const String& filename)
  244. {
  245. static const char* filesToKeep[] = { ".svn", ".cvs", "CMakeLists.txt" };
  246. for (int i = 0; i < numElementsInArray (filesToKeep); ++i)
  247. if (filename == filesToKeep[i])
  248. return true;
  249. return false;
  250. }
  251. void writeMainProjectFile()
  252. {
  253. std::unique_ptr<XmlElement> xml (project.getProjectRoot().createXml());
  254. jassert (xml != nullptr);
  255. if (xml != nullptr)
  256. {
  257. MemoryOutputStream mo;
  258. mo.setNewLineString (projectLineFeed);
  259. xml->writeToStream (mo, String());
  260. replaceFileIfDifferent (projectFile, mo);
  261. }
  262. }
  263. static int findLongestModuleName (const OwnedArray<LibraryModule>& modules)
  264. {
  265. int longest = 0;
  266. for (int i = modules.size(); --i >= 0;)
  267. longest = jmax (longest, modules.getUnchecked(i)->getID().length());
  268. return longest;
  269. }
  270. File getAppConfigFile() const { return generatedCodeFolder.getChildFile (project.getAppConfigFilename()); }
  271. String loadUserContentFromAppConfig() const
  272. {
  273. StringArray userContent;
  274. bool foundCodeSection = false;
  275. auto lines = StringArray::fromLines (getAppConfigFile().loadFileAsString());
  276. for (int i = 0; i < lines.size(); ++i)
  277. {
  278. if (lines[i].contains ("[BEGIN_USER_CODE_SECTION]"))
  279. {
  280. for (int j = i + 1; j < lines.size() && ! lines[j].contains ("[END_USER_CODE_SECTION]"); ++j)
  281. userContent.add (lines[j]);
  282. foundCodeSection = true;
  283. break;
  284. }
  285. }
  286. if (! foundCodeSection)
  287. {
  288. userContent.add ({});
  289. userContent.add ("// (You can add your own code in this section, and the Projucer will not overwrite it)");
  290. userContent.add ({});
  291. }
  292. return userContent.joinIntoString (projectLineFeed) + projectLineFeed;
  293. }
  294. void checkModuleValidity (OwnedArray<LibraryModule>& modules)
  295. {
  296. if (project.getNumExporters() == 0)
  297. {
  298. addError ("No exporters found!\n"
  299. "Please add an exporter before saving.");
  300. return;
  301. }
  302. for (LibraryModule** moduleIter = modules.begin(); moduleIter != modules.end(); ++moduleIter)
  303. {
  304. if (auto* module = *moduleIter)
  305. {
  306. if (! module->isValid())
  307. {
  308. addError ("At least one of your JUCE module paths is invalid!\n"
  309. "Please go to the Modules settings page and ensure each path points to the correct JUCE modules folder.");
  310. return;
  311. }
  312. if (project.getEnabledModules().getExtraDependenciesNeeded (module->getID()).size() > 0)
  313. {
  314. addError ("At least one of your modules has missing dependencies!\n"
  315. "Please go to the settings page of the highlighted modules and add the required dependencies.");
  316. return;
  317. }
  318. }
  319. else
  320. {
  321. // this should never happen!
  322. jassertfalse;
  323. }
  324. }
  325. }
  326. void writeAppConfig (MemoryOutputStream& out, const OwnedArray<LibraryModule>& modules, const String& userContent)
  327. {
  328. writeAutoGenWarningComment (out);
  329. out << " There's a section below where you can add your own custom code safely, and the" << newLine
  330. << " Projucer will preserve the contents of that block, but the best way to change" << newLine
  331. << " any of these definitions is by using the Projucer's project settings." << newLine
  332. << newLine
  333. << " Any commented-out settings will assume their default values." << newLine
  334. << newLine
  335. << "*/" << newLine
  336. << newLine;
  337. out << "#pragma once" << newLine
  338. << newLine
  339. << "//==============================================================================" << newLine
  340. << "// [BEGIN_USER_CODE_SECTION]" << newLine
  341. << userContent
  342. << "// [END_USER_CODE_SECTION]" << newLine;
  343. out << newLine
  344. << "/*" << newLine
  345. << " ==============================================================================" << newLine
  346. << newLine
  347. << " In accordance with the terms of the JUCE 5 End-Use License Agreement, the" << newLine
  348. << " JUCE Code in SECTION A cannot be removed, changed or otherwise rendered" << newLine
  349. << " ineffective unless you have a JUCE Indie or Pro license, or are using JUCE" << newLine
  350. << " under the GPL v3 license." << newLine
  351. << newLine
  352. << " End User License Agreement: www.juce.com/juce-5-licence" << newLine
  353. << newLine
  354. << " ==============================================================================" << newLine
  355. << "*/" << newLine
  356. << newLine
  357. << "// BEGIN SECTION A" << newLine
  358. << newLine
  359. << "#ifndef JUCE_DISPLAY_SPLASH_SCREEN" << newLine
  360. << " #define JUCE_DISPLAY_SPLASH_SCREEN " << (project.shouldDisplaySplashScreen() ? "1" : "0") << newLine
  361. << "#endif" << newLine << newLine
  362. << "#ifndef JUCE_REPORT_APP_USAGE" << newLine
  363. << " #define JUCE_REPORT_APP_USAGE " << (project.shouldReportAppUsage() ? "1" : "0") << newLine
  364. << "#endif" << newLine
  365. << newLine
  366. << "// END SECTION A" << newLine
  367. << newLine
  368. << "#define JUCE_USE_DARK_SPLASH_SCREEN " << (project.getSplashScreenColourString() == "Dark" ? "1" : "0") << newLine;
  369. out << newLine
  370. << "//==============================================================================" << newLine;
  371. auto longestName = findLongestModuleName (modules);
  372. for (int k = 0; k < modules.size(); ++k)
  373. {
  374. auto* m = modules.getUnchecked(k);
  375. out << "#define JUCE_MODULE_AVAILABLE_" << m->getID()
  376. << String::repeatedString (" ", longestName + 5 - m->getID().length()) << " 1" << newLine;
  377. }
  378. out << newLine << "#define JUCE_GLOBAL_MODULE_SETTINGS_INCLUDED 1" << newLine;
  379. for (int j = 0; j < modules.size(); ++j)
  380. {
  381. auto* m = modules.getUnchecked(j);
  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 (int i = 0; i < binaryDataFiles.size(); ++i)
  509. {
  510. auto& f = binaryDataFiles.getReference(i);
  511. filesCreated.add (f);
  512. generatedFilesGroup.addFileRetainingSortOrder (f, ! f.hasFileExtension (".h"));
  513. }
  514. }
  515. else
  516. {
  517. addError (r.getErrorMessage());
  518. }
  519. }
  520. else
  521. {
  522. for (int i = 20; --i >= 0;)
  523. project.getBinaryDataCppFile (i).deleteFile();
  524. binaryDataH.deleteFile();
  525. }
  526. }
  527. void writeReadmeFile()
  528. {
  529. MemoryOutputStream out;
  530. out.setNewLineString (projectLineFeed);
  531. out << newLine
  532. << " Important Note!!" << newLine
  533. << " ================" << newLine
  534. << newLine
  535. << "The purpose of this folder is to contain files that are auto-generated by the Projucer," << newLine
  536. << "and ALL files in this folder will be mercilessly DELETED and completely re-written whenever" << newLine
  537. << "the Projucer saves your project." << newLine
  538. << newLine
  539. << "Therefore, it's a bad idea to make any manual changes to the files in here, or to" << newLine
  540. << "put any of your own files in here if you don't want to lose them. (Of course you may choose" << newLine
  541. << "to add the folder's contents to your version-control system so that you can re-merge your own" << newLine
  542. << "modifications after the Projucer has saved its changes)." << newLine;
  543. replaceFileIfDifferent (generatedCodeFolder.getChildFile ("ReadMe.txt"), out);
  544. }
  545. void addError (const String& message)
  546. {
  547. const ScopedLock sl (errorLock);
  548. errors.add (message);
  549. }
  550. void writePluginCharacteristicsFile();
  551. void writeUnityScriptFile()
  552. {
  553. auto unityScriptContents = replaceLineFeeds (BinaryData::jucer_UnityPluginGUIScript_cs,
  554. projectLineFeed);
  555. auto projectName = Project::addUnityPluginPrefixIfNecessary (project.getProjectNameString());
  556. unityScriptContents = unityScriptContents.replace ("%%plugin_class_name%%", projectName.replace (" ", "_"))
  557. .replace ("%%plugin_name%%", projectName)
  558. .replace ("%%plugin_vendor%%", project.getPluginManufacturerString())
  559. .replace ("%%plugin_description%%", project.getPluginDescriptionString());
  560. auto f = getGeneratedCodeFolder().getChildFile (project.getUnityScriptName());
  561. MemoryOutputStream out;
  562. out << unityScriptContents;
  563. replaceFileIfDifferent (f, out);
  564. }
  565. void writeProjects (const OwnedArray<LibraryModule>&, const String&, bool);
  566. void saveExporter (ProjectExporter* exporter, const OwnedArray<LibraryModule>& modules)
  567. {
  568. try
  569. {
  570. exporter->create (modules);
  571. if (! exporter->isCLion())
  572. std::cout << "Finished saving: " << exporter->getName() << std::endl;
  573. }
  574. catch (ProjectExporter::SaveError& error)
  575. {
  576. addError (error.message);
  577. }
  578. }
  579. class ExporterJob : public ThreadPoolJob
  580. {
  581. public:
  582. ExporterJob (ProjectSaver& ps, ProjectExporter* pe,
  583. const OwnedArray<LibraryModule>& moduleList)
  584. : ThreadPoolJob ("export"),
  585. owner (ps), exporter (pe), modules (moduleList)
  586. {
  587. }
  588. JobStatus runJob() override
  589. {
  590. owner.saveExporter (exporter.get(), modules);
  591. return jobHasFinished;
  592. }
  593. private:
  594. ProjectSaver& owner;
  595. std::unique_ptr<ProjectExporter> exporter;
  596. const OwnedArray<LibraryModule>& modules;
  597. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ExporterJob)
  598. };
  599. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProjectSaver)
  600. };