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.

873 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. #include "../Application/jucer_Headers.h"
  20. #include "../ProjectSaving/jucer_ProjectSaver.h"
  21. #include "../ProjectSaving/jucer_ProjectExport_Xcode.h"
  22. #include "../Application/jucer_Application.h"
  23. //==============================================================================
  24. static var parseModuleDesc (const StringArray& lines)
  25. {
  26. DynamicObject* o = new DynamicObject();
  27. var result (o);
  28. for (auto line : lines)
  29. {
  30. line = trimCommentCharsFromStartOfLine (line);
  31. auto colon = line.indexOfChar (':');
  32. if (colon >= 0)
  33. {
  34. auto key = line.substring (0, colon).trim();
  35. auto value = line.substring (colon + 1).trim();
  36. o->setProperty (key, value);
  37. }
  38. }
  39. return result;
  40. }
  41. static var parseModuleDesc (const File& header)
  42. {
  43. StringArray lines;
  44. header.readLines (lines);
  45. for (int i = 0; i < lines.size(); ++i)
  46. {
  47. if (trimCommentCharsFromStartOfLine (lines[i]).startsWith ("BEGIN_JUCE_MODULE_DECLARATION"))
  48. {
  49. StringArray desc;
  50. for (int j = i + 1; j < lines.size(); ++j)
  51. {
  52. if (trimCommentCharsFromStartOfLine (lines[j]).startsWith ("END_JUCE_MODULE_DECLARATION"))
  53. return parseModuleDesc (desc);
  54. desc.add (lines[j]);
  55. }
  56. break;
  57. }
  58. }
  59. return {};
  60. }
  61. ModuleDescription::ModuleDescription (const File& folder)
  62. : moduleFolder (folder),
  63. moduleInfo (parseModuleDesc (getHeader()))
  64. {
  65. }
  66. File ModuleDescription::getHeader() const
  67. {
  68. if (moduleFolder != File())
  69. {
  70. const char* extensions[] = { ".h", ".hpp", ".hxx" };
  71. for (auto e : extensions)
  72. {
  73. auto header = moduleFolder.getChildFile (moduleFolder.getFileName() + e);
  74. if (header.existsAsFile())
  75. return header;
  76. }
  77. }
  78. return {};
  79. }
  80. StringArray ModuleDescription::getDependencies() const
  81. {
  82. auto deps = StringArray::fromTokens (moduleInfo ["dependencies"].toString(), " \t;,", "\"'");
  83. deps.trim();
  84. deps.removeEmptyStrings();
  85. return deps;
  86. }
  87. //==============================================================================
  88. static bool tryToAddModuleFromFolder (const File& path, ModuleIDAndFolderList& list)
  89. {
  90. ModuleDescription m (path);
  91. if (m.isValid())
  92. {
  93. list.push_back ({ m.getID(), path });
  94. return true;
  95. }
  96. return false;
  97. }
  98. static void addAllModulesInSubfoldersRecursively (const File& path, int depth, ModuleIDAndFolderList& list)
  99. {
  100. if (depth > 0)
  101. {
  102. for (DirectoryIterator iter (path, false, "*", File::findDirectories); iter.next();)
  103. {
  104. if (auto* job = ThreadPoolJob::getCurrentThreadPoolJob())
  105. if (job->shouldExit())
  106. return;
  107. auto childPath = iter.getFile().getLinkedTarget();
  108. if (! tryToAddModuleFromFolder (childPath, list))
  109. addAllModulesInSubfoldersRecursively (childPath, depth - 1, list);
  110. }
  111. }
  112. }
  113. static void addAllModulesInFolder (const File& path, ModuleIDAndFolderList& list)
  114. {
  115. if (! tryToAddModuleFromFolder (path, list))
  116. {
  117. int subfolders = 3;
  118. addAllModulesInSubfoldersRecursively (path, subfolders, list);
  119. }
  120. }
  121. static void sort (ModuleIDAndFolderList& listToSort)
  122. {
  123. std::sort (listToSort.begin(), listToSort.end(), [] (const ModuleIDAndFolder& m1, const ModuleIDAndFolder& m2)
  124. {
  125. return m1.first.compareIgnoreCase (m2.first) < 0;
  126. });
  127. }
  128. //==============================================================================
  129. struct ModuleScannerJob : public ThreadPoolJob
  130. {
  131. ModuleScannerJob (const Array<File>& paths, std::function<void (const ModuleIDAndFolderList&)>&& callback)
  132. : ThreadPoolJob ("ModuleScannerJob"),
  133. pathsToScan (paths),
  134. completionCallback (std::move (callback))
  135. {
  136. }
  137. JobStatus runJob() override
  138. {
  139. ModuleIDAndFolderList list;
  140. for (auto& p : pathsToScan)
  141. addAllModulesInFolder (p, list);
  142. if (! shouldExit())
  143. {
  144. sort (list);
  145. completionCallback (list);
  146. }
  147. return jobHasFinished;
  148. }
  149. Array<File> pathsToScan;
  150. std::function<void (const ModuleIDAndFolderList&)> completionCallback;
  151. };
  152. AvailableModuleList::AvailableModuleList()
  153. {
  154. }
  155. ThreadPoolJob* AvailableModuleList::createScannerJob (const Array<File>& paths)
  156. {
  157. return new ModuleScannerJob (paths, [this] (ModuleIDAndFolderList scannedModuleList)
  158. {
  159. {
  160. const ScopedLock swapLock (lock);
  161. moduleList.swap (scannedModuleList);
  162. }
  163. listeners.call ([] (Listener& l) { MessageManager::callAsync ([&] { l.availableModulesChanged(); }); });
  164. });
  165. }
  166. void AvailableModuleList::removePendingAndAddJob (ThreadPoolJob* jobToAdd)
  167. {
  168. scanPool.removeAllJobs (false, 100);
  169. scanPool.addJob (jobToAdd, true);
  170. }
  171. void AvailableModuleList::scanPaths (const Array<File>& paths)
  172. {
  173. auto* job = createScannerJob (paths);
  174. removePendingAndAddJob (job);
  175. scanPool.waitForJobToFinish (job, -1);
  176. }
  177. void AvailableModuleList::scanPathsAsync (const Array<File>& paths)
  178. {
  179. removePendingAndAddJob (createScannerJob (paths));
  180. }
  181. ModuleIDAndFolderList AvailableModuleList::getAllModules() const
  182. {
  183. const ScopedLock readLock (lock);
  184. return moduleList;
  185. }
  186. ModuleIDAndFolder AvailableModuleList::getModuleWithID (const String& id) const
  187. {
  188. const ScopedLock readLock (lock);
  189. for (auto& mod : moduleList)
  190. if (mod.first == id)
  191. return mod;
  192. return {};
  193. }
  194. void AvailableModuleList::removeDuplicates (const ModuleIDAndFolderList& other)
  195. {
  196. const ScopedLock readLock (lock);
  197. for (auto& m : other)
  198. {
  199. auto pos = std::find (moduleList.begin(), moduleList.end(), m);
  200. if (pos != moduleList.end())
  201. moduleList.erase (pos);
  202. }
  203. }
  204. //==============================================================================
  205. LibraryModule::LibraryModule (const ModuleDescription& d)
  206. : moduleInfo (d)
  207. {
  208. }
  209. //==============================================================================
  210. void LibraryModule::writeIncludes (ProjectSaver& projectSaver, OutputStream& out)
  211. {
  212. auto& project = projectSaver.project;
  213. auto& modules = project.getEnabledModules();
  214. auto id = getID();
  215. if (modules.shouldCopyModuleFilesLocally (id).getValue())
  216. {
  217. auto juceModuleFolder = moduleInfo.getFolder();
  218. auto localModuleFolder = project.getLocalModuleFolder (id);
  219. localModuleFolder.createDirectory();
  220. projectSaver.copyFolder (juceModuleFolder, localModuleFolder);
  221. }
  222. out << "#include <" << moduleInfo.moduleFolder.getFileName() << "/"
  223. << moduleInfo.getHeader().getFileName()
  224. << ">" << newLine;
  225. }
  226. //==============================================================================
  227. static void parseAndAddLibs (StringArray& libList, const String& libs)
  228. {
  229. libList.addTokens (libs, ", ", {});
  230. libList.trim();
  231. libList.sort (false);
  232. libList.removeDuplicates (false);
  233. }
  234. void LibraryModule::addSettingsForModuleToExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  235. {
  236. auto& project = exporter.getProject();
  237. auto moduleRelativePath = exporter.getModuleFolderRelativeToProject (getID());
  238. exporter.addToExtraSearchPaths (moduleRelativePath.getParentDirectory());
  239. String libDirPlatform;
  240. if (exporter.isLinux())
  241. libDirPlatform = "Linux";
  242. else if (exporter.isCodeBlocks() && exporter.isWindows())
  243. libDirPlatform = "MinGW";
  244. else
  245. libDirPlatform = exporter.getTargetFolder().getFileName();
  246. auto libSubdirPath = moduleRelativePath.toUnixStyle() + "/libs/" + libDirPlatform;
  247. auto moduleLibDir = File (project.getProjectFolder().getFullPathName() + "/" + libSubdirPath);
  248. if (moduleLibDir.exists())
  249. exporter.addToModuleLibPaths ({ libSubdirPath, moduleRelativePath.getRoot() });
  250. auto extraInternalSearchPaths = moduleInfo.getExtraSearchPaths().trim();
  251. if (extraInternalSearchPaths.isNotEmpty())
  252. {
  253. auto paths = StringArray::fromTokens (extraInternalSearchPaths, true);
  254. for (auto& path : paths)
  255. exporter.addToExtraSearchPaths (moduleRelativePath.getChildFile (path.unquoted()));
  256. }
  257. {
  258. auto extraDefs = moduleInfo.getPreprocessorDefs().trim();
  259. if (extraDefs.isNotEmpty())
  260. exporter.getExporterPreprocessorDefsValue() = exporter.getExporterPreprocessorDefsString() + "\n" + extraDefs;
  261. }
  262. {
  263. Array<File> compiled;
  264. auto& modules = project.getEnabledModules();
  265. auto id = getID();
  266. auto localModuleFolder = modules.shouldCopyModuleFilesLocally (id).getValue() ? project.getLocalModuleFolder (id)
  267. : moduleInfo.getFolder();
  268. findAndAddCompiledUnits (exporter, &projectSaver, compiled);
  269. if (modules.shouldShowAllModuleFilesInProject (id).getValue())
  270. addBrowseableCode (exporter, compiled, localModuleFolder);
  271. }
  272. if (exporter.isXcode())
  273. {
  274. auto& xcodeExporter = dynamic_cast<XcodeProjectExporter&> (exporter);
  275. if (project.isAUPluginHost())
  276. xcodeExporter.xcodeFrameworks.addTokens (xcodeExporter.isOSX() ? "AudioUnit CoreAudioKit" : "CoreAudioKit", false);
  277. auto frameworks = moduleInfo.moduleInfo [xcodeExporter.isOSX() ? "OSXFrameworks" : "iOSFrameworks"].toString();
  278. xcodeExporter.xcodeFrameworks.addTokens (frameworks, ", ", {});
  279. parseAndAddLibs (xcodeExporter.xcodeLibs, moduleInfo.moduleInfo [exporter.isOSX() ? "OSXLibs" : "iOSLibs"].toString());
  280. }
  281. else if (exporter.isLinux())
  282. {
  283. parseAndAddLibs (exporter.linuxLibs, moduleInfo.moduleInfo ["linuxLibs"].toString());
  284. parseAndAddLibs (exporter.linuxPackages, moduleInfo.moduleInfo ["linuxPackages"].toString());
  285. }
  286. else if (exporter.isWindows())
  287. {
  288. if (exporter.isCodeBlocks())
  289. parseAndAddLibs (exporter.mingwLibs, moduleInfo.moduleInfo ["mingwLibs"].toString());
  290. else
  291. parseAndAddLibs (exporter.windowsLibs, moduleInfo.moduleInfo ["windowsLibs"].toString());
  292. }
  293. else if (exporter.isAndroid())
  294. {
  295. parseAndAddLibs (exporter.androidLibs, moduleInfo.moduleInfo ["androidLibs"].toString());
  296. }
  297. }
  298. void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::ConfigFlag>& flags) const
  299. {
  300. auto header = moduleInfo.getHeader();
  301. jassert (header.exists());
  302. StringArray lines;
  303. header.readLines (lines);
  304. for (int i = 0; i < lines.size(); ++i)
  305. {
  306. auto line = lines[i].trim();
  307. if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:"))
  308. {
  309. std::unique_ptr<Project::ConfigFlag> config (new Project::ConfigFlag());
  310. config->sourceModuleID = getID();
  311. config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim();
  312. if (config->symbol.length() > 2)
  313. {
  314. ++i;
  315. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  316. {
  317. if (lines[i].trim().isNotEmpty())
  318. config->description = config->description.trim() + " " + lines[i].trim();
  319. ++i;
  320. }
  321. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  322. config->value = project.getConfigFlag (config->symbol);
  323. i += 2;
  324. if (lines[i].contains ("#define " + config->symbol))
  325. {
  326. auto value = lines[i].fromFirstOccurrenceOf ("#define " + config->symbol, false, true).trim();
  327. config->value.setDefault (value == "0" ? false : true);
  328. }
  329. auto currentValue = config->value.get().toString();
  330. if (currentValue == "enabled") config->value = true;
  331. else if (currentValue == "disabled") config->value = false;
  332. flags.add (config.release());
  333. }
  334. }
  335. }
  336. }
  337. //==============================================================================
  338. struct FileSorter
  339. {
  340. static int compareElements (const File& f1, const File& f2)
  341. {
  342. return f1.getFileName().compareNatural (f2.getFileName());
  343. }
  344. };
  345. bool LibraryModule::CompileUnit::hasSuffix (const File& f, const char* suffix)
  346. {
  347. auto fileWithoutSuffix = f.getFileNameWithoutExtension() + ".";
  348. return fileWithoutSuffix.containsIgnoreCase (suffix + String ("."))
  349. || fileWithoutSuffix.containsIgnoreCase (suffix + String ("_"));
  350. }
  351. void LibraryModule::CompileUnit::writeInclude (MemoryOutputStream&) const
  352. {
  353. }
  354. bool LibraryModule::CompileUnit::isNeededForExporter (ProjectExporter& exporter) const
  355. {
  356. if ((hasSuffix (file, "_OSX") && ! exporter.isOSX())
  357. || (hasSuffix (file, "_iOS") && ! exporter.isiOS())
  358. || (hasSuffix (file, "_Windows") && ! exporter.isWindows())
  359. || (hasSuffix (file, "_Linux") && ! exporter.isLinux())
  360. || (hasSuffix (file, "_Android") && ! exporter.isAndroid()))
  361. return false;
  362. auto targetType = Project::getTargetTypeFromFilePath (file, false);
  363. if (targetType != ProjectType::Target::unspecified && ! exporter.shouldBuildTargetType (targetType))
  364. return false;
  365. return exporter.usesMMFiles() ? isCompiledForObjC
  366. : isCompiledForNonObjC;
  367. }
  368. String LibraryModule::CompileUnit::getFilenameForProxyFile() const
  369. {
  370. return "include_" + file.getFileName();
  371. }
  372. Array<LibraryModule::CompileUnit> LibraryModule::getAllCompileUnits (ProjectType::Target::Type forTarget) const
  373. {
  374. auto files = getFolder().findChildFiles (File::findFiles, false);
  375. FileSorter sorter;
  376. files.sort (sorter);
  377. Array<LibraryModule::CompileUnit> units;
  378. for (auto& file : files)
  379. {
  380. if (file.getFileName().startsWithIgnoreCase (getID())
  381. && file.hasFileExtension (sourceFileExtensions))
  382. {
  383. if (forTarget == ProjectType::Target::unspecified
  384. || forTarget == Project::getTargetTypeFromFilePath (file, true))
  385. {
  386. CompileUnit cu;
  387. cu.file = file;
  388. units.add (cu);
  389. }
  390. }
  391. }
  392. for (auto& cu : units)
  393. {
  394. cu.isCompiledForObjC = true;
  395. cu.isCompiledForNonObjC = ! cu.file.hasFileExtension ("mm;m");
  396. if (cu.isCompiledForNonObjC)
  397. if (files.contains (cu.file.withFileExtension ("mm")))
  398. cu.isCompiledForObjC = false;
  399. jassert (cu.isCompiledForObjC || cu.isCompiledForNonObjC);
  400. }
  401. return units;
  402. }
  403. void LibraryModule::findAndAddCompiledUnits (ProjectExporter& exporter,
  404. ProjectSaver* projectSaver,
  405. Array<File>& result,
  406. ProjectType::Target::Type forTarget) const
  407. {
  408. for (auto& cu : getAllCompileUnits (forTarget))
  409. {
  410. if (cu.isNeededForExporter (exporter))
  411. {
  412. auto localFile = exporter.getProject().getGeneratedCodeFolder()
  413. .getChildFile (cu.getFilenameForProxyFile());
  414. result.add (localFile);
  415. if (projectSaver != nullptr)
  416. projectSaver->addFileToGeneratedGroup (localFile);
  417. }
  418. }
  419. }
  420. static void addFileWithGroups (Project::Item& group, const RelativePath& file, const String& path)
  421. {
  422. auto slash = path.indexOfChar (File::getSeparatorChar());
  423. if (slash >= 0)
  424. {
  425. auto topLevelGroup = path.substring (0, slash);
  426. auto remainingPath = path.substring (slash + 1);
  427. auto newGroup = group.getOrCreateSubGroup (topLevelGroup);
  428. addFileWithGroups (newGroup, file, remainingPath);
  429. }
  430. else
  431. {
  432. if (! group.containsChildForFile (file))
  433. group.addRelativeFile (file, -1, false);
  434. }
  435. }
  436. void LibraryModule::findBrowseableFiles (const File& folder, Array<File>& filesFound) const
  437. {
  438. Array<File> tempList;
  439. FileSorter sorter;
  440. DirectoryIterator iter (folder, true, "*", File::findFiles);
  441. bool isHiddenFile;
  442. while (iter.next (nullptr, &isHiddenFile, nullptr, nullptr, nullptr, nullptr))
  443. if (! isHiddenFile && iter.getFile().hasFileExtension (browseableFileExtensions))
  444. tempList.addSorted (sorter, iter.getFile());
  445. filesFound.addArray (tempList);
  446. }
  447. void LibraryModule::addBrowseableCode (ProjectExporter& exporter, const Array<File>& compiled, const File& localModuleFolder) const
  448. {
  449. if (sourceFiles.isEmpty())
  450. findBrowseableFiles (localModuleFolder, sourceFiles);
  451. auto sourceGroup = Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID(), false);
  452. auto moduleFromProject = exporter.getModuleFolderRelativeToProject (getID());
  453. auto moduleHeader = moduleInfo.getHeader();
  454. for (auto& sourceFile : sourceFiles)
  455. {
  456. auto pathWithinModule = FileHelpers::getRelativePathFrom (sourceFile, localModuleFolder);
  457. // (Note: in exporters like MSVC we have to avoid adding the same file twice, even if one of those instances
  458. // is flagged as being excluded from the build, because this overrides the other and it fails to compile)
  459. if ((exporter.canCopeWithDuplicateFiles() || ! compiled.contains (sourceFile)) && sourceFile != moduleHeader)
  460. addFileWithGroups (sourceGroup,
  461. moduleFromProject.getChildFile (pathWithinModule),
  462. pathWithinModule);
  463. }
  464. sourceGroup.sortAlphabetically (true, true);
  465. sourceGroup.addFileAtIndex (moduleHeader, -1, false);
  466. exporter.getModulesGroup().state.appendChild (sourceGroup.state.createCopy(), nullptr);
  467. }
  468. //==============================================================================
  469. EnabledModuleList::EnabledModuleList (Project& p, const ValueTree& s)
  470. : project (p), state (s)
  471. {
  472. }
  473. ModuleDescription EnabledModuleList::getModuleInfo (const String& moduleID)
  474. {
  475. return ModuleDescription (project.getModuleWithID (moduleID).second);
  476. }
  477. bool EnabledModuleList::isModuleEnabled (const String& moduleID) const
  478. {
  479. return state.getChildWithProperty (Ids::ID, moduleID).isValid();
  480. }
  481. bool EnabledModuleList::isAudioPluginModuleMissing() const
  482. {
  483. return project.getProjectType().isAudioPlugin()
  484. && ! isModuleEnabled ("juce_audio_plugin_client");
  485. }
  486. bool EnabledModuleList::shouldUseGlobalPath (const String& moduleID) const
  487. {
  488. return static_cast<bool> (state.getChildWithProperty (Ids::ID, moduleID)
  489. .getProperty (Ids::useGlobalPath));
  490. }
  491. Value EnabledModuleList::getShouldUseGlobalPathValue (const String& moduleID) const
  492. {
  493. return state.getChildWithProperty (Ids::ID, moduleID)
  494. .getPropertyAsValue (Ids::useGlobalPath, getUndoManager());
  495. }
  496. Value EnabledModuleList::shouldShowAllModuleFilesInProject (const String& moduleID)
  497. {
  498. return state.getChildWithProperty (Ids::ID, moduleID)
  499. .getPropertyAsValue (Ids::showAllCode, getUndoManager());
  500. }
  501. struct ModuleTreeSorter
  502. {
  503. static int compareElements (const ValueTree& m1, const ValueTree& m2)
  504. {
  505. return m1[Ids::ID].toString().compareIgnoreCase (m2[Ids::ID]);
  506. }
  507. };
  508. void EnabledModuleList::sortAlphabetically()
  509. {
  510. ModuleTreeSorter sorter;
  511. state.sort (sorter, getUndoManager(), false);
  512. }
  513. Value EnabledModuleList::shouldCopyModuleFilesLocally (const String& moduleID) const
  514. {
  515. return state.getChildWithProperty (Ids::ID, moduleID)
  516. .getPropertyAsValue (Ids::useLocalCopy, getUndoManager());
  517. }
  518. void EnabledModuleList::addModule (const File& moduleFolder, bool copyLocally, bool useGlobalPath, bool sendAnalyticsEvent)
  519. {
  520. ModuleDescription info (moduleFolder);
  521. if (info.isValid())
  522. {
  523. auto moduleID = info.getID();
  524. if (! isModuleEnabled (moduleID))
  525. {
  526. ValueTree module (Ids::MODULE);
  527. module.setProperty (Ids::ID, moduleID, getUndoManager());
  528. state.appendChild (module, getUndoManager());
  529. sortAlphabetically();
  530. shouldShowAllModuleFilesInProject (moduleID) = true;
  531. shouldCopyModuleFilesLocally (moduleID) = copyLocally;
  532. getShouldUseGlobalPathValue (moduleID) = useGlobalPath;
  533. RelativePath path (moduleFolder.getParentDirectory(),
  534. project.getProjectFolder(), RelativePath::projectFolder);
  535. for (Project::ExporterIterator exporter (project); exporter.next();)
  536. exporter->getPathForModuleValue (moduleID) = path.toUnixStyle();
  537. if (! useGlobalPath)
  538. project.rescanExporterPathModules (false);
  539. if (sendAnalyticsEvent)
  540. {
  541. StringPairArray data;
  542. data.set ("label", moduleID);
  543. Analytics::getInstance()->logEvent ("Module Added", data, ProjucerAnalyticsEvent::projectEvent);
  544. }
  545. }
  546. }
  547. }
  548. void EnabledModuleList::removeModule (String moduleID) // must be pass-by-value, and not a const ref!
  549. {
  550. for (auto i = state.getNumChildren(); --i >= 0;)
  551. if (state.getChild(i) [Ids::ID] == moduleID)
  552. state.removeChild (i, getUndoManager());
  553. for (Project::ExporterIterator exporter (project); exporter.next();)
  554. exporter->removePathForModule (moduleID);
  555. }
  556. void EnabledModuleList::createRequiredModules (OwnedArray<LibraryModule>& modules)
  557. {
  558. for (int i = 0; i < getNumModules(); ++i)
  559. modules.add (new LibraryModule (getModuleInfo (getModuleID (i))));
  560. }
  561. StringArray EnabledModuleList::getAllModules() const
  562. {
  563. StringArray moduleIDs;
  564. for (int i = 0; i < getNumModules(); ++i)
  565. moduleIDs.add (getModuleID (i));
  566. return moduleIDs;
  567. }
  568. static void getDependencies (Project& project, const String& moduleID, StringArray& dependencies)
  569. {
  570. auto info = project.getEnabledModules().getModuleInfo (moduleID);
  571. for (auto uid : info.getDependencies())
  572. {
  573. if (! dependencies.contains (uid, true))
  574. {
  575. dependencies.add (uid);
  576. getDependencies (project, uid, dependencies);
  577. }
  578. }
  579. }
  580. StringArray EnabledModuleList::getExtraDependenciesNeeded (const String& moduleID) const
  581. {
  582. StringArray dependencies, extraDepsNeeded;
  583. getDependencies (project, moduleID, dependencies);
  584. for (auto dep : dependencies)
  585. if (dep != moduleID && ! isModuleEnabled (dep))
  586. extraDepsNeeded.add (dep);
  587. return extraDepsNeeded;
  588. }
  589. bool EnabledModuleList::doesModuleHaveHigherCppStandardThanProject (const String& moduleID)
  590. {
  591. auto projectCppStandard = project.getCppStandardString();
  592. if (projectCppStandard == "latest")
  593. return false;
  594. auto moduleCppStandard = getModuleInfo (moduleID).getMinimumCppStandard();
  595. return (moduleCppStandard.getIntValue() > projectCppStandard.getIntValue());
  596. }
  597. bool EnabledModuleList::areMostModulesUsingGlobalPath() const
  598. {
  599. int numYes = 0, numNo = 0;
  600. for (auto i = getNumModules(); --i >= 0;)
  601. {
  602. if (shouldUseGlobalPath (getModuleID (i)))
  603. ++numYes;
  604. else
  605. ++numNo;
  606. }
  607. return numYes > numNo;
  608. }
  609. bool EnabledModuleList::areMostModulesCopiedLocally() const
  610. {
  611. int numYes = 0, numNo = 0;
  612. for (auto i = getNumModules(); --i >= 0;)
  613. {
  614. if (shouldCopyModuleFilesLocally (getModuleID (i)).getValue())
  615. ++numYes;
  616. else
  617. ++numNo;
  618. }
  619. return numYes > numNo;
  620. }
  621. void EnabledModuleList::setLocalCopyModeForAllModules (bool copyLocally)
  622. {
  623. for (auto i = getNumModules(); --i >= 0;)
  624. shouldCopyModuleFilesLocally (project.getEnabledModules().getModuleID (i)) = copyLocally;
  625. }
  626. File EnabledModuleList::findDefaultModulesFolder (Project& project)
  627. {
  628. File globalPath (getAppSettings().getStoredPath (Ids::defaultJuceModulePath).toString());
  629. if (globalPath != File())
  630. return globalPath;
  631. for (auto& exporterPathModule : project.getExporterPathsModuleList().getAllModules())
  632. {
  633. auto f = exporterPathModule.second;
  634. if (f.isDirectory())
  635. return f.getParentDirectory();
  636. }
  637. return File::getCurrentWorkingDirectory();
  638. }
  639. void EnabledModuleList::addModuleFromUserSelectedFile()
  640. {
  641. static auto lastLocation = findDefaultModulesFolder (project);
  642. FileChooser fc ("Select a module to add...", lastLocation, {});
  643. if (fc.browseForDirectory())
  644. {
  645. lastLocation = fc.getResult();
  646. addModuleOfferingToCopy (lastLocation, true);
  647. }
  648. }
  649. void EnabledModuleList::addModuleInteractive (const String& moduleID)
  650. {
  651. auto f = project.getModuleWithID (moduleID).second;
  652. if (f != File())
  653. {
  654. addModule (f, areMostModulesCopiedLocally(), areMostModulesUsingGlobalPath(), true);
  655. return;
  656. }
  657. addModuleFromUserSelectedFile();
  658. }
  659. void EnabledModuleList::addModuleOfferingToCopy (const File& f, bool isFromUserSpecifiedFolder)
  660. {
  661. ModuleDescription m (f);
  662. if (! m.isValid())
  663. {
  664. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  665. "Add Module", "This wasn't a valid module folder!");
  666. return;
  667. }
  668. if (isModuleEnabled (m.getID()))
  669. {
  670. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  671. "Add Module", "The project already contains this module!");
  672. return;
  673. }
  674. addModule (m.moduleFolder, areMostModulesCopiedLocally(),
  675. isFromUserSpecifiedFolder ? false : areMostModulesUsingGlobalPath(),
  676. true);
  677. }
  678. bool isJUCEFolder (const File& f)
  679. {
  680. return isJUCEModulesFolder (f.getChildFile ("modules"));
  681. }
  682. bool isJUCEModulesFolder (const File& f)
  683. {
  684. return f.isDirectory() && f.getChildFile ("juce_core").isDirectory();
  685. }