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.

456 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_PROJECTSAVER_JUCEHEADER__
  19. #define __JUCER_PROJECTSAVER_JUCEHEADER__
  20. #include "jucer_ResourceFile.h"
  21. #include "../Project/jucer_Module.h"
  22. #include "jucer_ProjectExporter.h"
  23. //==============================================================================
  24. class ProjectSaver
  25. {
  26. public:
  27. ProjectSaver (Project& project_, const File& projectFile_)
  28. : project (project_),
  29. projectFile (projectFile_),
  30. generatedCodeFolder (project.getGeneratedCodeFolder()),
  31. generatedFilesGroup (Project::Item::createGroup (project, getJuceCodeGroupName(), "__generatedcode__"))
  32. {
  33. generatedFilesGroup.setID (getGeneratedGroupID());
  34. if (generatedCodeFolder.exists())
  35. deleteNonHiddenFilesIn (generatedCodeFolder);
  36. }
  37. Project& getProject() noexcept { return project; }
  38. String save()
  39. {
  40. jassert (generatedFilesGroup.getNumChildren() == 0); // this method can't be called more than once!
  41. const File oldFile (project.getFile());
  42. project.setFile (projectFile);
  43. writeMainProjectFile();
  44. OwnedArray<LibraryModule> modules;
  45. {
  46. ModuleList moduleList;
  47. moduleList.rescan (ModuleList::getDefaultModulesFolder (&project));
  48. project.createRequiredModules (moduleList, modules);
  49. }
  50. if (errors.size() == 0)
  51. writeAppConfigFile (modules);
  52. if (errors.size() == 0)
  53. writeBinaryDataFiles();
  54. if (errors.size() == 0)
  55. writeAppHeader (modules);
  56. if (errors.size() == 0)
  57. writeProjects (modules);
  58. if (errors.size() == 0)
  59. writeAppConfigFile (modules); // (this is repeated in case the projects added anything to it)
  60. if (generatedCodeFolder.exists() && errors.size() == 0)
  61. writeReadmeFile();
  62. if (errors.size() > 0)
  63. project.setFile (oldFile);
  64. return errors[0];
  65. }
  66. Project::Item saveGeneratedFile (const String& filePath, const MemoryOutputStream& newData)
  67. {
  68. if (! generatedCodeFolder.createDirectory())
  69. {
  70. errors.add ("Couldn't create folder: " + generatedCodeFolder.getFullPathName());
  71. return Project::Item (project, ValueTree::invalid);
  72. }
  73. const File file (generatedCodeFolder.getChildFile (filePath));
  74. if (replaceFileIfDifferent (file, newData))
  75. return addFileToGeneratedGroup (file);
  76. return Project::Item (project, ValueTree::invalid);
  77. }
  78. Project::Item addFileToGeneratedGroup (const File& file)
  79. {
  80. Project::Item item (generatedFilesGroup.findItemForFile (file));
  81. if (! item.isValid())
  82. {
  83. generatedFilesGroup.addFile (file, -1, true);
  84. item = generatedFilesGroup.findItemForFile (file);
  85. }
  86. return item;
  87. }
  88. void setExtraAppConfigFileContent (const String& content)
  89. {
  90. extraAppConfigContent = content;
  91. }
  92. static void writeAutoGenWarningComment (OutputStream& out)
  93. {
  94. out << "/*" << newLine << newLine
  95. << " IMPORTANT! This file is auto-generated each time you save your" << newLine
  96. << " project - if you alter its contents, your changes may be overwritten!" << newLine
  97. << newLine;
  98. }
  99. static const char* getGeneratedGroupID() noexcept { return "__jucelibfiles"; }
  100. Project::Item& getGeneratedCodeGroup() { return generatedFilesGroup; }
  101. static String getJuceCodeGroupName() { return "Juce Library Code"; }
  102. File getGeneratedCodeFolder() const { return generatedCodeFolder; }
  103. bool replaceFileIfDifferent (const File& f, const MemoryOutputStream& newData)
  104. {
  105. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (f, newData))
  106. {
  107. errors.add ("Can't write to file: " + f.getFullPathName());
  108. return false;
  109. }
  110. return true;
  111. }
  112. private:
  113. Project& project;
  114. const File projectFile, generatedCodeFolder;
  115. Project::Item generatedFilesGroup;
  116. String extraAppConfigContent;
  117. StringArray errors;
  118. File appConfigFile, binaryDataCpp;
  119. // Recursively clears out a folder's contents, but leaves behind any folders
  120. // containing hidden files used by version-control systems.
  121. static bool deleteNonHiddenFilesIn (const File& parent)
  122. {
  123. bool folderIsNowEmpty = true;
  124. DirectoryIterator i (parent, false, "*", File::findFilesAndDirectories);
  125. Array<File> filesToDelete;
  126. bool isFolder;
  127. while (i.next (&isFolder, nullptr, nullptr, nullptr, nullptr, nullptr))
  128. {
  129. const File f (i.getFile());
  130. if (shouldFileBeKept (f.getFileName()))
  131. {
  132. folderIsNowEmpty = false;
  133. }
  134. else if (isFolder)
  135. {
  136. if (deleteNonHiddenFilesIn (f))
  137. filesToDelete.add (f);
  138. else
  139. folderIsNowEmpty = false;
  140. }
  141. else
  142. {
  143. filesToDelete.add (f);
  144. }
  145. }
  146. for (int j = filesToDelete.size(); --j >= 0;)
  147. filesToDelete.getReference(j).deleteRecursively();
  148. return folderIsNowEmpty;
  149. }
  150. static bool shouldFileBeKept (const String& filename)
  151. {
  152. const char* filesToKeep[] = { ".svn", ".cvs", "CMakeLists.txt" };
  153. for (int i = 0; i < numElementsInArray (filesToKeep); ++i)
  154. if (filename == filesToKeep[i])
  155. return true;
  156. return false;
  157. }
  158. void writeMainProjectFile()
  159. {
  160. ScopedPointer <XmlElement> xml (project.getProjectRoot().createXml());
  161. jassert (xml != nullptr);
  162. if (xml != nullptr)
  163. {
  164. MemoryOutputStream mo;
  165. xml->writeToStream (mo, String::empty);
  166. replaceFileIfDifferent (projectFile, mo);
  167. }
  168. }
  169. static int findLongestModuleName (const OwnedArray<LibraryModule>& modules)
  170. {
  171. int longest = 0;
  172. for (int i = modules.size(); --i >= 0;)
  173. longest = jmax (longest, modules.getUnchecked(i)->getID().length());
  174. return longest;
  175. }
  176. void writeAppConfig (OutputStream& out, const OwnedArray<LibraryModule>& modules)
  177. {
  178. writeAutoGenWarningComment (out);
  179. out << " If you want to change any of these values, use the Introjucer to do so," << newLine
  180. << " rather than editing this file directly!" << newLine
  181. << newLine
  182. << " Any commented-out settings will assume their default values." << newLine
  183. << newLine
  184. << "*/" << newLine
  185. << newLine;
  186. const String headerGuard ("__JUCE_APPCONFIG_" + project.getProjectUID().toUpperCase() + "__");
  187. out << "#ifndef " << headerGuard << newLine
  188. << "#define " << headerGuard << newLine
  189. << newLine
  190. << "//==============================================================================" << newLine;
  191. const int longestName = findLongestModuleName (modules);
  192. for (int k = 0; k < modules.size(); ++k)
  193. {
  194. LibraryModule* const m = modules.getUnchecked(k);
  195. out << "#define JUCE_MODULE_AVAILABLE_" << m->getID()
  196. << String::repeatedString (" ", longestName + 5 - m->getID().length()) << " 1" << newLine;
  197. }
  198. out << newLine;
  199. for (int j = 0; j < modules.size(); ++j)
  200. {
  201. LibraryModule* const m = modules.getUnchecked(j);
  202. OwnedArray <Project::ConfigFlag> flags;
  203. m->getConfigFlags (project, flags);
  204. if (flags.size() > 0)
  205. {
  206. out << "//==============================================================================" << newLine
  207. << "// " << m->getID() << " flags:" << newLine
  208. << newLine;
  209. for (int i = 0; i < flags.size(); ++i)
  210. {
  211. flags.getUnchecked(i)->value.referTo (project.getConfigFlag (flags.getUnchecked(i)->symbol));
  212. const Project::ConfigFlag* const f = flags[i];
  213. const String value (project.getConfigFlag (f->symbol).toString());
  214. if (value == Project::configFlagEnabled)
  215. out << "#define " << f->symbol << " 1";
  216. else if (value == Project::configFlagDisabled)
  217. out << "#define " << f->symbol << " 0";
  218. else
  219. out << "//#define " << f->symbol;
  220. out << newLine;
  221. }
  222. if (j < modules.size() - 1)
  223. out << newLine;
  224. }
  225. }
  226. if (extraAppConfigContent.isNotEmpty())
  227. out << newLine << extraAppConfigContent.trimEnd() << newLine;
  228. out << newLine
  229. << "#endif // " << headerGuard << newLine;
  230. }
  231. void writeAppConfigFile (const OwnedArray<LibraryModule>& modules)
  232. {
  233. appConfigFile = generatedCodeFolder.getChildFile (project.getAppConfigFilename());
  234. MemoryOutputStream mem;
  235. writeAppConfig (mem, modules);
  236. saveGeneratedFile (project.getAppConfigFilename(), mem);
  237. }
  238. void writeAppHeader (OutputStream& out, const OwnedArray<LibraryModule>& modules)
  239. {
  240. writeAutoGenWarningComment (out);
  241. out << " This is the header file that your files should include in order to get all the" << newLine
  242. << " JUCE library headers. You should avoid including the JUCE headers directly in" << newLine
  243. << " your own source files, because that wouldn't pick up the correct configuration" << newLine
  244. << " options for your app." << newLine
  245. << newLine
  246. << "*/" << newLine << newLine;
  247. String headerGuard ("__APPHEADERFILE_" + project.getProjectUID().toUpperCase() + "__");
  248. out << "#ifndef " << headerGuard << newLine
  249. << "#define " << headerGuard << newLine << newLine;
  250. if (appConfigFile.exists())
  251. out << CodeHelpers::createIncludeStatement (project.getAppConfigFilename()) << newLine;
  252. for (int i = 0; i < modules.size(); ++i)
  253. modules.getUnchecked(i)->writeIncludes (*this, out);
  254. if (binaryDataCpp.exists())
  255. out << CodeHelpers::createIncludeStatement (binaryDataCpp.withFileExtension (".h"), appConfigFile) << newLine;
  256. out << newLine
  257. << "#if ! DONT_SET_USING_JUCE_NAMESPACE" << newLine
  258. << " // If your code uses a lot of JUCE classes, then this will obviously save you" << newLine
  259. << " // a lot of typing, but can be disabled by setting DONT_SET_USING_JUCE_NAMESPACE." << newLine
  260. << " using namespace juce;" << newLine
  261. << "#endif" << newLine
  262. << newLine
  263. << "namespace ProjectInfo" << newLine
  264. << "{" << newLine
  265. << " const char* const projectName = " << CodeHelpers::addEscapeChars (project.getProjectName().toString()).quoted() << ";" << newLine
  266. << " const char* const versionString = " << CodeHelpers::addEscapeChars (project.getVersion().toString()).quoted() << ";" << newLine
  267. << " const int versionNumber = " << project.getVersionAsHex() << ";" << newLine
  268. << "}" << newLine
  269. << newLine
  270. << "#endif // " << headerGuard << newLine;
  271. }
  272. void writeAppHeader (const OwnedArray<LibraryModule>& modules)
  273. {
  274. MemoryOutputStream mem;
  275. writeAppHeader (mem, modules);
  276. saveGeneratedFile (project.getJuceSourceHFilename(), mem);
  277. }
  278. void writeBinaryDataFiles()
  279. {
  280. binaryDataCpp = generatedCodeFolder.getChildFile ("BinaryData.cpp");
  281. ResourceFile resourceFile (project);
  282. if (resourceFile.getNumFiles() > 0)
  283. {
  284. resourceFile.setClassName ("BinaryData");
  285. if (resourceFile.write (binaryDataCpp))
  286. {
  287. generatedFilesGroup.addFile (binaryDataCpp, -1, true);
  288. generatedFilesGroup.addFile (binaryDataCpp.withFileExtension (".h"), -1, false);
  289. }
  290. else
  291. {
  292. errors.add ("Can't create binary resources file: " + binaryDataCpp.getFullPathName());
  293. }
  294. }
  295. else
  296. {
  297. binaryDataCpp.deleteFile();
  298. binaryDataCpp.withFileExtension ("h").deleteFile();
  299. }
  300. }
  301. void writeReadmeFile()
  302. {
  303. MemoryOutputStream out;
  304. out << newLine
  305. << " Important Note!!" << newLine
  306. << " ================" << newLine
  307. << newLine
  308. << "The purpose of this folder is to contain files that are auto-generated by the Introjucer," << newLine
  309. << "and ALL files in this folder will be mercilessly DELETED and completely re-written whenever" << newLine
  310. << "the Introjucer saves your project." << newLine
  311. << newLine
  312. << "Therefore, it's a bad idea to make any manual changes to the files in here, or to" << newLine
  313. << "put any of your own files in here if you don't want to lose them. (Of course you may choose" << newLine
  314. << "to add the folder's contents to your version-control system so that you can re-merge your own" << newLine
  315. << "modifications after the Introjucer has saved its changes)." << newLine;
  316. replaceFileIfDifferent (generatedCodeFolder.getChildFile ("ReadMe.txt"), out);
  317. }
  318. static void sortGroupRecursively (Project::Item group)
  319. {
  320. group.sortAlphabetically (true);
  321. for (int i = group.getNumChildren(); --i >= 0;)
  322. sortGroupRecursively (group.getChild(i));
  323. }
  324. void writeProjects (const OwnedArray<LibraryModule>& modules)
  325. {
  326. // keep a copy of the basic generated files group, as each exporter may modify it.
  327. const ValueTree originalGeneratedGroup (generatedFilesGroup.getNode().createCopy());
  328. for (int i = project.getNumExporters(); --i >= 0;)
  329. {
  330. ScopedPointer <ProjectExporter> exporter (project.createExporter (i));
  331. std::cout << "Writing files for: " << exporter->getName() << std::endl;
  332. if (exporter->getTargetFolder().createDirectory())
  333. {
  334. exporter->addToExtraSearchPaths (RelativePath ("JuceLibraryCode", RelativePath::projectFolder));
  335. generatedFilesGroup.getNode() = originalGeneratedGroup.createCopy();
  336. project.getProjectType().prepareExporter (*exporter);
  337. for (int j = 0; j < modules.size(); ++j)
  338. modules.getUnchecked(j)->prepareExporter (*exporter, *this);
  339. sortGroupRecursively (generatedFilesGroup);
  340. exporter->groups.add (generatedFilesGroup);
  341. try
  342. {
  343. exporter->create();
  344. }
  345. catch (ProjectExporter::SaveError& error)
  346. {
  347. errors.add (error.message);
  348. }
  349. }
  350. else
  351. {
  352. errors.add ("Can't create folder: " + exporter->getTargetFolder().getFullPathName());
  353. }
  354. }
  355. }
  356. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProjectSaver);
  357. };
  358. #endif // __JUCER_PROJECTSAVER_JUCEHEADER__