Skip to content

Commit

Permalink
improve backups
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcl committed Jan 15, 2021
1 parent 11a91f9 commit 58965bf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "issie",
"version": "1.1.1",
"version": "1.1.2",
"description": "Schematic editor and Simulator",
"homepage": "https://github.com/tomcl/issie",
"bugs": {
Expand Down Expand Up @@ -39,7 +39,7 @@
"title": true
},
"build": {
"appId": "ISSIE.111",
"appId": "ISSIE.112",
"asar": true,
"win": {
"target": "zip"
Expand Down
5 changes: 3 additions & 2 deletions src/Renderer/Interface/FilesIO.fs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ let latestBackupFileData (path:string) (baseName: string) =
|> fun n -> n,fn)
|> List.sortDescending
|> List.tryHead
|> Option.bind (function | None,_ -> None | Some n, fn -> Some(n, fn))
|> Option.bind (function
| None,_ -> None
| Some n, fn -> Some(n, fn))

/// read canvas state from file found on filePath (which includes .dgm suffix etc).
/// return Error if file does not exist or cannot be parsed.
Expand Down Expand Up @@ -298,7 +300,6 @@ let loadAllComponentFiles (folderPath:string) =
match x with
| Error msg -> Error msg
| Ok x ->
printfn "loadAllComponentFiles %s %A" folderPath (x |> Seq.toList)
x
|> Seq.toList
|> List.filter (path.extname >> ((=) ".dgm"))
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/Interface/Version.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Version
let VERSION = [ 1 ; 1 ; 1]
let VERSION = [ 1 ; 1 ; 2]

// The first 12 white-space separated words in this file must be in the above format - note that spaces are required.
// This works as valid F# data for displaying the code version and can also be read programmatically from the master branch github file
Expand Down
35 changes: 20 additions & 15 deletions src/Renderer/UI/FileMenuView.fs
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,37 @@ let writeComponentToFile comp =
let data = stateToJsonString (comp.CanvasState,comp.WaveInfo)
writeFile comp.FilePath data

/// return an option containing sequence data and file path of the latest
/// return an option containing sequence data and file name and directory of the latest
/// backup file for given component, if it exists.
let readLastBackup comp =
let path = pathWithoutExtension comp.FilePath
let baseN = baseName path
let backupDir = pathJoin [| dirName path ; "backup" |]
latestBackupFileData backupDir baseN
|> Option.map (fun (seq, fName) -> seq, fName, backupDir)



/// Write comp to a backup file unless the latest backup canvas is within numChanges distance from
/// the comp canvas.
let writeComponentToBackupFile numChanges comp =
let nSeq, backFilePath =
let nSeq, backupFileName, backFilePath =
match readLastBackup comp with
| Some( n, fp) -> n+1,fp
| None -> 0, ""
| Some( n, fp, path) -> n+1,fp, path
| None -> 0, "", pathJoin [|comp.FilePath; "backup"|]
//printfn "seq=%d,name=%s,path=%s" nSeq backupFileName backFilePath
let wantToWrite =
if backFilePath = "" then
if backupFileName = "" then
true
else
match tryLoadComponentFromPath backFilePath with
let oldBackupFile = pathJoin [|backFilePath ; backupFileName|]
match tryLoadComponentFromPath (oldBackupFile) with
| Ok comp' ->
let nComps,nConns = quantifyChanges comp' comp
nComps + nConns >= numChanges
| _ -> true
nComps + nConns > numChanges
| err ->
printfn "Error: writeComponentToBackup\n%A" err
true
if wantToWrite then
let path = pathWithoutExtension comp.FilePath
let baseN = baseName path
Expand All @@ -111,7 +116,6 @@ let writeComponentToBackupFile numChanges comp =
ensureDirectory <| pathJoin [| dirName path ; "backup" |]
let backupDir = pathJoin [| dirName path ; "backup" |]
let backupPath = pathJoin [| dirName path ; "backup" ; sprintf "%s-%03d-%s.dgm" baseN nSeq suffix |]
printfn "Writing backup file to %s" backupPath
{comp with
TimeStamp = timestamp
FilePath = backupPath}
Expand Down Expand Up @@ -196,11 +200,10 @@ let updateLoadedComponents name (setFun: LoadedComponent -> LoadedComponent) (lc
printf "In updateLoadedcomponents can't find name='%s' in components:%A" name lcLst
lcLst
| Some n ->
let newLc = setFun lcLst.[n]
let nComps,nConns = quantifyChanges newLc lcLst.[n]
if nComps + nConns > 0 then
writeComponentToBackupFile 0 newLc
List.mapi (fun i x -> if i = n then setFun x else x) lcLst
let oldLc = lcLst.[n]
let newLc = setFun oldLc
writeComponentToBackupFile 0 oldLc
List.mapi (fun i x -> if i = n then newLc else x) lcLst

/// return current project with current sheet updated from canvas if needed
let updateProjectFromCanvas (model:Model) =
Expand Down Expand Up @@ -265,7 +268,9 @@ let saveOpenFileAction isAuto model =
let savedWaveSim =
Map.tryFind project.OpenFileName (fst model.WaveSim)
|> Option.map waveSimModel2SavedWaveInfo
Some (makeLoadedComponentFromCanvasData state origLdComp.FilePath DateTime.Now savedWaveSim, reducedState))
let newLdc, newState = makeLoadedComponentFromCanvasData state origLdComp.FilePath DateTime.Now savedWaveSim, reducedState
writeComponentToBackupFile 4 newLdc
Some (newLdc,newState))



Expand Down

0 comments on commit 58965bf

Please sign in to comment.