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.

801 lines
26KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #include "../Application/jucer_Headers.h"
  14. #include "../ProjectSaving/jucer_ProjectSaver.h"
  15. #include "../ProjectSaving/jucer_ProjectExport_Xcode.h"
  16. #include "../Application/jucer_Application.h"
  17. //==============================================================================
  18. ModuleDescription::ModuleDescription (const File& folder)
  19. : moduleFolder (folder),
  20. moduleInfo (parseJUCEHeaderMetadata (getHeader()))
  21. {
  22. }
  23. File ModuleDescription::getHeader() const
  24. {
  25. if (moduleFolder != File())
  26. {
  27. static const char* extensions[] = { ".h", ".hpp", ".hxx" };
  28. for (auto e : extensions)
  29. {
  30. auto header = moduleFolder.getChildFile (moduleFolder.getFileName() + e);
  31. if (header.existsAsFile())
  32. return header;
  33. }
  34. }
  35. return {};
  36. }
  37. StringArray ModuleDescription::getDependencies() const
  38. {
  39. auto moduleDependencies = StringArray::fromTokens (moduleInfo ["dependencies"].toString(), " \t;,", "\"'");
  40. moduleDependencies.trim();
  41. moduleDependencies.removeEmptyStrings();
  42. return moduleDependencies;
  43. }
  44. //==============================================================================
  45. static bool tryToAddModuleFromFolder (const File& path, AvailableModuleList::ModuleIDAndFolderList& list)
  46. {
  47. ModuleDescription m (path);
  48. if (m.isValid())
  49. {
  50. list.push_back ({ m.getID(), path });
  51. return true;
  52. }
  53. return false;
  54. }
  55. static void addAllModulesInSubfoldersRecursively (const File& path, int depth, AvailableModuleList::ModuleIDAndFolderList& list)
  56. {
  57. if (depth > 0)
  58. {
  59. for (const auto& iter : RangedDirectoryIterator (path, false, "*", File::findDirectories))
  60. {
  61. if (auto* job = ThreadPoolJob::getCurrentThreadPoolJob())
  62. if (job->shouldExit())
  63. return;
  64. auto childPath = iter.getFile();
  65. if (! tryToAddModuleFromFolder (childPath, list))
  66. addAllModulesInSubfoldersRecursively (childPath, depth - 1, list);
  67. }
  68. }
  69. }
  70. static void addAllModulesInFolder (const File& path, AvailableModuleList::ModuleIDAndFolderList& list)
  71. {
  72. if (! tryToAddModuleFromFolder (path, list))
  73. {
  74. static constexpr int subfolders = 3;
  75. addAllModulesInSubfoldersRecursively (path, subfolders, list);
  76. }
  77. }
  78. struct ModuleScannerJob : public ThreadPoolJob
  79. {
  80. ModuleScannerJob (const Array<File>& paths,
  81. std::function<void (const AvailableModuleList::ModuleIDAndFolderList&)>&& callback)
  82. : ThreadPoolJob ("ModuleScannerJob"),
  83. pathsToScan (paths),
  84. completionCallback (std::move (callback))
  85. {
  86. }
  87. JobStatus runJob() override
  88. {
  89. AvailableModuleList::ModuleIDAndFolderList list;
  90. for (auto& p : pathsToScan)
  91. addAllModulesInFolder (p, list);
  92. if (! shouldExit())
  93. {
  94. std::sort (list.begin(), list.end(), [] (const AvailableModuleList::ModuleIDAndFolder& m1,
  95. const AvailableModuleList::ModuleIDAndFolder& m2)
  96. {
  97. return m1.first.compareIgnoreCase (m2.first) < 0;
  98. });
  99. completionCallback (list);
  100. }
  101. return jobHasFinished;
  102. }
  103. Array<File> pathsToScan;
  104. std::function<void (const AvailableModuleList::ModuleIDAndFolderList&)> completionCallback;
  105. };
  106. ThreadPoolJob* AvailableModuleList::createScannerJob (const Array<File>& paths)
  107. {
  108. return new ModuleScannerJob (paths, [this] (AvailableModuleList::ModuleIDAndFolderList scannedModuleList)
  109. {
  110. {
  111. const ScopedLock swapLock (lock);
  112. moduleList.swap (scannedModuleList);
  113. }
  114. listeners.call ([] (Listener& l) { MessageManager::callAsync ([&] { l.availableModulesChanged(); }); });
  115. });
  116. }
  117. void AvailableModuleList::removePendingAndAddJob (ThreadPoolJob* jobToAdd)
  118. {
  119. scanPool.removeAllJobs (false, 100);
  120. scanPool.addJob (jobToAdd, true);
  121. }
  122. void AvailableModuleList::scanPaths (const Array<File>& paths)
  123. {
  124. auto* job = createScannerJob (paths);
  125. removePendingAndAddJob (job);
  126. scanPool.waitForJobToFinish (job, -1);
  127. }
  128. void AvailableModuleList::scanPathsAsync (const Array<File>& paths)
  129. {
  130. removePendingAndAddJob (createScannerJob (paths));
  131. }
  132. AvailableModuleList::ModuleIDAndFolderList AvailableModuleList::getAllModules() const
  133. {
  134. const ScopedLock readLock (lock);
  135. return moduleList;
  136. }
  137. AvailableModuleList::ModuleIDAndFolder AvailableModuleList::getModuleWithID (const String& id) const
  138. {
  139. const ScopedLock readLock (lock);
  140. for (auto& mod : moduleList)
  141. if (mod.first == id)
  142. return mod;
  143. return {};
  144. }
  145. void AvailableModuleList::removeDuplicates (const ModuleIDAndFolderList& other)
  146. {
  147. const ScopedLock readLock (lock);
  148. for (auto& m : other)
  149. {
  150. auto pos = std::find (moduleList.begin(), moduleList.end(), m);
  151. if (pos != moduleList.end())
  152. moduleList.erase (pos);
  153. }
  154. }
  155. //==============================================================================
  156. LibraryModule::LibraryModule (const ModuleDescription& d)
  157. : moduleInfo (d)
  158. {
  159. }
  160. void LibraryModule::writeIncludes (ProjectSaver& projectSaver, OutputStream& out)
  161. {
  162. auto& project = projectSaver.project;
  163. auto& modules = project.getEnabledModules();
  164. auto moduleID = getID();
  165. if (modules.shouldCopyModuleFilesLocally (moduleID))
  166. {
  167. auto juceModuleFolder = moduleInfo.getFolder();
  168. auto localModuleFolder = project.getLocalModuleFolder (moduleID);
  169. localModuleFolder.createDirectory();
  170. projectSaver.copyFolder (juceModuleFolder, localModuleFolder);
  171. }
  172. out << "#include <" << moduleInfo.moduleFolder.getFileName() << "/"
  173. << moduleInfo.getHeader().getFileName()
  174. << ">" << newLine;
  175. }
  176. void LibraryModule::addSearchPathsToExporter (ProjectExporter& exporter) const
  177. {
  178. auto moduleRelativePath = exporter.getModuleFolderRelativeToProject (getID());
  179. exporter.addToExtraSearchPaths (moduleRelativePath.getParentDirectory());
  180. String libDirPlatform;
  181. if (exporter.isLinux())
  182. libDirPlatform = "Linux";
  183. else if (exporter.isCodeBlocks() && exporter.isWindows())
  184. libDirPlatform = "MinGW";
  185. else
  186. libDirPlatform = exporter.getTargetFolder().getFileName();
  187. auto libSubdirPath = moduleRelativePath.toUnixStyle() + "/libs/" + libDirPlatform;
  188. auto moduleLibDir = File (exporter.getProject().getProjectFolder().getFullPathName() + "/" + libSubdirPath);
  189. if (moduleLibDir.exists())
  190. exporter.addToModuleLibPaths ({ libSubdirPath, moduleRelativePath.getRoot() });
  191. auto extraInternalSearchPaths = moduleInfo.getExtraSearchPaths().trim();
  192. if (extraInternalSearchPaths.isNotEmpty())
  193. {
  194. auto paths = StringArray::fromTokens (extraInternalSearchPaths, true);
  195. for (auto& path : paths)
  196. exporter.addToExtraSearchPaths (moduleRelativePath.getChildFile (path.unquoted()));
  197. }
  198. }
  199. void LibraryModule::addDefinesToExporter (ProjectExporter& exporter) const
  200. {
  201. auto extraDefs = moduleInfo.getPreprocessorDefs().trim();
  202. if (extraDefs.isNotEmpty())
  203. exporter.getExporterPreprocessorDefsValue() = exporter.getExporterPreprocessorDefsString() + "\n" + extraDefs;
  204. }
  205. void LibraryModule::addCompileUnitsToExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  206. {
  207. auto& project = exporter.getProject();
  208. auto& modules = project.getEnabledModules();
  209. auto moduleID = getID();
  210. auto localModuleFolder = modules.shouldCopyModuleFilesLocally (moduleID) ? project.getLocalModuleFolder (moduleID)
  211. : moduleInfo.getFolder();
  212. Array<File> compiled;
  213. findAndAddCompiledUnits (exporter, &projectSaver, compiled);
  214. if (modules.shouldShowAllModuleFilesInProject (moduleID))
  215. addBrowseableCode (exporter, compiled, localModuleFolder);
  216. }
  217. void LibraryModule::addLibsToExporter (ProjectExporter& exporter) const
  218. {
  219. auto parseAndAddLibsToList = [] (StringArray& libList, const String& libs)
  220. {
  221. libList.addTokens (libs, ", ", {});
  222. libList.trim();
  223. libList.removeDuplicates (false);
  224. };
  225. auto& project = exporter.getProject();
  226. if (exporter.isXcode())
  227. {
  228. auto& xcodeExporter = dynamic_cast<XcodeProjectExporter&> (exporter);
  229. if (project.isAUPluginHost())
  230. {
  231. xcodeExporter.xcodeFrameworks.add ("CoreAudioKit");
  232. if (xcodeExporter.isOSX())
  233. xcodeExporter.xcodeFrameworks.add ("AudioUnit");
  234. }
  235. auto frameworks = moduleInfo.moduleInfo [xcodeExporter.isOSX() ? "OSXFrameworks" : "iOSFrameworks"].toString();
  236. xcodeExporter.xcodeFrameworks.addTokens (frameworks, ", ", {});
  237. parseAndAddLibsToList (xcodeExporter.xcodeLibs, moduleInfo.moduleInfo [exporter.isOSX() ? "OSXLibs" : "iOSLibs"].toString());
  238. }
  239. else if (exporter.isLinux())
  240. {
  241. parseAndAddLibsToList (exporter.linuxLibs, moduleInfo.moduleInfo ["linuxLibs"].toString());
  242. parseAndAddLibsToList (exporter.linuxPackages, moduleInfo.moduleInfo ["linuxPackages"].toString());
  243. }
  244. else if (exporter.isWindows())
  245. {
  246. if (exporter.isCodeBlocks())
  247. parseAndAddLibsToList (exporter.mingwLibs, moduleInfo.moduleInfo ["mingwLibs"].toString());
  248. else
  249. parseAndAddLibsToList (exporter.windowsLibs, moduleInfo.moduleInfo ["windowsLibs"].toString());
  250. }
  251. else if (exporter.isAndroid())
  252. {
  253. parseAndAddLibsToList (exporter.androidLibs, moduleInfo.moduleInfo ["androidLibs"].toString());
  254. }
  255. }
  256. void LibraryModule::addSettingsForModuleToExporter (ProjectExporter& exporter, ProjectSaver& projectSaver) const
  257. {
  258. addSearchPathsToExporter (exporter);
  259. addDefinesToExporter (exporter);
  260. addCompileUnitsToExporter (exporter, projectSaver);
  261. addLibsToExporter (exporter);
  262. }
  263. void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::ConfigFlag>& flags) const
  264. {
  265. auto header = moduleInfo.getHeader();
  266. jassert (header.exists());
  267. StringArray lines;
  268. header.readLines (lines);
  269. for (int i = 0; i < lines.size(); ++i)
  270. {
  271. auto line = lines[i].trim();
  272. if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:"))
  273. {
  274. auto config = std::make_unique<Project::ConfigFlag>();
  275. config->sourceModuleID = getID();
  276. config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim();
  277. if (config->symbol.length() > 2)
  278. {
  279. ++i;
  280. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  281. {
  282. if (lines[i].trim().isNotEmpty())
  283. config->description = config->description.trim() + " " + lines[i].trim();
  284. ++i;
  285. }
  286. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  287. config->value = project.getConfigFlag (config->symbol);
  288. i += 2;
  289. if (lines[i].contains ("#define " + config->symbol))
  290. {
  291. auto value = lines[i].fromFirstOccurrenceOf ("#define " + config->symbol, false, true).trim();
  292. config->value.setDefault (value == "0" ? false : true);
  293. }
  294. auto currentValue = config->value.get().toString();
  295. if (currentValue == "enabled") config->value = true;
  296. else if (currentValue == "disabled") config->value = false;
  297. flags.add (std::move (config));
  298. }
  299. }
  300. }
  301. }
  302. static void addFileWithGroups (Project::Item& group, const build_tools::RelativePath& file, const String& path)
  303. {
  304. auto slash = path.indexOfChar (File::getSeparatorChar());
  305. if (slash >= 0)
  306. {
  307. auto topLevelGroup = path.substring (0, slash);
  308. auto remainingPath = path.substring (slash + 1);
  309. auto newGroup = group.getOrCreateSubGroup (topLevelGroup);
  310. addFileWithGroups (newGroup, file, remainingPath);
  311. }
  312. else
  313. {
  314. if (! group.containsChildForFile (file))
  315. group.addRelativeFile (file, -1, false);
  316. }
  317. }
  318. struct FileSorter
  319. {
  320. static int compareElements (const File& f1, const File& f2)
  321. {
  322. return f1.getFileName().compareNatural (f2.getFileName());
  323. }
  324. };
  325. void LibraryModule::findBrowseableFiles (const File& folder, Array<File>& filesFound) const
  326. {
  327. Array<File> tempList;
  328. FileSorter sorter;
  329. for (const auto& iter : RangedDirectoryIterator (folder, true, "*", File::findFiles))
  330. if (! iter.isHidden() && iter.getFile().hasFileExtension (browseableFileExtensions))
  331. tempList.addSorted (sorter, iter.getFile());
  332. filesFound.addArray (tempList);
  333. }
  334. bool LibraryModule::CompileUnit::isNeededForExporter (ProjectExporter& exporter) const
  335. {
  336. if ((hasSuffix (file, "_OSX") && ! exporter.isOSX())
  337. || (hasSuffix (file, "_iOS") && ! exporter.isiOS())
  338. || (hasSuffix (file, "_Windows") && ! exporter.isWindows())
  339. || (hasSuffix (file, "_Linux") && ! exporter.isLinux())
  340. || (hasSuffix (file, "_Android") && ! exporter.isAndroid()))
  341. return false;
  342. auto targetType = Project::getTargetTypeFromFilePath (file, false);
  343. if (targetType != build_tools::ProjectType::Target::unspecified && ! exporter.shouldBuildTargetType (targetType))
  344. return false;
  345. return exporter.usesMMFiles() ? isCompiledForObjC
  346. : isCompiledForNonObjC;
  347. }
  348. String LibraryModule::CompileUnit::getFilenameForProxyFile() const
  349. {
  350. return "include_" + file.getFileName();
  351. }
  352. bool LibraryModule::CompileUnit::hasSuffix (const File& f, const char* suffix)
  353. {
  354. auto fileWithoutSuffix = f.getFileNameWithoutExtension() + ".";
  355. return fileWithoutSuffix.containsIgnoreCase (suffix + String ("."))
  356. || fileWithoutSuffix.containsIgnoreCase (suffix + String ("_"));
  357. }
  358. Array<LibraryModule::CompileUnit> LibraryModule::getAllCompileUnits (build_tools::ProjectType::Target::Type forTarget) const
  359. {
  360. auto files = getFolder().findChildFiles (File::findFiles, false);
  361. FileSorter sorter;
  362. files.sort (sorter);
  363. Array<LibraryModule::CompileUnit> units;
  364. for (auto& file : files)
  365. {
  366. if (file.getFileName().startsWithIgnoreCase (getID())
  367. && file.hasFileExtension (sourceFileExtensions))
  368. {
  369. if (forTarget == build_tools::ProjectType::Target::unspecified
  370. || forTarget == Project::getTargetTypeFromFilePath (file, true))
  371. {
  372. CompileUnit cu;
  373. cu.file = file;
  374. units.add (std::move (cu));
  375. }
  376. }
  377. }
  378. for (auto& cu : units)
  379. {
  380. cu.isCompiledForObjC = true;
  381. cu.isCompiledForNonObjC = ! cu.file.hasFileExtension ("mm;m");
  382. if (cu.isCompiledForNonObjC)
  383. if (cu.file.withFileExtension ("mm").existsAsFile())
  384. cu.isCompiledForObjC = false;
  385. jassert (cu.isCompiledForObjC || cu.isCompiledForNonObjC);
  386. }
  387. return units;
  388. }
  389. void LibraryModule::findAndAddCompiledUnits (ProjectExporter& exporter,
  390. ProjectSaver* projectSaver,
  391. Array<File>& result,
  392. build_tools::ProjectType::Target::Type forTarget) const
  393. {
  394. for (auto& cu : getAllCompileUnits (forTarget))
  395. {
  396. if (cu.isNeededForExporter (exporter))
  397. {
  398. auto localFile = exporter.getProject().getGeneratedCodeFolder()
  399. .getChildFile (cu.getFilenameForProxyFile());
  400. result.add (localFile);
  401. if (projectSaver != nullptr)
  402. projectSaver->addFileToGeneratedGroup (localFile);
  403. }
  404. }
  405. }
  406. void LibraryModule::addBrowseableCode (ProjectExporter& exporter, const Array<File>& compiled, const File& localModuleFolder) const
  407. {
  408. if (sourceFiles.isEmpty())
  409. findBrowseableFiles (localModuleFolder, sourceFiles);
  410. auto sourceGroup = Project::Item::createGroup (exporter.getProject(), getID(), "__mainsourcegroup" + getID(), false);
  411. auto moduleFromProject = exporter.getModuleFolderRelativeToProject (getID());
  412. auto moduleHeader = moduleInfo.getHeader();
  413. auto& project = exporter.getProject();
  414. if (project.getEnabledModules().shouldCopyModuleFilesLocally (getID()))
  415. moduleHeader = project.getLocalModuleFolder (getID()).getChildFile (moduleHeader.getFileName());
  416. auto isModuleHeader = [&] (const File& f) { return f.getFileName() == moduleHeader.getFileName(); };
  417. for (auto& sourceFile : sourceFiles)
  418. {
  419. auto pathWithinModule = build_tools::getRelativePathFrom (sourceFile, localModuleFolder);
  420. // (Note: in exporters like MSVC we have to avoid adding the same file twice, even if one of those instances
  421. // is flagged as being excluded from the build, because this overrides the other and it fails to compile)
  422. if ((exporter.canCopeWithDuplicateFiles() || ! compiled.contains (sourceFile)) && ! isModuleHeader (sourceFile))
  423. addFileWithGroups (sourceGroup, moduleFromProject.getChildFile (pathWithinModule), pathWithinModule);
  424. }
  425. sourceGroup.sortAlphabetically (true, true);
  426. sourceGroup.addFileAtIndex (moduleHeader, -1, false);
  427. exporter.getModulesGroup().state.appendChild (sourceGroup.state.createCopy(), nullptr);
  428. }
  429. //==============================================================================
  430. EnabledModuleList::EnabledModuleList (Project& p, const ValueTree& s)
  431. : project (p), state (s)
  432. {
  433. }
  434. StringArray EnabledModuleList::getAllModules() const
  435. {
  436. StringArray moduleIDs;
  437. for (int i = 0; i < getNumModules(); ++i)
  438. moduleIDs.add (getModuleID (i));
  439. return moduleIDs;
  440. }
  441. void EnabledModuleList::createRequiredModules (OwnedArray<LibraryModule>& modules)
  442. {
  443. for (int i = 0; i < getNumModules(); ++i)
  444. modules.add (new LibraryModule (getModuleInfo (getModuleID (i))));
  445. }
  446. void EnabledModuleList::sortAlphabetically()
  447. {
  448. struct ModuleTreeSorter
  449. {
  450. static int compareElements (const ValueTree& m1, const ValueTree& m2)
  451. {
  452. return m1[Ids::ID].toString().compareIgnoreCase (m2[Ids::ID]);
  453. }
  454. };
  455. ModuleTreeSorter sorter;
  456. state.sort (sorter, getUndoManager(), false);
  457. }
  458. File EnabledModuleList::getDefaultModulesFolder() const
  459. {
  460. File globalPath (getAppSettings().getStoredPath (Ids::defaultJuceModulePath, TargetOS::getThisOS()).get().toString());
  461. if (globalPath.exists())
  462. return globalPath;
  463. for (auto& exporterPathModule : project.getExporterPathsModuleList().getAllModules())
  464. {
  465. auto f = exporterPathModule.second;
  466. if (f.isDirectory())
  467. return f.getParentDirectory();
  468. }
  469. return File::getCurrentWorkingDirectory();
  470. }
  471. ModuleDescription EnabledModuleList::getModuleInfo (const String& moduleID)
  472. {
  473. return ModuleDescription (project.getModuleWithID (moduleID).second);
  474. }
  475. bool EnabledModuleList::isModuleEnabled (const String& moduleID) const
  476. {
  477. return state.getChildWithProperty (Ids::ID, moduleID).isValid();
  478. }
  479. static void getDependencies (Project& project, const String& moduleID, StringArray& dependencies)
  480. {
  481. auto info = project.getEnabledModules().getModuleInfo (moduleID);
  482. for (auto uid : info.getDependencies())
  483. {
  484. if (! dependencies.contains (uid, true))
  485. {
  486. dependencies.add (uid);
  487. getDependencies (project, uid, dependencies);
  488. }
  489. }
  490. }
  491. StringArray EnabledModuleList::getExtraDependenciesNeeded (const String& moduleID) const
  492. {
  493. StringArray dependencies, extraDepsNeeded;
  494. getDependencies (project, moduleID, dependencies);
  495. for (auto dep : dependencies)
  496. if (dep != moduleID && ! isModuleEnabled (dep))
  497. extraDepsNeeded.add (dep);
  498. return extraDepsNeeded;
  499. }
  500. bool EnabledModuleList::doesModuleHaveHigherCppStandardThanProject (const String& moduleID)
  501. {
  502. auto projectCppStandard = project.getCppStandardString();
  503. if (projectCppStandard == "latest")
  504. return false;
  505. auto moduleCppStandard = getModuleInfo (moduleID).getMinimumCppStandard();
  506. return (moduleCppStandard.getIntValue() > projectCppStandard.getIntValue());
  507. }
  508. bool EnabledModuleList::shouldUseGlobalPath (const String& moduleID) const
  509. {
  510. return (bool) shouldUseGlobalPathValue (moduleID).getValue();
  511. }
  512. Value EnabledModuleList::shouldUseGlobalPathValue (const String& moduleID) const
  513. {
  514. return state.getChildWithProperty (Ids::ID, moduleID)
  515. .getPropertyAsValue (Ids::useGlobalPath, getUndoManager());
  516. }
  517. bool EnabledModuleList::shouldShowAllModuleFilesInProject (const String& moduleID) const
  518. {
  519. return (bool) shouldShowAllModuleFilesInProjectValue (moduleID).getValue();
  520. }
  521. Value EnabledModuleList::shouldShowAllModuleFilesInProjectValue (const String& moduleID) const
  522. {
  523. return state.getChildWithProperty (Ids::ID, moduleID)
  524. .getPropertyAsValue (Ids::showAllCode, getUndoManager());
  525. }
  526. bool EnabledModuleList::shouldCopyModuleFilesLocally (const String& moduleID) const
  527. {
  528. return (bool) shouldCopyModuleFilesLocallyValue (moduleID).getValue();
  529. }
  530. Value EnabledModuleList::shouldCopyModuleFilesLocallyValue (const String& moduleID) const
  531. {
  532. return state.getChildWithProperty (Ids::ID, moduleID)
  533. .getPropertyAsValue (Ids::useLocalCopy, getUndoManager());
  534. }
  535. bool EnabledModuleList::areMostModulesUsingGlobalPath() const
  536. {
  537. int numYes = 0, numNo = 0;
  538. for (auto i = getNumModules(); --i >= 0;)
  539. {
  540. if (shouldUseGlobalPath (getModuleID (i)))
  541. ++numYes;
  542. else
  543. ++numNo;
  544. }
  545. return numYes > numNo;
  546. }
  547. bool EnabledModuleList::areMostModulesCopiedLocally() const
  548. {
  549. int numYes = 0, numNo = 0;
  550. for (auto i = getNumModules(); --i >= 0;)
  551. {
  552. if (shouldCopyModuleFilesLocally (getModuleID (i)))
  553. ++numYes;
  554. else
  555. ++numNo;
  556. }
  557. return numYes > numNo;
  558. }
  559. void EnabledModuleList::addModule (const File& moduleFolder, bool copyLocally, bool useGlobalPath)
  560. {
  561. ModuleDescription info (moduleFolder);
  562. if (info.isValid())
  563. {
  564. auto moduleID = info.getID();
  565. if (! isModuleEnabled (moduleID))
  566. {
  567. ValueTree module (Ids::MODULE);
  568. module.setProperty (Ids::ID, moduleID, getUndoManager());
  569. state.appendChild (module, getUndoManager());
  570. sortAlphabetically();
  571. shouldShowAllModuleFilesInProjectValue (moduleID) = true;
  572. shouldCopyModuleFilesLocallyValue (moduleID) = copyLocally;
  573. shouldUseGlobalPathValue (moduleID) = useGlobalPath;
  574. build_tools::RelativePath path (moduleFolder.getParentDirectory(),
  575. project.getProjectFolder(),
  576. build_tools::RelativePath::projectFolder);
  577. for (Project::ExporterIterator exporter (project); exporter.next();)
  578. exporter->getPathForModuleValue (moduleID) = path.toUnixStyle();
  579. if (! useGlobalPath)
  580. project.rescanExporterPathModules (false);
  581. }
  582. }
  583. }
  584. void EnabledModuleList::addModuleInteractive (const String& moduleID)
  585. {
  586. auto f = project.getModuleWithID (moduleID).second;
  587. if (f != File())
  588. {
  589. addModule (f, areMostModulesCopiedLocally(), areMostModulesUsingGlobalPath());
  590. return;
  591. }
  592. addModuleFromUserSelectedFile();
  593. }
  594. void EnabledModuleList::addModuleFromUserSelectedFile()
  595. {
  596. auto lastLocation = getDefaultModulesFolder();
  597. FileChooser fc ("Select a module to add...", lastLocation, {});
  598. if (fc.browseForDirectory())
  599. {
  600. lastLocation = fc.getResult();
  601. addModuleOfferingToCopy (lastLocation, true);
  602. }
  603. }
  604. void EnabledModuleList::addModuleOfferingToCopy (const File& f, bool isFromUserSpecifiedFolder)
  605. {
  606. ModuleDescription m (f);
  607. if (! m.isValid())
  608. {
  609. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  610. "Add Module", "This wasn't a valid module folder!");
  611. return;
  612. }
  613. if (isModuleEnabled (m.getID()))
  614. {
  615. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  616. "Add Module", "The project already contains this module!");
  617. return;
  618. }
  619. addModule (m.moduleFolder, areMostModulesCopiedLocally(),
  620. isFromUserSpecifiedFolder ? false : areMostModulesUsingGlobalPath());
  621. }
  622. void EnabledModuleList::removeModule (String moduleID) // must be pass-by-value, and not a const ref!
  623. {
  624. for (auto i = state.getNumChildren(); --i >= 0;)
  625. if (state.getChild(i) [Ids::ID] == moduleID)
  626. state.removeChild (i, getUndoManager());
  627. for (Project::ExporterIterator exporter (project); exporter.next();)
  628. exporter->removePathForModule (moduleID);
  629. }