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.

1142 lines
38KB

  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. #include "jucer_Project.h"
  19. #include "jucer_ProjectType.h"
  20. #include "../Project Saving/jucer_ProjectExporter.h"
  21. #include "../Project Saving/jucer_ProjectSaver.h"
  22. #include "../Application/jucer_OpenDocumentManager.h"
  23. //==============================================================================
  24. namespace Tags
  25. {
  26. const Identifier projectRoot ("JUCERPROJECT");
  27. const Identifier projectMainGroup ("MAINGROUP");
  28. const Identifier group ("GROUP");
  29. const Identifier file ("FILE");
  30. const Identifier configurations ("CONFIGURATIONS");
  31. const Identifier configuration ("CONFIGURATION");
  32. const Identifier exporters ("EXPORTFORMATS");
  33. const Identifier configGroup ("JUCEOPTIONS");
  34. const Identifier modulesGroup ("MODULES");
  35. const Identifier module ("MODULE");
  36. }
  37. const char* Project::projectFileExtension = ".jucer";
  38. //==============================================================================
  39. Project::Project (const File& file_)
  40. : FileBasedDocument (projectFileExtension,
  41. String ("*") + projectFileExtension,
  42. "Choose a Jucer project to load",
  43. "Save Jucer project"),
  44. projectRoot (Tags::projectRoot)
  45. {
  46. setFile (file_);
  47. setMissingDefaultValues();
  48. setChangedFlag (false);
  49. mainProjectIcon.setImage (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize));
  50. projectRoot.addListener (this);
  51. }
  52. Project::~Project()
  53. {
  54. projectRoot.removeListener (this);
  55. OpenDocumentManager::getInstance()->closeAllDocumentsUsingProject (*this, false);
  56. }
  57. //==============================================================================
  58. void Project::setTitle (const String& newTitle)
  59. {
  60. projectRoot.setProperty (Ids::name, newTitle, getUndoManagerFor (projectRoot));
  61. getMainGroup().getName() = newTitle;
  62. }
  63. const String Project::getDocumentTitle()
  64. {
  65. return getProjectName().toString();
  66. }
  67. void Project::updateProjectSettings()
  68. {
  69. projectRoot.setProperty (Ids::jucerVersion, ProjectInfo::versionString, 0);
  70. projectRoot.setProperty (Ids::name, getDocumentTitle(), 0);
  71. }
  72. void Project::setMissingDefaultValues()
  73. {
  74. if (! projectRoot.hasProperty (Ids::id_))
  75. projectRoot.setProperty (Ids::id_, createAlphaNumericUID(), nullptr);
  76. // Create main file group if missing
  77. if (! projectRoot.getChildWithName (Tags::projectMainGroup).isValid())
  78. {
  79. Item mainGroup (*this, ValueTree (Tags::projectMainGroup));
  80. projectRoot.addChild (mainGroup.getNode(), 0, 0);
  81. }
  82. getMainGroup().initialiseNodeValues();
  83. if (getDocumentTitle().isEmpty())
  84. setTitle ("Juce Project");
  85. if (! projectRoot.hasProperty (Ids::projectType))
  86. getProjectTypeValue() = ProjectType::getGUIAppTypeName();
  87. if (! projectRoot.hasProperty (Ids::version))
  88. getVersion() = "1.0.0";
  89. // Create configs group
  90. if (! projectRoot.getChildWithName (Tags::configurations).isValid())
  91. {
  92. projectRoot.addChild (ValueTree (Tags::configurations), 0, 0);
  93. createDefaultConfigs();
  94. }
  95. if (! projectRoot.getChildWithName (Tags::exporters).isValid())
  96. createDefaultExporters();
  97. getProjectType().setMissingProjectProperties (*this);
  98. if (! projectRoot.hasProperty (Ids::bundleIdentifier))
  99. setBundleIdentifierToDefault();
  100. if (! projectRoot.getChildWithName (Tags::modulesGroup).isValid())
  101. addDefaultModules (false);
  102. }
  103. void Project::addDefaultModules (bool shouldCopyFilesLocally)
  104. {
  105. addModule ("juce_core", shouldCopyFilesLocally);
  106. if (! isConfigFlagEnabled ("JUCE_ONLY_BUILD_CORE_LIBRARY"))
  107. {
  108. addModule ("juce_events", shouldCopyFilesLocally);
  109. addModule ("juce_graphics", shouldCopyFilesLocally);
  110. addModule ("juce_data_structures", shouldCopyFilesLocally);
  111. addModule ("juce_gui_basics", shouldCopyFilesLocally);
  112. addModule ("juce_gui_extra", shouldCopyFilesLocally);
  113. addModule ("juce_gui_audio", shouldCopyFilesLocally);
  114. addModule ("juce_cryptography", shouldCopyFilesLocally);
  115. addModule ("juce_video", shouldCopyFilesLocally);
  116. addModule ("juce_opengl", shouldCopyFilesLocally);
  117. addModule ("juce_audio_basics", shouldCopyFilesLocally);
  118. addModule ("juce_audio_devices", shouldCopyFilesLocally);
  119. addModule ("juce_audio_formats", shouldCopyFilesLocally);
  120. addModule ("juce_audio_processors", shouldCopyFilesLocally);
  121. }
  122. }
  123. //==============================================================================
  124. const String Project::loadDocument (const File& file)
  125. {
  126. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  127. if (xml == nullptr || ! xml->hasTagName (Tags::projectRoot.toString()))
  128. return "Not a valid Jucer project!";
  129. ValueTree newTree (ValueTree::fromXml (*xml));
  130. if (! newTree.hasType (Tags::projectRoot))
  131. return "The document contains errors and couldn't be parsed!";
  132. StoredSettings::getInstance()->recentFiles.addFile (file);
  133. StoredSettings::getInstance()->flush();
  134. projectRoot = newTree;
  135. setMissingDefaultValues();
  136. return String::empty;
  137. }
  138. const String Project::saveDocument (const File& file)
  139. {
  140. updateProjectSettings();
  141. sanitiseConfigFlags();
  142. StoredSettings::getInstance()->recentFiles.addFile (file);
  143. ProjectSaver saver (*this, file);
  144. return saver.save();
  145. }
  146. //==============================================================================
  147. File Project::lastDocumentOpened;
  148. const File Project::getLastDocumentOpened()
  149. {
  150. return lastDocumentOpened;
  151. }
  152. void Project::setLastDocumentOpened (const File& file)
  153. {
  154. lastDocumentOpened = file;
  155. }
  156. //==============================================================================
  157. void Project::valueTreePropertyChanged (ValueTree& tree, const Identifier& property)
  158. {
  159. if (property == Ids::projectType)
  160. setMissingDefaultValues();
  161. changed();
  162. }
  163. void Project::valueTreeChildAdded (ValueTree& parentTree, ValueTree& childWhichHasBeenAdded)
  164. {
  165. changed();
  166. }
  167. void Project::valueTreeChildRemoved (ValueTree& parentTree, ValueTree& childWhichHasBeenRemoved)
  168. {
  169. changed();
  170. }
  171. void Project::valueTreeChildOrderChanged (ValueTree& parentTree)
  172. {
  173. changed();
  174. }
  175. void Project::valueTreeParentChanged (ValueTree& tree)
  176. {
  177. }
  178. //==============================================================================
  179. File Project::resolveFilename (String filename) const
  180. {
  181. if (filename.isEmpty())
  182. return File::nonexistent;
  183. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename)
  184. .replaceCharacter ('\\', '/');
  185. if (File::isAbsolutePath (filename))
  186. return File (filename);
  187. return getFile().getSiblingFile (filename);
  188. }
  189. String Project::getRelativePathForFile (const File& file) const
  190. {
  191. String filename (file.getFullPathName());
  192. File relativePathBase (getFile().getParentDirectory());
  193. String p1 (relativePathBase.getFullPathName());
  194. String p2 (file.getFullPathName());
  195. while (p1.startsWithChar (File::separator))
  196. p1 = p1.substring (1);
  197. while (p2.startsWithChar (File::separator))
  198. p2 = p2.substring (1);
  199. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  200. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  201. {
  202. filename = file.getRelativePathFrom (relativePathBase);
  203. }
  204. return filename;
  205. }
  206. //==============================================================================
  207. const ProjectType& Project::getProjectType() const
  208. {
  209. const ProjectType* type = ProjectType::findType (getProjectTypeValue().toString());
  210. jassert (type != nullptr);
  211. if (type == nullptr)
  212. {
  213. type = ProjectType::findType (ProjectType::getGUIAppTypeName());
  214. jassert (type != nullptr);
  215. }
  216. return *type;
  217. }
  218. //==============================================================================
  219. void Project::createPropertyEditors (Array <PropertyComponent*>& props)
  220. {
  221. props.add (new TextPropertyComponent (getProjectName(), "Project Name", 256, false));
  222. props.getLast()->setTooltip ("The name of the project.");
  223. props.add (new TextPropertyComponent (getVersion(), "Project Version", 16, false));
  224. props.getLast()->setTooltip ("The project's version number, This should be in the format major.minor.point");
  225. {
  226. StringArray projectTypeNames;
  227. Array<var> projectTypeCodes;
  228. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  229. for (int i = 0; i < types.size(); ++i)
  230. {
  231. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  232. projectTypeCodes.add (types.getUnchecked(i)->getType());
  233. }
  234. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  235. }
  236. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false));
  237. props.getLast()->setTooltip ("A unique identifier for this product, mainly for use in Mac builds. It should be something like 'com.yourcompanyname.yourproductname'");
  238. {
  239. OwnedArray<Project::Item> images;
  240. findAllImageItems (images);
  241. StringArray choices;
  242. Array<var> ids;
  243. choices.add ("<None>");
  244. ids.add (var::null);
  245. choices.add (String::empty);
  246. ids.add (var::null);
  247. for (int i = 0; i < images.size(); ++i)
  248. {
  249. choices.add (images.getUnchecked(i)->getName().toString());
  250. ids.add (images.getUnchecked(i)->getID());
  251. }
  252. props.add (new ChoicePropertyComponent (getSmallIconImageItemID(), "Icon (small)", choices, ids));
  253. props.getLast()->setTooltip ("Sets an icon to use for the executable.");
  254. props.add (new ChoicePropertyComponent (getBigIconImageItemID(), "Icon (large)", choices, ids));
  255. props.getLast()->setTooltip ("Sets an icon to use for the executable.");
  256. }
  257. getProjectType().createPropertyEditors(*this, props);
  258. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, false));
  259. props.getLast()->setTooltip ("Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace or commas to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  260. for (int i = props.size(); --i >= 0;)
  261. props.getUnchecked(i)->setPreferredHeight (22);
  262. }
  263. String Project::getVersionAsHex() const
  264. {
  265. StringArray configs;
  266. configs.addTokens (getVersion().toString(), ",.", String::empty);
  267. configs.trim();
  268. configs.removeEmptyStrings();
  269. int value = (configs[0].getIntValue() << 16) + (configs[1].getIntValue() << 8) + configs[2].getIntValue();
  270. if (configs.size() >= 4)
  271. value = (value << 8) + configs[3].getIntValue();
  272. return "0x" + String::toHexString (value);
  273. }
  274. Image Project::getBigIcon()
  275. {
  276. Item icon (getMainGroup().findItemWithID (getBigIconImageItemID().toString()));
  277. if (icon.isValid())
  278. return ImageCache::getFromFile (icon.getFile());
  279. return Image::null;
  280. }
  281. Image Project::getSmallIcon()
  282. {
  283. Item icon (getMainGroup().findItemWithID (getSmallIconImageItemID().toString()));
  284. if (icon.isValid())
  285. return ImageCache::getFromFile (icon.getFile());
  286. return Image::null;
  287. }
  288. StringPairArray Project::getPreprocessorDefs() const
  289. {
  290. return parsePreprocessorDefs (getProjectPreprocessorDefs().toString());
  291. }
  292. //==============================================================================
  293. Project::Item Project::getMainGroup()
  294. {
  295. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  296. }
  297. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  298. {
  299. if (item.isImageFile())
  300. {
  301. found.add (new Project::Item (item));
  302. }
  303. else if (item.isGroup())
  304. {
  305. for (int i = 0; i < item.getNumChildren(); ++i)
  306. findImages (item.getChild (i), found);
  307. }
  308. }
  309. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  310. {
  311. findImages (getMainGroup(), items);
  312. }
  313. //==============================================================================
  314. Project::Item::Item (Project& project_, const ValueTree& node_)
  315. : project (&project_), node (node_)
  316. {
  317. }
  318. Project::Item::Item (const Item& other)
  319. : project (other.project), node (other.node)
  320. {
  321. }
  322. Project::Item& Project::Item::operator= (const Project::Item& other)
  323. {
  324. project = other.project;
  325. node = other.node;
  326. return *this;
  327. }
  328. Project::Item::~Item()
  329. {
  330. }
  331. Project::Item Project::Item::createCopy() { Item i (*this); i.node = i.node.createCopy(); return i; }
  332. String Project::Item::getID() const { return node [Ids::id_]; }
  333. void Project::Item::setID (const String& newID) { node.setProperty (Ids::id_, newID, nullptr); }
  334. String Project::Item::getImageFileID() const { return "id:" + getID(); }
  335. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid)
  336. {
  337. Item group (project, ValueTree (Tags::group));
  338. group.setID (uid);
  339. group.initialiseNodeValues();
  340. group.getName() = name;
  341. return group;
  342. }
  343. bool Project::Item::isFile() const { return node.hasType (Tags::file); }
  344. bool Project::Item::isGroup() const { return node.hasType (Tags::group) || isMainGroup(); }
  345. bool Project::Item::isMainGroup() const { return node.hasType (Tags::projectMainGroup); }
  346. bool Project::Item::isImageFile() const { return isFile() && getFile().hasFileExtension ("png;jpg;jpeg;gif;drawable"); }
  347. Project::Item Project::Item::findItemWithID (const String& targetId) const
  348. {
  349. if (node [Ids::id_] == targetId)
  350. return *this;
  351. if (isGroup())
  352. {
  353. for (int i = getNumChildren(); --i >= 0;)
  354. {
  355. Item found (getChild(i).findItemWithID (targetId));
  356. if (found.isValid())
  357. return found;
  358. }
  359. }
  360. return Item (*project, ValueTree::invalid);
  361. }
  362. bool Project::Item::canContain (const Item& child) const
  363. {
  364. if (isFile())
  365. return false;
  366. if (isGroup())
  367. return child.isFile() || child.isGroup();
  368. jassertfalse
  369. return false;
  370. }
  371. bool Project::Item::shouldBeAddedToTargetProject() const
  372. {
  373. return isFile();
  374. }
  375. bool Project::Item::shouldBeCompiled() const { return getShouldCompileValue().getValue(); }
  376. Value Project::Item::getShouldCompileValue() const { return node.getPropertyAsValue (Ids::compile, getUndoManager()); }
  377. bool Project::Item::shouldBeAddedToBinaryResources() const { return getShouldAddToResourceValue().getValue(); }
  378. Value Project::Item::getShouldAddToResourceValue() const { return node.getPropertyAsValue (Ids::resource, getUndoManager()); }
  379. Value Project::Item::getShouldInhibitWarningsValue() const { return node.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  380. Value Project::Item::getShouldUseStdCallValue() const { return node.getPropertyAsValue (Ids::useStdCall, nullptr); }
  381. String Project::Item::getFilePath() const
  382. {
  383. if (isFile())
  384. return node [Ids::file].toString();
  385. else
  386. return String::empty;
  387. }
  388. File Project::Item::getFile() const
  389. {
  390. if (isFile())
  391. return getProject().resolveFilename (node [Ids::file].toString());
  392. else
  393. return File::nonexistent;
  394. }
  395. void Project::Item::setFile (const File& file)
  396. {
  397. setFile (RelativePath (getProject().getRelativePathForFile (file), RelativePath::projectFolder));
  398. jassert (getFile() == file);
  399. }
  400. void Project::Item::setFile (const RelativePath& file)
  401. {
  402. jassert (file.getRoot() == RelativePath::projectFolder);
  403. jassert (isFile());
  404. node.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  405. node.setProperty (Ids::name, file.getFileName(), getUndoManager());
  406. }
  407. bool Project::Item::renameFile (const File& newFile)
  408. {
  409. const File oldFile (getFile());
  410. if (oldFile.moveFileTo (newFile))
  411. {
  412. setFile (newFile);
  413. OpenDocumentManager::getInstance()->fileHasBeenRenamed (oldFile, newFile);
  414. return true;
  415. }
  416. return false;
  417. }
  418. bool Project::Item::containsChildForFile (const RelativePath& file) const
  419. {
  420. return node.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  421. }
  422. Project::Item Project::Item::findItemForFile (const File& file) const
  423. {
  424. if (getFile() == file)
  425. return *this;
  426. if (isGroup())
  427. {
  428. for (int i = getNumChildren(); --i >= 0;)
  429. {
  430. Item found (getChild(i).findItemForFile (file));
  431. if (found.isValid())
  432. return found;
  433. }
  434. }
  435. return Item (getProject(), ValueTree::invalid);
  436. }
  437. File Project::Item::determineGroupFolder() const
  438. {
  439. jassert (isGroup());
  440. File f;
  441. for (int i = 0; i < getNumChildren(); ++i)
  442. {
  443. f = getChild(i).getFile();
  444. if (f.exists())
  445. return f.getParentDirectory();
  446. }
  447. Item parent (getParent());
  448. if (parent != *this)
  449. {
  450. f = parent.determineGroupFolder();
  451. if (f.getChildFile (getName().toString()).isDirectory())
  452. f = f.getChildFile (getName().toString());
  453. }
  454. else
  455. {
  456. f = getProject().getFile().getParentDirectory();
  457. if (f.getChildFile ("Source").isDirectory())
  458. f = f.getChildFile ("Source");
  459. }
  460. return f;
  461. }
  462. void Project::Item::initialiseNodeValues()
  463. {
  464. if (! node.hasProperty (Ids::id_))
  465. setID (createAlphaNumericUID());
  466. if (isFile())
  467. {
  468. node.setProperty (Ids::name, getFile().getFileName(), 0);
  469. }
  470. else if (isGroup())
  471. {
  472. for (int i = getNumChildren(); --i >= 0;)
  473. getChild(i).initialiseNodeValues();
  474. }
  475. }
  476. Value Project::Item::getName() const
  477. {
  478. return node.getPropertyAsValue (Ids::name, getUndoManager());
  479. }
  480. void Project::Item::addChild (const Item& newChild, int insertIndex)
  481. {
  482. node.addChild (newChild.getNode(), insertIndex, getUndoManager());
  483. }
  484. void Project::Item::removeItemFromProject()
  485. {
  486. node.getParent().removeChild (node, getUndoManager());
  487. }
  488. Project::Item Project::Item::getParent() const
  489. {
  490. if (isMainGroup() || ! isGroup())
  491. return *this;
  492. return Item (getProject(), node.getParent());
  493. }
  494. struct ItemSorter
  495. {
  496. static int compareElements (const ValueTree& first, const ValueTree& second)
  497. {
  498. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  499. }
  500. };
  501. struct ItemSorterWithGroupsAtStart
  502. {
  503. static int compareElements (const ValueTree& first, const ValueTree& second)
  504. {
  505. const bool firstIsGroup = first.hasType (Tags::group);
  506. const bool secondIsGroup = second.hasType (Tags::group);
  507. if (firstIsGroup == secondIsGroup)
  508. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  509. else
  510. return firstIsGroup ? -1 : 1;
  511. }
  512. };
  513. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  514. {
  515. if (keepGroupsAtStart)
  516. {
  517. ItemSorterWithGroupsAtStart sorter;
  518. node.sort (sorter, getUndoManager(), true);
  519. }
  520. else
  521. {
  522. ItemSorter sorter;
  523. node.sort (sorter, getUndoManager(), true);
  524. }
  525. }
  526. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  527. {
  528. for (int i = node.getNumChildren(); --i >= 0;)
  529. {
  530. const ValueTree child (node.getChild (i));
  531. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  532. return Item (getProject(), child);
  533. }
  534. return addNewSubGroup (name, -1);
  535. }
  536. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  537. {
  538. Item group (createGroup (getProject(), name, createGUID (getID() + name + String (getNumChildren()))));
  539. jassert (canContain (group));
  540. addChild (group, insertIndex);
  541. return group;
  542. }
  543. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  544. {
  545. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  546. return false;
  547. if (file.isDirectory())
  548. {
  549. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  550. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  551. while (iter.next())
  552. {
  553. if (! getProject().getMainGroup().findItemForFile (iter.getFile()).isValid())
  554. group.addFile (iter.getFile(), -1, shouldCompile);
  555. }
  556. group.sortAlphabetically (false);
  557. }
  558. else if (file.existsAsFile())
  559. {
  560. if (! getProject().getMainGroup().findItemForFile (file).isValid())
  561. addFileUnchecked (file, insertIndex, shouldCompile);
  562. }
  563. else
  564. {
  565. jassertfalse;
  566. }
  567. return true;
  568. }
  569. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  570. {
  571. Item item (getProject(), ValueTree (Tags::file));
  572. item.initialiseNodeValues();
  573. item.getName() = file.getFileName();
  574. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  575. item.getShouldAddToResourceValue() = getProject().shouldBeAddedToBinaryResourcesByDefault (file);
  576. if (canContain (item))
  577. {
  578. item.setFile (file);
  579. addChild (item, insertIndex);
  580. }
  581. }
  582. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  583. {
  584. Item item (getProject(), ValueTree (Tags::file));
  585. item.initialiseNodeValues();
  586. item.getName() = file.getFileName();
  587. item.getShouldCompileValue() = shouldCompile;
  588. item.getShouldAddToResourceValue() = getProject().shouldBeAddedToBinaryResourcesByDefault (file);
  589. if (canContain (item))
  590. {
  591. item.setFile (file);
  592. addChild (item, insertIndex);
  593. return true;
  594. }
  595. return false;
  596. }
  597. const Drawable* Project::Item::getIcon() const
  598. {
  599. if (isFile())
  600. {
  601. if (isImageFile())
  602. return StoredSettings::getInstance()->getImageFileIcon();
  603. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  604. }
  605. else if (isMainGroup())
  606. {
  607. return &(getProject().mainProjectIcon);
  608. }
  609. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  610. }
  611. //==============================================================================
  612. ValueTree Project::getConfigNode()
  613. {
  614. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  615. }
  616. const char* const Project::configFlagDefault = "default";
  617. const char* const Project::configFlagEnabled = "enabled";
  618. const char* const Project::configFlagDisabled = "disabled";
  619. Value Project::getConfigFlag (const String& name)
  620. {
  621. const ValueTree configNode (getConfigNode());
  622. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  623. if (v.getValue().toString().isEmpty())
  624. v = configFlagDefault;
  625. return v;
  626. }
  627. bool Project::isConfigFlagEnabled (const String& name) const
  628. {
  629. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  630. }
  631. void Project::sanitiseConfigFlags()
  632. {
  633. ValueTree configNode (getConfigNode());
  634. for (int i = configNode.getNumProperties(); --i >= 0;)
  635. {
  636. const var value (configNode [configNode.getPropertyName(i)]);
  637. if (value != configFlagEnabled && value != configFlagDisabled)
  638. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  639. }
  640. }
  641. //==============================================================================
  642. ValueTree Project::getModulesNode()
  643. {
  644. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  645. }
  646. bool Project::isModuleEnabled (const String& moduleID) const
  647. {
  648. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  649. for (int i = 0; i < modules.getNumChildren(); ++i)
  650. if (modules.getChild(i) [Ids::id_] == moduleID)
  651. return true;
  652. return false;
  653. }
  654. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  655. {
  656. return getModulesNode().getChildWithProperty (Ids::id_, moduleID)
  657. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  658. }
  659. Value Project::shouldCopyModuleFilesLocally (const String& moduleID)
  660. {
  661. return getModulesNode().getChildWithProperty (Ids::id_, moduleID)
  662. .getPropertyAsValue (Ids::useLocalCopy, getUndoManagerFor (getModulesNode()));
  663. }
  664. void Project::addModule (const String& moduleID, bool shouldCopyFilesLocally)
  665. {
  666. if (! isModuleEnabled (moduleID))
  667. {
  668. ValueTree module (Tags::module);
  669. module.setProperty (Ids::id_, moduleID, nullptr);
  670. ValueTree modules (getModulesNode());
  671. modules.addChild (module, -1, getUndoManagerFor (modules));
  672. shouldShowAllModuleFilesInProject (moduleID) = true;
  673. }
  674. if (shouldCopyFilesLocally)
  675. shouldCopyModuleFilesLocally (moduleID) = true;
  676. }
  677. void Project::removeModule (const String& moduleID)
  678. {
  679. ValueTree modules (getModulesNode());
  680. for (int i = 0; i < modules.getNumChildren(); ++i)
  681. if (modules.getChild(i) [Ids::id_] == moduleID)
  682. modules.removeChild (i, getUndoManagerFor (modules));
  683. }
  684. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  685. {
  686. for (int i = 0; i < availableModules.modules.size(); ++i)
  687. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  688. modules.add (availableModules.modules.getUnchecked(i)->create());
  689. }
  690. int Project::getNumModules() const
  691. {
  692. return projectRoot.getChildWithName (Tags::modulesGroup).getNumChildren();
  693. }
  694. String Project::getModuleID (int index) const
  695. {
  696. return projectRoot.getChildWithName (Tags::modulesGroup).getChild (index) [Ids::id_].toString();
  697. }
  698. //==============================================================================
  699. ValueTree Project::getConfigurations() const
  700. {
  701. return projectRoot.getChildWithName (Tags::configurations);
  702. }
  703. int Project::getNumConfigurations() const
  704. {
  705. return getConfigurations().getNumChildren();
  706. }
  707. Project::BuildConfiguration Project::getConfiguration (int index)
  708. {
  709. jassert (index < getConfigurations().getNumChildren());
  710. return BuildConfiguration (this, getConfigurations().getChild (index));
  711. }
  712. bool Project::hasConfigurationNamed (const String& name) const
  713. {
  714. const ValueTree configs (getConfigurations());
  715. for (int i = configs.getNumChildren(); --i >= 0;)
  716. if (configs.getChild(i) [Ids::name].toString() == name)
  717. return true;
  718. return false;
  719. }
  720. String Project::getUniqueConfigName (String name) const
  721. {
  722. String nameRoot (name);
  723. while (CharacterFunctions::isDigit (nameRoot.getLastCharacter()))
  724. nameRoot = nameRoot.dropLastCharacters (1);
  725. nameRoot = nameRoot.trim();
  726. int suffix = 2;
  727. while (hasConfigurationNamed (name))
  728. name = nameRoot + " " + String (suffix++);
  729. return name;
  730. }
  731. void Project::addNewConfiguration (BuildConfiguration* configToCopy)
  732. {
  733. const String configName (getUniqueConfigName (configToCopy != nullptr ? configToCopy->config [Ids::name].toString()
  734. : "New Build Configuration"));
  735. ValueTree configs (getConfigurations());
  736. if (! configs.isValid())
  737. {
  738. projectRoot.addChild (ValueTree (Tags::configurations), 0, getUndoManagerFor (projectRoot));
  739. configs = getConfigurations();
  740. }
  741. ValueTree newConfig (Tags::configuration);
  742. if (configToCopy != nullptr)
  743. newConfig = configToCopy->config.createCopy();
  744. newConfig.setProperty (Ids::name, configName, 0);
  745. configs.addChild (newConfig, -1, getUndoManagerFor (configs));
  746. }
  747. void Project::deleteConfiguration (int index)
  748. {
  749. ValueTree configs (getConfigurations());
  750. configs.removeChild (index, getUndoManagerFor (getConfigurations()));
  751. }
  752. void Project::createDefaultConfigs()
  753. {
  754. for (int i = 0; i < 2; ++i)
  755. {
  756. addNewConfiguration (nullptr);
  757. BuildConfiguration config = getConfiguration (i);
  758. const bool debugConfig = i == 0;
  759. config.getName() = debugConfig ? "Debug" : "Release";
  760. config.isDebug() = debugConfig;
  761. config.getOptimisationLevel() = debugConfig ? 1 : 2;
  762. config.getTargetBinaryName() = getProjectFilenameRoot();
  763. }
  764. }
  765. //==============================================================================
  766. Project::BuildConfiguration::BuildConfiguration (Project* project_, const ValueTree& configNode)
  767. : project (project_),
  768. config (configNode)
  769. {
  770. }
  771. Project::BuildConfiguration::BuildConfiguration (const BuildConfiguration& other)
  772. : project (other.project),
  773. config (other.config)
  774. {
  775. }
  776. const Project::BuildConfiguration& Project::BuildConfiguration::operator= (const BuildConfiguration& other)
  777. {
  778. project = other.project;
  779. config = other.config;
  780. return *this;
  781. }
  782. Project::BuildConfiguration::~BuildConfiguration()
  783. {
  784. }
  785. String Project::BuildConfiguration::getGCCOptimisationFlag() const
  786. {
  787. const int level = (int) getOptimisationLevel().getValue();
  788. return String (level <= 1 ? "0" : (level == 2 ? "s" : "3"));
  789. }
  790. const char* const Project::BuildConfiguration::osxVersionDefault = "default";
  791. const char* const Project::BuildConfiguration::osxVersion10_4 = "10.4 SDK";
  792. const char* const Project::BuildConfiguration::osxVersion10_5 = "10.5 SDK";
  793. const char* const Project::BuildConfiguration::osxVersion10_6 = "10.6 SDK";
  794. const char* const Project::BuildConfiguration::osxArch_Default = "default";
  795. const char* const Project::BuildConfiguration::osxArch_Native = "Native";
  796. const char* const Project::BuildConfiguration::osxArch_32BitUniversal = "32BitUniversal";
  797. const char* const Project::BuildConfiguration::osxArch_64BitUniversal = "64BitUniversal";
  798. const char* const Project::BuildConfiguration::osxArch_64Bit = "64BitIntel";
  799. void Project::BuildConfiguration::createPropertyEditors (Array <PropertyComponent*>& props)
  800. {
  801. props.add (new TextPropertyComponent (getName(), "Name", 96, false));
  802. props.getLast()->setTooltip ("The name of this configuration.");
  803. props.add (new BooleanPropertyComponent (isDebug(), "Debug mode", "Debugging enabled"));
  804. props.getLast()->setTooltip ("If enabled, this means that the configuration should be built with debug synbols.");
  805. const char* optimisationLevels[] = { "No optimisation", "Optimise for size and speed", "Optimise for maximum speed", 0 };
  806. const int optimisationLevelValues[] = { 1, 2, 3, 0 };
  807. props.add (new ChoicePropertyComponent (getOptimisationLevel(), "Optimisation", StringArray (optimisationLevels), Array<var> (optimisationLevelValues)));
  808. props.getLast()->setTooltip ("The optimisation level for this configuration");
  809. props.add (new TextPropertyComponent (getTargetBinaryName(), "Binary name", 256, false));
  810. props.getLast()->setTooltip ("The filename to use for the destination binary executable file. Don't add a suffix to this, because platform-specific suffixes will be added for each target platform.");
  811. props.add (new TextPropertyComponent (getTargetBinaryRelativePath(), "Binary location", 1024, false));
  812. props.getLast()->setTooltip ("The folder in which the finished binary should be placed. Leave this blank to cause the binary to be placed in its default location in the build folder.");
  813. props.add (new TextPropertyComponent (getHeaderSearchPath(), "Header search path", 16384, false));
  814. props.getLast()->setTooltip ("Extra header search paths. Use semi-colons to separate multiple paths.");
  815. props.add (new TextPropertyComponent (getBuildConfigPreprocessorDefs(), "Preprocessor definitions", 32768, false));
  816. props.getLast()->setTooltip ("Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace or commas to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  817. if (getMacSDKVersion().toString().isEmpty())
  818. getMacSDKVersion() = osxVersionDefault;
  819. const char* osxVersions[] = { "Use Default", osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  820. const char* osxVersionValues[] = { osxVersionDefault, osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  821. props.add (new ChoicePropertyComponent (getMacSDKVersion(), "OSX Base SDK Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  822. props.getLast()->setTooltip ("The version of OSX to link against in the XCode build.");
  823. if (getMacCompatibilityVersion().toString().isEmpty())
  824. getMacCompatibilityVersion() = osxVersionDefault;
  825. props.add (new ChoicePropertyComponent (getMacCompatibilityVersion(), "OSX Compatibility Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  826. props.getLast()->setTooltip ("The minimum version of OSX that the target binary will be compatible with.");
  827. const char* osxArch[] = { "Use Default", "Native architecture of build machine", "Universal Binary (32-bit)", "Universal Binary (64-bit)", "64-bit Intel", 0 };
  828. const char* osxArchValues[] = { osxArch_Default, osxArch_Native, osxArch_32BitUniversal, osxArch_64BitUniversal, osxArch_64Bit, 0 };
  829. if (getMacArchitecture().toString().isEmpty())
  830. getMacArchitecture() = osxArch_Default;
  831. props.add (new ChoicePropertyComponent (getMacArchitecture(), "OSX Architecture", StringArray (osxArch), Array<var> (osxArchValues)));
  832. props.getLast()->setTooltip ("The type of OSX binary that will be produced.");
  833. for (int i = props.size(); --i >= 0;)
  834. props.getUnchecked(i)->setPreferredHeight (22);
  835. }
  836. StringPairArray Project::BuildConfiguration::getAllPreprocessorDefs() const
  837. {
  838. return mergePreprocessorDefs (project->getPreprocessorDefs(),
  839. parsePreprocessorDefs (getBuildConfigPreprocessorDefs().toString()));
  840. }
  841. StringArray Project::BuildConfiguration::getHeaderSearchPaths() const
  842. {
  843. StringArray s;
  844. s.addTokens (getHeaderSearchPath().toString(), ";", String::empty);
  845. return s;
  846. }
  847. //==============================================================================
  848. ValueTree Project::getExporters()
  849. {
  850. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  851. if (! exporters.isValid())
  852. {
  853. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  854. exporters = getExporters();
  855. }
  856. return exporters;
  857. }
  858. int Project::getNumExporters()
  859. {
  860. return getExporters().getNumChildren();
  861. }
  862. ProjectExporter* Project::createExporter (int index)
  863. {
  864. jassert (index >= 0 && index < getNumExporters());
  865. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  866. }
  867. void Project::addNewExporter (const String& exporterName)
  868. {
  869. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  870. ValueTree exporters (getExporters());
  871. exporters.addChild (exp->getSettings(), -1, getUndoManagerFor (exporters));
  872. }
  873. void Project::deleteExporter (int index)
  874. {
  875. ValueTree exporters (getExporters());
  876. exporters.removeChild (index, getUndoManagerFor (exporters));
  877. }
  878. void Project::createDefaultExporters()
  879. {
  880. ValueTree exporters (getExporters());
  881. exporters.removeAllChildren (getUndoManagerFor (exporters));
  882. const StringArray exporterNames (ProjectExporter::getDefaultExporters());
  883. for (int i = 0; i < exporterNames.size(); ++i)
  884. addNewExporter (exporterNames[i]);
  885. }
  886. //==============================================================================
  887. String Project::getFileTemplate (const String& templateName)
  888. {
  889. int dataSize;
  890. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  891. if (data == nullptr)
  892. {
  893. jassertfalse;
  894. return String::empty;
  895. }
  896. return String::fromUTF8 (data, dataSize);
  897. }