| | | 1 | | using System.Text.Json; |
| | | 2 | | |
| | | 3 | | namespace ZhEaIsNsAaBn.DbSeeder.Tool; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// The startup project must reference <c>ZhEaIsNsAaBn.DbSeeder.Design</c> with the tool's <c>major.minor</c> (patch and |
| | | 7 | | /// Read from its <c>deps.json</c> (<c>libraries["ZhEaIsNsAaBn.DbSeeder.Design/x.y.z"]</c>, package or project reference |
| | | 8 | | /// runtime exactly, so this one check fixes both. |
| | | 9 | | /// </summary> |
| | | 10 | | internal static class DesignVersionCheck |
| | | 11 | | { |
| | | 12 | | public const string DesignPackage = "ZhEaIsNsAaBn.DbSeeder.Design"; |
| | | 13 | | |
| | | 14 | | |
| | | 15 | | /// <returns>The error text, or <see langword="null"/> when the versions match.</returns> |
| | | 16 | | /// <param name="toolVersion">The tool's version (e.g. <c>1.1.0</c>, <c>1.1.0-dev-0042</c>).</param> |
| | | 17 | | /// <param name="startupName">The startup project's name.</param> |
| | | 18 | | /// <param name="startupDepsJson">The startup project's <c>deps.json</c> text.</param> |
| | | 19 | | /// <param name="projectName">The <c>--project</c> name when it differs from the startup project.</param> |
| | | 20 | | /// <param name="projectDepsJson">Its <c>deps.json</c> text, when it has one.</param> |
| | | 21 | | public static string? Check( string toolVersion, string startupName, string startupDepsJson, string? projectName = n |
| | | 22 | | { |
| | 32 | 23 | | var design = DesignVersion( startupDepsJson ); |
| | | 24 | | |
| | 32 | 25 | | if ( design is null ) |
| | | 26 | | { |
| | | 27 | | // "\n" joins, not a raw string: the text must not depend on the source file's line endings. |
| | 8 | 28 | | var missing = string.Join( "\n", $"{startupName} doesn't reference {DesignPackage}. Add it to the startup pr |
| | 8 | 29 | | $" <PackageReference Include=\"{DesignPackage}\" Version=\"{toolVersion}\" Priva |
| | 8 | 30 | | "PrivateAssets=\"all\" keeps it out of packages that reference this project." ); |
| | | 31 | | |
| | 8 | 32 | | return projectName is not null && projectDepsJson is not null && DesignVersion( projectDepsJson ) is not nul |
| | 8 | 33 | | ? $"{missing}\nIt is referenced by {projectName}, but PrivateAssets=\"all\" stops it there: add i |
| | 8 | 34 | | : missing; |
| | | 35 | | } |
| | | 36 | | |
| | 40 | 37 | | if ( MajorMinor( design ) == MajorMinor( toolVersion ) ) { return null; } |
| | | 38 | | |
| | | 39 | | |
| | 8 | 40 | | var (major, minor) = MajorMinor( design ); |
| | | 41 | | |
| | 8 | 42 | | return string.Join( "\n", $"The dbseeder tool is {toolVersion} but {startupName} uses {DesignPackage} {design}. |
| | 8 | 43 | | $" dotnet tool update ZhEaIsNsAaBn.DbSeeder.Tool --version {major}.{minor}.*", |
| | 8 | 44 | | $" or set the {DesignPackage} PackageReference to {Plain( toolVersion )}." ); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | |
| | | 48 | | /// The Design version in a deps.json, <see langword="null"/> when it isn't there. |
| | | 49 | | public static string? DesignVersion( string depsJson ) |
| | | 50 | | { |
| | 36 | 51 | | using var document = JsonDocument.Parse( depsJson ); |
| | | 52 | | |
| | 36 | 53 | | if ( !document.RootElement.TryGetProperty( "libraries", out var libraries ) ) { return null; } |
| | | 54 | | |
| | 36 | 55 | | return libraries.EnumerateObject() |
| | 36 | 56 | | .Select( l => l.Name ) |
| | 36 | 57 | | .FirstOrDefault( n => n.StartsWith( DesignPackage + "/", StringComparison.OrdinalIgnoreCase ) )? |
| | 36 | 58 | | } |
| | | 59 | | |
| | 64 | 60 | | private static string Plain( string version ) => version.Split( '-', '+' )[ 0 ]; |
| | | 61 | | |
| | | 62 | | private static (int Major, int Minor) MajorMinor( string version ) |
| | | 63 | | { |
| | 56 | 64 | | var parts = Plain( version ).Split( '.' ); |
| | | 65 | | |
| | 56 | 66 | | return ( int.Parse( parts[ 0 ], System.Globalization.CultureInfo.InvariantCulture ), |
| | 56 | 67 | | parts.Length > 1 ? int.Parse( parts[ 1 ], System.Globalization.CultureInfo.InvariantCulture ) : 0 ); |
| | | 68 | | } |
| | | 69 | | } |