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.

911 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. projectSaver.replaceFileIfDifferent (localHeader, out);
  255. }
  256. //==============================================================================
  257. static void parseAndAddLibs (StringArray& libList, const String& libs)
  258. {
  259. libList.addTokens (libs, ", ", StringRef());
  260. libList.trim();
  261. libList.sort (false);
  262. libList.removeDuplicates (false);
  263. }
  264. void LibraryModule::prepareExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  265. {
  266. Project& project = exporter.getProject();
  267. exporter.addToExtraSearchPaths (exporter.getModuleFolderRelativeToProject (getID()).getParentDirectory());
  268. const String extraDefs (moduleInfo.getPreprocessorDefs().trim());
  269. if (extraDefs.isNotEmpty())
  270. exporter.getExporterPreprocessorDefs() = exporter.getExporterPreprocessorDefsString() + "\n" + extraDefs;
  271. {
  272. Array<File> compiled;
  273. const File localModuleFolder = project.getModules().shouldCopyModuleFilesLocally (getID()).getValue()
  274. ? project.getLocalModuleFolder (getID())
  275. : moduleInfo.getFolder();
  276. findAndAddCompiledUnits (exporter, &projectSaver, localModuleFolder, compiled);
  277. if (project.getModules().shouldShowAllModuleFilesInProject (getID()).getValue())
  278. addBrowseableCode (exporter, compiled, localModuleFolder);
  279. }
  280. if (isVSTPluginHost (project)) VSTHelpers::addVSTFolderToPath (exporter, false);
  281. if (isVST3PluginHost (project)) VSTHelpers::addVSTFolderToPath (exporter, true);
  282. if (exporter.isXcode())
  283. {
  284. if (isAUPluginHost (project))
  285. exporter.xcodeFrameworks.addTokens ("AudioUnit CoreAudioKit", false);
  286. const String frameworks (moduleInfo.moduleInfo [exporter.isOSX() ? "OSXFrameworks" : "iOSFrameworks"].toString());
  287. exporter.xcodeFrameworks.addTokens (frameworks, ", ", StringRef());
  288. parseAndAddLibs (exporter.xcodeLibs, moduleInfo.moduleInfo [exporter.isOSX() ? "OSXLibs" : "iOSLibs"].toString());
  289. }
  290. else if (exporter.isLinux())
  291. {
  292. parseAndAddLibs (exporter.linuxLibs, moduleInfo.moduleInfo ["LinuxLibs"].toString());
  293. }
  294. else if (exporter.isCodeBlocksWindows())
  295. {
  296. parseAndAddLibs (exporter.mingwLibs, moduleInfo.moduleInfo ["mingwLibs"].toString());
  297. }
  298. if (moduleInfo.isPluginClient())
  299. {
  300. if (shouldBuildVST (project).getValue()) VSTHelpers::prepareExporter (exporter, projectSaver, false);
  301. if (shouldBuildVST3 (project).getValue()) VSTHelpers::prepareExporter (exporter, projectSaver, true);
  302. if (shouldBuildAU (project).getValue()) AUHelpers::prepareExporter (exporter, projectSaver);
  303. if (shouldBuildAAX (project).getValue()) AAXHelpers::prepareExporter (exporter, projectSaver);
  304. if (shouldBuildRTAS (project).getValue()) RTASHelpers::prepareExporter (exporter, projectSaver);
  305. }
  306. }
  307. void LibraryModule::createPropertyEditors (ProjectExporter& exporter, PropertyListBuilder& props) const
  308. {
  309. if (isVSTPluginHost (exporter.getProject())
  310. && ! (moduleInfo.isPluginClient() && shouldBuildVST (exporter.getProject()).getValue()))
  311. VSTHelpers::createVSTPathEditor (exporter, props, false);
  312. if (isVST3PluginHost (exporter.getProject())
  313. && ! (moduleInfo.isPluginClient() && shouldBuildVST3 (exporter.getProject()).getValue()))
  314. VSTHelpers::createVSTPathEditor (exporter, props, true);
  315. if (moduleInfo.isPluginClient())
  316. {
  317. if (shouldBuildVST (exporter.getProject()).getValue()) VSTHelpers::createPropertyEditors (exporter, props, false);
  318. if (shouldBuildVST3 (exporter.getProject()).getValue()) VSTHelpers::createPropertyEditors (exporter, props, true);
  319. if (shouldBuildRTAS (exporter.getProject()).getValue()) RTASHelpers::createPropertyEditors (exporter, props);
  320. if (shouldBuildAAX (exporter.getProject()).getValue()) AAXHelpers::createPropertyEditors (exporter, props);
  321. }
  322. }
  323. void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::ConfigFlag>& flags) const
  324. {
  325. const File header (getModuleHeaderFile (moduleInfo.getFolder()));
  326. jassert (header.exists());
  327. StringArray lines;
  328. header.readLines (lines);
  329. for (int i = 0; i < lines.size(); ++i)
  330. {
  331. String line (lines[i].trim());
  332. if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:"))
  333. {
  334. ScopedPointer <Project::ConfigFlag> config (new Project::ConfigFlag());
  335. config->sourceModuleID = getID();
  336. config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim();
  337. if (config->symbol.length() > 2)
  338. {
  339. ++i;
  340. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  341. {
  342. if (lines[i].trim().isNotEmpty())
  343. config->description = config->description.trim() + " " + lines[i].trim();
  344. ++i;
  345. }
  346. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  347. config->value.referTo (project.getConfigFlag (config->symbol));
  348. flags.add (config.release());
  349. }
  350. }
  351. }
  352. }
  353. //==============================================================================
  354. static bool exporterTargetMatches (const String& test, String target)
  355. {
  356. StringArray validTargets;
  357. validTargets.addTokens (target, ",;", "");
  358. validTargets.trim();
  359. validTargets.removeEmptyStrings();
  360. if (validTargets.size() == 0)
  361. return true;
  362. for (int i = validTargets.size(); --i >= 0;)
  363. {
  364. const String& targetName = validTargets[i];
  365. if (targetName == test
  366. || (targetName.startsWithChar ('!') && test != targetName.substring (1).trimStart()))
  367. return true;
  368. }
  369. return false;
  370. }
  371. struct FileSorter
  372. {
  373. static int compareElements (const File& f1, const File& f2)
  374. {
  375. return f1.getFileName().compareNatural (f2.getFileName());
  376. }
  377. };
  378. void LibraryModule::findWildcardMatches (const File& localModuleFolder, const String& wildcardPath, Array<File>& result) const
  379. {
  380. String path (wildcardPath.upToLastOccurrenceOf ("/", false, false));
  381. String wildCard (wildcardPath.fromLastOccurrenceOf ("/", false, false));
  382. Array<File> tempList;
  383. FileSorter sorter;
  384. DirectoryIterator iter (localModuleFolder.getChildFile (path), false, wildCard);
  385. bool isHiddenFile;
  386. while (iter.next (nullptr, &isHiddenFile, nullptr, nullptr, nullptr, nullptr))
  387. if (! isHiddenFile)
  388. tempList.addSorted (sorter, iter.getFile());
  389. result.addArray (tempList);
  390. }
  391. static bool fileTargetMatches (ProjectExporter& exporter, const String& target)
  392. {
  393. if (exporter.isXcode()) return exporterTargetMatches ("xcode", target);
  394. if (exporter.isWindows()) return exporterTargetMatches ("msvc", target);
  395. if (exporter.isLinux()) return exporterTargetMatches ("linux", target);
  396. if (exporter.isAndroid()) return exporterTargetMatches ("android", target);
  397. if (exporter.isCodeBlocksWindows()) return exporterTargetMatches ("mingw", target);
  398. return target.isEmpty();
  399. }
  400. static bool fileShouldBeCompiled (ProjectExporter& exporter, const var& properties)
  401. {
  402. if (! fileTargetMatches (exporter, properties["target"].toString()))
  403. return false;
  404. if (properties["RTASOnly"] && ! shouldBuildRTAS (exporter.getProject()).getValue())
  405. return false;
  406. if (properties["AudioUnitOnly"] && ! shouldBuildAU (exporter.getProject()).getValue())
  407. return false;
  408. return true;
  409. }
  410. void LibraryModule::findAndAddCompiledUnits (ProjectExporter& exporter, ProjectSaver* projectSaver,
  411. const File& localModuleFolder, Array<File>& result) const
  412. {
  413. const var compileArray (moduleInfo.moduleInfo ["compile"]); // careful to keep this alive while the array is in use!
  414. if (const Array<var>* const files = compileArray.getArray())
  415. {
  416. for (int i = 0; i < files->size(); ++i)
  417. {
  418. const var& file = files->getReference(i);
  419. const String filename (file ["file"].toString());
  420. if (filename.isNotEmpty() && fileShouldBeCompiled (exporter, file))
  421. {
  422. const File compiledFile (localModuleFolder.getChildFile (filename));
  423. result.add (compiledFile);
  424. if (projectSaver != nullptr)
  425. {
  426. Project::Item item (projectSaver->addFileToGeneratedGroup (compiledFile));
  427. if (file ["warnings"].toString().equalsIgnoreCase ("disabled"))
  428. item.getShouldInhibitWarningsValue() = true;
  429. if (file ["stdcall"])
  430. item.getShouldUseStdCallValue() = true;
  431. }
  432. }
  433. }
  434. }
  435. }
  436. static void addFileWithGroups (Project::Item& group, const RelativePath& file, const String& path)
  437. {
  438. const int slash = path.indexOfChar (File::separator);
  439. if (slash >= 0)
  440. {
  441. const String topLevelGroup (path.substring (0, slash));
  442. const String remainingPath (path.substring (slash + 1));
  443. Project::Item newGroup (group.getOrCreateSubGroup (topLevelGroup));
  444. addFileWithGroups (newGroup, file, remainingPath);
  445. }
  446. else
  447. {
  448. if (! group.containsChildForFile (file))
  449. group.addRelativeFile (file, -1, false);
  450. }
  451. }
  452. void LibraryModule::findBrowseableFiles (const File& localModuleFolder, Array<File>& filesFound) const
  453. {
  454. const var filesArray (moduleInfo.moduleInfo ["browse"]);
  455. if (const Array<var>* const files = filesArray.getArray())
  456. for (int i = 0; i < files->size(); ++i)
  457. findWildcardMatches (localModuleFolder, files->getReference(i), filesFound);
  458. }
  459. void LibraryModule::addBrowseableCode (ProjectExporter& exporter, const Array<File>& compiled, const File& localModuleFolder) const
  460. {
  461. if (sourceFiles.size() == 0)
  462. findBrowseableFiles (localModuleFolder, sourceFiles);
  463. Project::Item sourceGroup (Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID()));
  464. const RelativePath moduleFromProject (exporter.getModuleFolderRelativeToProject (getID()));
  465. for (int i = 0; i < sourceFiles.size(); ++i)
  466. {
  467. const String pathWithinModule (FileHelpers::getRelativePathFrom (sourceFiles.getReference(i), localModuleFolder));
  468. // (Note: in exporters like MSVC we have to avoid adding the same file twice, even if one of those instances
  469. // is flagged as being excluded from the build, because this overrides the other and it fails to compile)
  470. if (exporter.canCopeWithDuplicateFiles() || ! compiled.contains (sourceFiles.getReference(i)))
  471. addFileWithGroups (sourceGroup,
  472. moduleFromProject.getChildFile (pathWithinModule),
  473. pathWithinModule);
  474. }
  475. sourceGroup.addFileAtIndex (localModuleFolder.getChildFile (FileHelpers::getRelativePathFrom (moduleInfo.manifestFile,
  476. moduleInfo.getFolder())), -1, false);
  477. sourceGroup.addFileAtIndex (getModuleHeaderFile (localModuleFolder), -1, false);
  478. exporter.getModulesGroup().state.addChild (sourceGroup.state.createCopy(), -1, nullptr);
  479. }
  480. //==============================================================================
  481. EnabledModuleList::EnabledModuleList (Project& p, const ValueTree& s)
  482. : project (p), state (s)
  483. {
  484. }
  485. ModuleDescription EnabledModuleList::getModuleInfo (const String& moduleID)
  486. {
  487. return ModuleDescription (getModuleInfoFile (moduleID));
  488. }
  489. bool EnabledModuleList::isModuleEnabled (const String& moduleID) const
  490. {
  491. for (int i = 0; i < state.getNumChildren(); ++i)
  492. if (state.getChild(i) [Ids::ID] == moduleID)
  493. return true;
  494. return false;
  495. }
  496. bool EnabledModuleList::isAudioPluginModuleMissing() const
  497. {
  498. return project.getProjectType().isAudioPlugin()
  499. && ! isModuleEnabled ("juce_audio_plugin_client");
  500. }
  501. Value EnabledModuleList::shouldShowAllModuleFilesInProject (const String& moduleID)
  502. {
  503. return state.getChildWithProperty (Ids::ID, moduleID)
  504. .getPropertyAsValue (Ids::showAllCode, getUndoManager());
  505. }
  506. File EnabledModuleList::findLocalModuleInfoFile (const String& moduleID, bool useExportersForOtherOSes)
  507. {
  508. for (Project::ExporterIterator exporter (project); exporter.next();)
  509. {
  510. if (useExportersForOtherOSes || exporter->mayCompileOnCurrentOS())
  511. {
  512. const String path (exporter->getPathForModuleString (moduleID));
  513. if (path.isNotEmpty())
  514. {
  515. const File moduleFolder (project.resolveFilename (path));
  516. if (moduleFolder.exists())
  517. {
  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. }
  534. return File::nonexistent;
  535. }
  536. File EnabledModuleList::getModuleInfoFile (const String& moduleID)
  537. {
  538. const File f (findLocalModuleInfoFile (moduleID, false));
  539. if (f != File::nonexistent)
  540. return f;
  541. return findLocalModuleInfoFile (moduleID, true);
  542. }
  543. File EnabledModuleList::getModuleFolder (const String& moduleID)
  544. {
  545. const File infoFile (getModuleInfoFile (moduleID));
  546. return infoFile.exists() ? infoFile.getParentDirectory()
  547. : File::nonexistent;
  548. }
  549. struct ModuleTreeSorter
  550. {
  551. static int compareElements (const ValueTree& m1, const ValueTree& m2)
  552. {
  553. return m1[Ids::ID].toString().compareIgnoreCase (m2[Ids::ID]);
  554. }
  555. };
  556. void EnabledModuleList::sortAlphabetically()
  557. {
  558. ModuleTreeSorter sorter;
  559. state.sort (sorter, getUndoManager(), false);
  560. }
  561. Value EnabledModuleList::shouldCopyModuleFilesLocally (const String& moduleID) const
  562. {
  563. return state.getChildWithProperty (Ids::ID, moduleID)
  564. .getPropertyAsValue (Ids::useLocalCopy, getUndoManager());
  565. }
  566. void EnabledModuleList::addModule (const File& moduleManifestFile, bool copyLocally)
  567. {
  568. ModuleDescription info (moduleManifestFile);
  569. if (info.isValid())
  570. {
  571. const String moduleID (info.getID());
  572. if (! isModuleEnabled (moduleID))
  573. {
  574. ValueTree module (Ids::MODULE);
  575. module.setProperty (Ids::ID, moduleID, nullptr);
  576. state.addChild (module, -1, getUndoManager());
  577. sortAlphabetically();
  578. shouldShowAllModuleFilesInProject (moduleID) = true;
  579. shouldCopyModuleFilesLocally (moduleID) = copyLocally;
  580. RelativePath path (moduleManifestFile.getParentDirectory().getParentDirectory(),
  581. project.getProjectFolder(), RelativePath::projectFolder);
  582. for (Project::ExporterIterator exporter (project); exporter.next();)
  583. exporter->getPathForModuleValue (moduleID) = path.toUnixStyle();
  584. }
  585. }
  586. }
  587. void EnabledModuleList::removeModule (String moduleID) // must be pass-by-value, and not a const ref!
  588. {
  589. for (int i = state.getNumChildren(); --i >= 0;)
  590. if (state.getChild(i) [Ids::ID] == moduleID)
  591. state.removeChild (i, getUndoManager());
  592. for (Project::ExporterIterator exporter (project); exporter.next();)
  593. exporter->removePathForModule (moduleID);
  594. }
  595. void EnabledModuleList::createRequiredModules (OwnedArray<LibraryModule>& modules)
  596. {
  597. for (int i = 0; i < getNumModules(); ++i)
  598. modules.add (new LibraryModule (getModuleInfo (getModuleID (i))));
  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. }