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