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.

264 lines
8.5KB

  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 "../Project/jucer_Project.h"
  19. #include "../Project/jucer_Module.h"
  20. #include "jucer_CommandLine.h"
  21. //==============================================================================
  22. namespace
  23. {
  24. File getFile (const String& filename)
  25. {
  26. return File::getCurrentWorkingDirectory().getChildFile (filename.unquoted());
  27. }
  28. bool checkArgumentCount (const StringArray& tokens, int minNumArgs)
  29. {
  30. if (tokens.size() < minNumArgs)
  31. {
  32. std::cout << "Not enough arguments!" << std::endl;
  33. return false;
  34. }
  35. return true;
  36. }
  37. //==============================================================================
  38. /* Running a command-line of the form "introjucer --resave foobar.jucer" will try to load
  39. that project and re-export all of its targets.
  40. */
  41. int resaveProject (const File& file)
  42. {
  43. if (! file.exists())
  44. {
  45. std::cout << "The file " << file.getFullPathName() << " doesn't exist!" << std::endl;
  46. return 1;
  47. }
  48. if (! file.hasFileExtension (Project::projectFileExtension))
  49. {
  50. std::cout << file.getFullPathName() << " isn't a valid jucer project file!" << std::endl;
  51. return 1;
  52. }
  53. Project newDoc (file);
  54. if (! newDoc.loadFrom (file, true))
  55. {
  56. std::cout << "Failed to load the project file: " << file.getFullPathName() << std::endl;
  57. return 1;
  58. }
  59. std::cout << "The Introjucer - Re-saving file: " << file.getFullPathName() << std::endl;
  60. String error (newDoc.saveDocument (file));
  61. if (error.isNotEmpty())
  62. {
  63. std::cout << "Error when writing project: " << error << std::endl;
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. //==============================================================================
  69. String getModulePackageName (const LibraryModule& module)
  70. {
  71. return module.getID() + ".jucemodule";
  72. }
  73. int zipModule (const File& targetFolder, const File& moduleFolder)
  74. {
  75. jassert (targetFolder.isDirectory());
  76. const File moduleFolderParent (moduleFolder.getParentDirectory());
  77. LibraryModule module (moduleFolder.getChildFile (LibraryModule::getInfoFileName()));
  78. if (! module.isValid())
  79. {
  80. std::cout << moduleFolder.getFullPathName() << " is not a valid module folder!" << std::endl;
  81. return 1;
  82. }
  83. const File targetFile (targetFolder.getChildFile (getModulePackageName (module)));
  84. ZipFile::Builder zip;
  85. {
  86. DirectoryIterator i (moduleFolder, true, "*", File::findFiles);
  87. while (i.next())
  88. if (! i.getFile().isHidden())
  89. zip.addFile (i.getFile(), 9, i.getFile().getRelativePathFrom (moduleFolderParent));
  90. }
  91. std::cout << "Writing: " << targetFile.getFullPathName() << std::endl;
  92. TemporaryFile temp (targetFile);
  93. ScopedPointer<FileOutputStream> out (temp.getFile().createOutputStream());
  94. bool ok = out != nullptr && zip.writeToStream (*out);
  95. out = nullptr;
  96. ok = ok && temp.overwriteTargetFileWithTemporary();
  97. if (! ok)
  98. {
  99. std::cout << "Failed to write to the target file: " << targetFile.getFullPathName() << std::endl;
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. int buildModules (const StringArray& tokens, const bool buildAllWithIndex)
  105. {
  106. if (! checkArgumentCount (tokens, 3))
  107. return 1;
  108. const File targetFolder (getFile (tokens[1]));
  109. if (! targetFolder.isDirectory())
  110. {
  111. std::cout << "The first argument must be the directory to put the result." << std::endl;
  112. return 1;
  113. }
  114. if (buildAllWithIndex)
  115. {
  116. const File folderToSearch (getFile (tokens[2]));
  117. DirectoryIterator i (folderToSearch, false, "*", File::findDirectories);
  118. var infoList;
  119. while (i.next())
  120. {
  121. LibraryModule module (i.getFile().getChildFile (LibraryModule::getInfoFileName()));
  122. if (module.isValid())
  123. {
  124. const int result = zipModule (targetFolder, i.getFile());
  125. if (result != 0)
  126. return result;
  127. var moduleInfo (new DynamicObject());
  128. moduleInfo.getDynamicObject()->setProperty ("file", getModulePackageName (module));
  129. moduleInfo.getDynamicObject()->setProperty ("info", module.moduleInfo);
  130. infoList.append (moduleInfo);
  131. }
  132. }
  133. const File indexFile (targetFolder.getChildFile ("modulelist"));
  134. std::cout << "Writing: " << indexFile.getFullPathName() << std::endl;
  135. indexFile.replaceWithText (JSON::toString (infoList), false, false);
  136. }
  137. else
  138. {
  139. for (int i = 2; i < tokens.size(); ++i)
  140. {
  141. const int result = zipModule (targetFolder, getFile (tokens[i]));
  142. if (result != 0)
  143. return result;
  144. }
  145. }
  146. return 0;
  147. }
  148. int listModules()
  149. {
  150. std::cout << "Downloading list of available modules..." << std::endl;
  151. ModuleList list;
  152. list.loadFromWebsite();
  153. for (int i = 0; i < list.modules.size(); ++i)
  154. {
  155. ModuleList::Module* m = list.modules.getUnchecked(i);
  156. std::cout << m->uid << ": " << m->version << std::endl;
  157. }
  158. return 0;
  159. }
  160. int showStatus (const StringArray& tokens)
  161. {
  162. if (! checkArgumentCount (tokens, 2))
  163. return 1;
  164. const File projectFile (getFile (tokens[1]));
  165. Project proj (projectFile);
  166. if (proj.loadDocument (projectFile).isNotEmpty())
  167. {
  168. std::cout << "Failed to load project: " << projectFile.getFullPathName() << std::endl;
  169. return 1;
  170. }
  171. std::cout << "Project file: " << projectFile.getFullPathName() << std::endl
  172. << "Name: " << proj.getProjectName().toString() << std::endl
  173. << "UID: " << proj.getProjectUID() << std::endl;
  174. const int numModules = proj.getNumModules();
  175. if (numModules > 0)
  176. {
  177. std::cout << "Modules:" << std::endl;
  178. for (int i = 0; i < numModules; ++i)
  179. std::cout << " " << proj.getModuleID (i) << std::endl;
  180. }
  181. return 0;
  182. }
  183. }
  184. //==============================================================================
  185. int performCommandLine (const String& commandLine)
  186. {
  187. StringArray tokens;
  188. tokens.addTokens (commandLine, true);
  189. tokens.trim();
  190. if (tokens[0] == "-resave" || tokens[0] == "--resave" || tokens[0] == "resave")
  191. return resaveProject (getFile (tokens[1]));
  192. if (tokens[0] == "buildmodule")
  193. return buildModules (tokens, false);
  194. if (tokens[0] == "buildallmodules")
  195. return buildModules (tokens, true);
  196. if (tokens[0] == "listmodules")
  197. return listModules();
  198. if (tokens[0] == "status")
  199. return showStatus (tokens);
  200. return commandLineNotPerformed;
  201. }