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.

845 lines
29KB

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