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.

904 lines
36KB

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