@@ -380,6 +380,42 @@ module Program =
380
380
return 0
381
381
}
382
382
383
+ let migrateLibrary ( libraryFile : string ) =
384
+ result {
385
+ // Make sure the config no longer uses the `LastScanned` field.
386
+ do printfn " Making sure the `LastScanned` field is replaced with `LastUpdated`."
387
+ let! content = IO.readTextFromFile libraryFile
388
+ let updatedContent = content.Replace( " \" LastScanned\" : \" " , " \" LastUpdated\" : \" " )
389
+ let! library = Library.deserialize updatedContent
390
+ let updatedLibrary = { library with LastUpdated = DateTime.Now }
391
+
392
+ // Update paths in the config to be absolute.
393
+ do printfn " Making sure the sources use absolute paths."
394
+ let rootPath = Path.GetDirectoryName libraryFile
395
+ let rootPath = Path.Combine( Environment.CurrentDirectory, rootPath)
396
+ let updateSource ( book : Audiobook.Audiobook ) : Audiobook.Audiobook =
397
+ do printfn " Migrating relative path to use the root '%s '." rootPath
398
+ let combinator ( rootPath : string ) ( filename : string ) =
399
+ if filename.StartsWith( rootPath) then
400
+ do printfn " Skipping '%s ' because it already starts with the root path." filename
401
+ filename
402
+ else
403
+ let updated = Path.Combine( rootPath, filename)
404
+ do printfn " Updated '%s ' to '%s '." filename updated
405
+ updated
406
+
407
+ let configuredCombinator = combinator rootPath
408
+ let updatedSource = match book.Source with
409
+ | Audiobook.AudiobookSource.SingleFile f ->
410
+ configuredCombinator f |> Audiobook.AudiobookSource.SingleFile
411
+ | Audiobook.AudiobookSource.MultiFile m ->
412
+ m |> List.map configuredCombinator |> Audiobook.AudiobookSource.MultiFile
413
+ { book with Source = updatedSource }
414
+ let updatedFilesLibrary = { library with Audiobooks = updatedLibrary.Audiobooks |> List.map updateSource }
415
+ do ! updatedFilesLibrary |> Library.serialize |> IO.writeTextToFile libraryFile
416
+ return 0
417
+ }
418
+
383
419
[<EntryPoint>]
384
420
let main argv =
385
421
let r = result {
@@ -420,6 +456,8 @@ module Program =
420
456
return 1
421
457
| Config.Write writeConfig ->
422
458
return ! write config.LibraryFile writeConfig
459
+ | Config.Migrate ->
460
+ return ! migrateLibrary config.LibraryFile
423
461
| Config.Version ->
424
462
do printfn " %s " Version.current
425
463
return 0
0 commit comments