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.

905 lines
37KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #include "jucer_Headers.h"
  14. #include "jucer_Application.h"
  15. #include "../Utility/Helpers/jucer_TranslationHelpers.h"
  16. #include "jucer_CommandLine.h"
  17. //==============================================================================
  18. const char* preferredLineFeed = "\r\n";
  19. const char* getPreferredLineFeed() { return preferredLineFeed; }
  20. //==============================================================================
  21. namespace
  22. {
  23. static void hideDockIcon()
  24. {
  25. #if JUCE_MAC
  26. Process::setDockIconVisible (false);
  27. #endif
  28. }
  29. static Array<File> findAllSourceFiles (const File& folder)
  30. {
  31. Array<File> files;
  32. for (const auto& di : RangedDirectoryIterator (folder, true, "*.cpp;*.cxx;*.cc;*.c;*.h;*.hpp;*.hxx;*.hpp;*.mm;*.m;*.java;*.dox;*.soul;*.js", File::findFiles))
  33. if (! di.getFile().isSymbolicLink())
  34. files.add (di.getFile());
  35. return files;
  36. }
  37. static void replaceFile (const File& file, const String& newText, const String& message)
  38. {
  39. std::cout << message << file.getFullPathName() << std::endl;
  40. TemporaryFile temp (file);
  41. if (! temp.getFile().replaceWithText (newText, false, false, nullptr))
  42. ConsoleApplication::fail ("!!! ERROR Couldn't write to temp file!");
  43. if (! temp.overwriteTargetFileWithTemporary())
  44. ConsoleApplication::fail ("!!! ERROR Couldn't write to file!");
  45. }
  46. //==============================================================================
  47. struct LoadedProject
  48. {
  49. LoadedProject (const ArgumentList::Argument& fileToLoad)
  50. {
  51. hideDockIcon();
  52. auto projectFile = fileToLoad.resolveAsExistingFile();
  53. if (! projectFile.hasFileExtension (Project::projectFileExtension))
  54. ConsoleApplication::fail (projectFile.getFullPathName() + " isn't a valid jucer project file!");
  55. project.reset (new Project (projectFile));
  56. if (! project->loadFrom (projectFile, true, false))
  57. {
  58. project.reset();
  59. ConsoleApplication::fail ("Failed to load the project file: " + projectFile.getFullPathName());
  60. }
  61. preferredLineFeed = project->getProjectLineFeed().toRawUTF8();
  62. }
  63. void save (bool justSaveResources)
  64. {
  65. if (project != nullptr)
  66. {
  67. if (! justSaveResources)
  68. rescanModulePathsIfNecessary();
  69. auto error = justSaveResources ? project->saveResourcesOnly (project->getFile())
  70. : project->saveProject (project->getFile(), true);
  71. project.reset();
  72. if (error.failed())
  73. ConsoleApplication::fail ("Error when saving: " + error.getErrorMessage());
  74. }
  75. }
  76. void rescanModulePathsIfNecessary()
  77. {
  78. bool scanJUCEPath = false, scanUserPaths = false;
  79. const auto& modules = project->getEnabledModules();
  80. for (auto i = modules.getNumModules(); --i >= 0;)
  81. {
  82. const auto& id = modules.getModuleID (i);
  83. if (isJUCEModule (id) && ! scanJUCEPath)
  84. {
  85. if (modules.shouldUseGlobalPath (id))
  86. scanJUCEPath = true;
  87. }
  88. else if (! scanUserPaths)
  89. {
  90. if (modules.shouldUseGlobalPath (id))
  91. scanUserPaths = true;
  92. }
  93. }
  94. if (scanJUCEPath)
  95. ProjucerApplication::getApp().rescanJUCEPathModules();
  96. if (scanUserPaths)
  97. ProjucerApplication::getApp().rescanUserPathModules();
  98. }
  99. std::unique_ptr<Project> project;
  100. };
  101. //==============================================================================
  102. /* Running a command-line of the form "projucer --resave foobar.jucer" will try to load
  103. that project and re-export all of its targets.
  104. */
  105. static void resaveProject (const ArgumentList& args, bool justSaveResources)
  106. {
  107. args.checkMinNumArguments (2);
  108. LoadedProject proj (args[1]);
  109. std::cout << (justSaveResources ? "Re-saving project resources: "
  110. : "Re-saving file: ")
  111. << proj.project->getFile().getFullPathName() << std::endl;
  112. proj.save (justSaveResources);
  113. }
  114. //==============================================================================
  115. static void getVersion (const ArgumentList& args)
  116. {
  117. args.checkMinNumArguments (2);
  118. LoadedProject proj (args[1]);
  119. std::cout << proj.project->getVersionString() << std::endl;
  120. }
  121. //==============================================================================
  122. static void setVersion (const ArgumentList& args)
  123. {
  124. args.checkMinNumArguments (2);
  125. LoadedProject proj (args[2]);
  126. String version (args[1].text.trim());
  127. std::cout << "Setting project version: " << version << std::endl;
  128. proj.project->setProjectVersion (version);
  129. proj.save (false);
  130. }
  131. //==============================================================================
  132. static void bumpVersion (const ArgumentList& args)
  133. {
  134. args.checkMinNumArguments (2);
  135. LoadedProject proj (args[1]);
  136. String version = proj.project->getVersionString();
  137. version = version.upToLastOccurrenceOf (".", true, false)
  138. + String (version.getTrailingIntValue() + 1);
  139. std::cout << "Bumping project version to: " << version << std::endl;
  140. proj.project->setProjectVersion (version);
  141. proj.save (false);
  142. }
  143. static void gitTag (const ArgumentList& args)
  144. {
  145. args.checkMinNumArguments (2);
  146. LoadedProject proj (args[1]);
  147. String version (proj.project->getVersionString());
  148. if (version.trim().isEmpty())
  149. ConsoleApplication::fail ("Cannot read version number from project!");
  150. StringArray command;
  151. command.add ("git");
  152. command.add ("tag");
  153. command.add ("-a");
  154. command.add (version);
  155. command.add ("-m");
  156. command.add (version.quoted());
  157. std::cout << "Performing command: " << command.joinIntoString(" ") << std::endl;
  158. ChildProcess c;
  159. if (! c.start (command, 0))
  160. ConsoleApplication::fail ("Cannot run git!");
  161. c.waitForProcessToFinish (10000);
  162. if (c.getExitCode() != 0)
  163. ConsoleApplication::fail ("git command failed!");
  164. }
  165. //==============================================================================
  166. static void showStatus (const ArgumentList& args)
  167. {
  168. args.checkMinNumArguments (2);
  169. LoadedProject proj (args[1]);
  170. std::cout << "Project file: " << proj.project->getFile().getFullPathName() << std::endl
  171. << "Name: " << proj.project->getProjectNameString() << std::endl
  172. << "UID: " << proj.project->getProjectUIDString() << std::endl;
  173. EnabledModuleList& modules = proj.project->getEnabledModules();
  174. if (int numModules = modules.getNumModules())
  175. {
  176. std::cout << "Modules:" << std::endl;
  177. for (int i = 0; i < numModules; ++i)
  178. std::cout << " " << modules.getModuleID (i) << std::endl;
  179. }
  180. }
  181. //==============================================================================
  182. static String getModulePackageName (const LibraryModule& module)
  183. {
  184. return module.getID() + ".jucemodule";
  185. }
  186. static void zipModule (const File& targetFolder, const File& moduleFolder)
  187. {
  188. jassert (targetFolder.isDirectory());
  189. auto moduleFolderParent = moduleFolder.getParentDirectory();
  190. LibraryModule module (moduleFolder);
  191. if (! module.isValid())
  192. ConsoleApplication::fail (moduleFolder.getFullPathName() + " is not a valid module folder!");
  193. auto targetFile = targetFolder.getChildFile (getModulePackageName (module));
  194. ZipFile::Builder zip;
  195. {
  196. for (const auto& i : RangedDirectoryIterator (moduleFolder, true, "*", File::findFiles))
  197. if (! i.getFile().isHidden())
  198. zip.addFile (i.getFile(), 9, i.getFile().getRelativePathFrom (moduleFolderParent));
  199. }
  200. std::cout << "Writing: " << targetFile.getFullPathName() << std::endl;
  201. TemporaryFile temp (targetFile);
  202. {
  203. FileOutputStream out (temp.getFile());
  204. if (! (out.openedOk() && zip.writeToStream (out, nullptr)))
  205. ConsoleApplication::fail ("Failed to write to the target file: " + targetFile.getFullPathName());
  206. }
  207. if (! temp.overwriteTargetFileWithTemporary())
  208. ConsoleApplication::fail ("Failed to write to the target file: " + targetFile.getFullPathName());
  209. }
  210. static void buildModules (const ArgumentList& args, const bool buildAllWithIndex)
  211. {
  212. hideDockIcon();
  213. args.checkMinNumArguments (3);
  214. auto targetFolder = args[1].resolveAsFile();
  215. if (! targetFolder.isDirectory())
  216. ConsoleApplication::fail ("The first argument must be the directory to put the result.");
  217. if (buildAllWithIndex)
  218. {
  219. auto folderToSearch = args[2].resolveAsFile();
  220. var infoList;
  221. for (const auto& i : RangedDirectoryIterator (folderToSearch, false, "*", File::findDirectories))
  222. {
  223. LibraryModule module (i.getFile());
  224. if (module.isValid())
  225. {
  226. zipModule (targetFolder, i.getFile());
  227. var moduleInfo (new DynamicObject());
  228. moduleInfo.getDynamicObject()->setProperty ("file", getModulePackageName (module));
  229. moduleInfo.getDynamicObject()->setProperty ("info", module.moduleInfo.moduleInfo);
  230. infoList.append (moduleInfo);
  231. }
  232. }
  233. auto indexFile = targetFolder.getChildFile ("modulelist");
  234. std::cout << "Writing: " << indexFile.getFullPathName() << std::endl;
  235. indexFile.replaceWithText (JSON::toString (infoList), false, false);
  236. }
  237. else
  238. {
  239. for (int i = 2; i < args.size(); ++i)
  240. zipModule (targetFolder, args[i].resolveAsFile());
  241. }
  242. }
  243. //==============================================================================
  244. struct CleanupOptions
  245. {
  246. bool removeTabs;
  247. bool fixDividerComments;
  248. };
  249. static void cleanWhitespace (const File& file, CleanupOptions options)
  250. {
  251. auto content = file.loadFileAsString();
  252. if (content.contains ("%""%") && content.contains ("//["))
  253. return; // ignore projucer GUI template files
  254. StringArray lines;
  255. lines.addLines (content);
  256. bool anyTabsRemoved = false;
  257. for (int i = 0; i < lines.size(); ++i)
  258. {
  259. String& line = lines.getReference (i);
  260. if (options.removeTabs && line.containsChar ('\t'))
  261. {
  262. anyTabsRemoved = true;
  263. for (;;)
  264. {
  265. const int tabPos = line.indexOfChar ('\t');
  266. if (tabPos < 0)
  267. break;
  268. const int spacesPerTab = 4;
  269. const int spacesNeeded = spacesPerTab - (tabPos % spacesPerTab);
  270. line = line.replaceSection (tabPos, 1, String::repeatedString (" ", spacesNeeded));
  271. }
  272. }
  273. if (options.fixDividerComments)
  274. {
  275. auto afterIndent = line.trim();
  276. if (afterIndent.startsWith ("//") && afterIndent.length() > 20)
  277. {
  278. afterIndent = afterIndent.substring (2);
  279. if (afterIndent.containsOnly ("=")
  280. || afterIndent.containsOnly ("/")
  281. || afterIndent.containsOnly ("-"))
  282. {
  283. line = line.substring (0, line.indexOfChar ('/'))
  284. + "//" + String::repeatedString ("=", 78);
  285. }
  286. }
  287. }
  288. line = line.trimEnd();
  289. }
  290. if (options.removeTabs && ! anyTabsRemoved)
  291. return;
  292. auto newText = joinLinesIntoSourceFile (lines);
  293. if (newText != content && newText != content + getPreferredLineFeed())
  294. replaceFile (file, newText, options.removeTabs ? "Removing tabs in: "
  295. : "Cleaning file: ");
  296. }
  297. static void scanFilesForCleanup (const ArgumentList& args, CleanupOptions options)
  298. {
  299. args.checkMinNumArguments (2);
  300. for (auto it = args.arguments.begin() + 1; it < args.arguments.end(); ++it)
  301. {
  302. auto target = it->resolveAsFile();
  303. Array<File> files;
  304. if (target.isDirectory())
  305. files = findAllSourceFiles (target);
  306. else
  307. files.add (target);
  308. for (int i = 0; i < files.size(); ++i)
  309. cleanWhitespace (files.getReference(i), options);
  310. }
  311. }
  312. static void cleanWhitespace (const ArgumentList& args, bool replaceTabs)
  313. {
  314. CleanupOptions options = { replaceTabs, false };
  315. scanFilesForCleanup (args, options);
  316. }
  317. static void tidyDividerComments (const ArgumentList& args)
  318. {
  319. CleanupOptions options = { false, true };
  320. scanFilesForCleanup (args, options);
  321. }
  322. //==============================================================================
  323. static File findSimilarlyNamedHeader (const Array<File>& allFiles, const String& name, const File& sourceFile)
  324. {
  325. File result;
  326. for (auto& f : allFiles)
  327. {
  328. if (f.getFileName().equalsIgnoreCase (name) && f != sourceFile)
  329. {
  330. if (result.exists())
  331. return {}; // multiple possible results, so don't change it!
  332. result = f;
  333. }
  334. }
  335. return result;
  336. }
  337. static void fixIncludes (const File& file, const Array<File>& allFiles)
  338. {
  339. const String content (file.loadFileAsString());
  340. StringArray lines;
  341. lines.addLines (content);
  342. bool hasChanged = false;
  343. for (auto& line : lines)
  344. {
  345. if (line.trimStart().startsWith ("#include \""))
  346. {
  347. auto includedFile = line.fromFirstOccurrenceOf ("\"", true, false)
  348. .upToLastOccurrenceOf ("\"", true, false)
  349. .trim()
  350. .unquoted();
  351. auto target = file.getSiblingFile (includedFile);
  352. if (! target.exists())
  353. {
  354. auto header = findSimilarlyNamedHeader (allFiles, target.getFileName(), file);
  355. if (header.exists())
  356. {
  357. line = line.upToFirstOccurrenceOf ("#include \"", true, false)
  358. + header.getRelativePathFrom (file.getParentDirectory())
  359. .replaceCharacter ('\\', '/')
  360. + "\"";
  361. hasChanged = true;
  362. }
  363. }
  364. }
  365. }
  366. if (hasChanged)
  367. {
  368. auto newText = joinLinesIntoSourceFile (lines);
  369. if (newText != content && newText != content + getPreferredLineFeed())
  370. replaceFile (file, newText, "Fixing includes in: ");
  371. }
  372. }
  373. static void fixRelativeIncludePaths (const ArgumentList& args)
  374. {
  375. args.checkMinNumArguments (2);
  376. auto target = args[1].resolveAsExistingFolder();
  377. auto files = findAllSourceFiles (target);
  378. for (int i = 0; i < files.size(); ++i)
  379. fixIncludes (files.getReference(i), files);
  380. }
  381. //==============================================================================
  382. static String getStringConcatenationExpression (Random& rng, int start, int length)
  383. {
  384. jassert (length > 0);
  385. if (length == 1)
  386. return "s" + String (start);
  387. int breakPos = jlimit (1, length - 1, (length / 3) + rng.nextInt (jmax (1, length / 3)));
  388. return "(" + getStringConcatenationExpression (rng, start, breakPos)
  389. + " + " + getStringConcatenationExpression (rng, start + breakPos, length - breakPos) + ")";
  390. }
  391. static void generateObfuscatedStringCode (const ArgumentList& args)
  392. {
  393. args.checkMinNumArguments (2);
  394. auto originalText = args[1].text.unquoted();
  395. struct Section
  396. {
  397. String text;
  398. int position, index;
  399. void writeGenerator (MemoryOutputStream& out) const
  400. {
  401. String name ("s" + String (index));
  402. out << " String " << name << "; " << name;
  403. auto escapeIfSingleQuote = [] (const String& s) -> String
  404. {
  405. if (s == "\'")
  406. return "\\'";
  407. return s;
  408. };
  409. for (int i = 0; i < text.length(); ++i)
  410. out << " << '" << escapeIfSingleQuote (String::charToString (text[i])) << "'";
  411. out << ";" << preferredLineFeed;
  412. }
  413. };
  414. Array<Section> sections;
  415. String text = originalText;
  416. Random rng;
  417. while (text.isNotEmpty())
  418. {
  419. int pos = jmax (0, text.length() - (1 + rng.nextInt (6)));
  420. Section s = { text.substring (pos), pos, 0 };
  421. sections.insert (0, s);
  422. text = text.substring (0, pos);
  423. }
  424. for (int i = 0; i < sections.size(); ++i)
  425. sections.getReference(i).index = i;
  426. for (int i = 0; i < sections.size(); ++i)
  427. sections.swap (i, rng.nextInt (sections.size()));
  428. MemoryOutputStream out;
  429. out << "String createString()" << preferredLineFeed
  430. << "{" << preferredLineFeed;
  431. for (int i = 0; i < sections.size(); ++i)
  432. sections.getReference(i).writeGenerator (out);
  433. out << preferredLineFeed
  434. << " String result = " << getStringConcatenationExpression (rng, 0, sections.size()) << ";" << preferredLineFeed
  435. << preferredLineFeed
  436. << " jassert (result == " << originalText.quoted() << ");" << preferredLineFeed
  437. << " return result;" << preferredLineFeed
  438. << "}" << preferredLineFeed;
  439. std::cout << out.toString() << std::endl;
  440. }
  441. static void scanFoldersForTranslationFiles (const ArgumentList& args)
  442. {
  443. args.checkMinNumArguments (2);
  444. StringArray translations;
  445. for (auto it = args.arguments.begin() + 1; it != args.arguments.end(); ++it)
  446. {
  447. auto directoryToSearch = it->resolveAsExistingFolder();
  448. TranslationHelpers::scanFolderForTranslations (translations, directoryToSearch);
  449. }
  450. std::cout << TranslationHelpers::mungeStrings (translations) << std::endl;
  451. }
  452. static void createFinishedTranslationFile (const ArgumentList& args)
  453. {
  454. args.checkMinNumArguments (3);
  455. auto preTranslated = args[1].resolveAsExistingFile().loadFileAsString();
  456. auto postTranslated = args[2].resolveAsExistingFile().loadFileAsString();
  457. auto localisedContent = (args.size() > 3 ? args[3].resolveAsExistingFile().loadFileAsString() : String());
  458. auto localised = LocalisedStrings (localisedContent, false);
  459. using TH = TranslationHelpers;
  460. std::cout << TH::createFinishedTranslationFile (TH::withTrimmedEnds (TH::breakApart (preTranslated)),
  461. TH::withTrimmedEnds (TH::breakApart (postTranslated)),
  462. localised) << std::endl;
  463. }
  464. //==============================================================================
  465. static void encodeBinary (const ArgumentList& args)
  466. {
  467. args.checkMinNumArguments (3);
  468. auto source = args[1].resolveAsExistingFile();
  469. auto target = args[2].resolveAsExistingFile();
  470. MemoryOutputStream literal;
  471. size_t dataSize = 0;
  472. {
  473. MemoryBlock data;
  474. FileInputStream input (source);
  475. input.readIntoMemoryBlock (data);
  476. build_tools::writeDataAsCppLiteral (data, literal, true, true);
  477. dataSize = data.getSize();
  478. }
  479. auto variableName = build_tools::makeBinaryDataIdentifierName (source);
  480. MemoryOutputStream header, cpp;
  481. header << "// Auto-generated binary data by the Projucer" << preferredLineFeed
  482. << "// Source file: " << source.getRelativePathFrom (target.getParentDirectory()) << preferredLineFeed
  483. << preferredLineFeed;
  484. cpp << header.toString();
  485. if (target.hasFileExtension (headerFileExtensions))
  486. {
  487. header << "static constexpr unsigned char " << variableName << "[] =" << preferredLineFeed
  488. << literal.toString() << preferredLineFeed
  489. << preferredLineFeed;
  490. replaceFile (target, header.toString(), "Writing: ");
  491. }
  492. else if (target.hasFileExtension (cppFileExtensions))
  493. {
  494. header << "extern const char* " << variableName << ";" << preferredLineFeed
  495. << "const unsigned int " << variableName << "Size = " << (int) dataSize << ";" << preferredLineFeed
  496. << preferredLineFeed;
  497. cpp << CodeHelpers::createIncludeStatement (target.withFileExtension (".h").getFileName()) << preferredLineFeed
  498. << preferredLineFeed
  499. << "static constexpr unsigned char " << variableName << "_local[] =" << preferredLineFeed
  500. << literal.toString() << preferredLineFeed
  501. << preferredLineFeed
  502. << "const char* " << variableName << " = (const char*) " << variableName << "_local;" << preferredLineFeed;
  503. replaceFile (target, cpp.toString(), "Writing: ");
  504. replaceFile (target.withFileExtension (".h"), header.toString(), "Writing: ");
  505. }
  506. else
  507. {
  508. ConsoleApplication::fail ("You need to specify a .h or .cpp file as the target");
  509. }
  510. }
  511. //==============================================================================
  512. static bool isThisOS (const String& os)
  513. {
  514. auto targetOS = TargetOS::unknown;
  515. if (os == "osx") targetOS = TargetOS::osx;
  516. else if (os == "windows") targetOS = TargetOS::windows;
  517. else if (os == "linux") targetOS = TargetOS::linux;
  518. if (targetOS == TargetOS::unknown)
  519. ConsoleApplication::fail ("You need to specify a valid OS! Use osx, windows or linux");
  520. return targetOS == TargetOS::getThisOS();
  521. }
  522. static bool isValidPathIdentifier (const String& id, const String& os)
  523. {
  524. return id == "vstLegacyPath" || (id == "aaxPath" && os != "linux") || (id == "rtasPath" && os != "linux")
  525. || id == "androidSDKPath" || id == "androidNDKPath" || id == "defaultJuceModulePath" || id == "defaultUserModulePath";
  526. }
  527. static void setGlobalPath (const ArgumentList& args)
  528. {
  529. args.checkMinNumArguments (3);
  530. if (! isValidPathIdentifier (args[2].text, args[1].text))
  531. ConsoleApplication::fail ("Identifier " + args[2].text + " is not valid for the OS " + args[1].text);
  532. auto userAppData = File::getSpecialLocation (File::userApplicationDataDirectory);
  533. #if JUCE_MAC
  534. userAppData = userAppData.getChildFile ("Application Support");
  535. #endif
  536. auto settingsFile = userAppData.getChildFile ("Projucer").getChildFile ("Projucer.settings");
  537. auto xml = parseXML (settingsFile);
  538. if (xml == nullptr)
  539. ConsoleApplication::fail ("Settings file not valid!");
  540. auto settingsTree = ValueTree::fromXml (*xml);
  541. if (! settingsTree.isValid())
  542. ConsoleApplication::fail ("Settings file not valid!");
  543. ValueTree childToSet;
  544. if (isThisOS (args[1].text))
  545. {
  546. childToSet = settingsTree.getChildWithProperty (Ids::name, "PROJECT_DEFAULT_SETTINGS")
  547. .getOrCreateChildWithName ("PROJECT_DEFAULT_SETTINGS", nullptr);
  548. }
  549. else
  550. {
  551. childToSet = settingsTree.getChildWithProperty (Ids::name, "FALLBACK_PATHS")
  552. .getOrCreateChildWithName ("FALLBACK_PATHS", nullptr)
  553. .getOrCreateChildWithName (args[1].text + "Fallback", nullptr);
  554. }
  555. if (! childToSet.isValid())
  556. ConsoleApplication::fail ("Failed to set the requested setting!");
  557. childToSet.setProperty (args[2].text, args[3].resolveAsFile().getFullPathName(), nullptr);
  558. settingsFile.replaceWithText (settingsTree.toXmlString());
  559. }
  560. static void createProjectFromPIP (const ArgumentList& args)
  561. {
  562. args.checkMinNumArguments (3);
  563. auto pipFile = args[1].resolveAsFile();
  564. if (! pipFile.existsAsFile())
  565. ConsoleApplication::fail ("PIP file doesn't exist.");
  566. auto outputDir = args[2].resolveAsFile();
  567. if (! outputDir.exists())
  568. {
  569. auto res = outputDir.createDirectory();
  570. std::cout << "Creating directory " << outputDir.getFullPathName() << std::endl;
  571. }
  572. File juceModulesPath, userModulesPath;
  573. if (args.size() > 3)
  574. {
  575. juceModulesPath = args[3].resolveAsFile();
  576. if (! juceModulesPath.exists())
  577. ConsoleApplication::fail ("Specified JUCE modules directory doesn't exist.");
  578. if (args.size() == 5)
  579. {
  580. userModulesPath = args[4].resolveAsFile();
  581. if (! userModulesPath.exists())
  582. ConsoleApplication::fail ("Specified JUCE modules directory doesn't exist.");
  583. }
  584. }
  585. PIPGenerator generator (pipFile, outputDir, juceModulesPath, userModulesPath);
  586. auto createJucerFileResult = generator.createJucerFile();
  587. if (! createJucerFileResult)
  588. ConsoleApplication::fail (createJucerFileResult.getErrorMessage());
  589. auto createMainCppResult = generator.createMainCpp();
  590. if (! createMainCppResult)
  591. ConsoleApplication::fail (createMainCppResult.getErrorMessage());
  592. }
  593. //==============================================================================
  594. static void showHelp()
  595. {
  596. hideDockIcon();
  597. auto appName = JUCEApplication::getInstance()->getApplicationName();
  598. std::cout << appName << std::endl
  599. << std::endl
  600. << "Usage: " << std::endl
  601. << std::endl
  602. << " " << appName << " --resave project_file" << std::endl
  603. << " Resaves all files and resources in a project." << std::endl
  604. << std::endl
  605. << " " << appName << " --resave-resources project_file" << std::endl
  606. << " Resaves just the binary resources for a project." << std::endl
  607. << std::endl
  608. << " " << appName << " --get-version project_file" << std::endl
  609. << " Returns the version number of a project." << std::endl
  610. << std::endl
  611. << " " << appName << " --set-version version_number project_file" << std::endl
  612. << " Updates the version number in a project." << std::endl
  613. << std::endl
  614. << " " << appName << " --bump-version project_file" << std::endl
  615. << " Updates the minor version number in a project by 1." << std::endl
  616. << std::endl
  617. << " " << appName << " --git-tag-version project_file" << std::endl
  618. << " Invokes 'git tag' to attach the project's version number to the current git repository." << std::endl
  619. << std::endl
  620. << " " << appName << " --status project_file" << std::endl
  621. << " Displays information about a project." << std::endl
  622. << std::endl
  623. << " " << appName << " --buildmodule target_folder module_folder" << std::endl
  624. << " Zips a module into a downloadable file format." << std::endl
  625. << std::endl
  626. << " " << appName << " --buildallmodules target_folder module_folder" << std::endl
  627. << " Zips all modules in a given folder and creates an index for them." << std::endl
  628. << std::endl
  629. << " " << appName << " --trim-whitespace target_folder" << std::endl
  630. << " Scans the given folder for C/C++ source files (recursively), and trims any trailing whitespace from their lines, as well as normalising their line-endings to CR-LF." << std::endl
  631. << std::endl
  632. << " " << appName << " --remove-tabs target_folder" << std::endl
  633. << " Scans the given folder for C/C++ source files (recursively), and replaces any tab characters with 4 spaces." << std::endl
  634. << std::endl
  635. << " " << appName << " --tidy-divider-comments target_folder" << std::endl
  636. << " Scans the given folder for C/C++ source files (recursively), and normalises any juce-style comment division lines (i.e. any lines that look like //===== or //------- or /////////// will be replaced)." << std::endl
  637. << std::endl
  638. << " " << appName << " --fix-broken-include-paths target_folder" << std::endl
  639. << " Scans the given folder for C/C++ source files (recursively). Where a file contains an #include of one of the other filenames, it changes it to use the optimum relative path. Helpful for auto-fixing includes when re-arranging files and folders in a project." << std::endl
  640. << std::endl
  641. << " " << appName << " --obfuscated-string-code string_to_obfuscate" << std::endl
  642. << " Generates a C++ function which returns the given string, but in an obfuscated way." << std::endl
  643. << std::endl
  644. << " " << appName << " --encode-binary source_binary_file target_cpp_file" << std::endl
  645. << " Converts a binary file to a C++ file containing its contents as a block of data. Provide a .h file as the target if you want a single output file, or a .cpp file if you want a pair of .h/.cpp files." << std::endl
  646. << std::endl
  647. << " " << appName << " --trans target_folders..." << std::endl
  648. << " Scans each of the given folders (recursively) for any NEEDS_TRANS macros, and generates a translation file that can be used with Projucer's translation file builder" << std::endl
  649. << std::endl
  650. << " " << appName << " --trans-finish pre_translated_file post_translated_file optional_existing_translation_file" << std::endl
  651. << " Creates a completed translations mapping file, that can be used to initialise a LocalisedStrings object. This allows you to localise the strings in your project" << std::endl
  652. << std::endl
  653. << " " << appName << " --set-global-search-path os identifier_to_set new_path" << std::endl
  654. << " Sets the global path for a specified os and identifier. The os should be either osx, windows or linux and the identifiers can be any of the following: "
  655. << "defaultJuceModulePath, defaultUserModulePath, vstLegacyPath, aaxPath (not valid on linux), rtasPath (not valid on linux), androidSDKPath or androidNDKPath. " << std::endl
  656. << std::endl
  657. << " " << appName << " --create-project-from-pip path/to/PIP path/to/output path/to/JUCE/modules (optional) path/to/user/modules (optional)" << std::endl
  658. << " Generates a folder containing a JUCE project in the specified output path using the specified PIP file. Use the optional JUCE and user module paths to override "
  659. "the global module paths." << std::endl
  660. << std::endl
  661. << "Note that for any of the file-rewriting commands, add the option \"--lf\" if you want it to use LF linefeeds instead of CRLF" << std::endl
  662. << std::endl;
  663. }
  664. }
  665. //==============================================================================
  666. int performCommandLine (const ArgumentList& args)
  667. {
  668. return ConsoleApplication::invokeCatchingFailures ([&] () -> int
  669. {
  670. if (args.containsOption ("--lf"))
  671. preferredLineFeed = "\n";
  672. auto command = args[0];
  673. auto matchCommand = [&] (StringRef name) -> bool
  674. {
  675. return command == name || command.isLongOption (name);
  676. };
  677. if (matchCommand ("help")) { showHelp(); return 0; }
  678. if (matchCommand ("h")) { showHelp(); return 0; }
  679. if (matchCommand ("resave")) { resaveProject (args, false); return 0; }
  680. if (matchCommand ("resave-resources")) { resaveProject (args, true); return 0; }
  681. if (matchCommand ("get-version")) { getVersion (args); return 0; }
  682. if (matchCommand ("set-version")) { setVersion (args); return 0; }
  683. if (matchCommand ("bump-version")) { bumpVersion (args); return 0; }
  684. if (matchCommand ("git-tag-version")) { gitTag (args); return 0; }
  685. if (matchCommand ("buildmodule")) { buildModules (args, false); return 0; }
  686. if (matchCommand ("buildallmodules")) { buildModules (args, true); return 0; }
  687. if (matchCommand ("status")) { showStatus (args); return 0; }
  688. if (matchCommand ("trim-whitespace")) { cleanWhitespace (args, false); return 0; }
  689. if (matchCommand ("remove-tabs")) { cleanWhitespace (args, true); return 0; }
  690. if (matchCommand ("tidy-divider-comments")) { tidyDividerComments (args); return 0; }
  691. if (matchCommand ("fix-broken-include-paths")) { fixRelativeIncludePaths (args); return 0; }
  692. if (matchCommand ("obfuscated-string-code")) { generateObfuscatedStringCode (args); return 0; }
  693. if (matchCommand ("encode-binary")) { encodeBinary (args); return 0; }
  694. if (matchCommand ("trans")) { scanFoldersForTranslationFiles (args); return 0; }
  695. if (matchCommand ("trans-finish")) { createFinishedTranslationFile (args); return 0; }
  696. if (matchCommand ("set-global-search-path")) { setGlobalPath (args); return 0; }
  697. if (matchCommand ("create-project-from-pip")) { createProjectFromPIP (args); return 0; }
  698. if (command.isLongOption() || command.isShortOption())
  699. ConsoleApplication::fail ("Unrecognised command: " + command.text.quoted());
  700. return commandLineNotPerformed;
  701. });
  702. }