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.

929 lines
38KB

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