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.

222 lines
7.3KB

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