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.

908 lines
30KB

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