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.

913 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.project.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())
  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. static void parseAndAddLibs (StringArray& libList, const String& libs)
  256. {
  257. libList.addTokens (libs, ", ", StringRef());
  258. libList.trim();
  259. libList.sort (false);
  260. libList.removeDuplicates (false);
  261. }
  262. void LibraryModule::prepareExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  263. {
  264. Project& project = exporter.getProject();
  265. exporter.addToExtraSearchPaths (exporter.getModuleFolderRelativeToProject (getID()).getParentDirectory());
  266. const String extraDefs (moduleInfo.getPreprocessorDefs().trim());
  267. if (extraDefs.isNotEmpty())
  268. exporter.getExporterPreprocessorDefs() = exporter.getExporterPreprocessorDefsString() + "\n" + extraDefs;
  269. {
  270. Array<File> compiled;
  271. const File localModuleFolder = project.getModules().shouldCopyModuleFilesLocally (getID()).getValue()
  272. ? project.getLocalModuleFolder (getID())
  273. : moduleInfo.getFolder();
  274. findAndAddCompiledUnits (exporter, &projectSaver, localModuleFolder, compiled);
  275. if (project.getModules().shouldShowAllModuleFilesInProject (getID()).getValue())
  276. addBrowseableCode (exporter, compiled, localModuleFolder);
  277. }
  278. if (isVSTPluginHost (project)) VSTHelpers::addVSTFolderToPath (exporter, false);
  279. if (isVST3PluginHost (project)) VSTHelpers::addVSTFolderToPath (exporter, true);
  280. if (exporter.isXcode())
  281. {
  282. if (isAUPluginHost (project))
  283. exporter.xcodeFrameworks.addTokens ("AudioUnit CoreAudioKit", false);
  284. const String frameworks (moduleInfo.moduleInfo [exporter.isOSX() ? "OSXFrameworks" : "iOSFrameworks"].toString());
  285. exporter.xcodeFrameworks.addTokens (frameworks, ", ", StringRef());
  286. parseAndAddLibs (exporter.xcodeLibs, moduleInfo.moduleInfo [exporter.isOSX() ? "OSXLibs" : "iOSLibs"].toString());
  287. }
  288. else if (exporter.isLinux())
  289. {
  290. parseAndAddLibs (exporter.linuxLibs, moduleInfo.moduleInfo ["LinuxLibs"].toString());
  291. }
  292. else if (exporter.isCodeBlocksWindows())
  293. {
  294. parseAndAddLibs (exporter.mingwLibs, moduleInfo.moduleInfo ["mingwLibs"].toString());
  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. struct FileSorter
  370. {
  371. static int compareElements (const File& f1, const File& f2)
  372. {
  373. return f1.getFileName().compareNatural (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. static bool fileTargetMatches (ProjectExporter& exporter, const String& target)
  390. {
  391. if (exporter.isXcode()) return exporterTargetMatches ("xcode", target);
  392. if (exporter.isWindows()) return exporterTargetMatches ("msvc", target);
  393. if (exporter.isLinux()) return exporterTargetMatches ("linux", target);
  394. if (exporter.isAndroid()) return exporterTargetMatches ("android", target);
  395. if (exporter.isCodeBlocksWindows()) return exporterTargetMatches ("mingw", target);
  396. return target.isEmpty();
  397. }
  398. static bool fileShouldBeCompiled (ProjectExporter& exporter, const var& properties)
  399. {
  400. if (! fileTargetMatches (exporter, properties["target"].toString()))
  401. return false;
  402. if (properties["RTASOnly"] && ! shouldBuildRTAS (exporter.getProject()).getValue())
  403. return false;
  404. if (properties["AudioUnitOnly"] && ! shouldBuildAU (exporter.getProject()).getValue())
  405. return false;
  406. return true;
  407. }
  408. void LibraryModule::findAndAddCompiledUnits (ProjectExporter& exporter, ProjectSaver* projectSaver,
  409. const File& localModuleFolder, Array<File>& result) const
  410. {
  411. const var compileArray (moduleInfo.moduleInfo ["compile"]); // careful to keep this alive while the array is in use!
  412. if (const Array<var>* const files = compileArray.getArray())
  413. {
  414. for (int i = 0; i < files->size(); ++i)
  415. {
  416. const var& file = files->getReference(i);
  417. const String filename (file ["file"].toString());
  418. if (filename.isNotEmpty() && fileShouldBeCompiled (exporter, file))
  419. {
  420. const File compiledFile (localModuleFolder.getChildFile (filename));
  421. result.add (compiledFile);
  422. if (projectSaver != nullptr)
  423. {
  424. Project::Item item (projectSaver->addFileToGeneratedGroup (compiledFile));
  425. if (file ["warnings"].toString().equalsIgnoreCase ("disabled"))
  426. item.getShouldInhibitWarningsValue() = true;
  427. if (file ["stdcall"])
  428. item.getShouldUseStdCallValue() = true;
  429. }
  430. }
  431. }
  432. }
  433. }
  434. static void addFileWithGroups (Project::Item& group, const RelativePath& file, const String& path)
  435. {
  436. const int slash = path.indexOfChar (File::separator);
  437. if (slash >= 0)
  438. {
  439. const String topLevelGroup (path.substring (0, slash));
  440. const String remainingPath (path.substring (slash + 1));
  441. Project::Item newGroup (group.getOrCreateSubGroup (topLevelGroup));
  442. addFileWithGroups (newGroup, file, remainingPath);
  443. }
  444. else
  445. {
  446. if (! group.containsChildForFile (file))
  447. group.addRelativeFile (file, -1, false);
  448. }
  449. }
  450. void LibraryModule::findBrowseableFiles (const File& localModuleFolder, Array<File>& filesFound) const
  451. {
  452. const var filesArray (moduleInfo.moduleInfo ["browse"]);
  453. if (const Array<var>* const files = filesArray.getArray())
  454. for (int i = 0; i < files->size(); ++i)
  455. findWildcardMatches (localModuleFolder, files->getReference(i), filesFound);
  456. }
  457. void LibraryModule::addBrowseableCode (ProjectExporter& exporter, const Array<File>& compiled, const File& localModuleFolder) const
  458. {
  459. if (sourceFiles.size() == 0)
  460. findBrowseableFiles (localModuleFolder, sourceFiles);
  461. Project::Item sourceGroup (Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID()));
  462. const RelativePath moduleFromProject (exporter.getModuleFolderRelativeToProject (getID()));
  463. for (int i = 0; i < sourceFiles.size(); ++i)
  464. {
  465. const String pathWithinModule (FileHelpers::getRelativePathFrom (sourceFiles.getReference(i), localModuleFolder));
  466. // (Note: in exporters like MSVC we have to avoid adding the same file twice, even if one of those instances
  467. // is flagged as being excluded from the build, because this overrides the other and it fails to compile)
  468. if (exporter.canCopeWithDuplicateFiles() || ! compiled.contains (sourceFiles.getReference(i)))
  469. addFileWithGroups (sourceGroup,
  470. moduleFromProject.getChildFile (pathWithinModule),
  471. pathWithinModule);
  472. }
  473. sourceGroup.addFile (localModuleFolder.getChildFile (FileHelpers::getRelativePathFrom (moduleInfo.manifestFile,
  474. moduleInfo.getFolder())), -1, false);
  475. sourceGroup.addFile (getModuleHeaderFile (localModuleFolder), -1, false);
  476. exporter.getModulesGroup().state.addChild (sourceGroup.state.createCopy(), -1, nullptr);
  477. }
  478. //==============================================================================
  479. EnabledModuleList::EnabledModuleList (Project& p, const ValueTree& s)
  480. : project (p), state (s)
  481. {
  482. }
  483. ModuleDescription EnabledModuleList::getModuleInfo (const String& moduleID)
  484. {
  485. return ModuleDescription (getModuleInfoFile (moduleID));
  486. }
  487. bool EnabledModuleList::isModuleEnabled (const String& moduleID) const
  488. {
  489. for (int i = 0; i < state.getNumChildren(); ++i)
  490. if (state.getChild(i) [Ids::ID] == moduleID)
  491. return true;
  492. return false;
  493. }
  494. bool EnabledModuleList::isAudioPluginModuleMissing() const
  495. {
  496. return project.getProjectType().isAudioPlugin()
  497. && ! isModuleEnabled ("juce_audio_plugin_client");
  498. }
  499. Value EnabledModuleList::shouldShowAllModuleFilesInProject (const String& moduleID)
  500. {
  501. return state.getChildWithProperty (Ids::ID, moduleID)
  502. .getPropertyAsValue (Ids::showAllCode, getUndoManager());
  503. }
  504. File EnabledModuleList::findLocalModuleInfoFile (const String& moduleID, bool useExportersForOtherOSes)
  505. {
  506. for (Project::ExporterIterator exporter (project); exporter.next();)
  507. {
  508. if (useExportersForOtherOSes || exporter->mayCompileOnCurrentOS())
  509. {
  510. const String path (exporter->getPathForModuleString (moduleID));
  511. if (path.isNotEmpty())
  512. {
  513. const File moduleFolder (project.resolveFilename (path));
  514. if (moduleFolder.exists())
  515. {
  516. File f (moduleFolder.getChildFile (ModuleDescription::getManifestFileName()));
  517. if (f.exists())
  518. return f;
  519. f = moduleFolder.getChildFile (moduleID)
  520. .getChildFile (ModuleDescription::getManifestFileName());
  521. if (f.exists())
  522. return f;
  523. f = moduleFolder.getChildFile ("modules")
  524. .getChildFile (moduleID)
  525. .getChildFile (ModuleDescription::getManifestFileName());
  526. if (f.exists())
  527. return f;
  528. }
  529. }
  530. }
  531. }
  532. return File::nonexistent;
  533. }
  534. File EnabledModuleList::getModuleInfoFile (const String& moduleID)
  535. {
  536. const File f (findLocalModuleInfoFile (moduleID, false));
  537. if (f != File::nonexistent)
  538. return f;
  539. return findLocalModuleInfoFile (moduleID, true);
  540. }
  541. File EnabledModuleList::getModuleFolder (const String& moduleID)
  542. {
  543. const File infoFile (getModuleInfoFile (moduleID));
  544. return infoFile.exists() ? infoFile.getParentDirectory()
  545. : File::nonexistent;
  546. }
  547. struct ModuleTreeSorter
  548. {
  549. static int compareElements (const ValueTree& m1, const ValueTree& m2)
  550. {
  551. return m1[Ids::ID].toString().compareIgnoreCase (m2[Ids::ID]);
  552. }
  553. };
  554. void EnabledModuleList::sortAlphabetically()
  555. {
  556. ModuleTreeSorter sorter;
  557. state.sort (sorter, getUndoManager(), false);
  558. }
  559. Value EnabledModuleList::shouldCopyModuleFilesLocally (const String& moduleID) const
  560. {
  561. return state.getChildWithProperty (Ids::ID, moduleID)
  562. .getPropertyAsValue (Ids::useLocalCopy, getUndoManager());
  563. }
  564. void EnabledModuleList::addModule (const File& moduleManifestFile, bool copyLocally)
  565. {
  566. ModuleDescription info (moduleManifestFile);
  567. if (info.isValid())
  568. {
  569. const String moduleID (info.getID());
  570. if (! isModuleEnabled (moduleID))
  571. {
  572. ValueTree module (Ids::MODULES);
  573. module.setProperty (Ids::ID, moduleID, nullptr);
  574. state.addChild (module, -1, getUndoManager());
  575. sortAlphabetically();
  576. shouldShowAllModuleFilesInProject (moduleID) = true;
  577. shouldCopyModuleFilesLocally (moduleID) = copyLocally;
  578. RelativePath path (moduleManifestFile.getParentDirectory().getParentDirectory(),
  579. project.getProjectFolder(), RelativePath::projectFolder);
  580. for (Project::ExporterIterator exporter (project); exporter.next();)
  581. exporter->getPathForModuleValue (moduleID) = path.toUnixStyle();
  582. }
  583. }
  584. }
  585. void EnabledModuleList::removeModule (String moduleID) // must be pass-by-value, and not a const ref!
  586. {
  587. for (int i = state.getNumChildren(); --i >= 0;)
  588. if (state.getChild(i) [Ids::ID] == moduleID)
  589. state.removeChild (i, getUndoManager());
  590. for (Project::ExporterIterator exporter (project); exporter.next();)
  591. exporter->removePathForModule (moduleID);
  592. }
  593. void EnabledModuleList::createRequiredModules (OwnedArray<LibraryModule>& modules)
  594. {
  595. for (int i = 0; i < getNumModules(); ++i)
  596. {
  597. ModuleDescription info (getModuleInfo (getModuleID (i)));
  598. if (info.isValid())
  599. modules.add (new LibraryModule (info));
  600. }
  601. }
  602. StringArray EnabledModuleList::getAllModules() const
  603. {
  604. StringArray moduleIDs;
  605. for (int i = 0; i < getNumModules(); ++i)
  606. moduleIDs.add (getModuleID(i));
  607. return moduleIDs;
  608. }
  609. static void getDependencies (Project& project, const String& moduleID, StringArray& dependencies)
  610. {
  611. ModuleDescription info (project.getModules().getModuleInfo (moduleID));
  612. if (info.isValid())
  613. {
  614. const var depsArray (info.moduleInfo ["dependencies"]);
  615. if (const Array<var>* const deps = depsArray.getArray())
  616. {
  617. for (int i = 0; i < deps->size(); ++i)
  618. {
  619. const var& d = deps->getReference(i);
  620. String uid (d [Ids::ID].toString());
  621. String version (d [Ids::version].toString());
  622. if (! dependencies.contains (uid, true))
  623. {
  624. dependencies.add (uid);
  625. getDependencies (project, uid, dependencies);
  626. }
  627. }
  628. }
  629. }
  630. }
  631. StringArray EnabledModuleList::getExtraDependenciesNeeded (const String& moduleID) const
  632. {
  633. StringArray dependencies, extraDepsNeeded;
  634. getDependencies (project, moduleID, dependencies);
  635. for (int i = 0; i < dependencies.size(); ++i)
  636. if ((! isModuleEnabled (dependencies[i])) && dependencies[i] != moduleID)
  637. extraDepsNeeded.add (dependencies[i]);
  638. return extraDepsNeeded;
  639. }
  640. bool EnabledModuleList::areMostModulesCopiedLocally() const
  641. {
  642. int numYes = 0, numNo = 0;
  643. for (int i = getNumModules(); --i >= 0;)
  644. {
  645. if (shouldCopyModuleFilesLocally (getModuleID (i)).getValue())
  646. ++numYes;
  647. else
  648. ++numNo;
  649. }
  650. return numYes > numNo;
  651. }
  652. void EnabledModuleList::setLocalCopyModeForAllModules (bool copyLocally)
  653. {
  654. for (int i = getNumModules(); --i >= 0;)
  655. shouldCopyModuleFilesLocally (project.getModules().getModuleID (i)) = copyLocally;
  656. }
  657. File EnabledModuleList::findDefaultModulesFolder (Project& project)
  658. {
  659. ModuleList available;
  660. available.scanAllKnownFolders (project);
  661. for (int i = available.modules.size(); --i >= 0;)
  662. {
  663. File f (available.modules.getUnchecked(i)->getFolder());
  664. if (f.isDirectory())
  665. return f.getParentDirectory();
  666. }
  667. return File::getCurrentWorkingDirectory();
  668. }
  669. void EnabledModuleList::addModuleFromUserSelectedFile()
  670. {
  671. static File lastLocation (findDefaultModulesFolder (project));
  672. FileChooser fc ("Select a module to add...", lastLocation, String::empty, false);
  673. if (fc.browseForDirectory())
  674. {
  675. lastLocation = fc.getResult();
  676. addModuleOfferingToCopy (lastLocation);
  677. }
  678. }
  679. void EnabledModuleList::addModuleInteractive (const String& moduleID)
  680. {
  681. ModuleList list;
  682. list.scanAllKnownFolders (project);
  683. if (const ModuleDescription* info = list.getModuleWithID (moduleID))
  684. addModule (info->manifestFile, areMostModulesCopiedLocally());
  685. else
  686. addModuleFromUserSelectedFile();
  687. }
  688. void EnabledModuleList::addModuleOfferingToCopy (const File& f)
  689. {
  690. ModuleDescription m (f);
  691. if (! m.isValid())
  692. m = ModuleDescription (f.getChildFile (ModuleDescription::getManifestFileName()));
  693. if (! m.isValid())
  694. {
  695. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  696. "Add Module", "This wasn't a valid module folder!");
  697. return;
  698. }
  699. if (isModuleEnabled (m.getID()))
  700. {
  701. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  702. "Add Module", "The project already contains this module!");
  703. return;
  704. }
  705. addModule (m.manifestFile, areMostModulesCopiedLocally());
  706. }
  707. bool isJuceFolder (const File& f)
  708. {
  709. return isJuceModulesFolder (f.getChildFile ("modules"));
  710. }
  711. bool isJuceModulesFolder (const File& f)
  712. {
  713. return f.isDirectory() && f.getChildFile ("juce_core").isDirectory();
  714. }