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.

271 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - 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 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 "../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, true, true, false) + "AudioProcessor";
  86. processorClassName = processorClassName.substring (0, 1).toUpperCase() + processorClassName.substring (1);
  87. auto editorClassName = processorClassName + "Editor";
  88. tokenReplacements.insert ({"%%filter_headers%%", processorHInclude + newLine + editorHInclude });
  89. tokenReplacements.insert ({"%%filter_class_name%%", processorClassName });
  90. tokenReplacements.insert ({"%%editor_class_name%%", editorClassName });
  91. tokenReplacements.insert ({"%%editor_cpp_headers%%", processorHInclude + newLine + editorHInclude });
  92. tokenReplacements.insert ({"%%editor_headers%%", getJuceHeaderInclude() + newLine + processorHInclude });
  93. return tokenReplacements;
  94. }
  95. static bool addFiles (Project& project, const NewProjectTemplates::ProjectTemplate& projectTemplate,
  96. const String& name, var fileOptionsVar, StringArray& failedFiles)
  97. {
  98. auto sourceFolder = project.getFile().getSiblingFile ("Source");
  99. if (! sourceFolder.createDirectory())
  100. {
  101. failedFiles.add (sourceFolder.getFullPathName());
  102. return false;
  103. }
  104. auto fileOptions = NewProjectTemplates::getFileOptionForVar (fileOptionsVar);
  105. if (fileOptions == Opts::noFiles)
  106. return true;
  107. auto tokenReplacements = [&]() -> std::map<String, String>
  108. {
  109. if (NewProjectTemplates::isApplication (projectTemplate))
  110. return getApplicationFileTokenReplacements (name, fileOptions, sourceFolder);
  111. if (NewProjectTemplates::isPlugin (projectTemplate))
  112. return getPluginFileTokenReplacements (name, sourceFolder);
  113. jassertfalse;
  114. return {};
  115. }();
  116. auto sourceGroup = project.getMainGroup().getOrCreateSubGroup ("Source");
  117. for (auto& files : projectTemplate.getFilesForOption (fileOptions))
  118. {
  119. auto file = sourceFolder.getChildFile (files.first);
  120. auto fileContent = getFileTemplate (files.second);
  121. for (auto& tokenReplacement : tokenReplacements)
  122. fileContent = fileContent.replace (tokenReplacement.first, tokenReplacement.second, false);
  123. if (! build_tools::overwriteFileWithNewDataIfDifferent (file, fileContent))
  124. {
  125. failedFiles.add (file.getFullPathName());
  126. return false;
  127. }
  128. sourceGroup.addFileAtIndex (file, -1, (file.hasFileExtension (sourceFileExtensions)));
  129. }
  130. return true;
  131. }
  132. static void addModules (Project& project, Array<var> modules, const String& modulePath, bool useGlobalPath)
  133. {
  134. AvailableModulesList list;
  135. list.scanPaths ({ modulePath });
  136. for (auto& mod : list.getAllModules())
  137. if (modules.contains (mod.first))
  138. project.getEnabledModules().addModule (mod.second, false, useGlobalPath);
  139. }
  140. static void addExporters (Project& project, Array<var> exporters)
  141. {
  142. for (auto exporter : exporters)
  143. project.addNewExporter (exporter.toString());
  144. for (Project::ExporterIterator exporter (project); exporter.next();)
  145. for (ProjectExporter::ConfigIterator config (*exporter); config.next();)
  146. config->getValue (Ids::targetName) = project.getProjectFilenameRootString();
  147. }
  148. //==============================================================================
  149. File NewProjectWizard::getLastWizardFolder()
  150. {
  151. if (getAppSettings().lastWizardFolder.isDirectory())
  152. return getAppSettings().lastWizardFolder;
  153. #if JUCE_WINDOWS
  154. static File lastFolderFallback (File::getSpecialLocation (File::userDocumentsDirectory));
  155. #else
  156. static File lastFolderFallback (File::getSpecialLocation (File::userHomeDirectory));
  157. #endif
  158. return lastFolderFallback;
  159. }
  160. std::unique_ptr<Project> NewProjectWizard::createNewProject (const NewProjectTemplates::ProjectTemplate& projectTemplate,
  161. const File& targetFolder, const String& name, var modules, var exporters, var fileOptions,
  162. const String& modulePath, bool useGlobalModulePath)
  163. {
  164. StringArray failedFiles;
  165. if (! targetFolder.exists())
  166. {
  167. if (! targetFolder.createDirectory())
  168. failedFiles.add (targetFolder.getFullPathName());
  169. }
  170. else if (FileHelpers::containsAnyNonHiddenFiles (targetFolder))
  171. {
  172. if (! AlertWindow::showOkCancelBox (AlertWindow::InfoIcon,
  173. TRANS("New JUCE Project"),
  174. TRANS("You chose the folder:\n\nXFLDRX\n\n").replace ("XFLDRX", targetFolder.getFullPathName())
  175. + TRANS("This folder isn't empty - are you sure you want to create the project there?")
  176. + "\n\n"
  177. + TRANS("Any existing files with the same names may be overwritten by the new files.")))
  178. {
  179. return nullptr;
  180. }
  181. }
  182. auto project = std::make_unique<Project> (targetFolder.getChildFile (File::createLegalFileName (name))
  183. .withFileExtension (Project::projectFileExtension));
  184. if (failedFiles.isEmpty())
  185. {
  186. doBasicProjectSetup (*project, projectTemplate, name);
  187. if (addFiles (*project, projectTemplate, name, fileOptions, failedFiles))
  188. {
  189. addExporters (*project, *exporters.getArray());
  190. addModules (*project, *modules.getArray(), modulePath, useGlobalModulePath);
  191. if (project->save (false, true) == FileBasedDocument::savedOk)
  192. {
  193. project->setChangedFlag (false);
  194. project->loadFrom (project->getFile(), true);
  195. }
  196. else
  197. {
  198. failedFiles.add (project->getFile().getFullPathName());
  199. }
  200. }
  201. }
  202. if (! failedFiles.isEmpty())
  203. {
  204. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  205. TRANS("Errors in Creating Project!"),
  206. TRANS("The following files couldn't be written:")
  207. + "\n\n"
  208. + failedFiles.joinIntoString ("\n", 0, 10));
  209. return nullptr;
  210. }
  211. return project;
  212. }