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.

885 lines
30KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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_Module.h"
  18. #include "jucer_ProjectType.h"
  19. #include "../Project Saving/jucer_ProjectExporter.h"
  20. #include "../Project Saving/jucer_ProjectSaver.h"
  21. #include "jucer_AudioPluginModule.h"
  22. ModuleDescription::ModuleDescription (const File& manifest)
  23. : moduleInfo (JSON::parse (manifest)), manifestFile (manifest)
  24. {
  25. if (moduleInfo.isVoid() && manifestFile.exists())
  26. {
  27. var json;
  28. Result r (JSON::parse (manifestFile.loadFileAsString(), json));
  29. if (r.failed() && manifestFile.loadFileAsString().isNotEmpty())
  30. {
  31. DBG (r.getErrorMessage());
  32. jassertfalse; // broken JSON in a module manifest.
  33. }
  34. }
  35. }
  36. //==============================================================================
  37. ModuleList::ModuleList()
  38. {
  39. }
  40. ModuleList::ModuleList (const ModuleList& other)
  41. {
  42. operator= (other);
  43. }
  44. ModuleList& ModuleList::operator= (const ModuleList& other)
  45. {
  46. modules.clear();
  47. modules.addCopiesOf (other.modules);
  48. return *this;
  49. }
  50. const ModuleDescription* ModuleList::getModuleWithID (const String& moduleID) const
  51. {
  52. for (int i = 0; i < modules.size(); ++i)
  53. {
  54. ModuleDescription* m = modules.getUnchecked(i);
  55. if (m->getID() == moduleID)
  56. return m;
  57. }
  58. return nullptr;
  59. }
  60. struct ModuleSorter
  61. {
  62. static int compareElements (const ModuleDescription* m1, const ModuleDescription* m2)
  63. {
  64. return m1->getID().compareIgnoreCase (m2->getID());
  65. }
  66. };
  67. void ModuleList::sort()
  68. {
  69. ModuleSorter sorter;
  70. modules.sort (sorter);
  71. }
  72. StringArray ModuleList::getIDs() const
  73. {
  74. StringArray results;
  75. for (int i = 0; i < modules.size(); ++i)
  76. results.add (modules.getUnchecked(i)->getID());
  77. results.sort (true);
  78. return results;
  79. }
  80. Result ModuleList::addAllModulesInFolder (const File& path)
  81. {
  82. const File moduleDef (path.getChildFile (ModuleDescription::getManifestFileName()));
  83. if (moduleDef.exists())
  84. {
  85. ModuleDescription m (moduleDef);
  86. if (! m.isValid())
  87. return Result::fail ("Failed to load module manifest: " + moduleDef.getFullPathName());
  88. modules.add (new ModuleDescription (m));
  89. }
  90. else
  91. {
  92. for (DirectoryIterator iter (path, false, "*", File::findDirectories); iter.next();)
  93. {
  94. Result r = addAllModulesInFolder (iter.getFile().getLinkedTarget());
  95. if (r.failed())
  96. return r;
  97. }
  98. }
  99. return Result::ok();
  100. }
  101. static Array<File> getAllPossibleModulePaths (Project& project)
  102. {
  103. StringArray paths;
  104. for (Project::ExporterIterator exporter (project); exporter.next();)
  105. {
  106. for (int i = 0; i < project.getModules().getNumModules(); ++i)
  107. {
  108. const String path (exporter->getPathForModuleString (project.getModules().getModuleID (i)));
  109. if (path.isNotEmpty())
  110. paths.addIfNotAlreadyThere (path);
  111. }
  112. String oldPath (exporter->getLegacyModulePath());
  113. if (oldPath.isNotEmpty())
  114. paths.addIfNotAlreadyThere (oldPath);
  115. }
  116. Array<File> files;
  117. for (int i = 0; i < paths.size(); ++i)
  118. {
  119. const File f (project.resolveFilename (paths[i]));
  120. if (f.isDirectory())
  121. {
  122. files.add (f);
  123. if (f.getChildFile ("modules").isDirectory())
  124. files.addIfNotAlreadyThere (f.getChildFile ("modules"));
  125. }
  126. }
  127. return files;
  128. }
  129. Result ModuleList::scanAllKnownFolders (Project& project)
  130. {
  131. modules.clear();
  132. Result result (Result::ok());
  133. const Array<File> modulePaths (getAllPossibleModulePaths (project));
  134. for (int i = 0; i < modulePaths.size(); ++i)
  135. {
  136. result = addAllModulesInFolder (modulePaths.getReference(i));
  137. if (result.failed())
  138. break;
  139. }
  140. sort();
  141. return result;
  142. }
  143. bool ModuleList::loadFromWebsite()
  144. {
  145. modules.clear();
  146. URL baseURL ("http://www.juce.com/juce/modules");
  147. URL url (baseURL.getChildURL ("modulelist.php"));
  148. const ScopedPointer<InputStream> in (url.createInputStream (false, nullptr, nullptr, String::empty, 4000));
  149. if (in == nullptr)
  150. return false;
  151. var infoList (JSON::parse (in->readEntireStreamAsString()));
  152. if (! infoList.isArray())
  153. return false;
  154. const Array<var>* moduleList = infoList.getArray();
  155. for (int i = 0; i < moduleList->size(); ++i)
  156. {
  157. const var& m = moduleList->getReference(i);
  158. const String file (m [Ids::file].toString());
  159. if (file.isNotEmpty())
  160. {
  161. ModuleDescription lm (m [Ids::info]);
  162. if (lm.isValid())
  163. {
  164. lm.url = baseURL.getChildURL (file);
  165. modules.add (new ModuleDescription (lm));
  166. }
  167. }
  168. }
  169. sort();
  170. return true;
  171. }
  172. //==============================================================================
  173. LibraryModule::LibraryModule (const ModuleDescription& d)
  174. : moduleInfo (d)
  175. {
  176. }
  177. bool LibraryModule::isAUPluginHost (const Project& project) const { return getID() == "juce_audio_processors" && project.isConfigFlagEnabled ("JUCE_PLUGINHOST_AU"); }
  178. bool LibraryModule::isVSTPluginHost (const Project& project) const { return getID() == "juce_audio_processors" && project.isConfigFlagEnabled ("JUCE_PLUGINHOST_VST"); }
  179. bool LibraryModule::isVST3PluginHost (const Project& project) const { return getID() == "juce_audio_processors" && project.isConfigFlagEnabled ("JUCE_PLUGINHOST_VST3"); }
  180. File LibraryModule::getModuleHeaderFile (const File& folder) const
  181. {
  182. return folder.getChildFile (moduleInfo.getHeaderName());
  183. }
  184. //==============================================================================
  185. void LibraryModule::writeIncludes (ProjectSaver& projectSaver, OutputStream& out)
  186. {
  187. const File localModuleFolder (projectSaver.getLocalModuleFolder (getID()));
  188. const File localHeader (getModuleHeaderFile (localModuleFolder));
  189. localModuleFolder.createDirectory();
  190. if (projectSaver.project.getModules().shouldCopyModuleFilesLocally (getID()).getValue())
  191. {
  192. projectSaver.copyFolder (moduleInfo.getFolder(), localModuleFolder);
  193. }
  194. else
  195. {
  196. localModuleFolder.createDirectory();
  197. createLocalHeaderWrapper (projectSaver, getModuleHeaderFile (moduleInfo.getFolder()), localHeader);
  198. }
  199. out << CodeHelpers::createIncludeStatement (localHeader, projectSaver.getGeneratedCodeFolder()
  200. .getChildFile ("AppConfig.h")) << newLine;
  201. }
  202. static void writeGuardedInclude (OutputStream& out, StringArray paths, StringArray guards)
  203. {
  204. StringArray uniquePaths (paths);
  205. uniquePaths.removeDuplicates (false);
  206. if (uniquePaths.size() == 1)
  207. {
  208. out << "#include " << paths[0] << newLine;
  209. }
  210. else
  211. {
  212. for (int i = paths.size(); --i >= 0;)
  213. {
  214. for (int j = i; --j >= 0;)
  215. {
  216. if (paths[i] == paths[j] && guards[i] == guards[j])
  217. {
  218. paths.remove (i);
  219. guards.remove (i);
  220. }
  221. }
  222. }
  223. for (int i = 0; i < paths.size(); ++i)
  224. {
  225. out << (i == 0 ? "#if " : "#elif ") << guards[i] << newLine
  226. << " #include " << paths[i] << newLine;
  227. }
  228. out << "#else" << newLine
  229. << " #error \"This file is designed to be used in an Introjucer-generated project!\"" << newLine
  230. << "#endif" << newLine;
  231. }
  232. }
  233. void LibraryModule::createLocalHeaderWrapper (ProjectSaver& projectSaver, const File& originalHeader, const File& localHeader) const
  234. {
  235. Project& project = projectSaver.project;
  236. MemoryOutputStream out;
  237. out << "// This is an auto-generated file to redirect any included" << newLine
  238. << "// module headers to the correct external folder." << newLine
  239. << newLine;
  240. StringArray paths, guards;
  241. for (Project::ExporterIterator exporter (project); exporter.next();)
  242. {
  243. const RelativePath headerFromProject (exporter->getModuleFolderRelativeToProject (getID(), projectSaver)
  244. .getChildFile (originalHeader.getFileName()));
  245. const RelativePath fileFromHere (headerFromProject.rebased (project.getProjectFolder(),
  246. localHeader.getParentDirectory(), RelativePath::unknown));
  247. paths.add (fileFromHere.toUnixStyle().quoted());
  248. guards.add ("defined (" + exporter->getExporterIdentifierMacro() + ")");
  249. }
  250. writeGuardedInclude (out, paths, guards);
  251. out << newLine;
  252. projectSaver.replaceFileIfDifferent (localHeader, out);
  253. }
  254. //==============================================================================
  255. void LibraryModule::prepareExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  256. {
  257. Project& project = exporter.getProject();
  258. exporter.addToExtraSearchPaths (exporter.getModuleFolderRelativeToProject (getID(), projectSaver).getParentDirectory());
  259. const String extraDefs (moduleInfo.getPreprocessorDefs().trim());
  260. if (extraDefs.isNotEmpty())
  261. exporter.getExporterPreprocessorDefs() = exporter.getExporterPreprocessorDefsString() + "\n" + extraDefs;
  262. {
  263. Array<File> compiled;
  264. const File localModuleFolder = project.getModules().shouldCopyModuleFilesLocally (getID()).getValue()
  265. ? projectSaver.getLocalModuleFolder (getID())
  266. : moduleInfo.getFolder();
  267. findAndAddCompiledCode (exporter, projectSaver, localModuleFolder, compiled);
  268. if (project.getModules().shouldShowAllModuleFilesInProject (getID()).getValue())
  269. addBrowsableCode (exporter, projectSaver, compiled, moduleInfo.getFolder());
  270. }
  271. if (isVSTPluginHost (project)) VSTHelpers::addVSTFolderToPath (exporter, false);
  272. if (isVST3PluginHost (project)) VSTHelpers::addVSTFolderToPath (exporter, true);
  273. if (exporter.isXcode())
  274. {
  275. if (isAUPluginHost (project))
  276. exporter.xcodeFrameworks.addTokens ("AudioUnit CoreAudioKit", false);
  277. const String frameworks (moduleInfo.moduleInfo [exporter.isOSX() ? "OSXFrameworks" : "iOSFrameworks"].toString());
  278. exporter.xcodeFrameworks.addTokens (frameworks, ", ", String::empty);
  279. }
  280. else if (exporter.isLinux())
  281. {
  282. const String libs (moduleInfo.moduleInfo ["LinuxLibs"].toString());
  283. exporter.linuxLibs.addTokens (libs, ", ", String::empty);
  284. exporter.linuxLibs.trim();
  285. exporter.linuxLibs.sort (false);
  286. exporter.linuxLibs.removeDuplicates (false);
  287. }
  288. else if (exporter.isCodeBlocks())
  289. {
  290. const String libs (moduleInfo.moduleInfo ["mingwLibs"].toString());
  291. exporter.mingwLibs.addTokens (libs, ", ", String::empty);
  292. exporter.mingwLibs.trim();
  293. exporter.mingwLibs.sort (false);
  294. exporter.mingwLibs.removeDuplicates (false);
  295. }
  296. if (moduleInfo.isPluginClient())
  297. {
  298. if (shouldBuildVST (project).getValue()) VSTHelpers::prepareExporter (exporter, projectSaver, false);
  299. if (shouldBuildVST3 (project).getValue()) VSTHelpers::prepareExporter (exporter, projectSaver, true);
  300. if (shouldBuildAU (project).getValue()) AUHelpers::prepareExporter (exporter, projectSaver);
  301. if (shouldBuildAAX (project).getValue()) AAXHelpers::prepareExporter (exporter, projectSaver);
  302. if (shouldBuildRTAS (project).getValue()) RTASHelpers::prepareExporter (exporter, projectSaver);
  303. }
  304. }
  305. void LibraryModule::createPropertyEditors (ProjectExporter& exporter, PropertyListBuilder& props) const
  306. {
  307. if (isVSTPluginHost (exporter.getProject())
  308. && ! (moduleInfo.isPluginClient() && shouldBuildVST (exporter.getProject()).getValue()))
  309. VSTHelpers::createVSTPathEditor (exporter, props, false);
  310. if (isVST3PluginHost (exporter.getProject())
  311. && ! (moduleInfo.isPluginClient() && shouldBuildVST3 (exporter.getProject()).getValue()))
  312. VSTHelpers::createVSTPathEditor (exporter, props, true);
  313. if (moduleInfo.isPluginClient())
  314. {
  315. if (shouldBuildVST (exporter.getProject()).getValue()) VSTHelpers::createPropertyEditors (exporter, props, false);
  316. if (shouldBuildVST3 (exporter.getProject()).getValue()) VSTHelpers::createPropertyEditors (exporter, props, true);
  317. if (shouldBuildRTAS (exporter.getProject()).getValue()) RTASHelpers::createPropertyEditors (exporter, props);
  318. if (shouldBuildAAX (exporter.getProject()).getValue()) AAXHelpers::createPropertyEditors (exporter, props);
  319. }
  320. }
  321. void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::ConfigFlag>& flags) const
  322. {
  323. const File header (getModuleHeaderFile (moduleInfo.getFolder()));
  324. jassert (header.exists());
  325. StringArray lines;
  326. header.readLines (lines);
  327. for (int i = 0; i < lines.size(); ++i)
  328. {
  329. String line (lines[i].trim());
  330. if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:"))
  331. {
  332. ScopedPointer <Project::ConfigFlag> config (new Project::ConfigFlag());
  333. config->sourceModuleID = getID();
  334. config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim();
  335. if (config->symbol.length() > 2)
  336. {
  337. ++i;
  338. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  339. {
  340. if (lines[i].trim().isNotEmpty())
  341. config->description = config->description.trim() + " " + lines[i].trim();
  342. ++i;
  343. }
  344. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  345. config->value.referTo (project.getConfigFlag (config->symbol));
  346. flags.add (config.release());
  347. }
  348. }
  349. }
  350. }
  351. //==============================================================================
  352. static bool exporterTargetMatches (const String& test, String target)
  353. {
  354. StringArray validTargets;
  355. validTargets.addTokens (target, ",;", "");
  356. validTargets.trim();
  357. validTargets.removeEmptyStrings();
  358. if (validTargets.size() == 0)
  359. return true;
  360. for (int i = validTargets.size(); --i >= 0;)
  361. {
  362. const String& targetName = validTargets[i];
  363. if (targetName == test
  364. || (targetName.startsWithChar ('!') && test != targetName.substring (1).trimStart()))
  365. return true;
  366. }
  367. return false;
  368. }
  369. bool LibraryModule::fileTargetMatches (ProjectExporter& exporter, const String& target)
  370. {
  371. if (exporter.isXcode()) return exporterTargetMatches ("xcode", target);
  372. if (exporter.isWindows()) return exporterTargetMatches ("msvc", target);
  373. if (exporter.isLinux()) return exporterTargetMatches ("linux", target);
  374. if (exporter.isAndroid()) return exporterTargetMatches ("android", target);
  375. if (exporter.isCodeBlocks()) return exporterTargetMatches ("mingw", target);
  376. return target.isEmpty();
  377. }
  378. struct FileSorter
  379. {
  380. static int compareElements (const File& f1, const File& f2)
  381. {
  382. return f1.getFileName().compareIgnoreCase (f2.getFileName());
  383. }
  384. };
  385. void LibraryModule::findWildcardMatches (const File& localModuleFolder, const String& wildcardPath, Array<File>& result) const
  386. {
  387. String path (wildcardPath.upToLastOccurrenceOf ("/", false, false));
  388. String wildCard (wildcardPath.fromLastOccurrenceOf ("/", false, false));
  389. Array<File> tempList;
  390. FileSorter sorter;
  391. DirectoryIterator iter (localModuleFolder.getChildFile (path), false, wildCard);
  392. bool isHiddenFile;
  393. while (iter.next (nullptr, &isHiddenFile, nullptr, nullptr, nullptr, nullptr))
  394. if (! isHiddenFile)
  395. tempList.addSorted (sorter, iter.getFile());
  396. result.addArray (tempList);
  397. }
  398. void LibraryModule::findAndAddCompiledCode (ProjectExporter& exporter, ProjectSaver& projectSaver,
  399. const File& localModuleFolder, Array<File>& result) const
  400. {
  401. const var compileArray (moduleInfo.moduleInfo ["compile"]); // careful to keep this alive while the array is in use!
  402. if (const Array<var>* const files = compileArray.getArray())
  403. {
  404. for (int i = 0; i < files->size(); ++i)
  405. {
  406. const var& file = files->getReference(i);
  407. const String filename (file ["file"].toString());
  408. if (filename.isNotEmpty()
  409. && fileTargetMatches (exporter, file ["target"].toString()))
  410. {
  411. const File compiledFile (localModuleFolder.getChildFile (filename));
  412. result.add (compiledFile);
  413. Project::Item item (projectSaver.addFileToGeneratedGroup (compiledFile));
  414. if (file ["warnings"].toString().equalsIgnoreCase ("disabled"))
  415. item.getShouldInhibitWarningsValue() = true;
  416. if (file ["stdcall"])
  417. item.getShouldUseStdCallValue() = true;
  418. }
  419. }
  420. }
  421. }
  422. static void addFileWithGroups (Project::Item& group, const RelativePath& file, const String& path)
  423. {
  424. const int slash = path.indexOfChar (File::separator);
  425. if (slash >= 0)
  426. {
  427. const String topLevelGroup (path.substring (0, slash));
  428. const String remainingPath (path.substring (slash + 1));
  429. Project::Item 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& localModuleFolder, Array<File>& filesFound) const
  439. {
  440. const var filesArray (moduleInfo.moduleInfo ["browse"]);
  441. if (const Array<var>* const files = filesArray.getArray())
  442. for (int i = 0; i < files->size(); ++i)
  443. findWildcardMatches (localModuleFolder, files->getReference(i), filesFound);
  444. }
  445. void LibraryModule::addBrowsableCode (ProjectExporter& exporter, ProjectSaver& projectSaver,
  446. const Array<File>& compiled, const File& localModuleFolder) const
  447. {
  448. if (sourceFiles.size() == 0)
  449. findBrowseableFiles (localModuleFolder, sourceFiles);
  450. Project::Item sourceGroup (Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID()));
  451. const RelativePath moduleFromProject (exporter.getModuleFolderRelativeToProject (getID(), projectSaver));
  452. for (int i = 0; i < sourceFiles.size(); ++i)
  453. {
  454. const String pathWithinModule (FileHelpers::getRelativePathFrom (sourceFiles.getReference(i), localModuleFolder));
  455. // (Note: in exporters like MSVC we have to avoid adding the same file twice, even if one of those instances
  456. // is flagged as being excluded from the build, because this overrides the other and it fails to compile)
  457. if (exporter.canCopeWithDuplicateFiles() || ! compiled.contains (sourceFiles.getReference(i)))
  458. addFileWithGroups (sourceGroup,
  459. moduleFromProject.getChildFile (pathWithinModule),
  460. pathWithinModule);
  461. }
  462. sourceGroup.addFile (localModuleFolder.getChildFile (FileHelpers::getRelativePathFrom (moduleInfo.manifestFile,
  463. moduleInfo.getFolder())), -1, false);
  464. sourceGroup.addFile (getModuleHeaderFile (localModuleFolder), -1, false);
  465. exporter.getModulesGroup().state.addChild (sourceGroup.state.createCopy(), -1, nullptr);
  466. }
  467. //==============================================================================
  468. EnabledModuleList::EnabledModuleList (Project& p, const ValueTree& s)
  469. : project (p), state (s)
  470. {
  471. }
  472. ModuleDescription EnabledModuleList::getModuleInfo (const String& moduleID)
  473. {
  474. return ModuleDescription (getModuleInfoFile (moduleID));
  475. }
  476. bool EnabledModuleList::isModuleEnabled (const String& moduleID) const
  477. {
  478. for (int i = 0; i < state.getNumChildren(); ++i)
  479. if (state.getChild(i) [Ids::ID] == moduleID)
  480. return true;
  481. return false;
  482. }
  483. bool EnabledModuleList::isAudioPluginModuleMissing() const
  484. {
  485. return project.getProjectType().isAudioPlugin()
  486. && ! isModuleEnabled ("juce_audio_plugin_client");
  487. }
  488. Value EnabledModuleList::shouldShowAllModuleFilesInProject (const String& moduleID)
  489. {
  490. return state.getChildWithProperty (Ids::ID, moduleID)
  491. .getPropertyAsValue (Ids::showAllCode, getUndoManager());
  492. }
  493. File EnabledModuleList::findLocalModuleInfoFile (const String& moduleID, bool useExportersForOtherOSes)
  494. {
  495. for (Project::ExporterIterator exporter (project); exporter.next();)
  496. {
  497. if (useExportersForOtherOSes || exporter->mayCompileOnCurrentOS())
  498. {
  499. const String path (exporter->getPathForModuleString (moduleID));
  500. if (path.isNotEmpty())
  501. {
  502. const File moduleFolder (project.resolveFilename (path));
  503. if (moduleFolder.exists())
  504. {
  505. File f (moduleFolder.getChildFile (ModuleDescription::getManifestFileName()));
  506. if (f.exists())
  507. return f;
  508. f = moduleFolder.getChildFile (moduleID)
  509. .getChildFile (ModuleDescription::getManifestFileName());
  510. if (f.exists())
  511. return f;
  512. f = moduleFolder.getChildFile ("modules")
  513. .getChildFile (moduleID)
  514. .getChildFile (ModuleDescription::getManifestFileName());
  515. if (f.exists())
  516. return f;
  517. }
  518. }
  519. }
  520. }
  521. return File::nonexistent;
  522. }
  523. File EnabledModuleList::getModuleInfoFile (const String& moduleID)
  524. {
  525. const File f (findLocalModuleInfoFile (moduleID, false));
  526. if (f != File::nonexistent)
  527. return f;
  528. return findLocalModuleInfoFile (moduleID, true);
  529. }
  530. File EnabledModuleList::getModuleFolder (const String& moduleID)
  531. {
  532. const File infoFile (getModuleInfoFile (moduleID));
  533. return infoFile.exists() ? infoFile.getParentDirectory()
  534. : File::nonexistent;
  535. }
  536. struct ModuleTreeSorter
  537. {
  538. static int compareElements (const ValueTree& m1, const ValueTree& m2)
  539. {
  540. return m1[Ids::ID].toString().compareIgnoreCase (m2[Ids::ID]);
  541. }
  542. };
  543. void EnabledModuleList::sortAlphabetically()
  544. {
  545. ModuleTreeSorter sorter;
  546. state.sort (sorter, getUndoManager(), false);
  547. }
  548. Value EnabledModuleList::shouldCopyModuleFilesLocally (const String& moduleID) const
  549. {
  550. return state.getChildWithProperty (Ids::ID, moduleID)
  551. .getPropertyAsValue (Ids::useLocalCopy, getUndoManager());
  552. }
  553. void EnabledModuleList::addModule (const File& moduleManifestFile, bool copyLocally)
  554. {
  555. ModuleDescription info (moduleManifestFile);
  556. if (info.isValid())
  557. {
  558. const String moduleID (info.getID());
  559. if (! isModuleEnabled (moduleID))
  560. {
  561. ValueTree module (Ids::MODULES);
  562. module.setProperty (Ids::ID, moduleID, nullptr);
  563. state.addChild (module, -1, getUndoManager());
  564. sortAlphabetically();
  565. shouldShowAllModuleFilesInProject (moduleID) = true;
  566. shouldCopyModuleFilesLocally (moduleID) = copyLocally;
  567. RelativePath path (moduleManifestFile.getParentDirectory().getParentDirectory(),
  568. project.getProjectFolder(), RelativePath::projectFolder);
  569. for (Project::ExporterIterator exporter (project); exporter.next();)
  570. exporter->getPathForModuleValue (moduleID) = path.toUnixStyle();
  571. }
  572. }
  573. }
  574. void EnabledModuleList::removeModule (String moduleID) // must be pass-by-value, and not a const ref!
  575. {
  576. for (int i = state.getNumChildren(); --i >= 0;)
  577. if (state.getChild(i) [Ids::ID] == moduleID)
  578. state.removeChild (i, getUndoManager());
  579. for (Project::ExporterIterator exporter (project); exporter.next();)
  580. exporter->removePathForModule (moduleID);
  581. }
  582. void EnabledModuleList::createRequiredModules (OwnedArray<LibraryModule>& modules)
  583. {
  584. for (int i = 0; i < getNumModules(); ++i)
  585. {
  586. ModuleDescription info (getModuleInfo (getModuleID (i)));
  587. if (info.isValid())
  588. modules.add (new LibraryModule (info));
  589. }
  590. }
  591. StringArray EnabledModuleList::getAllModules() const
  592. {
  593. StringArray moduleIDs;
  594. for (int i = 0; i < getNumModules(); ++i)
  595. moduleIDs.add (getModuleID(i));
  596. return moduleIDs;
  597. }
  598. static void getDependencies (Project& project, const String& moduleID, StringArray& dependencies)
  599. {
  600. ModuleDescription info (project.getModules().getModuleInfo (moduleID));
  601. if (info.isValid())
  602. {
  603. const var depsArray (info.moduleInfo ["dependencies"]);
  604. if (const Array<var>* const deps = depsArray.getArray())
  605. {
  606. for (int i = 0; i < deps->size(); ++i)
  607. {
  608. const var& d = deps->getReference(i);
  609. String uid (d [Ids::ID].toString());
  610. String version (d [Ids::version].toString());
  611. if (! dependencies.contains (uid, true))
  612. {
  613. dependencies.add (uid);
  614. getDependencies (project, uid, dependencies);
  615. }
  616. }
  617. }
  618. }
  619. }
  620. StringArray EnabledModuleList::getExtraDependenciesNeeded (const String& moduleID) const
  621. {
  622. StringArray dependencies, extraDepsNeeded;
  623. getDependencies (project, moduleID, dependencies);
  624. for (int i = 0; i < dependencies.size(); ++i)
  625. if ((! isModuleEnabled (dependencies[i])) && dependencies[i] != moduleID)
  626. extraDepsNeeded.add (dependencies[i]);
  627. return extraDepsNeeded;
  628. }
  629. bool EnabledModuleList::areMostModulesCopiedLocally() const
  630. {
  631. int numYes = 0, numNo = 0;
  632. for (int i = getNumModules(); --i >= 0;)
  633. {
  634. if (shouldCopyModuleFilesLocally (getModuleID (i)).getValue())
  635. ++numYes;
  636. else
  637. ++numNo;
  638. }
  639. return numYes > numNo;
  640. }
  641. void EnabledModuleList::setLocalCopyModeForAllModules (bool copyLocally)
  642. {
  643. for (int i = getNumModules(); --i >= 0;)
  644. shouldCopyModuleFilesLocally (project.getModules().getModuleID (i)) = copyLocally;
  645. }
  646. File EnabledModuleList::findDefaultModulesFolder (Project& project)
  647. {
  648. ModuleList available;
  649. available.scanAllKnownFolders (project);
  650. for (int i = available.modules.size(); --i >= 0;)
  651. {
  652. File f (available.modules.getUnchecked(i)->getFolder());
  653. if (f.isDirectory())
  654. return f.getParentDirectory();
  655. }
  656. return File::getCurrentWorkingDirectory();
  657. }
  658. void EnabledModuleList::addModuleFromUserSelectedFile()
  659. {
  660. static File lastLocation (findDefaultModulesFolder (project));
  661. FileChooser fc ("Select a module to add...", lastLocation, String::empty, false);
  662. if (fc.browseForDirectory())
  663. {
  664. lastLocation = fc.getResult();
  665. addModuleOfferingToCopy (lastLocation);
  666. }
  667. }
  668. void EnabledModuleList::addModuleInteractive (const String& moduleID)
  669. {
  670. ModuleList list;
  671. list.scanAllKnownFolders (project);
  672. if (const ModuleDescription* info = list.getModuleWithID (moduleID))
  673. addModule (info->manifestFile, areMostModulesCopiedLocally());
  674. else
  675. addModuleFromUserSelectedFile();
  676. }
  677. void EnabledModuleList::addModuleOfferingToCopy (const File& f)
  678. {
  679. ModuleDescription m (f);
  680. if (! m.isValid())
  681. m = ModuleDescription (f.getChildFile (ModuleDescription::getManifestFileName()));
  682. if (! m.isValid())
  683. {
  684. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  685. "Add Module", "This wasn't a valid module folder!");
  686. return;
  687. }
  688. if (isModuleEnabled (m.getID()))
  689. {
  690. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  691. "Add Module", "The project already contains this module!");
  692. return;
  693. }
  694. addModule (m.manifestFile, areMostModulesCopiedLocally());
  695. }