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.

927 lines
38KB

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