|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +using Force.Blazer.Exe.CommandLine; |
| 7 | + |
| 8 | +namespace Force.Blazer.Exe |
| 9 | +{ |
| 10 | + public class FileNameHelper |
| 11 | + { |
| 12 | + public class FileOptions |
| 13 | + { |
| 14 | + public string ArchiveName { get; set; } |
| 15 | + |
| 16 | + public string[] SourceFiles { get; set; } |
| 17 | + } |
| 18 | + |
| 19 | + public static FileOptions ParseCompressOptions(CommandLineParser<BlazerCommandLineOptions> options) |
| 20 | + { |
| 21 | + var opt = options.Get(); |
| 22 | + var nonParamOptions = options.GetNonParamOptions(); |
| 23 | + if (nonParamOptions.Length == 0 && !opt.Stdout) |
| 24 | + { |
| 25 | + Console.WriteLine(options.GenerateHelp()); |
| 26 | + return null; |
| 27 | + } |
| 28 | + |
| 29 | + var listFile = options.GetNonParamOptions().FirstOrDefault(x => x[0] == '@'); |
| 30 | + if (listFile != null && nonParamOptions.Length != 2 - (opt.Stdout ? 1 : 0)) |
| 31 | + { |
| 32 | + Console.Error.WriteLine("When list file is provided, only archive name is allowed"); |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + if (listFile != null && opt.Stdin) |
| 37 | + { |
| 38 | + Console.Error.WriteLine("Stdin is not compatible with list file"); |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + if (opt.Stdin && nonParamOptions.Length > 1) |
| 43 | + { |
| 44 | + Console.Error.WriteLine("Stdin is not compatible with multiple files"); |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + var archiveName = opt.Stdout ? null : nonParamOptions[0]; |
| 49 | + string[] filesToCompress; |
| 50 | + |
| 51 | + if (listFile != null) |
| 52 | + { |
| 53 | + listFile = listFile.Remove(0, 1); |
| 54 | + if (!File.Exists(listFile)) |
| 55 | + { |
| 56 | + Console.Error.WriteLine("Invalid list file"); |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + filesToCompress = |
| 61 | + File.ReadAllLines(listFile).Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).ToArray(); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + filesToCompress = opt.Stdin ? new string[0] : nonParamOptions.Skip(1).ToArray(); |
| 66 | + |
| 67 | + if (filesToCompress.Length == 0 && archiveName != null && !opt.Stdin) filesToCompress = new[] { archiveName }; |
| 68 | + } |
| 69 | + |
| 70 | + bool hasMissingFiles; |
| 71 | + filesToCompress = ExpandFilesInList(filesToCompress, out hasMissingFiles); |
| 72 | + |
| 73 | + if (hasMissingFiles) |
| 74 | + { |
| 75 | + Console.Error.WriteLine("One or more of files to compress does not exist"); |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + if (archiveName != null && !archiveName.EndsWith(".blz")) archiveName += ".blz"; |
| 80 | + |
| 81 | + return new FileOptions |
| 82 | + { |
| 83 | + ArchiveName = archiveName, |
| 84 | + SourceFiles = filesToCompress |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + public static FileOptions ParseDecompressOptions(CommandLineParser<BlazerCommandLineOptions> options) |
| 89 | + { |
| 90 | + var opt = options.Get(); |
| 91 | + |
| 92 | + var nonParamOptions = options.GetNonParamOptions(); |
| 93 | + |
| 94 | + if (!opt.Stdin && nonParamOptions.Length == 0) |
| 95 | + { |
| 96 | + Console.Error.WriteLine("Archive name was not specified"); |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + var archiveName = opt.Stdin ? null : nonParamOptions[0]; |
| 101 | + |
| 102 | + if (archiveName != null && !File.Exists(archiveName)) |
| 103 | + { |
| 104 | + Console.Error.WriteLine("Archive file " + archiveName + " does not exist"); |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + var listFile = options.GetNonParamOptions().FirstOrDefault(x => x[0] == '@'); |
| 109 | + if (listFile != null && nonParamOptions.Length != 2 - (opt.Stdin ? 1 : 0)) |
| 110 | + { |
| 111 | + Console.Error.WriteLine("When list file is provided, only archive name is allowed"); |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + string[] customOutFileNames = null; |
| 116 | + |
| 117 | + if (listFile != null) |
| 118 | + { |
| 119 | + listFile = listFile.Remove(0, 1); |
| 120 | + if (!File.Exists(listFile)) |
| 121 | + { |
| 122 | + Console.Error.WriteLine("Invalid list file"); |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + customOutFileNames = File.ReadAllLines(listFile).Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).ToArray(); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + customOutFileNames = nonParamOptions.Skip(opt.Stdin ? 0 : 1).ToArray(); |
| 131 | + } |
| 132 | + |
| 133 | + return new FileOptions |
| 134 | + { |
| 135 | + ArchiveName = archiveName, |
| 136 | + SourceFiles = customOutFileNames |
| 137 | + }; |
| 138 | + } |
| 139 | + |
| 140 | + private static string[] ExpandFilesInList(string[] initialFiles, out bool hasMissingFiles) |
| 141 | + { |
| 142 | + hasMissingFiles = false; |
| 143 | + var l = new List<string>(); |
| 144 | + // todo: better search + unit tests |
| 145 | + foreach (var s in initialFiles) |
| 146 | + { |
| 147 | + if (File.Exists(s)) |
| 148 | + l.Add(s); |
| 149 | + else if (Directory.Exists(s)) l.Add(s); |
| 150 | + else |
| 151 | + { |
| 152 | + var asteriskIdx = s.IndexOf("*", StringComparison.InvariantCulture); |
| 153 | + if (asteriskIdx < 0) hasMissingFiles = true; |
| 154 | + else |
| 155 | + { |
| 156 | + var slashIdx = s.LastIndexOfAny( |
| 157 | + new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, asteriskIdx - 1, asteriskIdx - 1); |
| 158 | + var dirToSearch = string.Empty; |
| 159 | + if (slashIdx >= 0) dirToSearch = s.Substring(0, slashIdx); |
| 160 | + l.AddRange(Directory.GetFiles(dirToSearch, s.Remove(0, slashIdx + 1), SearchOption.AllDirectories)); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return l.ToArray(); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments