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.

321 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  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 "../jucer_Headers.h"
  14. #include "../jucer_Application.h"
  15. #include "../../ProjectSaving/jucer_ProjectExporter.h"
  16. #include "jucer_NewProjectWizard.h"
  17. //==============================================================================
  18. static String getFileTemplate (const String& templateName)
  19. {
  20. int dataSize = 0;
  21. if (auto* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize))
  22. return String::fromUTF8 (data, dataSize);
  23. jassertfalse;
  24. return {};
  25. }
  26. static String getJuceHeaderInclude()
  27. {
  28. return CodeHelpers::createIncludePathIncludeStatement (Project::getJuceSourceHFilename());
  29. }
  30. static String getContentComponentName()
  31. {
  32. return "MainComponent";
  33. }
  34. using Opts = NewProjectTemplates::FileCreationOptions;
  35. static bool shouldCreateHeaderFile (Opts opts) noexcept { return opts == Opts::header || opts == Opts::headerAndCpp; }
  36. static bool shouldCreateCppFile (Opts opts) noexcept { return opts == Opts::headerAndCpp; }
  37. static void doBasicProjectSetup (Project& project, const NewProjectTemplates::ProjectTemplate& projectTemplate, const String& name)
  38. {
  39. project.setTitle (name);
  40. project.setProjectType (projectTemplate.projectTypeString);
  41. project.getMainGroup().addNewSubGroup ("Source", 0);
  42. project.getConfigFlag ("JUCE_STRICT_REFCOUNTEDPOINTER") = true;
  43. project.getProjectValue (Ids::useAppConfig) = false;
  44. project.getProjectValue (Ids::addUsingNamespaceToJuceHeader) = false;
  45. if (! ProjucerApplication::getApp().getLicenseController().getCurrentState().canUnlockFullFeatures())
  46. project.getProjectValue (Ids::displaySplashScreen) = true;
  47. if (NewProjectTemplates::isPlugin (projectTemplate))
  48. project.getConfigFlag ("JUCE_VST3_CAN_REPLACE_VST2") = 0;
  49. }
  50. static std::map<String, String> getSharedFileTokenReplacements()
  51. {
  52. return { { "%%app_headers%%", getJuceHeaderInclude() } };
  53. }
  54. static std::map<String, String> getApplicationFileTokenReplacements (const String& name,
  55. NewProjectTemplates::FileCreationOptions fileOptions,
  56. const File& sourceFolder)
  57. {
  58. auto tokenReplacements = getSharedFileTokenReplacements();
  59. tokenReplacements.insert ({ "%%app_class_name%%",
  60. build_tools::makeValidIdentifier (name + "Application", false, true, false) });
  61. tokenReplacements.insert ({ "%%content_component_class%%",
  62. getContentComponentName() });
  63. tokenReplacements.insert ({ "%%include_juce%%",
  64. getJuceHeaderInclude() });
  65. if (shouldCreateHeaderFile (fileOptions))
  66. tokenReplacements["%%app_headers%%"] << newLine
  67. << CodeHelpers::createIncludeStatement (sourceFolder.getChildFile ("MainComponent.h"),
  68. sourceFolder.getChildFile ("Main.cpp"));
  69. if (shouldCreateCppFile (fileOptions))
  70. tokenReplacements.insert ({ "%%include_corresponding_header%%",
  71. CodeHelpers::createIncludeStatement (sourceFolder.getChildFile ("MainComponent.h"),
  72. sourceFolder.getChildFile ("MainComponent.cpp")) });
  73. return tokenReplacements;
  74. }
  75. static std::map<String, String> getPluginFileTokenReplacements (const String& name,
  76. const File& sourceFolder)
  77. {
  78. auto tokenReplacements = getSharedFileTokenReplacements();
  79. auto processorCppFile = sourceFolder.getChildFile ("PluginProcessor.cpp");
  80. auto processorHFile = processorCppFile.withFileExtension (".h");
  81. auto editorCppFile = sourceFolder.getChildFile ("PluginEditor.cpp");
  82. auto editorHFile = editorCppFile.withFileExtension (".h");
  83. auto processorHInclude = CodeHelpers::createIncludeStatement (processorHFile, processorCppFile);
  84. auto editorHInclude = CodeHelpers::createIncludeStatement (editorHFile, processorCppFile);
  85. auto processorClassName = build_tools::makeValidIdentifier (name, false, true, false) + "AudioProcessor";
  86. processorClassName = processorClassName.substring (0, 1).toUpperCase() + processorClassName.substring (1);
  87. auto editorClassName = processorClassName + "Editor";
  88. const auto araDocumentControllerCppFile = sourceFolder.getChildFile ("PluginARADocumentController.cpp");
  89. const auto araDocumentControllerHFile = araDocumentControllerCppFile.withFileExtension (".h");
  90. const auto araPlaybackRendererCppFile = sourceFolder.getChildFile ("PluginARAPlaybackRenderer.cpp");
  91. const auto araPlaybackRendererHFile = araPlaybackRendererCppFile.withFileExtension (".h");
  92. const auto araDocumentControllerHInclude = CodeHelpers::createIncludeStatement (araDocumentControllerHFile, araDocumentControllerCppFile);
  93. const auto araPlaybackRendererHInclude = CodeHelpers::createIncludeStatement (araPlaybackRendererHFile, araPlaybackRendererCppFile);
  94. auto araDocumentControllerClassName = build_tools::makeValidIdentifier (name, true, true, false) + "DocumentController";
  95. araDocumentControllerClassName = araDocumentControllerClassName.substring (0, 1).toUpperCase() + araDocumentControllerClassName.substring (1);
  96. auto araPlaybackRendererClassName = build_tools::makeValidIdentifier (name, true, true, false) + "PlaybackRenderer";
  97. araPlaybackRendererClassName = araPlaybackRendererClassName.substring (0, 1).toUpperCase() + araPlaybackRendererClassName.substring (1);
  98. tokenReplacements.insert ({"%%filter_headers%%", processorHInclude + newLine + editorHInclude });
  99. tokenReplacements.insert ({"%%filter_class_name%%", processorClassName });
  100. tokenReplacements.insert ({"%%editor_class_name%%", editorClassName });
  101. tokenReplacements.insert ({"%%editor_cpp_headers%%", processorHInclude + newLine + editorHInclude });
  102. tokenReplacements.insert ({"%%editor_headers%%", getJuceHeaderInclude() + newLine + processorHInclude });
  103. tokenReplacements.insert ({"%%aradocumentcontroller_headers%%", araDocumentControllerHInclude });
  104. tokenReplacements.insert ({"%%aradocumentcontroller_class_name%%", araDocumentControllerClassName });
  105. tokenReplacements.insert ({"%%araplaybackrenderer_headers%%", araPlaybackRendererHInclude });
  106. tokenReplacements.insert ({"%%araplaybackrenderer_class_name%%", araPlaybackRendererClassName });
  107. return tokenReplacements;
  108. }
  109. static bool addFiles (Project& project, const NewProjectTemplates::ProjectTemplate& projectTemplate,
  110. const String& name, var fileOptionsVar, StringArray& failedFiles)
  111. {
  112. auto sourceFolder = project.getFile().getSiblingFile ("Source");
  113. if (! sourceFolder.createDirectory())
  114. {
  115. failedFiles.add (sourceFolder.getFullPathName());
  116. return false;
  117. }
  118. auto fileOptions = NewProjectTemplates::getFileOptionForVar (fileOptionsVar);
  119. if (fileOptions == Opts::noFiles)
  120. return true;
  121. auto tokenReplacements = [&]() -> std::map<String, String>
  122. {
  123. if (NewProjectTemplates::isApplication (projectTemplate))
  124. return getApplicationFileTokenReplacements (name, fileOptions, sourceFolder);
  125. if (NewProjectTemplates::isPlugin (projectTemplate))
  126. return getPluginFileTokenReplacements (name, sourceFolder);
  127. jassertfalse;
  128. return {};
  129. }();
  130. auto sourceGroup = project.getMainGroup().getOrCreateSubGroup ("Source");
  131. for (auto& files : projectTemplate.getFilesForOption (fileOptions))
  132. {
  133. auto file = sourceFolder.getChildFile (files.first);
  134. auto fileContent = getFileTemplate (files.second);
  135. for (auto& tokenReplacement : tokenReplacements)
  136. fileContent = fileContent.replace (tokenReplacement.first, tokenReplacement.second, false);
  137. if (! build_tools::overwriteFileWithNewDataIfDifferent (file, fileContent))
  138. {
  139. failedFiles.add (file.getFullPathName());
  140. return false;
  141. }
  142. sourceGroup.addFileAtIndex (file, -1, (file.hasFileExtension (sourceFileExtensions)));
  143. }
  144. return true;
  145. }
  146. static void addModules (Project& project, Array<var> modules, const String& modulePath, bool useGlobalPath)
  147. {
  148. AvailableModulesList list;
  149. list.scanPaths ({ modulePath });
  150. auto& projectModules = project.getEnabledModules();
  151. for (auto& mod : list.getAllModules())
  152. if (modules.contains (mod.first))
  153. projectModules.addModule (mod.second, false, useGlobalPath);
  154. for (auto& mod : projectModules.getModulesWithMissingDependencies())
  155. projectModules.tryToFixMissingDependencies (mod);
  156. }
  157. static void addExporters (Project& project, Array<var> exporters)
  158. {
  159. for (auto exporter : exporters)
  160. project.addNewExporter (exporter.toString());
  161. for (Project::ExporterIterator exporter (project); exporter.next();)
  162. for (ProjectExporter::ConfigIterator config (*exporter); config.next();)
  163. config->getValue (Ids::targetName) = project.getProjectFilenameRootString();
  164. }
  165. //==============================================================================
  166. File NewProjectWizard::getLastWizardFolder()
  167. {
  168. if (getAppSettings().lastWizardFolder.isDirectory())
  169. return getAppSettings().lastWizardFolder;
  170. #if JUCE_WINDOWS
  171. static File lastFolderFallback (File::getSpecialLocation (File::userDocumentsDirectory));
  172. #else
  173. static File lastFolderFallback (File::getSpecialLocation (File::userHomeDirectory));
  174. #endif
  175. return lastFolderFallback;
  176. }
  177. static void displayFailedFilesMessage (const StringArray& failedFiles)
  178. {
  179. AlertWindow::showMessageBoxAsync (MessageBoxIconType::WarningIcon,
  180. TRANS("Errors in Creating Project!"),
  181. TRANS("The following files couldn't be written:")
  182. + "\n\n"
  183. + failedFiles.joinIntoString ("\n", 0, 10));
  184. }
  185. template <typename Callback>
  186. static void prepareDirectory (const File& targetFolder, Callback&& callback)
  187. {
  188. StringArray failedFiles;
  189. if (! targetFolder.exists())
  190. {
  191. if (! targetFolder.createDirectory())
  192. {
  193. displayFailedFilesMessage ({ targetFolder.getFullPathName() });
  194. return;
  195. }
  196. }
  197. else if (FileHelpers::containsAnyNonHiddenFiles (targetFolder))
  198. {
  199. AlertWindow::showOkCancelBox (MessageBoxIconType::InfoIcon,
  200. TRANS("New JUCE Project"),
  201. TRANS("You chose the folder:\n\nXFLDRX\n\n").replace ("XFLDRX", targetFolder.getFullPathName())
  202. + TRANS("This folder isn't empty - are you sure you want to create the project there?")
  203. + "\n\n"
  204. + TRANS("Any existing files with the same names may be overwritten by the new files."),
  205. {},
  206. {},
  207. nullptr,
  208. ModalCallbackFunction::create ([callback] (int result)
  209. {
  210. if (result != 0)
  211. callback();
  212. }));
  213. return;
  214. }
  215. callback();
  216. }
  217. void NewProjectWizard::createNewProject (const NewProjectTemplates::ProjectTemplate& projectTemplate,
  218. const File& targetFolder, const String& name, var modules, var exporters, var fileOptions,
  219. const String& modulePath, bool useGlobalModulePath,
  220. std::function<void (std::unique_ptr<Project>)> callback)
  221. {
  222. prepareDirectory (targetFolder, [=]
  223. {
  224. auto project = std::make_unique<Project> (targetFolder.getChildFile (File::createLegalFileName (name))
  225. .withFileExtension (Project::projectFileExtension));
  226. doBasicProjectSetup (*project, projectTemplate, name);
  227. StringArray failedFiles;
  228. if (addFiles (*project, projectTemplate, name, fileOptions, failedFiles))
  229. {
  230. addExporters (*project, *exporters.getArray());
  231. addModules (*project, *modules.getArray(), modulePath, useGlobalModulePath);
  232. auto sharedProject = std::make_shared<std::unique_ptr<Project>> (std::move (project));
  233. (*sharedProject)->saveAsync (false, true, [sharedProject, failedFiles, callback] (FileBasedDocument::SaveResult r)
  234. {
  235. auto uniqueProject = std::move (*sharedProject.get());
  236. if (r == FileBasedDocument::savedOk)
  237. {
  238. uniqueProject->setChangedFlag (false);
  239. uniqueProject->loadFrom (uniqueProject->getFile(), true);
  240. callback (std::move (uniqueProject));
  241. return;
  242. }
  243. auto failedFilesCopy = failedFiles;
  244. failedFilesCopy.add (uniqueProject->getFile().getFullPathName());
  245. displayFailedFilesMessage (failedFilesCopy);
  246. });
  247. return;
  248. }
  249. displayFailedFilesMessage (failedFiles);
  250. });
  251. }