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.

820 lines
27KB

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