Program.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. using System.Buffers;
  2. using System.Diagnostics;
  3. using System.IO.Compression;
  4. namespace PackTool
  5. {
  6. internal static partial class ZipFileUtils
  7. {
  8. // Per the .ZIP File Format Specification 4.4.17.1 all slashes should be forward slashes
  9. private const char PathSeparatorChar = '/';
  10. private const string PathSeparatorString = "/";
  11. public static string EntryFromPath(string entry, int offset, int length, ref char[] buffer, bool appendPathSeparator = false)
  12. {
  13. Debug.Assert(length <= entry.Length - offset);
  14. Debug.Assert(buffer != null);
  15. // Remove any leading slashes from the entry name:
  16. while (length > 0)
  17. {
  18. if (entry[offset] != Path.DirectorySeparatorChar &&
  19. entry[offset] != Path.AltDirectorySeparatorChar)
  20. break;
  21. offset++;
  22. length--;
  23. }
  24. if (length == 0)
  25. return appendPathSeparator ? PathSeparatorString : string.Empty;
  26. int resultLength = appendPathSeparator ? length + 1 : length;
  27. EnsureCapacity(ref buffer, resultLength);
  28. entry.CopyTo(offset, buffer, 0, length);
  29. // '/' is a more broadly recognized directory separator on all platforms (eg: mac, linux)
  30. // We don't use Path.DirectorySeparatorChar or AltDirectorySeparatorChar because this is
  31. // explicitly trying to standardize to '/'
  32. for (int i = 0; i < length; i++)
  33. {
  34. char ch = buffer[i];
  35. if (ch == Path.DirectorySeparatorChar || ch == Path.AltDirectorySeparatorChar)
  36. buffer[i] = PathSeparatorChar;
  37. }
  38. if (appendPathSeparator)
  39. buffer[length] = PathSeparatorChar;
  40. return new string(buffer, 0, resultLength);
  41. }
  42. public static void EnsureCapacity(ref char[] buffer, int min)
  43. {
  44. Debug.Assert(buffer != null);
  45. Debug.Assert(min > 0);
  46. if (buffer.Length < min)
  47. {
  48. int newCapacity = buffer.Length * 2;
  49. if (newCapacity < min)
  50. newCapacity = min;
  51. char[] oldBuffer = buffer;
  52. buffer = ArrayPool<char>.Shared.Rent(newCapacity);
  53. ArrayPool<char>.Shared.Return(oldBuffer);
  54. }
  55. }
  56. public static bool IsDirEmpty(DirectoryInfo possiblyEmptyDir)
  57. {
  58. using (IEnumerator<string> enumerator = Directory.EnumerateFileSystemEntries(possiblyEmptyDir.FullName).GetEnumerator())
  59. return !enumerator.MoveNext();
  60. }
  61. }
  62. class Program
  63. {
  64. private static void CreateZipFileFromDir(string sourceDirectoryName, string destinationArchiveFileName, string[] excludeFolders)
  65. {
  66. sourceDirectoryName = Path.GetFullPath(sourceDirectoryName);
  67. destinationArchiveFileName = Path.GetFullPath(destinationArchiveFileName);
  68. using (ZipArchive archive = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Create))
  69. {
  70. //add files and directories
  71. DirectoryInfo di = new DirectoryInfo(sourceDirectoryName);
  72. var basePath = di.FullName;
  73. // Windows' MaxPath (260) is used as an arbitrary default capacity, as it is likely
  74. // to be greater than the length of typical entry names from the file system, even
  75. // on non-Windows platforms. The capacity will be increased, if needed.
  76. const int DefaultCapacity = 260;
  77. char[] entryNameBuffer = ArrayPool<char>.Shared.Rent(DefaultCapacity);
  78. try
  79. {
  80. foreach (FileSystemInfo file in di.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
  81. {
  82. bool exclude = false;
  83. var fileName = file.FullName.ToLower();
  84. foreach(var excludeFolder in excludeFolders)
  85. {
  86. if (fileName.StartsWith(excludeFolder))
  87. {
  88. exclude = true;
  89. break;
  90. }
  91. }
  92. if (exclude)
  93. {
  94. continue;
  95. }
  96. int entryNameLength = file.FullName.Length - basePath.Length;
  97. if (file is FileInfo)
  98. {
  99. // Create entry for file:
  100. string entryName = ZipFileUtils.EntryFromPath(file.FullName, basePath.Length, entryNameLength, ref entryNameBuffer);
  101. ZipFileExtensions.CreateEntryFromFile(archive, file.FullName, entryName);
  102. }
  103. else
  104. {
  105. // Entry marking an empty dir:
  106. if (file is DirectoryInfo possiblyEmpty && ZipFileUtils.IsDirEmpty(possiblyEmpty))
  107. {
  108. // FullName never returns a directory separator character on the end,
  109. // but Zip archives require it to specify an explicit directory:
  110. string entryName = ZipFileUtils.EntryFromPath(file.FullName, basePath.Length, entryNameLength, ref entryNameBuffer, appendPathSeparator: true);
  111. archive.CreateEntry(entryName);
  112. }
  113. }
  114. } // foreach
  115. }
  116. finally
  117. {
  118. ArrayPool<char>.Shared.Return(entryNameBuffer);
  119. }
  120. }
  121. }
  122. private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
  123. {
  124. // Get the subdirectories for the specified directory.
  125. DirectoryInfo dir = new DirectoryInfo(sourceDirName);
  126. if (!dir.Exists)
  127. {
  128. throw new DirectoryNotFoundException(
  129. "Source directory does not exist or could not be found: "
  130. + sourceDirName);
  131. }
  132. DirectoryInfo[] dirs = dir.GetDirectories();
  133. // If the destination directory doesn't exist, create it.
  134. Directory.CreateDirectory(destDirName);
  135. // Get the files in the directory and copy them to the new location.
  136. FileInfo[] files = dir.GetFiles();
  137. foreach (FileInfo file in files)
  138. {
  139. string tempPath = Path.Combine(destDirName, file.Name);
  140. file.CopyTo(tempPath, true);
  141. }
  142. // If copying subdirectories, copy them and their contents to new location.
  143. if (copySubDirs)
  144. {
  145. foreach (DirectoryInfo subdir in dirs)
  146. {
  147. string tempPath = Path.Combine(destDirName, subdir.Name);
  148. DirectoryCopy(subdir.FullName, tempPath, copySubDirs);
  149. }
  150. }
  151. }
  152. private static void CopyFiles(string reveiveSource, string copyDest, string repoName, string productName, bool unzip)
  153. {
  154. var distRootFolder = Environment.GetEnvironmentVariable("DistFolder");
  155. if (string.IsNullOrEmpty(distRootFolder))
  156. {
  157. Console.WriteLine("The environment variable [DistFolder] is missing, use default dist folder.");
  158. //Using default repackFolder
  159. distRootFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Packages");
  160. }
  161. if (!Directory.Exists(distRootFolder))
  162. {
  163. Directory.CreateDirectory(distRootFolder);
  164. }
  165. var repoDistFolder = Path.Combine(distRootFolder, repoName, "Dist");
  166. if (!Directory.Exists(repoDistFolder))
  167. {
  168. Directory.CreateDirectory(repoDistFolder);
  169. }
  170. var productRootFolder = Environment.GetEnvironmentVariable("ProductFolder");
  171. if (string.IsNullOrEmpty(productRootFolder))
  172. {
  173. Console.WriteLine("The environment variable [ProductFolder] is missing, use default product folder.");
  174. //Using default repackFolder
  175. productRootFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Product");
  176. }
  177. if (!Directory.Exists(productRootFolder))
  178. {
  179. Directory.CreateDirectory(productRootFolder);
  180. }
  181. var productFolder = Path.Combine(productRootFolder, productName);
  182. if (!Directory.Exists(productFolder))
  183. {
  184. Directory.CreateDirectory(productFolder);
  185. }
  186. var source = Path.Combine(repoDistFolder, reveiveSource);
  187. var dest = Path.Combine(productFolder, copyDest);
  188. if (!File.Exists(source) && !Directory.Exists(source))
  189. {
  190. Console.WriteLine($"The received file/s {reveiveSource} can not be found.");
  191. return;
  192. }
  193. if (File.Exists(source))
  194. {
  195. if (unzip)
  196. {
  197. ZipFile.ExtractToDirectory(source, dest, true);
  198. }
  199. else
  200. {
  201. var destInfo = new FileInfo(dest);
  202. if (destInfo.Directory != null)
  203. {
  204. var destFolder = destInfo.Directory.FullName;
  205. if (!Directory.Exists(destFolder))
  206. {
  207. Directory.CreateDirectory(destFolder);
  208. }
  209. }
  210. File.Copy(source, dest, true);
  211. }
  212. }
  213. else
  214. {
  215. if (!Directory.Exists(dest))
  216. {
  217. Directory.CreateDirectory(dest);
  218. }
  219. DirectoryCopy(source, dest, true);
  220. }
  221. }
  222. private static void UpdateVersion(string productName, string repoName, string repoVersion)
  223. {
  224. var productRootFolder = Environment.GetEnvironmentVariable("ProductFolder");
  225. if (string.IsNullOrEmpty(productRootFolder))
  226. {
  227. Console.WriteLine("The environment variable [ProductFolder] is missing, use default product folder.");
  228. //Using default repackFolder
  229. productRootFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Product");
  230. }
  231. if (!Directory.Exists(productRootFolder))
  232. {
  233. Directory.CreateDirectory(productRootFolder);
  234. }
  235. var productFolder = Path.Combine(productRootFolder, productName);
  236. if (!Directory.Exists(productFolder))
  237. {
  238. Directory.CreateDirectory(productFolder);
  239. }
  240. var projectVersionFile = Path.Combine(productFolder, "Versions.txt");
  241. var versions = new List<string>();
  242. if (File.Exists(projectVersionFile))
  243. {
  244. versions = File.ReadLines(projectVersionFile).ToList();
  245. bool updated = false;
  246. for (var i = 0; i < versions.Count; i++)
  247. {
  248. var line = versions[i];
  249. var nameAndVersion = line.Split('=');
  250. var name = nameAndVersion[0];
  251. if (name == repoName)
  252. {
  253. versions[i] = repoName + "=" + repoVersion;
  254. File.WriteAllLines(projectVersionFile, versions);
  255. updated = true;
  256. break;
  257. }
  258. }
  259. if (!updated)
  260. {
  261. versions.Add(repoName + "=" + repoVersion);
  262. File.WriteAllLines(projectVersionFile, versions);
  263. }
  264. }
  265. else
  266. {
  267. versions.Add(repoName + "=" + repoVersion);
  268. File.WriteAllLines(projectVersionFile, versions);
  269. }
  270. }
  271. private static void RepackProduct(string productName, string[] excludeFolderNames, bool attachTimestamp)
  272. {
  273. var productRootFolder = Environment.GetEnvironmentVariable("ProductFolder");
  274. if (string.IsNullOrEmpty(productRootFolder))
  275. {
  276. Console.WriteLine("The environment variable [ProductFolder] is missing, use default product folder.");
  277. //Using default repackFolder
  278. productRootFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Product");
  279. }
  280. if (!Directory.Exists(productRootFolder))
  281. {
  282. Directory.CreateDirectory(productRootFolder);
  283. }
  284. var productFolder = Path.Combine(productRootFolder, productName);
  285. if (!Directory.Exists(productFolder))
  286. {
  287. Directory.CreateDirectory(productFolder);
  288. }
  289. var excludeFolders = new List<string>();
  290. foreach (var excludeFolderName in excludeFolderNames)
  291. {
  292. excludeFolders.Add(Path.Combine(productFolder.ToLower(), excludeFolderName.ToLower()));
  293. }
  294. var repackRootFolder = Environment.GetEnvironmentVariable("RepackFolder");
  295. if (string.IsNullOrEmpty(repackRootFolder))
  296. {
  297. Console.WriteLine("The environment variable [RepackFolder] is missing, use default repack folder.");
  298. //Using default repackFolder
  299. repackRootFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Repack");
  300. }
  301. if (!Directory.Exists(repackRootFolder))
  302. {
  303. Directory.CreateDirectory(repackRootFolder);
  304. }
  305. var repackFolder = Path.Combine(repackRootFolder, productName, DateTime.Now.ToString("yyyyMMdd"));
  306. if (!Directory.Exists(repackFolder))
  307. {
  308. Directory.CreateDirectory(repackFolder);
  309. }
  310. var repackFilePath = Path.Combine(repackFolder, $"{productName}.zip");
  311. if (attachTimestamp)
  312. {
  313. var timestamp = DateTime.Now.ToString("yyyyMMdd");
  314. var buildTime = 1;
  315. var formattedBuildTime = buildTime.ToString("D2");
  316. repackFilePath = Path.Combine(repackFolder, $"{productName}_{timestamp}.{formattedBuildTime}.zip");
  317. while (File.Exists(repackFilePath))
  318. {
  319. buildTime++;
  320. formattedBuildTime = buildTime.ToString("D2");
  321. repackFilePath = Path.Combine(repackFolder, $"{productName}_{timestamp}.{formattedBuildTime}.zip");
  322. }
  323. }
  324. if (File.Exists(repackFilePath))
  325. {
  326. File.Delete(repackFilePath);
  327. }
  328. CreateZipFileFromDir(productFolder, repackFilePath, excludeFolders.ToArray());
  329. }
  330. private static void HandleCopy(Dictionary<string, string> argMap)
  331. {
  332. //Copy the file/s
  333. if (!argMap.TryGetValue("/rs", out var receivedSource))
  334. {
  335. Console.WriteLine($"The [/rs] - received source argument is missing.");
  336. return;
  337. }
  338. if (!argMap.TryGetValue("/cd", out var extractDest))
  339. {
  340. Console.WriteLine($"The [/cd] - copy dest argument is missing.");
  341. return;
  342. }
  343. if (!argMap.TryGetValue("/pn", out var productName))
  344. {
  345. Console.WriteLine($"The [/pn] - product name argument is missing.");
  346. return;
  347. }
  348. if (!argMap.TryGetValue("/rn", out var repoName))
  349. {
  350. Console.WriteLine($"The [/rn] - repo name argument is missing.");
  351. return;
  352. }
  353. var unzip = false;
  354. if (argMap.TryGetValue("/unzip", out var unzipStr))
  355. {
  356. if (!bool.TryParse(unzipStr, out unzip))
  357. {
  358. Console.WriteLine($"The unzip argument {unzipStr} is not a bool value.");
  359. }
  360. }
  361. CopyFiles(receivedSource, extractDest, repoName, productName, unzip);
  362. }
  363. private static void HandleUpdateVersion(Dictionary<string, string> argMap)
  364. {
  365. //Only update version.
  366. if (!argMap.TryGetValue("/pn", out var productName))
  367. {
  368. Console.WriteLine($"The [/pn] - product name argument is missing.");
  369. return;
  370. }
  371. if (!argMap.TryGetValue("/rn", out var repoName))
  372. {
  373. Console.WriteLine($"The [/rn] - repo name argument is missing.");
  374. return;
  375. }
  376. if (!argMap.TryGetValue("/rv", out var repoVersion))
  377. {
  378. Console.WriteLine($"The [/rv] - repo version argument is missing.");
  379. return;
  380. }
  381. UpdateVersion(productName, repoName, repoVersion);
  382. }
  383. private static void HandleRepack(Dictionary<string, string> argMap)
  384. {
  385. if (!argMap.TryGetValue("/pn", out var productName))
  386. {
  387. Console.WriteLine($"The [/pn] - product name argument is missing.");
  388. return;
  389. }
  390. var excludFoldereNames = new List<string>();
  391. if (argMap.TryGetValue("/ef", out var excludes))
  392. {
  393. excludFoldereNames.AddRange(excludes.Split(','));
  394. }
  395. Console.WriteLine($"Exclude folders: [{string.Join(',', excludFoldereNames)}]");
  396. var attachTimestamp = true;
  397. if (argMap.TryGetValue("/at", out var attachTimestampStr))
  398. {
  399. if (!bool.TryParse(attachTimestampStr, out attachTimestamp))
  400. {
  401. Console.WriteLine($"The attachTimestamp argument {attachTimestampStr} is not a bool value.");
  402. }
  403. }
  404. RepackProduct(productName, excludFoldereNames.ToArray(), attachTimestamp);
  405. }
  406. private static void HandleFull(Dictionary<string, string> argMap)
  407. {
  408. if (!argMap.TryGetValue("/rs", out var receivedSource))
  409. {
  410. Console.WriteLine($"The [/rs] - received source argument is missing.");
  411. return;
  412. }
  413. if (!argMap.TryGetValue("/cd", out var extractDest))
  414. {
  415. Console.WriteLine($"The [/cd] - copy dest argument is missing.");
  416. return;
  417. }
  418. if (!argMap.TryGetValue("/pn", out var productName))
  419. {
  420. Console.WriteLine($"The [/pn] - product name argument is missing.");
  421. return;
  422. }
  423. if (!argMap.TryGetValue("/rn", out var repoName))
  424. {
  425. Console.WriteLine($"The [/rn] - repo name argument is missing.");
  426. return;
  427. }
  428. if (!argMap.TryGetValue("/rv", out var repoVersion))
  429. {
  430. Console.WriteLine($"The [/rv] - repo version argument is missing.");
  431. return;
  432. }
  433. var excludFoldereNames = new List<string>();
  434. if (argMap.TryGetValue("/ef", out var excludes))
  435. {
  436. excludFoldereNames.AddRange(excludes.Split(','));
  437. }
  438. var unzip = false;
  439. if (argMap.TryGetValue("/unzip", out var unzipStr))
  440. {
  441. if (!bool.TryParse(unzipStr, out unzip))
  442. {
  443. Console.WriteLine($"The unzip argument {unzipStr} is not a bool value.");
  444. }
  445. }
  446. CopyFiles(receivedSource, extractDest, repoName, productName, unzip);
  447. UpdateVersion(productName, repoName, repoVersion);
  448. var attachTimestamp = true;
  449. if (argMap.TryGetValue("/at", out var attachTimestampStr))
  450. {
  451. if (!bool.TryParse(attachTimestampStr, out attachTimestamp))
  452. {
  453. Console.WriteLine($"The attachTimestamp argument {attachTimestampStr} is not a bool value.");
  454. }
  455. }
  456. RepackProduct(productName, excludFoldereNames.ToArray(), attachTimestamp);
  457. }
  458. private static void StartInno(string innoSourceFileName, string setupSource, string productName, string productVersion, string outputDir)
  459. {
  460. var productRootFolder = Environment.GetEnvironmentVariable("ProductFolder");
  461. if (string.IsNullOrEmpty(productRootFolder))
  462. {
  463. Console.WriteLine("The environment variable [ProductFolder] is missing, use default product folder.");
  464. //Using default repackFolder
  465. productRootFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Product");
  466. }
  467. if (!Directory.Exists(productRootFolder))
  468. {
  469. Directory.CreateDirectory(productRootFolder);
  470. }
  471. var productFolder = Path.Combine(productRootFolder, productName);
  472. if (!Directory.Exists(productFolder))
  473. {
  474. Directory.CreateDirectory(productFolder);
  475. }
  476. var innoSourceFilePath = Path.Combine(productFolder, innoSourceFileName);
  477. var projectSetupSource = Path.Combine(productFolder, setupSource);
  478. var setupOutputDir = Path.Combine(productFolder, outputDir);
  479. if (!File.Exists(innoSourceFilePath))
  480. {
  481. Console.WriteLine("The inno setup source file does not exist.");
  482. return;
  483. }
  484. if (!Directory.Exists(projectSetupSource))
  485. {
  486. Console.WriteLine("The setup project source folder does not exist");
  487. return;
  488. }
  489. if (Directory.GetFiles(projectSetupSource).Length == 0)
  490. {
  491. Console.WriteLine("No file in setup project source folder.");
  492. return;
  493. }
  494. if (!Directory.Exists(setupOutputDir))
  495. {
  496. Directory.CreateDirectory(setupOutputDir);
  497. }
  498. var iscc_argument = $"/dProjectFolder={projectSetupSource} /dSetupAppVersion={productVersion} /dSetupOutputDir={setupOutputDir} {innoSourceFilePath}";
  499. Console.WriteLine($"Inno arg: {iscc_argument}");
  500. var process = Process.Start(new ProcessStartInfo("ISCC", iscc_argument) { UseShellExecute = true });
  501. process?.WaitForExit();
  502. }
  503. private static void HandleInno(Dictionary<string, string> argMap)
  504. {
  505. if (!argMap.TryGetValue("/pn", out var productName))
  506. {
  507. Console.WriteLine($"The [/pn] - product name argument is missing.");
  508. return;
  509. }
  510. if (!argMap.TryGetValue("/pv", out var productVersion))
  511. {
  512. Console.WriteLine($"The [/pv] - product version argument is missing.");
  513. return;
  514. }
  515. if (!argMap.TryGetValue("/ss", out var setupSource))
  516. {
  517. Console.WriteLine($"The [/ss] - setup source argument is missing.");
  518. return;
  519. }
  520. if (!argMap.TryGetValue("/od", out var outputDir))
  521. {
  522. Console.WriteLine($"The [/od] - output dir argument is missing.");
  523. return;
  524. }
  525. if (!argMap.TryGetValue("/iss", out var issFile))
  526. {
  527. Console.WriteLine($"The [/iss] - inno setup source argument is missing.");
  528. return;
  529. }
  530. StartInno(issFile, setupSource, productName, productVersion, outputDir);
  531. }
  532. private static void DeleteFileOrFolder(string productName, string deleteTraget)
  533. {
  534. var productRootFolder = Environment.GetEnvironmentVariable("ProductFolder");
  535. if (string.IsNullOrEmpty(productRootFolder))
  536. {
  537. Console.WriteLine("The environment variable [ProductFolder] is missing, use default product folder.");
  538. //Using default repackFolder
  539. productRootFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Product");
  540. }
  541. if (!Directory.Exists(productRootFolder))
  542. {
  543. Directory.CreateDirectory(productRootFolder);
  544. }
  545. var productFolder = Path.Combine(productRootFolder, productName);
  546. if (!Directory.Exists(productFolder))
  547. {
  548. Directory.CreateDirectory(productFolder);
  549. }
  550. var target = Path.Combine(productFolder, deleteTraget);
  551. if (File.Exists(target))
  552. {
  553. File.Delete(target);
  554. }
  555. else if (Directory.Exists(target))
  556. {
  557. Directory.Delete(target, true);
  558. }
  559. else
  560. {
  561. Console.WriteLine($"File or dir {deleteTraget} does not exist.");
  562. }
  563. }
  564. private static void HandleDelete(Dictionary<string, string> argMap)
  565. {
  566. if (!argMap.TryGetValue("/pn", out var productName))
  567. {
  568. Console.WriteLine($"The [/pn] - product name argument is missing.");
  569. return;
  570. }
  571. if (!argMap.TryGetValue("/dt", out var deleteTarget))
  572. {
  573. Console.WriteLine($"The [/dt] - delete target argument is missing.");
  574. return;
  575. }
  576. DeleteFileOrFolder(productName, deleteTarget);
  577. }
  578. static void Main(string[] args)
  579. {
  580. var argMap = new Dictionary<string, string>();
  581. foreach (var arg in args)
  582. {
  583. var nameAndValue = arg.Split('=');
  584. if (nameAndValue.Length == 2)
  585. {
  586. var name = nameAndValue[0].ToLower();
  587. if (string.IsNullOrEmpty(name))
  588. {
  589. Console.WriteLine("An empty arg name is not acceptable.");
  590. return;
  591. }
  592. if (argMap.ContainsKey(name))
  593. {
  594. Console.WriteLine($"Arg name {name} already exists");
  595. return;
  596. }
  597. argMap.Add(name, nameAndValue[1]);
  598. }
  599. }
  600. if (!argMap.TryGetValue("/mode", out var mode))
  601. {
  602. mode = "full";
  603. }
  604. Console.WriteLine($"Run in {mode} mode.");
  605. switch (mode)
  606. {
  607. case "full":
  608. //Copy files then pack the files then update the version then pack the files.
  609. HandleFull(argMap);
  610. break;
  611. case "copy":
  612. HandleCopy(argMap);
  613. break;
  614. case "version":
  615. HandleUpdateVersion(argMap);
  616. break;
  617. case "repack":
  618. //Obly Pack the files.
  619. HandleRepack(argMap);
  620. break;
  621. case "inno":
  622. HandleInno(argMap);
  623. break;
  624. case "delete":
  625. HandleDelete(argMap);
  626. break;
  627. default:
  628. Console.WriteLine($"Not suported mode: {mode}");
  629. break;
  630. }
  631. }
  632. }
  633. }