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.

912 lines
30KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "jucer_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. if (exporter->isWindows() && fileFromHere.isAbsolute())
  248. paths.add (fileFromHere.toWindowsStyle().quoted());
  249. else
  250. paths.add (fileFromHere.toUnixStyle().quoted());
  251. guards.add ("defined (" + exporter->getExporterIdentifierMacro() + ")");
  252. }
  253. writeGuardedInclude (out, paths, guards);
  254. out << newLine;
  255. projectSaver.replaceFileIfDifferent (localHeader, out);
  256. }
  257. //==============================================================================
  258. static void parseAndAddLibs (StringArray& libList, const String& libs)
  259. {
  260. libList.addTokens (libs, ", ", StringRef());
  261. libList.trim();
  262. libList.sort (false);
  263. libList.removeDuplicates (false);
  264. }
  265. void LibraryModule::prepareExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  266. {
  267. Project& project = exporter.getProject();
  268. exporter.addToExtraSearchPaths (exporter.getModuleFolderRelativeToProject (getID()).getParentDirectory());
  269. const String extraDefs (moduleInfo.getPreprocessorDefs().trim());
  270. if (extraDefs.isNotEmpty())
  271. exporter.getExporterPreprocessorDefs() = exporter.getExporterPreprocessorDefsString() + "\n" + extraDefs;
  272. {
  273. Array<File> compiled;
  274. const File localModuleFolder = project.getModules().shouldCopyModuleFilesLocally (getID()).getValue()
  275. ? project.getLocalModuleFolder (getID())
  276. : moduleInfo.getFolder();
  277. findAndAddCompiledUnits (exporter, &projectSaver, localModuleFolder, compiled);
  278. if (project.getModules().shouldShowAllModuleFilesInProject (getID()).getValue())
  279. addBrowseableCode (exporter, compiled, localModuleFolder);
  280. }
  281. if (isVSTPluginHost (project)) VSTHelpers::addVSTFolderToPath (exporter, false);
  282. if (isVST3PluginHost (project)) VSTHelpers::addVSTFolderToPath (exporter, true);
  283. if (exporter.isXcode())
  284. {
  285. if (isAUPluginHost (project))
  286. exporter.xcodeFrameworks.addTokens ("AudioUnit CoreAudioKit", false);
  287. const String frameworks (moduleInfo.moduleInfo [exporter.isOSX() ? "OSXFrameworks" : "iOSFrameworks"].toString());
  288. exporter.xcodeFrameworks.addTokens (frameworks, ", ", StringRef());
  289. parseAndAddLibs (exporter.xcodeLibs, moduleInfo.moduleInfo [exporter.isOSX() ? "OSXLibs" : "iOSLibs"].toString());
  290. }
  291. else if (exporter.isLinux())
  292. {
  293. parseAndAddLibs (exporter.linuxLibs, moduleInfo.moduleInfo ["LinuxLibs"].toString());
  294. }
  295. else if (exporter.isCodeBlocksWindows())
  296. {
  297. parseAndAddLibs (exporter.mingwLibs, moduleInfo.moduleInfo ["mingwLibs"].toString());
  298. }
  299. if (moduleInfo.isPluginClient())
  300. {
  301. if (shouldBuildVST (project).getValue()) VSTHelpers::prepareExporter (exporter, projectSaver, false);
  302. if (shouldBuildVST3 (project).getValue()) VSTHelpers::prepareExporter (exporter, projectSaver, true);
  303. if (shouldBuildAU (project).getValue()) AUHelpers::prepareExporter (exporter, projectSaver);
  304. if (shouldBuildAAX (project).getValue()) AAXHelpers::prepareExporter (exporter, projectSaver);
  305. if (shouldBuildRTAS (project).getValue()) RTASHelpers::prepareExporter (exporter, projectSaver);
  306. }
  307. }
  308. void LibraryModule::createPropertyEditors (ProjectExporter& exporter, PropertyListBuilder& props) const
  309. {
  310. if (isVSTPluginHost (exporter.getProject())
  311. && ! (moduleInfo.isPluginClient() && shouldBuildVST (exporter.getProject()).getValue()))
  312. VSTHelpers::createVSTPathEditor (exporter, props, false);
  313. if (isVST3PluginHost (exporter.getProject())
  314. && ! (moduleInfo.isPluginClient() && shouldBuildVST3 (exporter.getProject()).getValue()))
  315. VSTHelpers::createVSTPathEditor (exporter, props, true);
  316. if (moduleInfo.isPluginClient())
  317. {
  318. if (shouldBuildVST (exporter.getProject()).getValue()) VSTHelpers::createPropertyEditors (exporter, props, false);
  319. if (shouldBuildVST3 (exporter.getProject()).getValue()) VSTHelpers::createPropertyEditors (exporter, props, true);
  320. if (shouldBuildRTAS (exporter.getProject()).getValue()) RTASHelpers::createPropertyEditors (exporter, props);
  321. if (shouldBuildAAX (exporter.getProject()).getValue()) AAXHelpers::createPropertyEditors (exporter, props);
  322. }
  323. }
  324. void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::ConfigFlag>& flags) const
  325. {
  326. const File header (getModuleHeaderFile (moduleInfo.getFolder()));
  327. jassert (header.exists());
  328. StringArray lines;
  329. header.readLines (lines);
  330. for (int i = 0; i < lines.size(); ++i)
  331. {
  332. String line (lines[i].trim());
  333. if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:"))
  334. {
  335. ScopedPointer <Project::ConfigFlag> config (new Project::ConfigFlag());
  336. config->sourceModuleID = getID();
  337. config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim();
  338. if (config->symbol.length() > 2)
  339. {
  340. ++i;
  341. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  342. {
  343. if (lines[i].trim().isNotEmpty())
  344. config->description = config->description.trim() + " " + lines[i].trim();
  345. ++i;
  346. }
  347. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  348. config->value.referTo (project.getConfigFlag (config->symbol));
  349. flags.add (config.release());
  350. }
  351. }
  352. }
  353. }
  354. //==============================================================================
  355. static bool exporterTargetMatches (const String& test, String target)
  356. {
  357. StringArray validTargets;
  358. validTargets.addTokens (target, ",;", "");
  359. validTargets.trim();
  360. validTargets.removeEmptyStrings();
  361. if (validTargets.size() == 0)
  362. return true;
  363. for (int i = validTargets.size(); --i >= 0;)
  364. {
  365. const String& targetName = validTargets[i];
  366. if (targetName == test
  367. || (targetName.startsWithChar ('!') && test != targetName.substring (1).trimStart()))
  368. return true;
  369. }
  370. return false;
  371. }
  372. struct FileSorter
  373. {
  374. static int compareElements (const File& f1, const File& f2)
  375. {
  376. return f1.getFileName().compareNatural (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. static bool fileTargetMatches (ProjectExporter& exporter, const String& target)
  393. {
  394. if (exporter.isXcode()) return exporterTargetMatches ("xcode", target);
  395. if (exporter.isWindows()) return exporterTargetMatches ("msvc", target);
  396. if (exporter.isLinux()) return exporterTargetMatches ("linux", target);
  397. if (exporter.isAndroid()) return exporterTargetMatches ("android", target);
  398. if (exporter.isCodeBlocksWindows()) return exporterTargetMatches ("mingw", target);
  399. return target.isEmpty();
  400. }
  401. static bool fileShouldBeCompiled (ProjectExporter& exporter, const var& properties)
  402. {
  403. if (! fileTargetMatches (exporter, properties["target"].toString()))
  404. return false;
  405. if (properties["RTASOnly"] && ! shouldBuildRTAS (exporter.getProject()).getValue())
  406. return false;
  407. if (properties["AudioUnitOnly"] && ! shouldBuildAU (exporter.getProject()).getValue())
  408. return false;
  409. return true;
  410. }
  411. void LibraryModule::findAndAddCompiledUnits (ProjectExporter& exporter, ProjectSaver* projectSaver,
  412. const File& localModuleFolder, Array<File>& result) const
  413. {
  414. const var compileArray (moduleInfo.moduleInfo ["compile"]); // careful to keep this alive while the array is in use!
  415. if (const Array<var>* const files = compileArray.getArray())
  416. {
  417. for (int i = 0; i < files->size(); ++i)
  418. {
  419. const var& file = files->getReference(i);
  420. const String filename (file ["file"].toString());
  421. if (filename.isNotEmpty() && fileShouldBeCompiled (exporter, file))
  422. {
  423. const File compiledFile (localModuleFolder.getChildFile (filename));
  424. result.add (compiledFile);
  425. if (projectSaver != nullptr)
  426. {
  427. Project::Item item (projectSaver->addFileToGeneratedGroup (compiledFile));
  428. if (file ["warnings"].toString().equalsIgnoreCase ("disabled"))
  429. item.getShouldInhibitWarningsValue() = true;
  430. if (file ["stdcall"])
  431. item.getShouldUseStdCallValue() = true;
  432. }
  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::addBrowseableCode (ProjectExporter& exporter, const Array<File>& compiled, const File& localModuleFolder) const
  461. {
  462. if (sourceFiles.size() == 0)
  463. findBrowseableFiles (localModuleFolder, sourceFiles);
  464. Project::Item sourceGroup (Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID()));
  465. const RelativePath moduleFromProject (exporter.getModuleFolderRelativeToProject (getID()));
  466. for (int i = 0; i < sourceFiles.size(); ++i)
  467. {
  468. const String pathWithinModule (FileHelpers::getRelativePathFrom (sourceFiles.getReference(i), localModuleFolder));
  469. // (Note: in exporters like MSVC we have to avoid adding the same file twice, even if one of those instances
  470. // is flagged as being excluded from the build, because this overrides the other and it fails to compile)
  471. if (exporter.canCopeWithDuplicateFiles() || ! compiled.contains (sourceFiles.getReference(i)))
  472. addFileWithGroups (sourceGroup,
  473. moduleFromProject.getChildFile (pathWithinModule),
  474. pathWithinModule);
  475. }
  476. sourceGroup.addFileAtIndex (localModuleFolder.getChildFile (FileHelpers::getRelativePathFrom (moduleInfo.manifestFile,
  477. moduleInfo.getFolder())), -1, false);
  478. sourceGroup.addFileAtIndex (getModuleHeaderFile (localModuleFolder), -1, false);
  479. exporter.getModulesGroup().state.addChild (sourceGroup.state.createCopy(), -1, nullptr);
  480. }
  481. //==============================================================================
  482. EnabledModuleList::EnabledModuleList (Project& p, const ValueTree& s)
  483. : project (p), state (s)
  484. {
  485. }
  486. ModuleDescription EnabledModuleList::getModuleInfo (const String& moduleID)
  487. {
  488. return ModuleDescription (getModuleInfoFile (moduleID));
  489. }
  490. bool EnabledModuleList::isModuleEnabled (const String& moduleID) const
  491. {
  492. for (int i = 0; i < state.getNumChildren(); ++i)
  493. if (state.getChild(i) [Ids::ID] == moduleID)
  494. return true;
  495. return false;
  496. }
  497. bool EnabledModuleList::isAudioPluginModuleMissing() const
  498. {
  499. return project.getProjectType().isAudioPlugin()
  500. && ! isModuleEnabled ("juce_audio_plugin_client");
  501. }
  502. Value EnabledModuleList::shouldShowAllModuleFilesInProject (const String& moduleID)
  503. {
  504. return state.getChildWithProperty (Ids::ID, moduleID)
  505. .getPropertyAsValue (Ids::showAllCode, getUndoManager());
  506. }
  507. File EnabledModuleList::findLocalModuleInfoFile (const String& moduleID, bool useExportersForOtherOSes)
  508. {
  509. for (Project::ExporterIterator exporter (project); exporter.next();)
  510. {
  511. if (useExportersForOtherOSes || exporter->mayCompileOnCurrentOS())
  512. {
  513. const String path (exporter->getPathForModuleString (moduleID));
  514. if (path.isNotEmpty())
  515. {
  516. const File moduleFolder (project.resolveFilename (path));
  517. if (moduleFolder.exists())
  518. {
  519. File f (moduleFolder.getChildFile (ModuleDescription::getManifestFileName()));
  520. if (f.exists())
  521. return f;
  522. f = moduleFolder.getChildFile (moduleID)
  523. .getChildFile (ModuleDescription::getManifestFileName());
  524. if (f.exists())
  525. return f;
  526. f = moduleFolder.getChildFile ("modules")
  527. .getChildFile (moduleID)
  528. .getChildFile (ModuleDescription::getManifestFileName());
  529. if (f.exists())
  530. return f;
  531. }
  532. }
  533. }
  534. }
  535. return File::nonexistent;
  536. }
  537. File EnabledModuleList::getModuleInfoFile (const String& moduleID)
  538. {
  539. const File f (findLocalModuleInfoFile (moduleID, false));
  540. if (f != File::nonexistent)
  541. return f;
  542. return findLocalModuleInfoFile (moduleID, true);
  543. }
  544. File EnabledModuleList::getModuleFolder (const String& moduleID)
  545. {
  546. const File infoFile (getModuleInfoFile (moduleID));
  547. return infoFile.exists() ? infoFile.getParentDirectory()
  548. : File::nonexistent;
  549. }
  550. struct ModuleTreeSorter
  551. {
  552. static int compareElements (const ValueTree& m1, const ValueTree& m2)
  553. {
  554. return m1[Ids::ID].toString().compareIgnoreCase (m2[Ids::ID]);
  555. }
  556. };
  557. void EnabledModuleList::sortAlphabetically()
  558. {
  559. ModuleTreeSorter sorter;
  560. state.sort (sorter, getUndoManager(), false);
  561. }
  562. Value EnabledModuleList::shouldCopyModuleFilesLocally (const String& moduleID) const
  563. {
  564. return state.getChildWithProperty (Ids::ID, moduleID)
  565. .getPropertyAsValue (Ids::useLocalCopy, getUndoManager());
  566. }
  567. void EnabledModuleList::addModule (const File& moduleManifestFile, bool copyLocally)
  568. {
  569. ModuleDescription info (moduleManifestFile);
  570. if (info.isValid())
  571. {
  572. const String moduleID (info.getID());
  573. if (! isModuleEnabled (moduleID))
  574. {
  575. ValueTree module (Ids::MODULE);
  576. module.setProperty (Ids::ID, moduleID, nullptr);
  577. state.addChild (module, -1, getUndoManager());
  578. sortAlphabetically();
  579. shouldShowAllModuleFilesInProject (moduleID) = true;
  580. shouldCopyModuleFilesLocally (moduleID) = copyLocally;
  581. RelativePath path (moduleManifestFile.getParentDirectory().getParentDirectory(),
  582. project.getProjectFolder(), RelativePath::projectFolder);
  583. for (Project::ExporterIterator exporter (project); exporter.next();)
  584. exporter->getPathForModuleValue (moduleID) = path.toUnixStyle();
  585. }
  586. }
  587. }
  588. void EnabledModuleList::removeModule (String moduleID) // must be pass-by-value, and not a const ref!
  589. {
  590. for (int i = state.getNumChildren(); --i >= 0;)
  591. if (state.getChild(i) [Ids::ID] == moduleID)
  592. state.removeChild (i, getUndoManager());
  593. for (Project::ExporterIterator exporter (project); exporter.next();)
  594. exporter->removePathForModule (moduleID);
  595. }
  596. void EnabledModuleList::createRequiredModules (OwnedArray<LibraryModule>& modules)
  597. {
  598. for (int i = 0; i < getNumModules(); ++i)
  599. modules.add (new LibraryModule (getModuleInfo (getModuleID (i))));
  600. }
  601. StringArray EnabledModuleList::getAllModules() const
  602. {
  603. StringArray moduleIDs;
  604. for (int i = 0; i < getNumModules(); ++i)
  605. moduleIDs.add (getModuleID(i));
  606. return moduleIDs;
  607. }
  608. static void getDependencies (Project& project, const String& moduleID, StringArray& dependencies)
  609. {
  610. ModuleDescription info (project.getModules().getModuleInfo (moduleID));
  611. if (info.isValid())
  612. {
  613. const var depsArray (info.moduleInfo ["dependencies"]);
  614. if (const Array<var>* const deps = depsArray.getArray())
  615. {
  616. for (int i = 0; i < deps->size(); ++i)
  617. {
  618. const var& d = deps->getReference(i);
  619. String uid (d [Ids::ID].toString());
  620. String version (d [Ids::version].toString());
  621. if (! dependencies.contains (uid, true))
  622. {
  623. dependencies.add (uid);
  624. getDependencies (project, uid, dependencies);
  625. }
  626. }
  627. }
  628. }
  629. }
  630. StringArray EnabledModuleList::getExtraDependenciesNeeded (const String& moduleID) const
  631. {
  632. StringArray dependencies, extraDepsNeeded;
  633. getDependencies (project, moduleID, dependencies);
  634. for (int i = 0; i < dependencies.size(); ++i)
  635. if ((! isModuleEnabled (dependencies[i])) && dependencies[i] != moduleID)
  636. extraDepsNeeded.add (dependencies[i]);
  637. return extraDepsNeeded;
  638. }
  639. bool EnabledModuleList::areMostModulesCopiedLocally() const
  640. {
  641. int numYes = 0, numNo = 0;
  642. for (int i = getNumModules(); --i >= 0;)
  643. {
  644. if (shouldCopyModuleFilesLocally (getModuleID (i)).getValue())
  645. ++numYes;
  646. else
  647. ++numNo;
  648. }
  649. return numYes > numNo;
  650. }
  651. void EnabledModuleList::setLocalCopyModeForAllModules (bool copyLocally)
  652. {
  653. for (int i = getNumModules(); --i >= 0;)
  654. shouldCopyModuleFilesLocally (project.getModules().getModuleID (i)) = copyLocally;
  655. }
  656. File EnabledModuleList::findDefaultModulesFolder (Project& project)
  657. {
  658. ModuleList available;
  659. available.scanAllKnownFolders (project);
  660. for (int i = available.modules.size(); --i >= 0;)
  661. {
  662. File f (available.modules.getUnchecked(i)->getFolder());
  663. if (f.isDirectory())
  664. return f.getParentDirectory();
  665. }
  666. return File::getCurrentWorkingDirectory();
  667. }
  668. void EnabledModuleList::addModuleFromUserSelectedFile()
  669. {
  670. static File lastLocation (findDefaultModulesFolder (project));
  671. FileChooser fc ("Select a module to add...", lastLocation, String::empty, false);
  672. if (fc.browseForDirectory())
  673. {
  674. lastLocation = fc.getResult();
  675. addModuleOfferingToCopy (lastLocation);
  676. }
  677. }
  678. void EnabledModuleList::addModuleInteractive (const String& moduleID)
  679. {
  680. ModuleList list;
  681. list.scanAllKnownFolders (project);
  682. if (const ModuleDescription* info = list.getModuleWithID (moduleID))
  683. addModule (info->manifestFile, areMostModulesCopiedLocally());
  684. else
  685. addModuleFromUserSelectedFile();
  686. }
  687. void EnabledModuleList::addModuleOfferingToCopy (const File& f)
  688. {
  689. ModuleDescription m (f);
  690. if (! m.isValid())
  691. m = ModuleDescription (f.getChildFile (ModuleDescription::getManifestFileName()));
  692. if (! m.isValid())
  693. {
  694. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  695. "Add Module", "This wasn't a valid module folder!");
  696. return;
  697. }
  698. if (isModuleEnabled (m.getID()))
  699. {
  700. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  701. "Add Module", "The project already contains this module!");
  702. return;
  703. }
  704. addModule (m.manifestFile, areMostModulesCopiedLocally());
  705. }
  706. bool isJuceFolder (const File& f)
  707. {
  708. return isJuceModulesFolder (f.getChildFile ("modules"));
  709. }
  710. bool isJuceModulesFolder (const File& f)
  711. {
  712. return f.isDirectory() && f.getChildFile ("juce_core").isDirectory();
  713. }