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.

934 lines
38KB

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