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.

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