Skip to content

Commit

Permalink
SJ manifest doesn't need to be generated if files are unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
Atulin committed Feb 4, 2024
1 parent f17679c commit 7dab623
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ public void Generate(params string[] directories)

// We're using static logging here because the generator is not a part of the DI container and thus, we cannot inject it
Log.Information("Preparing JS manifest");

var existingManifest = File.Exists(_manifestPath)
? JsonSerializer.Deserialize(File.ReadAllText(_manifestPath), ManifestJsonContext.Default.Manifest)
: null;

ConcurrentDictionary<string, string> filesAndHashes = new();
ConcurrentDictionary<string, string> filesAndHashesConcurrent = new();

var files = directories
.SelectMany(directory => Directory.GetFiles(Path.Join(Root, directory.Replace(Root, "")), "*.js", SearchOption.AllDirectories))
Expand All @@ -50,7 +54,7 @@ public void Generate(params string[] directories)
if (fileInfo.Exists)
{
var hash = GetHashForFile(fileInfo);
_ = filesAndHashes.TryAdd(file, hash);
_ = filesAndHashesConcurrent.TryAdd(file, hash);
Log.Verbose("\t📃 File {FileName} was found with hash {Hash}", file, hash);
}
else
Expand All @@ -59,14 +63,23 @@ public void Generate(params string[] directories)
}
});

var manifest = JsonSerializer.Serialize(new Manifest(DateTime.UtcNow, filesAndHashes.ToDictionary()), ManifestJsonContext.Default.Manifest);
var filesAndHashes = filesAndHashesConcurrent.ToImmutableSortedDictionary(new AlphaComparer());

if (existingManifest is not null && filesAndHashes.SequenceEqual(existingManifest.Files))
{
stopwatch.Stop();
Log.Information("Files are unchanged, stopping manifest generation after {Time}ms", stopwatch.ElapsedMilliseconds);
return;
}

var manifest = JsonSerializer.Serialize(new Manifest(DateTime.UtcNow, filesAndHashes), ManifestJsonContext.Default.Manifest);
File.WriteAllText(_manifestPath, manifest);

stopwatch.Stop();
Log.Information("Manifest ready ({Time} ms). {FilesFound} files out of {AllFiles} were found.", stopwatch.ElapsedMilliseconds, filesAndHashes.Count, files.Count);
if (files.Except(filesAndHashes.Keys).Any())
Log.Information("Manifest ready ({Time} ms). {FilesFound} files out of {AllFiles} were found.", stopwatch.ElapsedMilliseconds, filesAndHashesConcurrent.Count, files.Count);
if (files.Except(filesAndHashesConcurrent.Keys).Any())
{
Log.Information("Files missing from the manifest: {Files}", files.Except(filesAndHashes.Keys));
Log.Information("Files missing from the manifest: {Files}", files.Except(filesAndHashesConcurrent.Keys));
}
}

Expand All @@ -76,9 +89,17 @@ private static string GetHashForFile(IFileInfo fileInfo)
var hash = SHA256.HashData(readStream);
return WebEncoders.Base64UrlEncode(hash);
}

private class AlphaComparer : IComparer<string>
{
public int Compare(string? x, string? y)
{
return string.CompareOrdinal(x, y);
}
}
}

public sealed record Manifest(DateTime GeneratedAt, Dictionary<string, string> Files);
public sealed record Manifest(DateTime GeneratedAt, ImmutableSortedDictionary<string, string> Files);

[JsonSerializable(typeof(Manifest))]
public partial class ManifestJsonContext : JsonSerializerContext;
2 changes: 1 addition & 1 deletion Ogma3/wwwroot/manifest.js.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"GeneratedAt":"2024-01-31T19:57:50.4259434Z","Files":{"/js/dist/admin/quotes.js":"kXsyB-Vx2Amyv6UfE3Eu_HtEY5rRHGQuDmHqDv_RzeI","/js/dist/tts.js":"n8EcAC1dbOEJf8ZBUDK0sojtPT6NBjDUX_cVuUC-7p0","/js/dist/components/comment-component.js":"C0Ba8_QM7JI8Jp0ikunfXom6zj1LCnaU4-PKMc8Je1Q","/js/dist/input-progressbars.js":"JVfvcMk9qd5FqmZR72AQzYnr9B5QSK2N8Pz4_OS5w_E","/js/dist/components/rating-component.js":"ne4DrPCyAAgGpKf--0zpU7nmxbp8gA01rBC41K7sT9o","/js/dist/admin/users.js":"uTkBcCFTcTZei0p6o7MxGmq0lF-Fs5wEo6SAEfTZnKk","/js/dist/passwords.js":"dusNRJRGZzgkRoGYmqj4VbzRFIOhnY8Vee_L7QmfeAM","/js/dist/admin/categories.js":"mVS9cqjyeOYSTzjnW4MQRsUNCEq4dg6sAAo6z7i0ZVs","/js/dist/account/blacklists.js":"7Fy2n3RB96PoQEx-2xuZagkzb4uTpqxBwdFB7rBOJXg","/js/dist/local-settings.js":"30r0VCZ28IODtwlY4Wb1wWrtds1BOViq2GnQ8kRnse8","/js/dist/admin/tags.js":"18c8azo8hy58Xj6klvdt1jpcQ7K8R4XB-4S8-9QpVEc","/js/dist/themeswap.js":"I7yGQuK797X7HfTZxiPRtX5dV4zb4QxI3UgKIlM4Z9o","/js/dist/components/textarea-counter-component.js":"FuiHKM4Iz5_OhF04nWFmfXdbTl_ntgOvXe8M8zNBwc4","/js/dist/comments.js":"iMUu2K5hz9P2KN2iYWR8ADI7DsvdC0ziRJkHlxt_M0o","/js/dist/watch-chapter-read.js":"-gucQOznoZ2z03nItSzaY5gxkYkHtnc5if0Bxf1PsGU","/js/dist/account/invite-codes.js":"sZuc9uDJyQFtUAuUyNl-fwNQe1b_Y_3LysL4WIqi5Mg","/js/dist/admin/roles.js":"HklsAZbGSwUnI40aFKtHVk3XVSkQ-zMSDZVFKyebno0","/js/bundle/components.js":"0QRQ6WRRz8oM4jf6cWljit3oo1IPGV5cROidDGsBZKU","/js/dist/components/report-modal.js":"8CicbplqflGOYZEr-etObjWw3jM6eD452LpT6UNX4yo","/js/dist/alerts.js":"0xrTKxc16Ry05brwq-WgMdd8RpBzGEOjnRGRNWS-dr0","/js/dist/admin/lib/client.js":"zlpYJOXHw4kkIdXIf8CjvqigkaPuG9ban0OmOF38Q0I","/js/dist/components/input-blog-tags-component.js":"bAiz9BfjVdFPwbpDDoA8dNFYkFpoczzi3B4VrOGf_AE","/js/dist/sw/cache-service-worker.js":"Z6tA3zM5vuBVjyBklXdMmNubQu1qv_iZntioGXoawfo","/js/dist/admin/invite-codes.js":"fowX9tYbWX72ZMCXSfS2U1Lpk0L6zE4YrKDLxtj0pCM","/js/dist/admin/components/manage-infraction-component.js":"-l311L0zq--8kw0QQss036JC-LIHqtTmcdOdXgWhs3k","/js/dist/login.js":"zN3Rs1qrrCrRPkO9gpdl7VD1tdNCkcOS3ktWoYbnpJU","/js/dist/editors/markdown-toolbar.js":"ei3tIeLC6Yi67-1-sZv5Mm8r5Vob6q8Ctda5IhrTnEY","/js/dist/admin/lit-components/table-info.js":"FhKZmmFz99Qm-ClB19awRfvYhC9r_SI2kuAJXnvgVgs","/js/dist/admin/ratings.js":"zN-xY6fAfYXrlTDbE12eZ7Y40NPvvY1M7GgoVF-Qwb8","/js/dist/account/register.js":"lTI6nc18_Dd3jf4E_VZ_r3wZMh86pPb7A-ti_PGeJ88","/js/dist/components/input-counter-component.js":"V-fTwyBNoday6ixZuNgE_m8p2eA_Ehd37Mut048yRQU","/js/dist/components/status-select-component.js":"mlf7A_h4_A2T6DB8JxyWWXtDonbpJVMyepsciSyrHLs","/js/dist/components/toggle-component.js":"A0OAE1B2-Odfr2fQAJLjCO_zk_ExIA54R7qA9Fouz18","/js/dist/components/input-file-component.js":"jIzG9K_xz6IOHTpuQ3ciSvaZKiE47RnoqksrRZwaKU8","/js/dist/admin/faqs.js":"OQ1DMnTYck81jCNLrcjjb5l_T3sKuT9af-OTTv3SAgs","/js/dist/cookie-consent.js":"t_dYerrAhrXLqZqcDmsmhRL4WdizfrBfXVeHv27TkjA","/js/dist/search/blog.js":"IZCZXHNyA1NAlOAqszkhPM5PuXyCus9oqNPXswHIGfM","/js/dist/navbar.js":"4pz9qAGgdWruboXc4Um1qOf8hpf1ZhYVDA1KqkD_kUo","/js/dist/admin/lit-components/cache-info.js":"h7AARyR7b3B4S9ciMuaKs3Eze8nraxtfBgL1WGQ35Ok","/js/dist/vue-directives/closeable.js":"8b1NCfYqzeWlRHch9mhkdc7M45PivuWOQr-7sT453OU","/js/dist/chapter-reads.js":"ZqBGl6ctR3OFI7_3waUAZyLt7vfinoDjmg9cfFaFp-M","/js/dist/shelves.js":"ifj9rogfJQT8u7dPUPrAy1eXxsVIF88TQ1QQLXgcA4E","/js/dist/components/tag-search-select-component.js":"0kkwkqOnv2EiCIGVSLNy42nnRGEETp4A4AxPIwUYY00","/js/dist/search/story.js":"mCwWSkJ83yxJxfX9Uh7seu-ql1bhnDpFzizDlcH2Q7w","/js/dist/site.js":"SvA4PuzeCQwyKpa_s7We3S_9HlMDqNcl69YJTuMI_Mw","/js/dist/admin/settings.js":"R6edHou-OmbP2tpX2SP8_X7ZkgGeOqwrYwvXVr840lc","/js/dist/admin/namespaces.js":"4q9LaBguuoIvQDnnZNMch_D-oyCXLYt3HlNrd_lsqg4","/js/dist/watch-chapter.read.js":"4BO7pAZY8jJ944xdKbl2eHCmILK5ySIymlib7B13UeA","/js/dist/notifications.js":"5t9xxxnjudfs8jxDqmGm864_tMOqocgPvkWmyK7eS9s"}}
{"GeneratedAt":"2024-02-03T23:57:50.7506116Z","Files":{"/js/bundle/components.js":"0QRQ6WRRz8oM4jf6cWljit3oo1IPGV5cROidDGsBZKU","/js/dist/account/blacklists.js":"7Fy2n3RB96PoQEx-2xuZagkzb4uTpqxBwdFB7rBOJXg","/js/dist/account/invite-codes.js":"sZuc9uDJyQFtUAuUyNl-fwNQe1b_Y_3LysL4WIqi5Mg","/js/dist/account/register.js":"lTI6nc18_Dd3jf4E_VZ_r3wZMh86pPb7A-ti_PGeJ88","/js/dist/admin/categories.js":"mVS9cqjyeOYSTzjnW4MQRsUNCEq4dg6sAAo6z7i0ZVs","/js/dist/admin/components/manage-infraction-component.js":"-l311L0zq--8kw0QQss036JC-LIHqtTmcdOdXgWhs3k","/js/dist/admin/faqs.js":"OQ1DMnTYck81jCNLrcjjb5l_T3sKuT9af-OTTv3SAgs","/js/dist/admin/invite-codes.js":"fowX9tYbWX72ZMCXSfS2U1Lpk0L6zE4YrKDLxtj0pCM","/js/dist/admin/lib/client.js":"zlpYJOXHw4kkIdXIf8CjvqigkaPuG9ban0OmOF38Q0I","/js/dist/admin/lit-components/cache-info.js":"h7AARyR7b3B4S9ciMuaKs3Eze8nraxtfBgL1WGQ35Ok","/js/dist/admin/lit-components/table-info.js":"FhKZmmFz99Qm-ClB19awRfvYhC9r_SI2kuAJXnvgVgs","/js/dist/admin/namespaces.js":"4q9LaBguuoIvQDnnZNMch_D-oyCXLYt3HlNrd_lsqg4","/js/dist/admin/quotes.js":"kXsyB-Vx2Amyv6UfE3Eu_HtEY5rRHGQuDmHqDv_RzeI","/js/dist/admin/ratings.js":"zN-xY6fAfYXrlTDbE12eZ7Y40NPvvY1M7GgoVF-Qwb8","/js/dist/admin/roles.js":"HklsAZbGSwUnI40aFKtHVk3XVSkQ-zMSDZVFKyebno0","/js/dist/admin/settings.js":"R6edHou-OmbP2tpX2SP8_X7ZkgGeOqwrYwvXVr840lc","/js/dist/admin/tags.js":"18c8azo8hy58Xj6klvdt1jpcQ7K8R4XB-4S8-9QpVEc","/js/dist/admin/users.js":"uTkBcCFTcTZei0p6o7MxGmq0lF-Fs5wEo6SAEfTZnKk","/js/dist/alerts.js":"0xrTKxc16Ry05brwq-WgMdd8RpBzGEOjnRGRNWS-dr0","/js/dist/chapter-reads.js":"ZqBGl6ctR3OFI7_3waUAZyLt7vfinoDjmg9cfFaFp-M","/js/dist/comments.js":"iMUu2K5hz9P2KN2iYWR8ADI7DsvdC0ziRJkHlxt_M0o","/js/dist/components/comment-component.js":"C0Ba8_QM7JI8Jp0ikunfXom6zj1LCnaU4-PKMc8Je1Q","/js/dist/components/input-blog-tags-component.js":"bAiz9BfjVdFPwbpDDoA8dNFYkFpoczzi3B4VrOGf_AE","/js/dist/components/input-counter-component.js":"V-fTwyBNoday6ixZuNgE_m8p2eA_Ehd37Mut048yRQU","/js/dist/components/input-file-component.js":"jIzG9K_xz6IOHTpuQ3ciSvaZKiE47RnoqksrRZwaKU8","/js/dist/components/rating-component.js":"ne4DrPCyAAgGpKf--0zpU7nmxbp8gA01rBC41K7sT9o","/js/dist/components/report-modal.js":"8CicbplqflGOYZEr-etObjWw3jM6eD452LpT6UNX4yo","/js/dist/components/status-select-component.js":"mlf7A_h4_A2T6DB8JxyWWXtDonbpJVMyepsciSyrHLs","/js/dist/components/tag-search-select-component.js":"0kkwkqOnv2EiCIGVSLNy42nnRGEETp4A4AxPIwUYY00","/js/dist/components/textarea-counter-component.js":"FuiHKM4Iz5_OhF04nWFmfXdbTl_ntgOvXe8M8zNBwc4","/js/dist/components/toggle-component.js":"A0OAE1B2-Odfr2fQAJLjCO_zk_ExIA54R7qA9Fouz18","/js/dist/cookie-consent.js":"t_dYerrAhrXLqZqcDmsmhRL4WdizfrBfXVeHv27TkjA","/js/dist/editors/markdown-toolbar.js":"ei3tIeLC6Yi67-1-sZv5Mm8r5Vob6q8Ctda5IhrTnEY","/js/dist/input-progressbars.js":"JVfvcMk9qd5FqmZR72AQzYnr9B5QSK2N8Pz4_OS5w_E","/js/dist/local-settings.js":"30r0VCZ28IODtwlY4Wb1wWrtds1BOViq2GnQ8kRnse8","/js/dist/login.js":"zN3Rs1qrrCrRPkO9gpdl7VD1tdNCkcOS3ktWoYbnpJU","/js/dist/navbar.js":"4pz9qAGgdWruboXc4Um1qOf8hpf1ZhYVDA1KqkD_kUo","/js/dist/notifications.js":"5t9xxxnjudfs8jxDqmGm864_tMOqocgPvkWmyK7eS9s","/js/dist/passwords.js":"dusNRJRGZzgkRoGYmqj4VbzRFIOhnY8Vee_L7QmfeAM","/js/dist/search/blog.js":"IZCZXHNyA1NAlOAqszkhPM5PuXyCus9oqNPXswHIGfM","/js/dist/search/story.js":"mCwWSkJ83yxJxfX9Uh7seu-ql1bhnDpFzizDlcH2Q7w","/js/dist/shelves.js":"ifj9rogfJQT8u7dPUPrAy1eXxsVIF88TQ1QQLXgcA4E","/js/dist/site.js":"SvA4PuzeCQwyKpa_s7We3S_9HlMDqNcl69YJTuMI_Mw","/js/dist/sw/cache-service-worker.js":"Z6tA3zM5vuBVjyBklXdMmNubQu1qv_iZntioGXoawfo","/js/dist/themeswap.js":"I7yGQuK797X7HfTZxiPRtX5dV4zb4QxI3UgKIlM4Z9o","/js/dist/tts.js":"n8EcAC1dbOEJf8ZBUDK0sojtPT6NBjDUX_cVuUC-7p0","/js/dist/vue-directives/closeable.js":"8b1NCfYqzeWlRHch9mhkdc7M45PivuWOQr-7sT453OU","/js/dist/watch-chapter-read.js":"-gucQOznoZ2z03nItSzaY5gxkYkHtnc5if0Bxf1PsGU","/js/dist/watch-chapter.read.js":"4BO7pAZY8jJ944xdKbl2eHCmILK5ySIymlib7B13UeA"}}

0 comments on commit 7dab623

Please sign in to comment.