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.

847 lines
26KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_Module.h"
  19. #include "jucer_ProjectType.h"
  20. #include "../Project Saving/jucer_ProjectExporter.h"
  21. #include "../Project Saving/jucer_ProjectSaver.h"
  22. #include "../Project Saving/jucer_ProjectExport_XCode.h"
  23. static String trimCommentCharsFromStartOfLine (const String& line)
  24. {
  25. return line.trimStart().trimCharactersAtStart ("*/").trimStart();
  26. }
  27. static var parseModuleDesc (const StringArray& lines)
  28. {
  29. DynamicObject* o = new DynamicObject();
  30. var result (o);
  31. for (int i = 0; i < lines.size(); ++i)
  32. {
  33. String line = trimCommentCharsFromStartOfLine (lines[i]);
  34. int colon = line.indexOfChar (':');
  35. if (colon >= 0)
  36. {
  37. String key = line.substring (0, colon).trim();
  38. String value = line.substring (colon + 1).trim();
  39. o->setProperty (key, value);
  40. }
  41. }
  42. return result;
  43. }
  44. static var parseModuleDesc (const File& header)
  45. {
  46. StringArray lines;
  47. header.readLines (lines);
  48. for (int i = 0; i < lines.size(); ++i)
  49. {
  50. if (trimCommentCharsFromStartOfLine (lines[i]).startsWith ("BEGIN_JUCE_MODULE_DECLARATION"))
  51. {
  52. StringArray desc;
  53. for (int j = i + 1; j < lines.size(); ++j)
  54. {
  55. if (trimCommentCharsFromStartOfLine (lines[j]).startsWith ("END_JUCE_MODULE_DECLARATION"))
  56. return parseModuleDesc (desc);
  57. desc.add (lines[j]);
  58. }
  59. break;
  60. }
  61. }
  62. return {};
  63. }
  64. ModuleDescription::ModuleDescription (const File& folder)
  65. : moduleFolder (folder),
  66. moduleInfo (parseModuleDesc (getHeader()))
  67. {
  68. }
  69. File ModuleDescription::getHeader() const
  70. {
  71. if (moduleFolder != File())
  72. {
  73. const char* extensions[] = { ".h", ".hpp", ".hxx" };
  74. for (auto e : extensions)
  75. {
  76. File header (moduleFolder.getChildFile (moduleFolder.getFileName() + e));
  77. if (header.existsAsFile())
  78. return header;
  79. }
  80. }
  81. return {};
  82. }
  83. StringArray ModuleDescription::getDependencies() const
  84. {
  85. auto deps = StringArray::fromTokens (moduleInfo ["dependencies"].toString(), " \t;,", "\"'");
  86. deps.trim();
  87. deps.removeEmptyStrings();
  88. return deps;
  89. }
  90. //==============================================================================
  91. ModuleList::ModuleList()
  92. {
  93. }
  94. ModuleList::ModuleList (const ModuleList& other)
  95. {
  96. operator= (other);
  97. }
  98. ModuleList& ModuleList::operator= (const ModuleList& other)
  99. {
  100. modules.clear();
  101. modules.addCopiesOf (other.modules);
  102. return *this;
  103. }
  104. const ModuleDescription* ModuleList::getModuleWithID (const String& moduleID) const
  105. {
  106. for (auto* m : modules)
  107. if (m->getID() == moduleID)
  108. return m;
  109. return nullptr;
  110. }
  111. struct ModuleSorter
  112. {
  113. static int compareElements (const ModuleDescription* m1, const ModuleDescription* m2)
  114. {
  115. return m1->getID().compareIgnoreCase (m2->getID());
  116. }
  117. };
  118. void ModuleList::sort()
  119. {
  120. ModuleSorter sorter;
  121. modules.sort (sorter);
  122. }
  123. StringArray ModuleList::getIDs() const
  124. {
  125. StringArray results;
  126. for (auto* m : modules)
  127. results.add (m->getID());
  128. results.sort (true);
  129. return results;
  130. }
  131. Result ModuleList::tryToAddModuleFromFolder (const File& path)
  132. {
  133. ModuleDescription m (path);
  134. if (m.isValid())
  135. {
  136. modules.add (new ModuleDescription (m));
  137. return Result::ok();
  138. }
  139. return Result::fail (path.getFullPathName() + " is not a valid module");
  140. }
  141. Result ModuleList::addAllModulesInFolder (const File& path)
  142. {
  143. if (! tryToAddModuleFromFolder (path))
  144. {
  145. const int subfolders = 2;
  146. return addAllModulesInSubfoldersRecursively (path, subfolders);
  147. }
  148. return Result::ok();
  149. }
  150. Result ModuleList::addAllModulesInSubfoldersRecursively (const File& path, int depth)
  151. {
  152. if (depth > 0)
  153. {
  154. for (DirectoryIterator iter (path, false, "*", File::findDirectories); iter.next();)
  155. {
  156. const File& childPath = iter.getFile().getLinkedTarget();
  157. if (! tryToAddModuleFromFolder (childPath))
  158. addAllModulesInSubfoldersRecursively (childPath, depth - 1);
  159. }
  160. }
  161. return Result::ok();
  162. }
  163. static Array<File> getAllPossibleModulePaths (Project& project)
  164. {
  165. StringArray paths;
  166. for (Project::ExporterIterator exporter (project); exporter.next();)
  167. {
  168. for (int i = 0; i < project.getModules().getNumModules(); ++i)
  169. {
  170. const String path (exporter->getPathForModuleString (project.getModules().getModuleID (i)));
  171. if (path.isNotEmpty())
  172. paths.addIfNotAlreadyThere (path);
  173. }
  174. String oldPath (exporter->getLegacyModulePath());
  175. if (oldPath.isNotEmpty())
  176. paths.addIfNotAlreadyThere (oldPath);
  177. }
  178. Array<File> files;
  179. for (int i = 0; i < paths.size(); ++i)
  180. {
  181. const File f (project.resolveFilename (paths[i]));
  182. if (f.isDirectory())
  183. {
  184. files.add (f);
  185. if (f.getChildFile ("modules").isDirectory())
  186. files.addIfNotAlreadyThere (f.getChildFile ("modules"));
  187. }
  188. }
  189. return files;
  190. }
  191. Result ModuleList::scanAllKnownFolders (Project& project)
  192. {
  193. modules.clear();
  194. Result result (Result::ok());
  195. const Array<File> modulePaths (getAllPossibleModulePaths (project));
  196. for (int i = 0; i < modulePaths.size(); ++i)
  197. {
  198. result = addAllModulesInFolder (modulePaths.getReference(i));
  199. if (result.failed())
  200. break;
  201. }
  202. sort();
  203. return result;
  204. }
  205. //==============================================================================
  206. LibraryModule::LibraryModule (const ModuleDescription& d)
  207. : moduleInfo (d)
  208. {
  209. }
  210. //==============================================================================
  211. void LibraryModule::writeIncludes (ProjectSaver& projectSaver, OutputStream& out)
  212. {
  213. Project& project = projectSaver.project;
  214. EnabledModuleList& modules = project.getModules();
  215. const String id (getID());
  216. if (modules.shouldCopyModuleFilesLocally (id).getValue())
  217. {
  218. const File juceModuleFolder (moduleInfo.getFolder());
  219. const File localModuleFolder (project.getLocalModuleFolder (id));
  220. localModuleFolder.createDirectory();
  221. projectSaver.copyFolder (juceModuleFolder, localModuleFolder);
  222. }
  223. out << "#include <" << moduleInfo.moduleFolder.getFileName() << "/"
  224. << moduleInfo.getHeader().getFileName()
  225. << ">" << newLine;
  226. }
  227. //==============================================================================
  228. static void parseAndAddLibs (StringArray& libList, const String& libs)
  229. {
  230. libList.addTokens (libs, ", ", StringRef());
  231. libList.trim();
  232. libList.sort (false);
  233. libList.removeDuplicates (false);
  234. }
  235. void LibraryModule::addSettingsForModuleToExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  236. {
  237. auto& project = exporter.getProject();
  238. const auto moduleRelativePath = exporter.getModuleFolderRelativeToProject (getID());
  239. exporter.addToExtraSearchPaths (moduleRelativePath.getParentDirectory());
  240. String libDirPlatform;
  241. if (exporter.isLinux())
  242. libDirPlatform = "Linux";
  243. else if (exporter.isCodeBlocks() && exporter.isWindows())
  244. libDirPlatform = "MinGW";
  245. else
  246. libDirPlatform = exporter.getTargetFolder().getFileName();
  247. const auto libSubdirPath = String (moduleRelativePath.toUnixStyle() + "/libs/") + libDirPlatform;
  248. const auto moduleLibDir = File (project.getProjectFolder().getFullPathName() + "/" + libSubdirPath);
  249. if (moduleLibDir.exists())
  250. exporter.addToModuleLibPaths (RelativePath (libSubdirPath, moduleRelativePath.getRoot()));
  251. const auto extraInternalSearchPaths = moduleInfo.getExtraSearchPaths().trim();
  252. if (extraInternalSearchPaths.isNotEmpty())
  253. {
  254. StringArray paths;
  255. paths.addTokens (extraInternalSearchPaths, true);
  256. for (int i = 0; i < paths.size(); ++i)
  257. exporter.addToExtraSearchPaths (moduleRelativePath.getChildFile (paths.getReference(i)));
  258. }
  259. {
  260. const String extraDefs (moduleInfo.getPreprocessorDefs().trim());
  261. if (extraDefs.isNotEmpty())
  262. exporter.getExporterPreprocessorDefs() = exporter.getExporterPreprocessorDefsString() + "\n" + extraDefs;
  263. }
  264. {
  265. Array<File> compiled;
  266. const File localModuleFolder = project.getModules().shouldCopyModuleFilesLocally (getID()).getValue()
  267. ? project.getLocalModuleFolder (getID())
  268. : moduleInfo.getFolder();
  269. findAndAddCompiledUnits (exporter, &projectSaver, compiled);
  270. if (project.getModules().shouldShowAllModuleFilesInProject (getID()).getValue())
  271. addBrowseableCode (exporter, compiled, localModuleFolder);
  272. }
  273. if (exporter.isXcode())
  274. {
  275. XCodeProjectExporter& xcodeExporter = dynamic_cast<XCodeProjectExporter&> (exporter);
  276. if (project.isAUPluginHost())
  277. xcodeExporter.xcodeFrameworks.addTokens (xcodeExporter.isOSX() ? "AudioUnit CoreAudioKit" : "CoreAudioKit", false);
  278. const String frameworks (moduleInfo.moduleInfo [xcodeExporter.isOSX() ? "OSXFrameworks" : "iOSFrameworks"].toString());
  279. xcodeExporter.xcodeFrameworks.addTokens (frameworks, ", ", StringRef());
  280. parseAndAddLibs (xcodeExporter.xcodeLibs, moduleInfo.moduleInfo [exporter.isOSX() ? "OSXLibs" : "iOSLibs"].toString());
  281. }
  282. else if (exporter.isLinux())
  283. {
  284. parseAndAddLibs (exporter.linuxLibs, moduleInfo.moduleInfo ["linuxLibs"].toString());
  285. parseAndAddLibs (exporter.linuxPackages, moduleInfo.moduleInfo ["linuxPackages"].toString());
  286. }
  287. else if (exporter.isWindows())
  288. {
  289. if (exporter.isCodeBlocks())
  290. parseAndAddLibs (exporter.mingwLibs, moduleInfo.moduleInfo ["mingwLibs"].toString());
  291. else
  292. parseAndAddLibs (exporter.windowsLibs, moduleInfo.moduleInfo ["windowsLibs"].toString());
  293. }
  294. }
  295. void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::ConfigFlag>& flags) const
  296. {
  297. const File header (moduleInfo.getHeader());
  298. jassert (header.exists());
  299. StringArray lines;
  300. header.readLines (lines);
  301. for (int i = 0; i < lines.size(); ++i)
  302. {
  303. String line (lines[i].trim());
  304. if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:"))
  305. {
  306. ScopedPointer<Project::ConfigFlag> config (new Project::ConfigFlag());
  307. config->sourceModuleID = getID();
  308. config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim();
  309. if (config->symbol.length() > 2)
  310. {
  311. ++i;
  312. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  313. {
  314. if (lines[i].trim().isNotEmpty())
  315. config->description = config->description.trim() + " " + lines[i].trim();
  316. ++i;
  317. }
  318. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  319. config->value.referTo (project.getConfigFlag (config->symbol));
  320. flags.add (config.release());
  321. }
  322. }
  323. }
  324. }
  325. //==============================================================================
  326. struct FileSorter
  327. {
  328. static int compareElements (const File& f1, const File& f2)
  329. {
  330. return f1.getFileName().compareNatural (f2.getFileName());
  331. }
  332. };
  333. bool LibraryModule::CompileUnit::hasSuffix (const File& f, const char* suffix)
  334. {
  335. String fileWithoutSuffix = f.getFileNameWithoutExtension() + String (".");
  336. return fileWithoutSuffix.containsIgnoreCase (suffix + String ("."))
  337. || fileWithoutSuffix.containsIgnoreCase (suffix + String ("_"));
  338. }
  339. void LibraryModule::CompileUnit::writeInclude (MemoryOutputStream&) const
  340. {
  341. }
  342. bool LibraryModule::CompileUnit::isNeededForExporter (ProjectExporter& exporter) const
  343. {
  344. if ((hasSuffix (file, "_OSX") && ! exporter.isOSX())
  345. || (hasSuffix (file, "_iOS") && ! exporter.isiOS())
  346. || (hasSuffix (file, "_Windows") && ! exporter.isWindows())
  347. || (hasSuffix (file, "_Linux") && ! exporter.isLinux())
  348. || (hasSuffix (file, "_Android") && ! exporter.isAndroid()))
  349. return false;
  350. const ProjectType::Target::Type targetType = Project::getTargetTypeFromFilePath (file, false);
  351. if (targetType != ProjectType::Target::unspecified && ! exporter.shouldBuildTargetType (targetType))
  352. return false;
  353. return exporter.usesMMFiles() ? isCompiledForObjC
  354. : isCompiledForNonObjC;
  355. }
  356. Array<LibraryModule::CompileUnit> LibraryModule::getAllCompileUnits() const
  357. {
  358. Array<File> files;
  359. getFolder().findChildFiles (files, File::findFiles, false);
  360. FileSorter sorter;
  361. files.sort (sorter);
  362. Array<LibraryModule::CompileUnit> units;
  363. for (int i = 0; i < files.size(); ++i)
  364. {
  365. CompileUnit cu;
  366. cu.file = files.getReference(i);
  367. if (cu.file.getFileName().startsWithIgnoreCase (getID())
  368. && cu.file.hasFileExtension (sourceFileExtensions))
  369. {
  370. units.add (cu);
  371. }
  372. }
  373. for (int i = 0; i < units.size(); ++i)
  374. {
  375. CompileUnit& cu = units.getReference(i);
  376. cu.isCompiledForObjC = true;
  377. cu.isCompiledForNonObjC = ! cu.file.hasFileExtension ("mm;m");
  378. if (cu.isCompiledForNonObjC)
  379. if (files.contains (cu.file.withFileExtension ("mm")))
  380. cu.isCompiledForObjC = false;
  381. jassert (cu.isCompiledForObjC || cu.isCompiledForNonObjC);
  382. }
  383. return units;
  384. }
  385. void LibraryModule::findAndAddCompiledUnits (ProjectExporter& exporter,
  386. ProjectSaver* projectSaver,
  387. Array<File>& result) const
  388. {
  389. Array<CompileUnit> units = getAllCompileUnits();
  390. for (int i = 0; i < units.size(); ++i)
  391. {
  392. const CompileUnit& cu = units.getReference(i);
  393. if (cu.isNeededForExporter (exporter))
  394. {
  395. // .r files require a different include scheme, with a different file name
  396. auto filename = cu.file.getFileName();
  397. if (cu.file.getFileExtension() == ".r")
  398. filename = cu.file.getFileNameWithoutExtension() + "_r.r";
  399. File localFile = exporter.getProject().getGeneratedCodeFolder().getChildFile (filename);
  400. result.add (localFile);
  401. if (projectSaver != nullptr)
  402. projectSaver->addFileToGeneratedGroup (localFile);
  403. }
  404. }
  405. }
  406. static void addFileWithGroups (Project::Item& group, const RelativePath& file, const String& path)
  407. {
  408. const int slash = path.indexOfChar (File::separator);
  409. if (slash >= 0)
  410. {
  411. const String topLevelGroup (path.substring (0, slash));
  412. const String remainingPath (path.substring (slash + 1));
  413. Project::Item newGroup (group.getOrCreateSubGroup (topLevelGroup));
  414. addFileWithGroups (newGroup, file, remainingPath);
  415. }
  416. else
  417. {
  418. if (! group.containsChildForFile (file))
  419. group.addRelativeFile (file, -1, false);
  420. }
  421. }
  422. void LibraryModule::findBrowseableFiles (const File& folder, Array<File>& filesFound) const
  423. {
  424. Array<File> tempList;
  425. FileSorter sorter;
  426. DirectoryIterator iter (folder, true, "*", File::findFiles);
  427. bool isHiddenFile;
  428. while (iter.next (nullptr, &isHiddenFile, nullptr, nullptr, nullptr, nullptr))
  429. if (! isHiddenFile && iter.getFile().hasFileExtension (browseableFileExtensions))
  430. tempList.addSorted (sorter, iter.getFile());
  431. filesFound.addArray (tempList);
  432. }
  433. void LibraryModule::addBrowseableCode (ProjectExporter& exporter, const Array<File>& compiled, const File& localModuleFolder) const
  434. {
  435. if (sourceFiles.size() == 0)
  436. findBrowseableFiles (localModuleFolder, sourceFiles);
  437. Project::Item sourceGroup (Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID(), false));
  438. const RelativePath moduleFromProject (exporter.getModuleFolderRelativeToProject (getID()));
  439. for (int i = 0; i < sourceFiles.size(); ++i)
  440. {
  441. const String pathWithinModule (FileHelpers::getRelativePathFrom (sourceFiles.getReference(i), localModuleFolder));
  442. // (Note: in exporters like MSVC we have to avoid adding the same file twice, even if one of those instances
  443. // is flagged as being excluded from the build, because this overrides the other and it fails to compile)
  444. if (exporter.canCopeWithDuplicateFiles() || ! compiled.contains (sourceFiles.getReference(i)))
  445. addFileWithGroups (sourceGroup,
  446. moduleFromProject.getChildFile (pathWithinModule),
  447. pathWithinModule);
  448. }
  449. sourceGroup.sortAlphabetically (true, true);
  450. sourceGroup.addFileAtIndex (moduleInfo.getHeader(), -1, false);
  451. exporter.getModulesGroup().state.addChild (sourceGroup.state.createCopy(), -1, nullptr);
  452. }
  453. //==============================================================================
  454. EnabledModuleList::EnabledModuleList (Project& p, const ValueTree& s)
  455. : project (p), state (s)
  456. {
  457. }
  458. ModuleDescription EnabledModuleList::getModuleInfo (const String& moduleID)
  459. {
  460. return ModuleDescription (getModuleFolder (moduleID));
  461. }
  462. bool EnabledModuleList::isModuleEnabled (const String& moduleID) const
  463. {
  464. for (int i = 0; i < state.getNumChildren(); ++i)
  465. if (state.getChild(i) [Ids::ID] == moduleID)
  466. return true;
  467. return false;
  468. }
  469. bool EnabledModuleList::isAudioPluginModuleMissing() const
  470. {
  471. return project.getProjectType().isAudioPlugin()
  472. && ! isModuleEnabled ("juce_audio_plugin_client");
  473. }
  474. Value EnabledModuleList::shouldShowAllModuleFilesInProject (const String& moduleID)
  475. {
  476. return state.getChildWithProperty (Ids::ID, moduleID)
  477. .getPropertyAsValue (Ids::showAllCode, getUndoManager());
  478. }
  479. File EnabledModuleList::findLocalModuleFolder (const String& moduleID, bool useExportersForOtherOSes)
  480. {
  481. for (Project::ExporterIterator exporter (project); exporter.next();)
  482. {
  483. if (useExportersForOtherOSes || exporter->mayCompileOnCurrentOS())
  484. {
  485. const String path (exporter->getPathForModuleString (moduleID));
  486. if (path.isNotEmpty())
  487. {
  488. const File moduleFolder (project.resolveFilename (path));
  489. if (moduleFolder.exists())
  490. {
  491. if (ModuleDescription (moduleFolder).isValid())
  492. return moduleFolder;
  493. File f = moduleFolder.getChildFile (moduleID);
  494. if (ModuleDescription (f).isValid())
  495. return f;
  496. }
  497. }
  498. }
  499. }
  500. return {};
  501. }
  502. File EnabledModuleList::getModuleFolder (const String& moduleID)
  503. {
  504. File f = findLocalModuleFolder (moduleID, false);
  505. if (f == File())
  506. f = findLocalModuleFolder (moduleID, true);
  507. return f;
  508. }
  509. struct ModuleTreeSorter
  510. {
  511. static int compareElements (const ValueTree& m1, const ValueTree& m2)
  512. {
  513. return m1[Ids::ID].toString().compareIgnoreCase (m2[Ids::ID]);
  514. }
  515. };
  516. void EnabledModuleList::sortAlphabetically()
  517. {
  518. ModuleTreeSorter sorter;
  519. state.sort (sorter, getUndoManager(), false);
  520. }
  521. Value EnabledModuleList::shouldCopyModuleFilesLocally (const String& moduleID) const
  522. {
  523. return state.getChildWithProperty (Ids::ID, moduleID)
  524. .getPropertyAsValue (Ids::useLocalCopy, getUndoManager());
  525. }
  526. void EnabledModuleList::addModule (const File& moduleFolder, bool copyLocally)
  527. {
  528. ModuleDescription info (moduleFolder);
  529. if (info.isValid())
  530. {
  531. const String moduleID (info.getID());
  532. if (! isModuleEnabled (moduleID))
  533. {
  534. ValueTree module (Ids::MODULE);
  535. module.setProperty (Ids::ID, moduleID, nullptr);
  536. state.addChild (module, -1, getUndoManager());
  537. sortAlphabetically();
  538. shouldShowAllModuleFilesInProject (moduleID) = true;
  539. shouldCopyModuleFilesLocally (moduleID) = copyLocally;
  540. RelativePath path (moduleFolder.getParentDirectory(),
  541. project.getProjectFolder(), RelativePath::projectFolder);
  542. for (Project::ExporterIterator exporter (project); exporter.next();)
  543. exporter->getPathForModuleValue (moduleID) = path.toUnixStyle();
  544. }
  545. }
  546. }
  547. void EnabledModuleList::removeModule (String moduleID) // must be pass-by-value, and not a const ref!
  548. {
  549. for (int i = state.getNumChildren(); --i >= 0;)
  550. if (state.getChild(i) [Ids::ID] == moduleID)
  551. state.removeChild (i, getUndoManager());
  552. for (Project::ExporterIterator exporter (project); exporter.next();)
  553. exporter->removePathForModule (moduleID);
  554. }
  555. void EnabledModuleList::createRequiredModules (OwnedArray<LibraryModule>& modules)
  556. {
  557. for (int i = 0; i < getNumModules(); ++i)
  558. modules.add (new LibraryModule (getModuleInfo (getModuleID (i))));
  559. }
  560. StringArray EnabledModuleList::getAllModules() const
  561. {
  562. StringArray moduleIDs;
  563. for (int i = 0; i < getNumModules(); ++i)
  564. moduleIDs.add (getModuleID(i));
  565. return moduleIDs;
  566. }
  567. static void getDependencies (Project& project, const String& moduleID, StringArray& dependencies)
  568. {
  569. ModuleDescription info (project.getModules().getModuleInfo (moduleID));
  570. for (auto uid : info.getDependencies())
  571. {
  572. if (! dependencies.contains (uid, true))
  573. {
  574. dependencies.add (uid);
  575. getDependencies (project, uid, dependencies);
  576. }
  577. }
  578. }
  579. StringArray EnabledModuleList::getExtraDependenciesNeeded (const String& moduleID) const
  580. {
  581. StringArray dependencies, extraDepsNeeded;
  582. getDependencies (project, moduleID, dependencies);
  583. for (auto dep : dependencies)
  584. if (dep != moduleID && ! isModuleEnabled (dep))
  585. extraDepsNeeded.add (dep);
  586. return extraDepsNeeded;
  587. }
  588. bool EnabledModuleList::areMostModulesCopiedLocally() const
  589. {
  590. int numYes = 0, numNo = 0;
  591. for (int i = getNumModules(); --i >= 0;)
  592. {
  593. if (shouldCopyModuleFilesLocally (getModuleID (i)).getValue())
  594. ++numYes;
  595. else
  596. ++numNo;
  597. }
  598. return numYes > numNo;
  599. }
  600. void EnabledModuleList::setLocalCopyModeForAllModules (bool copyLocally)
  601. {
  602. for (int i = getNumModules(); --i >= 0;)
  603. shouldCopyModuleFilesLocally (project.getModules().getModuleID (i)) = copyLocally;
  604. }
  605. File EnabledModuleList::findDefaultModulesFolder (Project& project)
  606. {
  607. ModuleList available;
  608. available.scanAllKnownFolders (project);
  609. for (int i = available.modules.size(); --i >= 0;)
  610. {
  611. File f (available.modules.getUnchecked(i)->getFolder());
  612. if (f.isDirectory())
  613. return f.getParentDirectory();
  614. }
  615. return File::getCurrentWorkingDirectory();
  616. }
  617. void EnabledModuleList::addModuleFromUserSelectedFile()
  618. {
  619. static File lastLocation (findDefaultModulesFolder (project));
  620. FileChooser fc ("Select a module to add...", lastLocation, String());
  621. if (fc.browseForDirectory())
  622. {
  623. lastLocation = fc.getResult();
  624. addModuleOfferingToCopy (lastLocation);
  625. }
  626. }
  627. void EnabledModuleList::addModuleInteractive (const String& moduleID)
  628. {
  629. ModuleList list;
  630. list.scanAllKnownFolders (project);
  631. if (const ModuleDescription* info = list.getModuleWithID (moduleID))
  632. addModule (info->moduleFolder, areMostModulesCopiedLocally());
  633. else
  634. addModuleFromUserSelectedFile();
  635. }
  636. void EnabledModuleList::addModuleOfferingToCopy (const File& f)
  637. {
  638. ModuleDescription m (f);
  639. if (! m.isValid())
  640. {
  641. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  642. "Add Module", "This wasn't a valid module folder!");
  643. return;
  644. }
  645. if (isModuleEnabled (m.getID()))
  646. {
  647. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  648. "Add Module", "The project already contains this module!");
  649. return;
  650. }
  651. addModule (m.moduleFolder, areMostModulesCopiedLocally());
  652. }
  653. bool isJuceFolder (const File& f)
  654. {
  655. return isJuceModulesFolder (f.getChildFile ("modules"));
  656. }
  657. bool isJuceModulesFolder (const File& f)
  658. {
  659. return f.isDirectory() && f.getChildFile ("juce_core").isDirectory();
  660. }