├── .gitattributes ├── .gitignore ├── MyCompanyName.MyProjectName.sln ├── install-tool.cmd └── src └── MyCompanyName.MyProjectName ├── Args ├── CommandLineArgs.cs ├── CommandLineArgumentParser.cs ├── CommandLineOptions.cs └── ICommandLineArgumentParser.cs ├── CliConst.cs ├── CliOptions.cs ├── CliService.cs ├── CliUsageException.cs ├── Commands ├── CommandSelector.cs ├── ConsoleCommandBase.cs ├── HelpCommand.cs ├── ICommandSelector.cs ├── IConsoleCommand.cs └── VersionCommand.cs ├── MyCompanyName.MyProjectName.csproj ├── MyProjectNameDomainModule.cs ├── Program.cs └── Properties └── launchSettings.json /.gitattributes: -------------------------------------------------------------------------------- 1 | **/wwwroot/libs/** linguist-vendored 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.vspscc 94 | *.vssscc 95 | .builds 96 | *.pidb 97 | *.svclog 98 | *.scc 99 | 100 | # Chutzpah Test files 101 | _Chutzpah* 102 | 103 | # Visual C++ cache files 104 | ipch/ 105 | *.aps 106 | *.ncb 107 | *.opendb 108 | *.opensdf 109 | *.sdf 110 | *.cachefile 111 | *.VC.db 112 | *.VC.VC.opendb 113 | 114 | # Visual Studio profiler 115 | *.psess 116 | *.vsp 117 | *.vspx 118 | *.sap 119 | 120 | # Visual Studio Trace Files 121 | *.e2e 122 | 123 | # TFS 2012 Local Workspace 124 | $tf/ 125 | 126 | # Guidance Automation Toolkit 127 | *.gpState 128 | 129 | # ReSharper is a .NET coding add-in 130 | _ReSharper*/ 131 | *.[Rr]e[Ss]harper 132 | *.DotSettings.user 133 | 134 | # TeamCity is a build add-in 135 | _TeamCity* 136 | 137 | # DotCover is a Code Coverage Tool 138 | *.dotCover 139 | 140 | # AxoCover is a Code Coverage Tool 141 | .axoCover/* 142 | !.axoCover/settings.json 143 | 144 | # Coverlet is a free, cross platform Code Coverage Tool 145 | coverage*.json 146 | coverage*.xml 147 | coverage*.info 148 | 149 | # Visual Studio code coverage results 150 | *.coverage 151 | *.coveragexml 152 | 153 | # NCrunch 154 | _NCrunch_* 155 | .*crunch*.local.xml 156 | nCrunchTemp_* 157 | 158 | # MightyMoose 159 | *.mm.* 160 | AutoTest.Net/ 161 | 162 | # Web workbench (sass) 163 | .sass-cache/ 164 | 165 | # Installshield output folder 166 | [Ee]xpress/ 167 | 168 | # DocProject is a documentation generator add-in 169 | DocProject/buildhelp/ 170 | DocProject/Help/*.HxT 171 | DocProject/Help/*.HxC 172 | DocProject/Help/*.hhc 173 | DocProject/Help/*.hhk 174 | DocProject/Help/*.hhp 175 | DocProject/Help/Html2 176 | DocProject/Help/html 177 | 178 | # Click-Once directory 179 | publish/ 180 | 181 | # Publish Web Output 182 | *.[Pp]ublish.xml 183 | *.azurePubxml 184 | # Note: Comment the next line if you want to checkin your web deploy settings, 185 | # but database connection strings (with potential passwords) will be unencrypted 186 | *.pubxml 187 | *.publishproj 188 | 189 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 190 | # checkin your Azure Web App publish settings, but sensitive information contained 191 | # in these scripts will be unencrypted 192 | PublishScripts/ 193 | 194 | # NuGet Packages 195 | *.nupkg 196 | # NuGet Symbol Packages 197 | *.snupkg 198 | # The packages folder can be ignored because of Package Restore 199 | **/[Pp]ackages/* 200 | # except build/, which is used as an MSBuild target. 201 | !**/[Pp]ackages/build/ 202 | # Uncomment if necessary however generally it will be regenerated when needed 203 | #!**/[Pp]ackages/repositories.config 204 | # NuGet v3's project.json files produces more ignorable files 205 | *.nuget.props 206 | *.nuget.targets 207 | 208 | # Microsoft Azure Build Output 209 | csx/ 210 | *.build.csdef 211 | 212 | # Microsoft Azure Emulator 213 | ecf/ 214 | rcf/ 215 | 216 | # Windows Store app package directories and files 217 | AppPackages/ 218 | BundleArtifacts/ 219 | Package.StoreAssociation.xml 220 | _pkginfo.txt 221 | *.appx 222 | *.appxbundle 223 | *.appxupload 224 | 225 | # Visual Studio cache files 226 | # files ending in .cache can be ignored 227 | *.[Cc]ache 228 | # but keep track of directories ending in .cache 229 | !?*.[Cc]ache/ 230 | 231 | # Others 232 | ClientBin/ 233 | ~$* 234 | *~ 235 | *.dbmdl 236 | *.dbproj.schemaview 237 | *.jfm 238 | *.pfx 239 | *.publishsettings 240 | orleans.codegen.cs 241 | 242 | # Including strong name files can present a security risk 243 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 244 | #*.snk 245 | 246 | # Since there are multiple workflows, uncomment next line to ignore bower_components 247 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 248 | #bower_components/ 249 | 250 | # RIA/Silverlight projects 251 | Generated_Code/ 252 | 253 | # Backup & report files from converting an old project file 254 | # to a newer Visual Studio version. Backup files are not needed, 255 | # because we have git ;-) 256 | _UpgradeReport_Files/ 257 | Backup*/ 258 | UpgradeLog*.XML 259 | UpgradeLog*.htm 260 | ServiceFabricBackup/ 261 | *.rptproj.bak 262 | 263 | # SQL Server files 264 | *.mdf 265 | *.ldf 266 | *.ndf 267 | 268 | # Business Intelligence projects 269 | *.rdl.data 270 | *.bim.layout 271 | *.bim_*.settings 272 | *.rptproj.rsuser 273 | *- [Bb]ackup.rdl 274 | *- [Bb]ackup ([0-9]).rdl 275 | *- [Bb]ackup ([0-9][0-9]).rdl 276 | 277 | # Microsoft Fakes 278 | FakesAssemblies/ 279 | 280 | # GhostDoc plugin setting file 281 | *.GhostDoc.xml 282 | 283 | # Node.js Tools for Visual Studio 284 | .ntvs_analysis.dat 285 | node_modules/ 286 | 287 | # Visual Studio 6 build log 288 | *.plg 289 | 290 | # Visual Studio 6 workspace options file 291 | *.opt 292 | 293 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 294 | *.vbw 295 | 296 | # Visual Studio LightSwitch build output 297 | **/*.HTMLClient/GeneratedArtifacts 298 | **/*.DesktopClient/GeneratedArtifacts 299 | **/*.DesktopClient/ModelManifest.xml 300 | **/*.Server/GeneratedArtifacts 301 | **/*.Server/ModelManifest.xml 302 | _Pvt_Extensions 303 | 304 | # Paket dependency manager 305 | .paket/paket.exe 306 | paket-files/ 307 | 308 | # FAKE - F# Make 309 | .fake/ 310 | 311 | # CodeRush personal settings 312 | .cr/personal 313 | 314 | # Python Tools for Visual Studio (PTVS) 315 | __pycache__/ 316 | *.pyc 317 | 318 | # Cake - Uncomment if you are using it 319 | # tools/** 320 | # !tools/packages.config 321 | 322 | # Tabs Studio 323 | *.tss 324 | 325 | # Telerik's JustMock configuration file 326 | *.jmconfig 327 | 328 | # BizTalk build output 329 | *.btp.cs 330 | *.btm.cs 331 | *.odx.cs 332 | *.xsd.cs 333 | 334 | # OpenCover UI analysis results 335 | OpenCover/ 336 | 337 | # Azure Stream Analytics local run output 338 | ASALocalRun/ 339 | 340 | # MSBuild Binary and Structured Log 341 | *.binlog 342 | 343 | # NVidia Nsight GPU debugger configuration file 344 | *.nvuser 345 | 346 | # MFractors (Xamarin productivity tool) working folder 347 | .mfractor/ 348 | 349 | # Local History for Visual Studio 350 | .localhistory/ 351 | 352 | # BeatPulse healthcheck temp database 353 | healthchecksdb 354 | 355 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 356 | MigrationBackup/ 357 | 358 | # Ionide (cross platform F# VS Code tools) working folder 359 | .ionide/ 360 | 361 | # Fody - auto-generated XML schema 362 | FodyWeavers.xsd 363 | 364 | # Rider 365 | .idea/ 366 | -------------------------------------------------------------------------------- /MyCompanyName.MyProjectName.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assistant", "src\MyCompanyName.MyProjectName\MyCompanyName.MyProjectName.csproj", "{384C907D-0F41-46E4-9FDE-A2E3BD7C2D2E}" 4 | EndProject 5 | Global 6 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 7 | Debug|Any CPU = Debug|Any CPU 8 | Release|Any CPU = Release|Any CPU 9 | EndGlobalSection 10 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 11 | {384C907D-0F41-46E4-9FDE-A2E3BD7C2D2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 12 | {384C907D-0F41-46E4-9FDE-A2E3BD7C2D2E}.Debug|Any CPU.Build.0 = Debug|Any CPU 13 | {384C907D-0F41-46E4-9FDE-A2E3BD7C2D2E}.Release|Any CPU.ActiveCfg = Release|Any CPU 14 | {384C907D-0F41-46E4-9FDE-A2E3BD7C2D2E}.Release|Any CPU.Build.0 = Release|Any CPU 15 | EndGlobalSection 16 | EndGlobal 17 | -------------------------------------------------------------------------------- /install-tool.cmd: -------------------------------------------------------------------------------- 1 | del /Q .\src\MyCompanyName.MyProjectName\bin 2 | dotnet build -c Release 3 | dotnet pack -c Release 4 | dotnet tool uninstall -g MyCompanyName.MyProjectName 5 | dotnet tool install -g MyCompanyName.MyProjectName --add-source .\src\MyCompanyName.MyProjectName\bin\nupkg 6 | pause -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/Args/CommandLineArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | using JetBrains.Annotations; 5 | 6 | namespace MyCompanyName.MyProjectName.Args 7 | { 8 | public class CommandLineArgs 9 | { 10 | [CanBeNull] 11 | public string Command { get; } 12 | 13 | [CanBeNull] 14 | public string Target { get; } 15 | 16 | [NotNull] 17 | public CommandLineOptions Options { get; } 18 | 19 | public CommandLineArgs([CanBeNull] string command = null, [CanBeNull] string target = null) 20 | { 21 | Command = command; 22 | Target = target; 23 | Options = new CommandLineOptions(); 24 | } 25 | 26 | public static CommandLineArgs Empty() 27 | { 28 | return new CommandLineArgs(); 29 | } 30 | 31 | public bool IsCommand(string command) 32 | { 33 | return string.Equals(Command, command, StringComparison.OrdinalIgnoreCase); 34 | } 35 | 36 | public override string ToString() 37 | { 38 | var sb = new StringBuilder(); 39 | 40 | if (Command != null) 41 | { 42 | sb.AppendLine($"Command: {Command}"); 43 | } 44 | 45 | if (Target != null) 46 | { 47 | sb.AppendLine($"Target: {Target}"); 48 | } 49 | 50 | if (Options.Any()) 51 | { 52 | sb.AppendLine("Options:"); 53 | foreach (var option in Options) 54 | { 55 | sb.AppendLine($" - {option.Key} = {option.Value}"); 56 | } 57 | } 58 | 59 | if (sb.Length <= 0) 60 | { 61 | sb.Append(""); 62 | } 63 | 64 | return sb.ToString(); 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/Args/CommandLineArgumentParser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Volo.Abp.DependencyInjection; 5 | 6 | namespace MyCompanyName.MyProjectName.Args 7 | { 8 | public class CommandLineArgumentParser : ICommandLineArgumentParser, ITransientDependency 9 | { 10 | public CommandLineArgs Parse(string[] args) 11 | { 12 | if (args.IsNullOrEmpty()) 13 | { 14 | return CommandLineArgs.Empty(); 15 | } 16 | 17 | var argumentList = args.ToList(); 18 | 19 | //Command 20 | 21 | var command = argumentList[0]; 22 | argumentList.RemoveAt(0); 23 | 24 | if (!argumentList.Any()) 25 | { 26 | return new CommandLineArgs(command); 27 | } 28 | 29 | //Target 30 | 31 | var target = argumentList[0]; 32 | if (target.StartsWith("-")) 33 | { 34 | target = null; 35 | } 36 | else 37 | { 38 | argumentList.RemoveAt(0); 39 | } 40 | 41 | if (!argumentList.Any()) 42 | { 43 | return new CommandLineArgs(command, target); 44 | } 45 | 46 | //Options 47 | 48 | var commandLineArgs = new CommandLineArgs(command, target); 49 | 50 | while (argumentList.Any()) 51 | { 52 | var optionName = ParseOptionName(argumentList[0]); 53 | argumentList.RemoveAt(0); 54 | 55 | if (!argumentList.Any()) 56 | { 57 | commandLineArgs.Options[optionName] = null; 58 | break; 59 | } 60 | 61 | if (IsOptionName(argumentList[0])) 62 | { 63 | commandLineArgs.Options[optionName] = null; 64 | continue; 65 | } 66 | 67 | commandLineArgs.Options[optionName] = argumentList[0]; 68 | argumentList.RemoveAt(0); 69 | } 70 | 71 | return commandLineArgs; 72 | } 73 | 74 | private static bool IsOptionName(string argument) 75 | { 76 | return argument.StartsWith("-") || argument.StartsWith("--"); 77 | } 78 | 79 | private static string ParseOptionName(string argument) 80 | { 81 | if (argument.StartsWith("--")) 82 | { 83 | if (argument.Length <= 2) 84 | { 85 | throw new ArgumentException("Should specify an option name after '--' prefix!"); 86 | } 87 | 88 | return argument.RemovePreFix("--"); 89 | } 90 | 91 | if (argument.StartsWith("-")) 92 | { 93 | if (argument.Length <= 1) 94 | { 95 | throw new ArgumentException("Should specify an option name after '--' prefix!"); 96 | } 97 | 98 | return argument.RemovePreFix("-"); 99 | } 100 | 101 | throw new ArgumentException("Option names should start with '-' or '--'."); 102 | } 103 | } 104 | } -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/Args/CommandLineOptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using JetBrains.Annotations; 4 | using Volo.Abp; 5 | 6 | namespace MyCompanyName.MyProjectName.Args 7 | { 8 | public class CommandLineOptions : Dictionary 9 | { 10 | [CanBeNull] 11 | public string GetOrNull([NotNull] string name, params string[] alternativeNames) 12 | { 13 | Check.NotNullOrWhiteSpace(name, nameof(name)); 14 | 15 | var value = this.GetOrDefault(name); 16 | if (!value.IsNullOrWhiteSpace()) 17 | { 18 | return value; 19 | } 20 | 21 | if (!alternativeNames.IsNullOrEmpty()) 22 | { 23 | foreach (var alternativeName in alternativeNames) 24 | { 25 | value = this.GetOrDefault(alternativeName); 26 | if (!value.IsNullOrWhiteSpace()) 27 | { 28 | return value; 29 | } 30 | } 31 | } 32 | 33 | return null; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/Args/ICommandLineArgumentParser.cs: -------------------------------------------------------------------------------- 1 | namespace MyCompanyName.MyProjectName.Args 2 | { 3 | public interface ICommandLineArgumentParser 4 | { 5 | CommandLineArgs Parse(string[] args); 6 | } 7 | } -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/CliConst.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | 4 | namespace MyCompanyName.MyProjectName 5 | { 6 | public static class CliConst 7 | { 8 | public static readonly string ConfigDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".MyCompanyName.MyProjectName"); 9 | 10 | public static readonly string LogFileName = Path.Combine(ConfigDirectory, "logs", "logs.txt"); 11 | 12 | public static readonly string ToolCommandName = "MyProjectName"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/CliOptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace MyCompanyName.MyProjectName 5 | { 6 | public class CliOptions 7 | { 8 | public Dictionary Commands { get; } 9 | 10 | public CliOptions() 11 | { 12 | Commands = new Dictionary(StringComparer.OrdinalIgnoreCase); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/CliService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using Microsoft.Extensions.DependencyInjection; 4 | using Microsoft.Extensions.Logging; 5 | using Microsoft.Extensions.Logging.Abstractions; 6 | using MyCompanyName.MyProjectName.Args; 7 | using MyCompanyName.MyProjectName.Commands; 8 | using Volo.Abp.DependencyInjection; 9 | 10 | namespace MyCompanyName.MyProjectName 11 | { 12 | public class CliService : ITransientDependency 13 | { 14 | public ILogger Logger { get; set; } 15 | protected ICommandLineArgumentParser CommandLineArgumentParser { get; } 16 | protected ICommandSelector CommandSelector { get; } 17 | protected IServiceScopeFactory ServiceScopeFactory { get; } 18 | 19 | public CliService( 20 | ICommandLineArgumentParser commandLineArgumentParser, 21 | ICommandSelector commandSelector, 22 | IServiceScopeFactory serviceScopeFactory) 23 | { 24 | CommandLineArgumentParser = commandLineArgumentParser; 25 | CommandSelector = commandSelector; 26 | ServiceScopeFactory = serviceScopeFactory; 27 | 28 | Logger = NullLogger.Instance; 29 | } 30 | 31 | public async Task RunAsync(string[] args) 32 | { 33 | var commandLineArgs = CommandLineArgumentParser.Parse(args); 34 | 35 | var commandType = CommandSelector.Select(commandLineArgs); 36 | 37 | using (var scope = ServiceScopeFactory.CreateScope()) 38 | { 39 | var command = (IConsoleCommand)scope.ServiceProvider.GetRequiredService(commandType); 40 | 41 | try 42 | { 43 | await command.ExecuteAsync(commandLineArgs); 44 | } 45 | catch (CliUsageException usageException) 46 | { 47 | Logger.LogWarning(usageException.Message); 48 | } 49 | catch (Exception ex) 50 | { 51 | Logger.LogException(ex); 52 | } 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/CliUsageException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MyCompanyName.MyProjectName 4 | { 5 | public class CliUsageException : Exception 6 | { 7 | public CliUsageException(string message) 8 | : base(message) 9 | { 10 | 11 | } 12 | 13 | public CliUsageException(string message, Exception innerException) 14 | : base(message, innerException) 15 | { 16 | 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/Commands/CommandSelector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Microsoft.Extensions.Options; 4 | using MyCompanyName.MyProjectName.Args; 5 | using Volo.Abp.DependencyInjection; 6 | 7 | namespace MyCompanyName.MyProjectName.Commands 8 | { 9 | public class CommandSelector : ICommandSelector, ITransientDependency 10 | { 11 | protected CliOptions Options { get; } 12 | 13 | public CommandSelector(IOptions options) 14 | { 15 | Options = options.Value; 16 | } 17 | 18 | public Type Select(CommandLineArgs commandLineArgs) 19 | { 20 | if (commandLineArgs.Command.IsNullOrWhiteSpace()) 21 | { 22 | return typeof(HelpCommand); 23 | } 24 | 25 | return Options.Commands.GetOrDefault(commandLineArgs.Command) ?? typeof(HelpCommand); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/Commands/ConsoleCommandBase.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using Microsoft.Extensions.Logging; 3 | using Microsoft.Extensions.Logging.Abstractions; 4 | using MyCompanyName.MyProjectName.Args; 5 | using Volo.Abp.DependencyInjection; 6 | 7 | namespace MyCompanyName.MyProjectName.Commands 8 | { 9 | public abstract class ConsoleCommandBase : IConsoleCommand, ITransientDependency 10 | { 11 | public ILogger Logger { get; set; } 12 | 13 | public ConsoleCommandBase() 14 | { 15 | Logger = NullLogger.Instance; 16 | } 17 | 18 | public abstract Task ExecuteAsync(CommandLineArgs commandLineArgs); 19 | 20 | public abstract string GetUsageInfo(); 21 | 22 | public abstract string GetShortDescription(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/Commands/HelpCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Text; 3 | using System.Threading.Tasks; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.Logging; 6 | using Microsoft.Extensions.Logging.Abstractions; 7 | using Microsoft.Extensions.Options; 8 | using MyCompanyName.MyProjectName.Args; 9 | using Volo.Abp.DependencyInjection; 10 | 11 | namespace MyCompanyName.MyProjectName.Commands 12 | { 13 | public class HelpCommand : IConsoleCommand, ITransientDependency 14 | { 15 | public ILogger Logger { get; set; } 16 | protected CliOptions CliOptions { get; } 17 | protected IServiceScopeFactory ServiceScopeFactory { get; } 18 | 19 | public HelpCommand(IOptions cliOptions, 20 | IServiceScopeFactory serviceScopeFactory) 21 | { 22 | ServiceScopeFactory = serviceScopeFactory; 23 | Logger = NullLogger.Instance; 24 | CliOptions = cliOptions.Value; 25 | } 26 | 27 | public Task ExecuteAsync(CommandLineArgs commandLineArgs) 28 | { 29 | if (string.IsNullOrWhiteSpace(commandLineArgs.Target)) 30 | { 31 | Logger.LogInformation(GetUsageInfo()); 32 | return Task.CompletedTask; 33 | } 34 | 35 | if (!CliOptions.Commands.ContainsKey(commandLineArgs.Target)) 36 | { 37 | Logger.LogWarning($"There is no command named {commandLineArgs.Target}."); 38 | Logger.LogInformation(GetUsageInfo()); 39 | return Task.CompletedTask; 40 | } 41 | 42 | var commandType = CliOptions.Commands[commandLineArgs.Target]; 43 | 44 | using (var scope = ServiceScopeFactory.CreateScope()) 45 | { 46 | var command = (IConsoleCommand) scope.ServiceProvider.GetRequiredService(commandType); 47 | Logger.LogInformation(command.GetUsageInfo()); 48 | } 49 | 50 | return Task.CompletedTask; 51 | } 52 | 53 | public string GetUsageInfo() 54 | { 55 | var sb = new StringBuilder(); 56 | 57 | sb.AppendLine(); 58 | sb.AppendLine("Usage:"); 59 | sb.AppendLine($"\t{CliConst.ToolCommandName} [options]"); 60 | sb.AppendLine(); 61 | sb.AppendLine("Command List:"); 62 | 63 | foreach (var command in CliOptions.Commands.ToArray()) 64 | { 65 | string shortDescription; 66 | 67 | using (var scope = ServiceScopeFactory.CreateScope()) 68 | { 69 | shortDescription = ((IConsoleCommand) scope.ServiceProvider 70 | .GetRequiredService(command.Value)).GetShortDescription(); 71 | } 72 | 73 | sb.Append("\t> "); 74 | sb.Append(command.Key); 75 | sb.Append(string.IsNullOrWhiteSpace(shortDescription) ? "" : ": "); 76 | sb.AppendLine(shortDescription); 77 | } 78 | 79 | sb.AppendLine(); 80 | sb.AppendLine("More info: https://github.com/maliming/cli-template"); 81 | 82 | return sb.ToString(); 83 | } 84 | 85 | public string GetShortDescription() 86 | { 87 | return $"Show command line help. Write `{CliConst.ToolCommandName} help `"; 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/Commands/ICommandSelector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using MyCompanyName.MyProjectName.Args; 3 | 4 | namespace MyCompanyName.MyProjectName.Commands 5 | { 6 | public interface ICommandSelector 7 | { 8 | Type Select(CommandLineArgs commandLineArgs); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/Commands/IConsoleCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using MyCompanyName.MyProjectName.Args; 3 | 4 | namespace MyCompanyName.MyProjectName.Commands 5 | { 6 | public interface IConsoleCommand 7 | { 8 | Task ExecuteAsync(CommandLineArgs commandLineArgs); 9 | 10 | string GetUsageInfo(); 11 | 12 | string GetShortDescription(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/Commands/VersionCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Text; 3 | using System.Threading.Tasks; 4 | using Microsoft.Extensions.Logging; 5 | using MyCompanyName.MyProjectName.Args; 6 | 7 | namespace MyCompanyName.MyProjectName.Commands 8 | { 9 | public class VersionCommand : ConsoleCommandBase 10 | { 11 | public override Task ExecuteAsync(CommandLineArgs commandLineArgs) 12 | { 13 | Logger.LogInformation(typeof(VersionCommand).Assembly.GetFileVersion()); 14 | return Task.CompletedTask; 15 | } 16 | 17 | public override string GetUsageInfo() 18 | { 19 | var sb = new StringBuilder(); 20 | 21 | sb.AppendLine("Usage:"); 22 | sb.AppendLine($"\t{CliConst.ToolCommandName} version"); 23 | 24 | return sb.ToString(); 25 | } 26 | 27 | public override string GetShortDescription() 28 | { 29 | return "Get version."; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/MyCompanyName.MyProjectName.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | true 8 | MyProjectName 9 | ./bin/nupkg 10 | true 11 | MyCompanyName.MyProjectName 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/MyProjectNameDomainModule.cs: -------------------------------------------------------------------------------- 1 | using MyCompanyName.MyProjectName.Commands; 2 | using Volo.Abp.Autofac; 3 | using Volo.Abp.Modularity; 4 | 5 | namespace MyCompanyName.MyProjectName 6 | { 7 | [DependsOn( 8 | typeof(AbpAutofacModule) 9 | )] 10 | public class MyProjectNameDomainModule : AbpModule 11 | { 12 | public override void ConfigureServices(ServiceConfigurationContext context) 13 | { 14 | Configure(options => 15 | { 16 | options.Commands["help"] = typeof(HelpCommand); 17 | options.Commands["version"] = typeof(VersionCommand); 18 | 19 | //add more commands here. 20 | }); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using Microsoft.Extensions.DependencyInjection; 4 | using Serilog; 5 | using Serilog.Events; 6 | using Volo.Abp; 7 | 8 | namespace MyCompanyName.MyProjectName 9 | { 10 | class Program 11 | { 12 | static async Task Main(string[] args) 13 | { 14 | Console.OutputEncoding = System.Text.Encoding.UTF8; 15 | 16 | Log.Logger = new LoggerConfiguration() 17 | #if DEBUG 18 | .MinimumLevel.Debug() 19 | #else 20 | .MinimumLevel.Information() 21 | #endif 22 | .MinimumLevel.Override("Volo.Abp", LogEventLevel.Warning) 23 | .Enrich.FromLogContext() 24 | .WriteTo.File(CliConst.LogFileName) 25 | .WriteTo.Console(outputTemplate: "{Message:lj}{NewLine}{Exception}") 26 | .CreateLogger(); 27 | 28 | using (var application = AbpApplicationFactory.Create( 29 | options => 30 | { 31 | options.UseAutofac(); 32 | options.Services.AddLogging(c => c.AddSerilog()); 33 | })) 34 | { 35 | application.Initialize(); 36 | 37 | await application.ServiceProvider 38 | .GetRequiredService() 39 | .RunAsync(args); 40 | 41 | application.Shutdown(); 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/MyCompanyName.MyProjectName/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/launchsettings.json", 3 | "profiles": { 4 | "MyCompanyName.MyProjectName": { 5 | "commandName": "Project", 6 | "commandLineArgs": "help", 7 | "environmentVariables": { 8 | "ASPNETCORE_ENVIRONMENT": "Development" 9 | } 10 | } 11 | } 12 | } 13 | --------------------------------------------------------------------------------