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.

917 lines
37KB

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