|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Collections.Specialized; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | + |
| 9 | +namespace Paket.Bootstrapper |
| 10 | +{ |
| 11 | + public static class ArgumentParser |
| 12 | + { |
| 13 | + public static class CommandArgs |
| 14 | + { |
| 15 | + public const string Help = "--help"; |
| 16 | + public const string PreferNuget = "--prefer-nuget"; |
| 17 | + public const string ForceNuget = "--force-nuget"; |
| 18 | + public const string Prerelease = "prerelease"; |
| 19 | + public const string NugetSourceArgPrefix = "--nuget-source="; |
| 20 | + public const string SelfUpdate = "--self"; |
| 21 | + public const string Silent = "-s"; |
| 22 | + public const string IgnoreCache = "-f"; |
| 23 | + } |
| 24 | + public static class AppSettingKeys |
| 25 | + { |
| 26 | + public const string PreferNugetAppSettingsKey = "PreferNuget"; |
| 27 | + public const string ForceNugetAppSettingsKey = "ForceNuget"; |
| 28 | + public const string PaketVersionAppSettingsKey = "PaketVersion"; |
| 29 | + } |
| 30 | + public static class EnvArgs |
| 31 | + { |
| 32 | + public const string PaketVersionEnv = "PAKET.VERSION"; |
| 33 | + } |
| 34 | + |
| 35 | + public static BootstrapperOptions ParseArgumentsAndConfigurations(IEnumerable<string> arguments, NameValueCollection appSettings, IDictionary envVariables) |
| 36 | + { |
| 37 | + var options = new BootstrapperOptions(); |
| 38 | + |
| 39 | + var commandArgs = arguments.ToList(); |
| 40 | + |
| 41 | + if (commandArgs.Contains(CommandArgs.PreferNuget) || appSettings.GetKey(AppSettingKeys.PreferNugetAppSettingsKey).ToLowerSafe() == "true") |
| 42 | + { |
| 43 | + options.PreferNuget = true; |
| 44 | + commandArgs.Remove(CommandArgs.PreferNuget); |
| 45 | + } |
| 46 | + if (commandArgs.Contains(CommandArgs.ForceNuget) || appSettings.GetKey(AppSettingKeys.ForceNugetAppSettingsKey).ToLowerSafe() == "true") |
| 47 | + { |
| 48 | + options.ForceNuget = true; |
| 49 | + commandArgs.Remove(CommandArgs.ForceNuget); |
| 50 | + } |
| 51 | + if (commandArgs.Contains(CommandArgs.Silent)) |
| 52 | + { |
| 53 | + options.Silent = true; |
| 54 | + commandArgs.Remove(CommandArgs.Silent); |
| 55 | + } |
| 56 | + if (commandArgs.Contains(CommandArgs.Help)) |
| 57 | + { |
| 58 | + options.ShowHelp = true; |
| 59 | + commandArgs.Remove(CommandArgs.Help); |
| 60 | + } |
| 61 | + |
| 62 | + commandArgs = EvaluateDownloadOptions(options.DownloadArguments, commandArgs, appSettings, envVariables).ToList(); |
| 63 | + |
| 64 | + options.UnprocessedCommandArgs = commandArgs; |
| 65 | + return options; |
| 66 | + } |
| 67 | + |
| 68 | + private static IEnumerable<string> EvaluateDownloadOptions(DownloadArguments downloadArguments, IEnumerable<string> args, NameValueCollection appSettings, IDictionary envVariables) |
| 69 | + { |
| 70 | + var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); |
| 71 | + var target = Path.Combine(folder, "paket.exe"); |
| 72 | + string nugetSource = null; |
| 73 | + |
| 74 | + var latestVersion = appSettings.GetKey(AppSettingKeys.PaketVersionAppSettingsKey) ?? envVariables.GetKey(EnvArgs.PaketVersionEnv) ?? String.Empty; |
| 75 | + var ignorePrerelease = true; |
| 76 | + bool doSelfUpdate = false; |
| 77 | + var ignoreCache = false; |
| 78 | + var commandArgs = args.ToList(); |
| 79 | + |
| 80 | + if (commandArgs.Contains(CommandArgs.SelfUpdate)) |
| 81 | + { |
| 82 | + commandArgs.Remove(CommandArgs.SelfUpdate); |
| 83 | + doSelfUpdate = true; |
| 84 | + } |
| 85 | + var nugetSourceArg = commandArgs.SingleOrDefault(x => x.StartsWith(CommandArgs.NugetSourceArgPrefix)); |
| 86 | + if (nugetSourceArg != null) |
| 87 | + { |
| 88 | + commandArgs = commandArgs.Where(x => !x.StartsWith(CommandArgs.NugetSourceArgPrefix)).ToList(); |
| 89 | + nugetSource = nugetSourceArg.Substring(CommandArgs.NugetSourceArgPrefix.Length); |
| 90 | + } |
| 91 | + if (commandArgs.Contains(CommandArgs.IgnoreCache)) |
| 92 | + { |
| 93 | + commandArgs.Remove(CommandArgs.IgnoreCache); |
| 94 | + ignoreCache = true; |
| 95 | + } |
| 96 | + if (commandArgs.Count >= 1) |
| 97 | + { |
| 98 | + if (commandArgs[0] == CommandArgs.Prerelease) |
| 99 | + { |
| 100 | + ignorePrerelease = false; |
| 101 | + latestVersion = String.Empty; |
| 102 | + commandArgs.Remove(CommandArgs.Prerelease); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + latestVersion = commandArgs[0]; |
| 107 | + commandArgs.Remove(commandArgs[0]); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + downloadArguments.LatestVersion = latestVersion; |
| 112 | + downloadArguments.IgnorePrerelease = ignorePrerelease; |
| 113 | + downloadArguments.IgnoreCache = ignoreCache; |
| 114 | + downloadArguments.NugetSource = nugetSource; |
| 115 | + downloadArguments.DoSelfUpdate = doSelfUpdate; |
| 116 | + downloadArguments.Target = target; |
| 117 | + downloadArguments.Folder = folder; |
| 118 | + return commandArgs; |
| 119 | + } |
| 120 | + |
| 121 | + private static string GetKey(this NameValueCollection appSettings, string key) |
| 122 | + { |
| 123 | + if (appSettings != null && appSettings.AllKeys.Any(x => x == key)) |
| 124 | + return appSettings.Get(key); |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + private static string GetKey(this IDictionary dictionary, string key) |
| 129 | + { |
| 130 | + if (dictionary != null && dictionary.Keys.Cast<string>().Any(x => x == key)) |
| 131 | + return dictionary[key].ToString(); |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + private static string ToLowerSafe(this string value) |
| 136 | + { |
| 137 | + if (value != null) |
| 138 | + return value.ToLower(); |
| 139 | + return null; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments