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