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.

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