From 93ef4bf794b0e52e67346d1afde2700f0a8bd9fc Mon Sep 17 00:00:00 2001
From: Lewin Probst <info@emirror.de>
Date: Sun, 26 Apr 2020 13:56:50 +0200
Subject: [PATCH] Updated versioning system. Artoa now checks if your loaded
 interim file is compatible to the artoa version you are using.

---
 VERSION                                       |   2 +-
 lib/+artoa/+controller/+file/loadInterim.m    |   8 +-
 .../+controller/+main/getVersionString.m      |  12 ---
 lib/+artoa/+gui/main.m                        |   2 +-
 lib/+artoa/+migrations/apply.m                |  85 ---------------
 lib/+artoa/+versioning/+migrations/apply.m    | 100 ++++++++++++++++++
 .../+migrations/getLatestVersionNumber.m      |  17 +++
 .../+migrations/versions/v4176.m              |   0
 lib/+artoa/+versioning/checkInterim.m         |  44 ++++++++
 lib/+artoa/+versioning/getVersionString.m     |  24 +++++
 10 files changed, 194 insertions(+), 100 deletions(-)
 delete mode 100644 lib/+artoa/+controller/+main/getVersionString.m
 delete mode 100644 lib/+artoa/+migrations/apply.m
 create mode 100644 lib/+artoa/+versioning/+migrations/apply.m
 create mode 100644 lib/+artoa/+versioning/+migrations/getLatestVersionNumber.m
 rename lib/+artoa/{ => +versioning}/+migrations/versions/v4176.m (100%)
 create mode 100644 lib/+artoa/+versioning/checkInterim.m
 create mode 100644 lib/+artoa/+versioning/getVersionString.m

diff --git a/VERSION b/VERSION
index 12e2555..6fc1e6e 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-177
\ No newline at end of file
+178
\ No newline at end of file
diff --git a/lib/+artoa/+controller/+file/loadInterim.m b/lib/+artoa/+controller/+file/loadInterim.m
index c6ecba3..35b55b5 100644
--- a/lib/+artoa/+controller/+file/loadInterim.m
+++ b/lib/+artoa/+controller/+file/loadInterim.m
@@ -2,7 +2,7 @@ function [] = loadInterim(~, ~)
 %LOADINTERIM Loads the current state of ARTOA4 from the file specified by dialog.
 %   
 
-global artoaDataInput;
+global artoaDataInput artoaWorkspace;
 
 %% Get required variables
 folder = artoa.data.getMember(artoaDataInput, {'ini', 'directory', 'interim'}, pwd());
@@ -35,5 +35,11 @@ artoa.controller.edit.offsets.updateGui();
 %% Update menu buttons
 artoa.controller.updateMenuButtons();
 
+%% Notify user if he loaded an outdated interim version
+[upToDate, message] = artoa.versioning.checkInterim(artoaWorkspace);
+if ~upToDate
+    msgbox(message, 'Interim file version outdated!', 'warn');
+end
+
 end
 
diff --git a/lib/+artoa/+controller/+main/getVersionString.m b/lib/+artoa/+controller/+main/getVersionString.m
deleted file mode 100644
index 9ae4668..0000000
--- a/lib/+artoa/+controller/+main/getVersionString.m
+++ /dev/null
@@ -1,12 +0,0 @@
-function [versionString] = getVersionString()
-%GETVERSIONSTRING Summary of this function goes here
-%   Detailed explanation goes here
-
-%% Get version information
-versionString = [ ...
-    'Version 4.' ...
-    strtrim(fileread(fullfile(fileparts(mfilename('fullpath')), '..', '..', '..', '..', 'VERSION'))) ...
-];
-
-end
-
diff --git a/lib/+artoa/+gui/main.m b/lib/+artoa/+gui/main.m
index 4a73ae7..248b10a 100644
--- a/lib/+artoa/+gui/main.m
+++ b/lib/+artoa/+gui/main.m
@@ -238,7 +238,7 @@ gitInfo = getGitInfo();
 
 artoaGui.main.textVersionInfo = uicontrol( ...
     'Parent', artoaGui.figures.main, ...
-    'String', artoa.controller.main.getVersionString(), ...
+    'String', artoa.versioning.getVersionString(), ...
     'Style', 'text', ...
     'FontSize', 10, ...
     'Units', 'characters', ...
diff --git a/lib/+artoa/+migrations/apply.m b/lib/+artoa/+migrations/apply.m
deleted file mode 100644
index e9b1cf3..0000000
--- a/lib/+artoa/+migrations/apply.m
+++ /dev/null
@@ -1,85 +0,0 @@
-function [] = apply(pInterimFilename)
-%APPLY Applies all migrations that are required to upgrade to the latest version.
-%   
-
-global artoaWorkspace artoaDataInput;
-
-workspaceSaved = false;
-
-%% Check if workspace needs to be saved
-if ~isempty(artoaWorkspace) && ~isempty(artoaDataInput)
-    workspaceSaved = true;
-end
-%% Save workspace
-if workspaceSaved
-    disp('Saving workspace...');
-    tmpWorkspace = artoaWorkspace;
-    tmpDataInput = artoaDataInput;
-    clear artoaWorkspace artoaDataInput;
-    disp('...success!');
-end
-
-%% Check if file exists
-if (~isfile(pInterimFilename))
-    warning('The given file does not exist! Aborting...');
-    restoreWorkspace();
-    return
-end
-
-%% Find all versions available
-[versionsFilePath, ~, ~] = fileparts(mfilename('fullpath'));
-versionsFileNames = dir(fullfile(versionsFilePath, 'versions', 'v*.m'));
-
-%% Load the interim file
-load(pInterimFilename, 'artoaWorkspace', 'artoaDataInput', '-mat');
-
-%% Get itm version
-itmVersion = 'unknown';
-if isfield(artoaWorkspace, 'artoaVersion')
-    itmVersion = artoaWorkspace.artoaVersion;
-end
-disp(['Starting migration from Version ' itmVersion ' to Version ' versionsFileNames(end).name(2:end - 2)]);
-
-%% Modify required variables
-for o = 1:size(versionsFileNames)
-    if (versionsFileNames(o).isdir)
-        warning([versionsFileNames(o).name 'is a directory and will be omitted...']);
-        continue;
-    end
-    currentVersionNumber = versionsFileNames(o).name(2:end - 2);
-    if ~strcmp('unknown Version', itmVersion) & (currentVersionNumber <= itmVersion)
-        continue;
-    end
-    disp(['Applying Version' currentVersionNumber '...']);
-    run(fullfile(versionsFileNames(o).folder, versionsFileNames(o).name));
-    artoaWorkspace.artoaVersion = currentVersionNumber;
-    disp('...done!');
-end
-
-%% Save with added version number
-
-[path, name, ext] = fileparts(pInterimFilename);
-adjustedFilename = fullfile(path, [name '_' versionsFileNames(end).name(2:end - 2) ext]);
-save( ...
-    adjustedFilename, ...
-    'artoaWorkspace', 'artoaDataInput', ...
-    '-mat' ...
-    );
-
-%% Restore workspace
-if workspaceSaved
-    restoreWorkspace();
-end
-
-disp(['Adjusted interim file has been written to:' newline adjustedFilename]);
-
-    function restoreWorkspace()
-        disp('Restoring workspace...');
-        clear artoaWorkspace artoaDataInput;
-        artoaWorkspace = tmpWorkspace;
-        artoaDataInput = tmpDataInput;
-        disp('...success!');
-    end
-
-end
-
diff --git a/lib/+artoa/+versioning/+migrations/apply.m b/lib/+artoa/+versioning/+migrations/apply.m
new file mode 100644
index 0000000..abc0b3a
--- /dev/null
+++ b/lib/+artoa/+versioning/+migrations/apply.m
@@ -0,0 +1,100 @@
+function [] = apply(pInterimFilename)
+%APPLY Applies all migrations that are required to upgrade to the latest version.
+%   
+
+global artoaWorkspace artoaDataInput;
+
+workspaceSaved = false;
+success = true;
+
+%% Check if workspace needs to be saved
+if ~isempty(artoaWorkspace) && ~isempty(artoaDataInput)
+    workspaceSaved = true;
+end
+%% Save workspace
+if workspaceSaved
+    disp('Saving workspace...');
+    tmpWorkspace = artoaWorkspace;
+    tmpDataInput = artoaDataInput;
+    artoaWorkspace = [];
+    artoaDataInput = [];
+    disp('...success!');
+end
+
+try
+    %% Check if file exists
+    if (~isfile(pInterimFilename))
+        warning('The given file does not exist! Aborting...');
+        restoreWorkspace();
+        return
+    end
+
+    %% Find all versions available
+    [versionsFilePath, ~, ~] = fileparts(mfilename('fullpath'));
+    versionsFileNames = dir(fullfile(versionsFilePath, 'versions', 'v*.m'));
+
+    %% Load the interim file
+    load(pInterimFilename, 'artoaWorkspace', 'artoaDataInput', '-mat');
+
+    %% Get itm version
+    itmVersion = 'unknown';
+    if isfield(artoaWorkspace, 'artoaVersion')
+        itmVersion = artoaWorkspace.artoaVersion;
+    end
+    disp(['Starting migration from Version ' itmVersion ' to Version ' versionsFileNames(end).name(2:end - 2)]);
+
+    %% Modify required variables
+    for o = 1:size(versionsFileNames)
+        if (versionsFileNames(o).isdir)
+            warning([versionsFileNames(o).name 'is a directory and will be omitted...']);
+            continue;
+        end
+        currentVersionNumber = versionsFileNames(o).name(2:end - 2);
+        if ~strcmp('unknown', itmVersion) & (currentVersionNumber <= itmVersion)
+            continue;
+        end
+        disp(['Applying Version' currentVersionNumber '...']);
+        run(fullfile(versionsFileNames(o).folder, versionsFileNames(o).name));
+        artoaWorkspace.artoaVersion = currentVersionNumber;
+        disp('...done!');
+    end
+
+    %% Save with added version number
+
+    [path, name, ext] = fileparts(pInterimFilename);
+    adjustedFilename = fullfile(path, [name '_' versionsFileNames(end).name(2:end - 2) ext]);
+    save( ...
+        adjustedFilename, ...
+        'artoaWorkspace', 'artoaDataInput', ...
+        '-mat' ...
+        );
+catch Ex
+    disp(Ex);
+    success = false;
+end
+    
+%% Restore workspace
+if workspaceSaved
+    restoreWorkspace();
+else
+    clearvars -global artoaWorkspace artoaDataInput;
+end
+
+if success
+    disp(['Adjusted interim file has been written to:' newline adjustedFilename]);
+end
+
+
+%% Nested functions
+
+    function restoreWorkspace()
+        disp('Restoring workspace...');
+        artoaWorkspace = [];
+        artoaDataInput = [];
+        artoaWorkspace = tmpWorkspace;
+        artoaDataInput = tmpDataInput;
+        disp('...success!');
+    end
+
+end
+
diff --git a/lib/+artoa/+versioning/+migrations/getLatestVersionNumber.m b/lib/+artoa/+versioning/+migrations/getLatestVersionNumber.m
new file mode 100644
index 0000000..95da646
--- /dev/null
+++ b/lib/+artoa/+versioning/+migrations/getLatestVersionNumber.m
@@ -0,0 +1,17 @@
+function [maxVersion] = getLatestVersionNumber()
+%GETLATESTVERSIONNUMBER Summary of this function goes here
+%   Detailed explanation goes here
+
+
+%% Get all migration versions
+[versionsFilePath, ~, ~] = fileparts(mfilename('fullpath'));
+versionsFileNames = dir(fullfile(versionsFilePath, 'versions', 'v*.m'));
+
+%% Extract version numbers
+versions = {versionsFileNames(:).name};
+versions = str2double(cellfun(@(x) x(3:end-2), versions, 'UniformOutput', false));
+
+maxVersion = max(versions);
+
+end
+
diff --git a/lib/+artoa/+migrations/versions/v4176.m b/lib/+artoa/+versioning/+migrations/versions/v4176.m
similarity index 100%
rename from lib/+artoa/+migrations/versions/v4176.m
rename to lib/+artoa/+versioning/+migrations/versions/v4176.m
diff --git a/lib/+artoa/+versioning/checkInterim.m b/lib/+artoa/+versioning/checkInterim.m
new file mode 100644
index 0000000..f776e1a
--- /dev/null
+++ b/lib/+artoa/+versioning/checkInterim.m
@@ -0,0 +1,44 @@
+function [upToDate, message] = checkInterim(pArtoaWorkspace)
+%CHECKINTERIMVERSION Summary of this function goes here
+%   Detailed explanation goes here
+
+
+%% Initialize return variables
+upToDate = true;
+message = '';
+
+%% Check if the stored version is the most recent one
+currentArtoaVersion = str2num(artoa.versioning.getVersionString(true));
+maxMigration = artoa.versioning.migrations.getLatestVersionNumber();
+
+if ~isfield(pArtoaWorkspace, 'artoaVersion')
+    message = [ ...
+    'You loaded an iterim file with an unknown Version.' newline ...
+    'Your artoa4 instance is on Version 4.' num2str(currentArtoaVersion) '.' newline ...
+    'Please consider upgrading your interim file using the' newline newline ...
+    'artoa.versioning.migrations.apply(yourArtoaFilePath)' newline newline ...
+    'command.' newline ...
+    'Artoa might be unstable otherwise!'
+    ];
+    upToDate = false;
+    return;
+end
+
+workspaceVersion = str2num(pArtoaWorkspace.artoaVersion(2:end));
+if workspaceVersion < currentArtoaVersion & workspaceVersion < maxMigration
+    message = [ ...
+        'You loaded an iterim file that has Version 4.' ...
+        num2str(workspaceVersion) ...
+        '.' newline ...
+        'Your artoa4 instance is on Version 4.' num2str(currentArtoaVersion) '.' newline ...
+        'Please consider upgrading your interim file using the' newline newline ...
+        'artoa.versioning.migrations.apply(yourArtoaFilePath)' newline newline ...
+        'command.' newline ...
+        'Artoa might be unstable otherwise!'
+        ];
+    upToDate = false;
+else 
+
+
+end
+
diff --git a/lib/+artoa/+versioning/getVersionString.m b/lib/+artoa/+versioning/getVersionString.m
new file mode 100644
index 0000000..c82b965
--- /dev/null
+++ b/lib/+artoa/+versioning/getVersionString.m
@@ -0,0 +1,24 @@
+function [versionString] = getVersionString(pCommitNumberOnly)
+%GETVERSIONSTRING Summary of this function goes here
+%   Detailed explanation goes here
+
+%% Initialize input parameter
+if nargin == 0
+    pCommitNumberOnly = false;
+end
+
+%% Read version string
+versionString = strtrim( ...
+    fileread( ...
+    fullfile(fileparts(mfilename('fullpath')), '..', '..', '..', 'VERSION') ...
+    ) ...
+    );
+
+if pCommitNumberOnly
+    return;
+end
+
+versionString = ['Version 4.' versionString];
+
+end
+
-- 
GitLab