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.

906 lines
37KB

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