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
29KB

  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. File LibraryModule::getModuleHeaderFile (const File& folder) const
  180. {
  181. return folder.getChildFile (moduleInfo.getHeaderName());
  182. }
  183. //==============================================================================
  184. void LibraryModule::writeIncludes (ProjectSaver& projectSaver, OutputStream& out)
  185. {
  186. const File localModuleFolder (projectSaver.getLocalModuleFolder (getID()));
  187. const File localHeader (getModuleHeaderFile (localModuleFolder));
  188. localModuleFolder.createDirectory();
  189. if (projectSaver.project.getModules().shouldCopyModuleFilesLocally (getID()).getValue())
  190. {
  191. projectSaver.copyFolder (moduleInfo.getFolder(), localModuleFolder);
  192. }
  193. else
  194. {
  195. localModuleFolder.createDirectory();
  196. createLocalHeaderWrapper (projectSaver, getModuleHeaderFile (moduleInfo.getFolder()), localHeader);
  197. }
  198. out << CodeHelpers::createIncludeStatement (localHeader, projectSaver.getGeneratedCodeFolder()
  199. .getChildFile ("AppConfig.h")) << newLine;
  200. }
  201. static void writeGuardedInclude (OutputStream& out, StringArray paths, StringArray guards)
  202. {
  203. StringArray uniquePaths (paths);
  204. uniquePaths.removeDuplicates (false);
  205. if (uniquePaths.size() == 1)
  206. {
  207. out << "#include " << paths[0] << newLine;
  208. }
  209. else
  210. {
  211. for (int i = paths.size(); --i >= 0;)
  212. {
  213. for (int j = i; --j >= 0;)
  214. {
  215. if (paths[i] == paths[j] && guards[i] == guards[j])
  216. {
  217. paths.remove (i);
  218. guards.remove (i);
  219. }
  220. }
  221. }
  222. for (int i = 0; i < paths.size(); ++i)
  223. {
  224. out << (i == 0 ? "#if " : "#elif ") << guards[i] << newLine
  225. << " #include " << paths[i] << newLine;
  226. }
  227. out << "#else" << newLine
  228. << " #error \"This file is designed to be used in an Introjucer-generated project!\"" << newLine
  229. << "#endif" << newLine;
  230. }
  231. }
  232. void LibraryModule::createLocalHeaderWrapper (ProjectSaver& projectSaver, const File& originalHeader, const File& localHeader) const
  233. {
  234. Project& project = projectSaver.project;
  235. MemoryOutputStream out;
  236. out << "// This is an auto-generated file to redirect any included" << newLine
  237. << "// module headers to the correct external folder." << newLine
  238. << newLine;
  239. StringArray paths, guards;
  240. for (Project::ExporterIterator exporter (project); exporter.next();)
  241. {
  242. const RelativePath headerFromProject (exporter->getModuleFolderRelativeToProject (getID(), projectSaver)
  243. .getChildFile (originalHeader.getFileName()));
  244. const RelativePath fileFromHere (headerFromProject.rebased (project.getProjectFolder(),
  245. localHeader.getParentDirectory(), RelativePath::unknown));
  246. paths.add (fileFromHere.toUnixStyle().quoted());
  247. guards.add ("defined (" + exporter->getExporterIdentifierMacro() + ")");
  248. }
  249. writeGuardedInclude (out, paths, guards);
  250. out << newLine;
  251. projectSaver.replaceFileIfDifferent (localHeader, out);
  252. }
  253. //==============================================================================
  254. void LibraryModule::prepareExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  255. {
  256. Project& project = exporter.getProject();
  257. exporter.addToExtraSearchPaths (exporter.getModuleFolderRelativeToProject (getID(), projectSaver).getParentDirectory());
  258. const String extraDefs (moduleInfo.getPreprocessorDefs().trim());
  259. if (extraDefs.isNotEmpty())
  260. exporter.getExporterPreprocessorDefs() = exporter.getExporterPreprocessorDefsString() + "\n" + extraDefs;
  261. {
  262. Array<File> compiled;
  263. const File localModuleFolder = project.getModules().shouldCopyModuleFilesLocally (getID()).getValue()
  264. ? projectSaver.getLocalModuleFolder (getID())
  265. : moduleInfo.getFolder();
  266. findAndAddCompiledCode (exporter, projectSaver, localModuleFolder, compiled);
  267. if (project.getModules().shouldShowAllModuleFilesInProject (getID()).getValue())
  268. addBrowsableCode (exporter, projectSaver, compiled, moduleInfo.getFolder());
  269. }
  270. if (isVSTPluginHost (project))
  271. VSTHelpers::addVSTFolderToPath (exporter, exporter.extraSearchPaths);
  272. if (exporter.isXcode())
  273. {
  274. if (isAUPluginHost (project))
  275. exporter.xcodeFrameworks.addTokens ("AudioUnit CoreAudioKit", false);
  276. const String frameworks (moduleInfo.moduleInfo [exporter.isOSX() ? "OSXFrameworks" : "iOSFrameworks"].toString());
  277. exporter.xcodeFrameworks.addTokens (frameworks, ", ", String::empty);
  278. }
  279. else if (exporter.isLinux())
  280. {
  281. const String libs (moduleInfo.moduleInfo ["LinuxLibs"].toString());
  282. exporter.linuxLibs.addTokens (libs, ", ", String::empty);
  283. exporter.linuxLibs.trim();
  284. exporter.linuxLibs.sort (false);
  285. exporter.linuxLibs.removeDuplicates (false);
  286. }
  287. else if (exporter.isCodeBlocks())
  288. {
  289. const String libs (moduleInfo.moduleInfo ["mingwLibs"].toString());
  290. exporter.mingwLibs.addTokens (libs, ", ", String::empty);
  291. exporter.mingwLibs.trim();
  292. exporter.mingwLibs.sort (false);
  293. exporter.mingwLibs.removeDuplicates (false);
  294. }
  295. if (moduleInfo.isPluginClient())
  296. {
  297. if (shouldBuildVST (project).getValue()) VSTHelpers::prepareExporter (exporter, projectSaver);
  298. if (shouldBuildAU (project).getValue()) AUHelpers::prepareExporter (exporter, projectSaver);
  299. if (shouldBuildAAX (project).getValue()) AAXHelpers::prepareExporter (exporter, projectSaver);
  300. if (shouldBuildRTAS (project).getValue()) RTASHelpers::prepareExporter (exporter, projectSaver);
  301. }
  302. }
  303. void LibraryModule::createPropertyEditors (ProjectExporter& exporter, PropertyListBuilder& props) const
  304. {
  305. if (isVSTPluginHost (exporter.getProject())
  306. && ! (moduleInfo.isPluginClient() && shouldBuildVST (exporter.getProject()).getValue()))
  307. VSTHelpers::createVSTPathEditor (exporter, props);
  308. if (moduleInfo.isPluginClient())
  309. {
  310. if (shouldBuildVST (exporter.getProject()).getValue()) VSTHelpers::createPropertyEditors (exporter, props);
  311. if (shouldBuildRTAS (exporter.getProject()).getValue()) RTASHelpers::createPropertyEditors (exporter, props);
  312. if (shouldBuildAAX (exporter.getProject()).getValue()) AAXHelpers::createPropertyEditors (exporter, props);
  313. }
  314. }
  315. void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::ConfigFlag>& flags) const
  316. {
  317. const File header (getModuleHeaderFile (moduleInfo.getFolder()));
  318. jassert (header.exists());
  319. StringArray lines;
  320. header.readLines (lines);
  321. for (int i = 0; i < lines.size(); ++i)
  322. {
  323. String line (lines[i].trim());
  324. if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:"))
  325. {
  326. ScopedPointer <Project::ConfigFlag> config (new Project::ConfigFlag());
  327. config->sourceModuleID = getID();
  328. config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim();
  329. if (config->symbol.length() > 2)
  330. {
  331. ++i;
  332. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  333. {
  334. if (lines[i].trim().isNotEmpty())
  335. config->description = config->description.trim() + " " + lines[i].trim();
  336. ++i;
  337. }
  338. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  339. config->value.referTo (project.getConfigFlag (config->symbol));
  340. flags.add (config.release());
  341. }
  342. }
  343. }
  344. }
  345. //==============================================================================
  346. static bool exporterTargetMatches (const String& test, String target)
  347. {
  348. StringArray validTargets;
  349. validTargets.addTokens (target, ",;", "");
  350. validTargets.trim();
  351. validTargets.removeEmptyStrings();
  352. if (validTargets.size() == 0)
  353. return true;
  354. for (int i = validTargets.size(); --i >= 0;)
  355. {
  356. const String& targetName = validTargets[i];
  357. if (targetName == test
  358. || (targetName.startsWithChar ('!') && test != targetName.substring (1).trimStart()))
  359. return true;
  360. }
  361. return false;
  362. }
  363. bool LibraryModule::fileTargetMatches (ProjectExporter& exporter, const String& target)
  364. {
  365. if (exporter.isXcode()) return exporterTargetMatches ("xcode", target);
  366. if (exporter.isWindows()) return exporterTargetMatches ("msvc", target);
  367. if (exporter.isLinux()) return exporterTargetMatches ("linux", target);
  368. if (exporter.isAndroid()) return exporterTargetMatches ("android", target);
  369. if (exporter.isCodeBlocks()) return exporterTargetMatches ("mingw", target);
  370. return target.isEmpty();
  371. }
  372. struct FileSorter
  373. {
  374. static int compareElements (const File& f1, const File& f2)
  375. {
  376. return f1.getFileName().compareIgnoreCase (f2.getFileName());
  377. }
  378. };
  379. void LibraryModule::findWildcardMatches (const File& localModuleFolder, const String& wildcardPath, Array<File>& result) const
  380. {
  381. String path (wildcardPath.upToLastOccurrenceOf ("/", false, false));
  382. String wildCard (wildcardPath.fromLastOccurrenceOf ("/", false, false));
  383. Array<File> tempList;
  384. FileSorter sorter;
  385. DirectoryIterator iter (localModuleFolder.getChildFile (path), false, wildCard);
  386. bool isHiddenFile;
  387. while (iter.next (nullptr, &isHiddenFile, nullptr, nullptr, nullptr, nullptr))
  388. if (! isHiddenFile)
  389. tempList.addSorted (sorter, iter.getFile());
  390. result.addArray (tempList);
  391. }
  392. void LibraryModule::findAndAddCompiledCode (ProjectExporter& exporter, ProjectSaver& projectSaver,
  393. const File& localModuleFolder, Array<File>& result) const
  394. {
  395. const var compileArray (moduleInfo.moduleInfo ["compile"]); // careful to keep this alive while the array is in use!
  396. if (const Array<var>* const files = compileArray.getArray())
  397. {
  398. for (int i = 0; i < files->size(); ++i)
  399. {
  400. const var& file = files->getReference(i);
  401. const String filename (file ["file"].toString());
  402. if (filename.isNotEmpty()
  403. && fileTargetMatches (exporter, file ["target"].toString()))
  404. {
  405. const File compiledFile (localModuleFolder.getChildFile (filename));
  406. result.add (compiledFile);
  407. Project::Item item (projectSaver.addFileToGeneratedGroup (compiledFile));
  408. if (file ["warnings"].toString().equalsIgnoreCase ("disabled"))
  409. item.getShouldInhibitWarningsValue() = true;
  410. if (file ["stdcall"])
  411. item.getShouldUseStdCallValue() = true;
  412. }
  413. }
  414. }
  415. }
  416. static void addFileWithGroups (Project::Item& group, const RelativePath& file, const String& path)
  417. {
  418. const int slash = path.indexOfChar (File::separator);
  419. if (slash >= 0)
  420. {
  421. const String topLevelGroup (path.substring (0, slash));
  422. const String remainingPath (path.substring (slash + 1));
  423. Project::Item newGroup (group.getOrCreateSubGroup (topLevelGroup));
  424. addFileWithGroups (newGroup, file, remainingPath);
  425. }
  426. else
  427. {
  428. if (! group.containsChildForFile (file))
  429. group.addRelativeFile (file, -1, false);
  430. }
  431. }
  432. void LibraryModule::findBrowseableFiles (const File& localModuleFolder, Array<File>& filesFound) const
  433. {
  434. const var filesArray (moduleInfo.moduleInfo ["browse"]);
  435. if (const Array<var>* const files = filesArray.getArray())
  436. for (int i = 0; i < files->size(); ++i)
  437. findWildcardMatches (localModuleFolder, files->getReference(i), filesFound);
  438. }
  439. void LibraryModule::addBrowsableCode (ProjectExporter& exporter, ProjectSaver& projectSaver,
  440. const Array<File>& compiled, const File& localModuleFolder) const
  441. {
  442. if (sourceFiles.size() == 0)
  443. findBrowseableFiles (localModuleFolder, sourceFiles);
  444. Project::Item sourceGroup (Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID()));
  445. const RelativePath moduleFromProject (exporter.getModuleFolderRelativeToProject (getID(), projectSaver));
  446. for (int i = 0; i < sourceFiles.size(); ++i)
  447. {
  448. const String pathWithinModule (FileHelpers::getRelativePathFrom (sourceFiles.getReference(i), localModuleFolder));
  449. // (Note: in exporters like MSVC we have to avoid adding the same file twice, even if one of those instances
  450. // is flagged as being excluded from the build, because this overrides the other and it fails to compile)
  451. if (exporter.canCopeWithDuplicateFiles() || ! compiled.contains (sourceFiles.getReference(i)))
  452. addFileWithGroups (sourceGroup,
  453. moduleFromProject.getChildFile (pathWithinModule),
  454. pathWithinModule);
  455. }
  456. sourceGroup.addFile (localModuleFolder.getChildFile (FileHelpers::getRelativePathFrom (moduleInfo.manifestFile,
  457. moduleInfo.getFolder())), -1, false);
  458. sourceGroup.addFile (getModuleHeaderFile (localModuleFolder), -1, false);
  459. exporter.getModulesGroup().state.addChild (sourceGroup.state.createCopy(), -1, nullptr);
  460. }
  461. //==============================================================================
  462. EnabledModuleList::EnabledModuleList (Project& p, const ValueTree& s)
  463. : project (p), state (s)
  464. {
  465. }
  466. ModuleDescription EnabledModuleList::getModuleInfo (const String& moduleID)
  467. {
  468. return ModuleDescription (getModuleInfoFile (moduleID));
  469. }
  470. bool EnabledModuleList::isModuleEnabled (const String& moduleID) const
  471. {
  472. for (int i = 0; i < state.getNumChildren(); ++i)
  473. if (state.getChild(i) [Ids::ID] == moduleID)
  474. return true;
  475. return false;
  476. }
  477. bool EnabledModuleList::isAudioPluginModuleMissing() const
  478. {
  479. return project.getProjectType().isAudioPlugin()
  480. && ! isModuleEnabled ("juce_audio_plugin_client");
  481. }
  482. Value EnabledModuleList::shouldShowAllModuleFilesInProject (const String& moduleID)
  483. {
  484. return state.getChildWithProperty (Ids::ID, moduleID)
  485. .getPropertyAsValue (Ids::showAllCode, getUndoManager());
  486. }
  487. File EnabledModuleList::findLocalModuleInfoFile (const String& moduleID, bool useExportersForOtherOSes)
  488. {
  489. for (Project::ExporterIterator exporter (project); exporter.next();)
  490. {
  491. if (useExportersForOtherOSes || exporter->mayCompileOnCurrentOS())
  492. {
  493. const String path (exporter->getPathForModuleString (moduleID));
  494. if (path.isNotEmpty())
  495. {
  496. const File moduleFolder (project.resolveFilename (path));
  497. if (moduleFolder.exists())
  498. {
  499. File f (moduleFolder.getChildFile (ModuleDescription::getManifestFileName()));
  500. if (f.exists())
  501. return f;
  502. f = moduleFolder.getChildFile (moduleID)
  503. .getChildFile (ModuleDescription::getManifestFileName());
  504. if (f.exists())
  505. return f;
  506. f = moduleFolder.getChildFile ("modules")
  507. .getChildFile (moduleID)
  508. .getChildFile (ModuleDescription::getManifestFileName());
  509. if (f.exists())
  510. return f;
  511. }
  512. }
  513. }
  514. }
  515. return File::nonexistent;
  516. }
  517. File EnabledModuleList::getModuleInfoFile (const String& moduleID)
  518. {
  519. const File f (findLocalModuleInfoFile (moduleID, false));
  520. if (f != File::nonexistent)
  521. return f;
  522. return findLocalModuleInfoFile (moduleID, true);
  523. }
  524. File EnabledModuleList::getModuleFolder (const String& moduleID)
  525. {
  526. const File infoFile (getModuleInfoFile (moduleID));
  527. return infoFile.exists() ? infoFile.getParentDirectory()
  528. : File::nonexistent;
  529. }
  530. struct ModuleTreeSorter
  531. {
  532. static int compareElements (const ValueTree& m1, const ValueTree& m2)
  533. {
  534. return m1[Ids::ID].toString().compareIgnoreCase (m2[Ids::ID]);
  535. }
  536. };
  537. void EnabledModuleList::sortAlphabetically()
  538. {
  539. ModuleTreeSorter sorter;
  540. state.sort (sorter, getUndoManager(), false);
  541. }
  542. Value EnabledModuleList::shouldCopyModuleFilesLocally (const String& moduleID) const
  543. {
  544. return state.getChildWithProperty (Ids::ID, moduleID)
  545. .getPropertyAsValue (Ids::useLocalCopy, getUndoManager());
  546. }
  547. void EnabledModuleList::addModule (const File& moduleManifestFile, bool copyLocally)
  548. {
  549. ModuleDescription info (moduleManifestFile);
  550. if (info.isValid())
  551. {
  552. const String moduleID (info.getID());
  553. if (! isModuleEnabled (moduleID))
  554. {
  555. ValueTree module (Ids::MODULES);
  556. module.setProperty (Ids::ID, moduleID, nullptr);
  557. state.addChild (module, -1, getUndoManager());
  558. sortAlphabetically();
  559. shouldShowAllModuleFilesInProject (moduleID) = true;
  560. shouldCopyModuleFilesLocally (moduleID) = copyLocally;
  561. RelativePath path (moduleManifestFile.getParentDirectory().getParentDirectory(),
  562. project.getProjectFolder(), RelativePath::projectFolder);
  563. for (Project::ExporterIterator exporter (project); exporter.next();)
  564. exporter->getPathForModuleValue (moduleID) = path.toUnixStyle();
  565. }
  566. }
  567. }
  568. void EnabledModuleList::removeModule (String moduleID) // must be pass-by-value, and not a const ref!
  569. {
  570. for (int i = state.getNumChildren(); --i >= 0;)
  571. if (state.getChild(i) [Ids::ID] == moduleID)
  572. state.removeChild (i, getUndoManager());
  573. for (Project::ExporterIterator exporter (project); exporter.next();)
  574. exporter->removePathForModule (moduleID);
  575. }
  576. void EnabledModuleList::createRequiredModules (OwnedArray<LibraryModule>& modules)
  577. {
  578. for (int i = 0; i < getNumModules(); ++i)
  579. {
  580. ModuleDescription info (getModuleInfo (getModuleID (i)));
  581. if (info.isValid())
  582. modules.add (new LibraryModule (info));
  583. }
  584. }
  585. StringArray EnabledModuleList::getAllModules() const
  586. {
  587. StringArray moduleIDs;
  588. for (int i = 0; i < getNumModules(); ++i)
  589. moduleIDs.add (getModuleID(i));
  590. return moduleIDs;
  591. }
  592. static void getDependencies (Project& project, const String& moduleID, StringArray& dependencies)
  593. {
  594. ModuleDescription info (project.getModules().getModuleInfo (moduleID));
  595. if (info.isValid())
  596. {
  597. const var depsArray (info.moduleInfo ["dependencies"]);
  598. if (const Array<var>* const deps = depsArray.getArray())
  599. {
  600. for (int i = 0; i < deps->size(); ++i)
  601. {
  602. const var& d = deps->getReference(i);
  603. String uid (d [Ids::ID].toString());
  604. String version (d [Ids::version].toString());
  605. if (! dependencies.contains (uid, true))
  606. {
  607. dependencies.add (uid);
  608. getDependencies (project, uid, dependencies);
  609. }
  610. }
  611. }
  612. }
  613. }
  614. StringArray EnabledModuleList::getExtraDependenciesNeeded (const String& moduleID) const
  615. {
  616. StringArray dependencies, extraDepsNeeded;
  617. getDependencies (project, moduleID, dependencies);
  618. for (int i = 0; i < dependencies.size(); ++i)
  619. if ((! isModuleEnabled (dependencies[i])) && dependencies[i] != moduleID)
  620. extraDepsNeeded.add (dependencies[i]);
  621. return extraDepsNeeded;
  622. }
  623. bool EnabledModuleList::areMostModulesCopiedLocally() const
  624. {
  625. int numYes = 0, numNo = 0;
  626. for (int i = getNumModules(); --i >= 0;)
  627. {
  628. if (shouldCopyModuleFilesLocally (getModuleID (i)).getValue())
  629. ++numYes;
  630. else
  631. ++numNo;
  632. }
  633. return numYes > numNo;
  634. }
  635. void EnabledModuleList::setLocalCopyModeForAllModules (bool copyLocally)
  636. {
  637. for (int i = getNumModules(); --i >= 0;)
  638. shouldCopyModuleFilesLocally (project.getModules().getModuleID (i)) = copyLocally;
  639. }
  640. File EnabledModuleList::findDefaultModulesFolder (Project& project)
  641. {
  642. ModuleList available;
  643. available.scanAllKnownFolders (project);
  644. for (int i = available.modules.size(); --i >= 0;)
  645. {
  646. File f (available.modules.getUnchecked(i)->getFolder());
  647. if (f.isDirectory())
  648. return f.getParentDirectory();
  649. }
  650. return File::getCurrentWorkingDirectory();
  651. }
  652. void EnabledModuleList::addModuleFromUserSelectedFile()
  653. {
  654. static File lastLocation (findDefaultModulesFolder (project));
  655. FileChooser fc ("Select a module to add...", lastLocation, String::empty, false);
  656. if (fc.browseForDirectory())
  657. {
  658. lastLocation = fc.getResult();
  659. addModuleOfferingToCopy (lastLocation);
  660. }
  661. }
  662. void EnabledModuleList::addModuleInteractive (const String& moduleID)
  663. {
  664. ModuleList list;
  665. list.scanAllKnownFolders (project);
  666. if (const ModuleDescription* info = list.getModuleWithID (moduleID))
  667. addModule (info->manifestFile, areMostModulesCopiedLocally());
  668. else
  669. addModuleFromUserSelectedFile();
  670. }
  671. void EnabledModuleList::addModuleOfferingToCopy (const File& f)
  672. {
  673. ModuleDescription m (f);
  674. if (! m.isValid())
  675. m = ModuleDescription (f.getChildFile (ModuleDescription::getManifestFileName()));
  676. if (! m.isValid())
  677. {
  678. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  679. "Add Module", "This wasn't a valid module folder!");
  680. return;
  681. }
  682. if (isModuleEnabled (m.getID()))
  683. {
  684. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  685. "Add Module", "The project already contains this module!");
  686. return;
  687. }
  688. addModule (m.manifestFile, areMostModulesCopiedLocally());
  689. }