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.

438 lines
16KB

  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. //==============================================================================
  22. class ProjectSaver
  23. {
  24. public:
  25. ProjectSaver (Project& project_, const File& projectFile_)
  26. : project (project_),
  27. projectFile (projectFile_),
  28. generatedCodeFolder (project.getGeneratedCodeFolder()),
  29. generatedFilesGroup (Project::Item::createGroup (project, getJuceCodeGroupName(), "__generatedcode__"))
  30. {
  31. generatedFilesGroup.setID (getGeneratedGroupID());
  32. if (generatedCodeFolder.exists())
  33. {
  34. Array<File> subFiles;
  35. generatedCodeFolder.findChildFiles (subFiles, File::findFilesAndDirectories, false);
  36. for (int i = subFiles.size(); --i >= 0;)
  37. subFiles.getReference(i).deleteRecursively();
  38. }
  39. }
  40. Project& getProject() noexcept { return project; }
  41. String save()
  42. {
  43. jassert (generatedFilesGroup.getNumChildren() == 0); // this method can't be called more than once!
  44. const File oldFile (project.getFile());
  45. project.setFile (projectFile);
  46. writeMainProjectFile();
  47. if (errors.size() == 0)
  48. writeAppConfigFile();
  49. if (errors.size() == 0)
  50. writeBinaryDataFiles();
  51. if (errors.size() == 0)
  52. writeAppHeader();
  53. if (errors.size() == 0)
  54. writeProjects();
  55. if (errors.size() == 0)
  56. writeAppConfigFile(); // (this is repeated in case the projects added anything to it)
  57. if (generatedCodeFolder.exists() && errors.size() == 0)
  58. writeReadmeFile();
  59. if (errors.size() > 0)
  60. project.setFile (oldFile);
  61. return errors[0];
  62. }
  63. Project::Item saveGeneratedFile (const String& filePath, const MemoryOutputStream& newData)
  64. {
  65. if (! generatedCodeFolder.createDirectory())
  66. {
  67. errors.add ("Couldn't create folder: " + generatedCodeFolder.getFullPathName());
  68. return Project::Item (project, ValueTree::invalid);
  69. }
  70. const File file (generatedCodeFolder.getChildFile (filePath));
  71. if (replaceFileIfDifferent (file, newData))
  72. return addFileToGeneratedGroup (file);
  73. return Project::Item (project, ValueTree::invalid);
  74. }
  75. Project::Item addFileToGeneratedGroup (const File& file)
  76. {
  77. Project::Item item (generatedFilesGroup.findItemForFile (file));
  78. if (! item.isValid())
  79. {
  80. generatedFilesGroup.addFile (file, -1, true);
  81. item = generatedFilesGroup.findItemForFile (file);
  82. }
  83. return item;
  84. }
  85. void setExtraAppConfigFileContent (const String& content)
  86. {
  87. extraAppConfigContent = content;
  88. }
  89. static void writeAutoGenWarningComment (OutputStream& out)
  90. {
  91. out << "/*" << newLine << newLine
  92. << " IMPORTANT! This file is auto-generated each time you save your" << newLine
  93. << " project - if you alter its contents, your changes may be overwritten!" << newLine
  94. << newLine;
  95. }
  96. static void writeGuardedInclude (OutputStream& out, StringArray paths, StringArray guards)
  97. {
  98. StringArray uniquePaths (paths);
  99. uniquePaths.removeDuplicates (false);
  100. if (uniquePaths.size() == 1)
  101. {
  102. out << "#include " << paths[0] << newLine;
  103. }
  104. else
  105. {
  106. int i = paths.size();
  107. for (; --i >= 0;)
  108. {
  109. for (int j = i; --j >= 0;)
  110. {
  111. if (paths[i] == paths[j] && guards[i] == guards[j])
  112. {
  113. paths.remove (i);
  114. guards.remove (i);
  115. }
  116. }
  117. }
  118. for (i = 0; i < paths.size(); ++i)
  119. {
  120. out << (i == 0 ? "#if " : "#elif ") << guards[i] << newLine
  121. << " #include " << paths[i] << newLine;
  122. }
  123. out << "#endif" << newLine;
  124. }
  125. }
  126. static const char* getGeneratedGroupID() noexcept { return "__jucelibfiles"; }
  127. Project::Item& getGeneratedCodeGroup() { return generatedFilesGroup; }
  128. static String getJuceCodeGroupName() { return "Juce Library Code"; }
  129. private:
  130. Project& project;
  131. const File projectFile, generatedCodeFolder;
  132. Project::Item generatedFilesGroup;
  133. String extraAppConfigContent;
  134. StringArray errors;
  135. File appConfigFile, binaryDataCpp;
  136. void writeMainProjectFile()
  137. {
  138. ScopedPointer <XmlElement> xml (project.getProjectRoot().createXml());
  139. jassert (xml != nullptr);
  140. if (xml != nullptr)
  141. {
  142. #if JUCE_DEBUG
  143. {
  144. MemoryOutputStream mo;
  145. project.getProjectRoot().writeToStream (mo);
  146. MemoryInputStream mi (mo.getData(), mo.getDataSize(), false);
  147. ValueTree v = ValueTree::readFromStream (mi);
  148. ScopedPointer <XmlElement> xml2 (v.createXml());
  149. // This bit just tests that ValueTree save/load works reliably.. Let me know if this asserts for you!
  150. jassert (xml->isEquivalentTo (xml2, true));
  151. }
  152. #endif
  153. MemoryOutputStream mo;
  154. xml->writeToStream (mo, String::empty);
  155. replaceFileIfDifferent (projectFile, mo);
  156. }
  157. }
  158. bool writeAppConfig (OutputStream& out)
  159. {
  160. writeAutoGenWarningComment (out);
  161. out << " If you want to change any of these values, use the Introjucer to do so," << newLine
  162. << " rather than editing this file directly!" << newLine
  163. << newLine
  164. << " Any commented-out settings will assume their default values." << newLine
  165. << newLine
  166. << "*/" << newLine << newLine;
  167. OwnedArray<LibraryModule> modules;
  168. project.getProjectType().createRequiredModules (project, ModuleList::getInstance(), modules);
  169. bool anyFlags = false;
  170. for (int j = 0; j < modules.size(); ++j)
  171. {
  172. LibraryModule* const m = modules.getUnchecked(j);
  173. OwnedArray <Project::ConfigFlag> flags;
  174. m->getConfigFlags (project, flags);
  175. if (flags.size() > 0)
  176. {
  177. anyFlags = true;
  178. out << "//==============================================================================" << newLine
  179. << "// " << m->getID() << " flags:" << newLine
  180. << newLine;
  181. for (int i = 0; i < flags.size(); ++i)
  182. {
  183. flags.getUnchecked(i)->value.referTo (project.getConfigFlag (flags.getUnchecked(i)->symbol));
  184. const Project::ConfigFlag* const f = flags[i];
  185. const String value (project.getConfigFlag (f->symbol).toString());
  186. if (value == Project::configFlagEnabled)
  187. out << "#define " << f->symbol << " 1";
  188. else if (value == Project::configFlagDisabled)
  189. out << "#define " << f->symbol << " 0";
  190. else
  191. out << "//#define " << f->symbol;
  192. out << newLine;
  193. }
  194. if (j < modules.size() - 1)
  195. out << newLine;
  196. }
  197. }
  198. if (extraAppConfigContent.isNotEmpty())
  199. {
  200. out << newLine << extraAppConfigContent.trimEnd() << newLine;
  201. return true;
  202. }
  203. return anyFlags;
  204. }
  205. void writeAppConfigFile()
  206. {
  207. appConfigFile = generatedCodeFolder.getChildFile (project.getAppConfigFilename());
  208. MemoryOutputStream mem;
  209. if (writeAppConfig (mem))
  210. saveGeneratedFile (project.getAppConfigFilename(), mem);
  211. }
  212. void writeAppHeader (OutputStream& out)
  213. {
  214. writeAutoGenWarningComment (out);
  215. out << " This is the header file that your files should include in order to get all the" << newLine
  216. << " JUCE library headers. You should avoid including the JUCE headers directly in" << newLine
  217. << " your own source files, because that wouldn't pick up the correct configuration" << newLine
  218. << " options for your app." << newLine
  219. << newLine
  220. << "*/" << newLine << newLine;
  221. String headerGuard ("__APPHEADERFILE_" + project.getProjectUID().toUpperCase() + "__");
  222. out << "#ifndef " << headerGuard << newLine
  223. << "#define " << headerGuard << newLine << newLine;
  224. if (appConfigFile.exists())
  225. out << CodeHelpers::createIncludeStatement (project.getAppConfigFilename()) << newLine;
  226. {
  227. OwnedArray<LibraryModule> modules;
  228. project.getProjectType().createRequiredModules (project, ModuleList::getInstance(), modules);
  229. for (int i = 0; i < modules.size(); ++i)
  230. modules.getUnchecked(i)->writeIncludes (project, out);
  231. }
  232. if (binaryDataCpp.exists())
  233. out << CodeHelpers::createIncludeStatement (binaryDataCpp.withFileExtension (".h"), appConfigFile) << newLine;
  234. out << newLine
  235. << "#if ! DONT_SET_USING_JUCE_NAMESPACE" << newLine
  236. << " // If your code uses a lot of JUCE classes, then this will obviously save you" << newLine
  237. << " // a lot of typing, but can be disabled by setting DONT_SET_USING_JUCE_NAMESPACE." << newLine
  238. << " using namespace JUCE_NAMESPACE;" << newLine
  239. << "#endif" << newLine
  240. << newLine
  241. << "namespace ProjectInfo" << newLine
  242. << "{" << newLine
  243. << " const char* const projectName = " << CodeHelpers::addEscapeChars (project.getProjectName().toString()).quoted() << ";" << newLine
  244. << " const char* const versionString = " << CodeHelpers::addEscapeChars (project.getVersion().toString()).quoted() << ";" << newLine
  245. << " const int versionNumber = " << project.getVersionAsHex() << ";" << newLine
  246. << "}" << newLine
  247. << newLine
  248. << "#endif // " << headerGuard << newLine;
  249. }
  250. void writeAppHeader()
  251. {
  252. MemoryOutputStream mem;
  253. writeAppHeader (mem);
  254. saveGeneratedFile (project.getJuceSourceHFilename(), mem);
  255. }
  256. void writeBinaryDataFiles()
  257. {
  258. binaryDataCpp = generatedCodeFolder.getChildFile ("BinaryData.cpp");
  259. ResourceFile resourceFile (project);
  260. if (resourceFile.getNumFiles() > 0)
  261. {
  262. resourceFile.setClassName ("BinaryData");
  263. if (resourceFile.write (binaryDataCpp))
  264. {
  265. generatedFilesGroup.addFile (binaryDataCpp, -1, true);
  266. generatedFilesGroup.addFile (binaryDataCpp.withFileExtension (".h"), -1, false);
  267. }
  268. else
  269. {
  270. errors.add ("Can't create binary resources file: " + binaryDataCpp.getFullPathName());
  271. }
  272. }
  273. else
  274. {
  275. binaryDataCpp.deleteFile();
  276. binaryDataCpp.withFileExtension ("h").deleteFile();
  277. }
  278. }
  279. void writeReadmeFile()
  280. {
  281. MemoryOutputStream out;
  282. out << newLine
  283. << " Important Note!!" << newLine
  284. << " ================" << newLine
  285. << newLine
  286. << "The purpose of this folder is to contain files that are auto-generated by the Introjucer," << newLine
  287. << "and ALL files in this folder will be mercilessly DELETED and completely re-written whenever" << newLine
  288. << "the Introjucer saves your project." << newLine
  289. << newLine
  290. << "Therefore, it's a bad idea to make any manual changes to the files in here, or to" << newLine
  291. << "put any of your own files in here if you don't want to lose them. (Of course you may choose" << newLine
  292. << "to add the folder's contents to your version-control system so that you can re-merge your own" << newLine
  293. << "modifications after the Introjucer has saved its changes)." << newLine;
  294. replaceFileIfDifferent (generatedCodeFolder.getChildFile ("ReadMe.txt"), out);
  295. }
  296. static void sortGroupRecursively (Project::Item group)
  297. {
  298. group.sortAlphabetically (true);
  299. for (int i = group.getNumChildren(); --i >= 0;)
  300. sortGroupRecursively (group.getChild(i));
  301. }
  302. void writeProjects()
  303. {
  304. // keep a copy of the basic generated files group, as each exporter may modify it.
  305. const ValueTree originalGeneratedGroup (generatedFilesGroup.getNode().createCopy());
  306. OwnedArray<LibraryModule> modules;
  307. project.getProjectType().createRequiredModules (project, ModuleList::getInstance(), modules);
  308. for (int i = project.getNumExporters(); --i >= 0;)
  309. {
  310. ScopedPointer <ProjectExporter> exporter (project.createExporter (i));
  311. std::cout << "Writing files for: " << exporter->getName() << std::endl;
  312. if (exporter->getTargetFolder().createDirectory())
  313. {
  314. generatedFilesGroup.getNode() = originalGeneratedGroup.createCopy();
  315. project.getProjectType().prepareExporter (*exporter);
  316. for (int j = 0; j < modules.size(); ++j)
  317. modules.getUnchecked(j)->prepareExporter (*exporter, *this);
  318. sortGroupRecursively (generatedFilesGroup);
  319. exporter->groups.add (generatedFilesGroup);
  320. try
  321. {
  322. exporter->create();
  323. }
  324. catch (ProjectExporter::SaveError& error)
  325. {
  326. errors.add (error.message);
  327. }
  328. }
  329. else
  330. {
  331. errors.add ("Can't create folder: " + exporter->getTargetFolder().getFullPathName());
  332. }
  333. }
  334. }
  335. bool replaceFileIfDifferent (const File& f, const MemoryOutputStream& newData)
  336. {
  337. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (f, newData))
  338. {
  339. errors.add ("Can't write to file: " + f.getFullPathName());
  340. return false;
  341. }
  342. return true;
  343. }
  344. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProjectSaver);
  345. };
  346. #endif // __JUCER_PROJECTSAVER_JUCEHEADER__