| | | 1 | | using System.Reflection; |
| | | 2 | | |
| | | 3 | | namespace ZhEaIsNsAaBn.DbSeeder.Tool; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// <c>dotnet dbseeder</c>, the way <c>dotnet ef</c> works: build the startup project, check its Design version, then |
| | | 7 | | /// <c>dotnet exec --depsfile App.deps.json --runtimeconfig App.runtimeconfig.json --additionalprobingpath … bootstrap.d |
| | | 8 | | /// so the commands run on the app's own EF Core / provider / Design. Exit code: the command's, or 2 for a tool error. |
| | | 9 | | /// </summary> |
| | | 10 | | internal static class DbSeederTool |
| | | 11 | | { |
| | | 12 | | public const int ToolError = 2; |
| | | 13 | | |
| | | 14 | | public const string BootstrapFile = "ZhEaIsNsAaBn.DbSeeder.Tool.Bootstrap.dll"; |
| | | 15 | | |
| | | 16 | | |
| | | 17 | | public static string Version |
| | 0 | 18 | | => typeof( DbSeederTool ).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVer |
| | 0 | 19 | | ?? typeof( DbSeederTool ).Assembly.GetName().Version!.ToString( 3 ); |
| | | 20 | | |
| | | 21 | | |
| | | 22 | | public static async Task<int> RunAsync( IReadOnlyList<string> args, TextWriter output, TextWriter errors, string cur |
| | | 23 | | { |
| | 0 | 24 | | if ( !ToolOptions.TryParse( args, out var options, out var parseError ) ) { return Fail( errors, parseError ); } |
| | | 25 | | |
| | 0 | 26 | | if ( options.Version ) |
| | | 27 | | { |
| | 0 | 28 | | await output.WriteLineAsync( Version ).ConfigureAwait( false ); |
| | | 29 | | |
| | 0 | 30 | | return 0; |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | if ( options.Help || options.Command.Count == 0 ) |
| | | 34 | | { |
| | 0 | 35 | | await output.WriteLineAsync( ToolOptions.Usage ).ConfigureAwait( false ); |
| | | 36 | | |
| | 0 | 37 | | return options.Help ? 0 : ToolError; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | |
| | | 41 | | try |
| | | 42 | | { |
| | 0 | 43 | | var process = new ToolProcess( errors, options.Verbose ); |
| | 0 | 44 | | var projectFile = ProjectMetadata.ResolveProjectFile( options.Project, currentDirectory ); |
| | 0 | 45 | | var startupFile = options.StartupProject is null ? projectFile : ProjectMetadata.ResolveProjectFile( options |
| | 0 | 46 | | var framework = await ProjectMetadata.TargetFrameworkAsync( startupFile, options.Framework, options.Config |
| | | 47 | | |
| | 0 | 48 | | if ( !options.NoBuild ) { await BuildAsync( startupFile, framework, options, process, errors ).ConfigureAwai |
| | | 49 | | |
| | 0 | 50 | | var startup = await ProjectMetadata.ReadAsync( startupFile, framework, options.Configuration, process ).Conf |
| | 0 | 51 | | var project = PathsEqual( projectFile, startupFile ) ? startup : await ProjectMetadata.ReadAsync( projectFil |
| | | 52 | | |
| | 0 | 53 | | if ( !File.Exists( startup.RuntimeConfigFile ) ) |
| | | 54 | | { |
| | 0 | 55 | | throw new ToolException( File.Exists( startup.TargetPath ) |
| | 0 | 56 | | ? $"{startup.Name} has no runtimeconfig.json. Use an executable startup pro |
| | 0 | 57 | | : $"{startup.Name} isn't built ({startup.TargetPath}). Build it, or drop -- |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | var versionError = DesignVersionCheck.Check( Version, startup.Name, await File.ReadAllTextAsync( startup.Dep |
| | 0 | 61 | | project == startup ? null : project.Name, |
| | 0 | 62 | | project == startup || !File.Exists( project.DepsFile ) ? null : |
| | | 63 | | |
| | 0 | 64 | | if ( versionError is not null ) { throw new ToolException( versionError ); } |
| | | 65 | | |
| | | 66 | | |
| | 0 | 67 | | return await process.RunAsync( ExecArguments( startup, project, options ), startup.ProjectDirectory, ChildEn |
| | | 68 | | } |
| | 0 | 69 | | catch ( ToolException exception ) { return Fail( errors, exception.Message ); } |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | |
| | | 73 | | /// <summary>The <c>dotnet exec</c> command line for the bootstrap.</summary> |
| | | 74 | | public static List<string> ExecArguments( ProjectMetadata startup, ProjectMetadata project, ToolOptions options ) |
| | | 75 | | { |
| | 0 | 76 | | List<string> args = [ "exec", "--depsfile", startup.DepsFile, "--runtimeconfig", startup.RuntimeConfigFile ]; |
| | | 77 | | |
| | 0 | 78 | | foreach ( var folder in startup.PackageFolders ) { args.AddRange( [ "--additionalprobingpath", folder ] ); } |
| | | 79 | | |
| | 0 | 80 | | args.AddRange( [ Path.Combine( AppContext.BaseDirectory, "bootstrap", BootstrapFile ), "--startup-assembly", sta |
| | | 81 | | |
| | 0 | 82 | | if ( project != startup ) { args.AddRange( [ "--project-assembly", project.TargetPath ] ); } |
| | | 83 | | |
| | 0 | 84 | | args.AddRange( [ "--project-dir", project.ProjectDirectory.TrimEnd( Path.DirectorySeparatorChar, Path.AltDirecto |
| | | 85 | | |
| | 0 | 86 | | if ( options.Context is not null ) { args.AddRange( [ "--context", options.Context ] ); } |
| | | 87 | | |
| | 0 | 88 | | foreach ( var arg in options.AppArgs ) { args.AddRange( [ "--app-arg", arg ] ); } |
| | | 89 | | |
| | 0 | 90 | | args.Add( "--" ); |
| | 0 | 91 | | args.AddRange( options.Command ); |
| | | 92 | | |
| | 0 | 93 | | return args; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// <c>--environment</c> sets both variables; with neither set, both become <c>Development</c> (as <c>dotnet ef</c> |
| | | 98 | | /// doesn't default to Production. An environment the user set is kept. |
| | | 99 | | /// </summary> |
| | | 100 | | public static Dictionary<string, string> ChildEnvironment( ToolOptions options ) |
| | | 101 | | { |
| | 12 | 102 | | var environment = options.Environment |
| | 12 | 103 | | ?? ( string.IsNullOrEmpty( System.Environment.GetEnvironmentVariable( "DOTNET_ENVIRONMENT" ) ) |
| | 12 | 104 | | && string.IsNullOrEmpty( System.Environment.GetEnvironmentVariable( "ASPNETCORE_ENVIRONMENT" |
| | 12 | 105 | | ? "Development" |
| | 12 | 106 | | : null ); |
| | | 107 | | |
| | 12 | 108 | | return environment is null ? [] : new Dictionary<string, string> { [ "DOTNET_ENVIRONMENT" ] = environment, [ "AS |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | |
| | | 112 | | private static async Task BuildAsync( string startupFile, string framework, ToolOptions options, ToolProcess process |
| | | 113 | | { |
| | 0 | 114 | | if ( options.Verbose ) { await errors.WriteLineAsync( $"Building {Path.GetFileName( startupFile )} ({framework}, |
| | | 115 | | |
| | 0 | 116 | | var (code, output) = await process.CaptureAsync( [ "build", startupFile, "-c", options.Configuration, "-f", fram |
| | | 117 | | |
| | 0 | 118 | | if ( options.Verbose || code != 0 ) { await errors.WriteLineAsync( output ).ConfigureAwait( false ); } |
| | | 119 | | |
| | 0 | 120 | | if ( code != 0 ) { throw new ToolException( $"Build failed: {Path.GetFileName( startupFile )}." ); } |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | private static int Fail( TextWriter errors, string message ) |
| | | 124 | | { |
| | 0 | 125 | | errors.WriteLine( $"error: {message}" ); |
| | | 126 | | |
| | 0 | 127 | | return ToolError; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | private static bool PathsEqual( string left, string right ) |
| | 0 | 131 | | => string.Equals( Path.GetFullPath( left ), Path.GetFullPath( right ), OperatingSystem.IsWindows() ? StringCompa |
| | | 132 | | } |