From 1d057a42688bd9d78885f61dd52b4bd3cb6495b1 Mon Sep 17 00:00:00 2001
From: Mauco03 <marco.gaibotti@skywarder.eu>
Date: Sun, 29 Dec 2024 21:12:34 +0100
Subject: [PATCH] [chad-updates] Mission now adds corresponding path
 automatically

When setting the mission name, the corresponding mission gets added to the MATLAB path, while others are removed.

To disable this, set 'changeMatlabPath' to false during creation of the mission
---
 classes/Mission.m | 49 ++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 38 insertions(+), 11 deletions(-)

diff --git a/classes/Mission.m b/classes/Mission.m
index 3b5cb60..25b1eac 100644
--- a/classes/Mission.m
+++ b/classes/Mission.m
@@ -24,12 +24,14 @@ classdef Mission < Config
     properties(Access = protected)
         configName = 'missionConfig.m'
         variableName = ''
+        changeMatlabPath (1,1)  logical
     end
 
     methods
-        function obj = Mission(mission)
+        function obj = Mission(mission, options)
             arguments
                 mission {mustBeA(mission, {'logical', 'string', 'char'})} = false
+                options.changeMatlabPath = true;
             end
 
             if nargin == 0
@@ -39,6 +41,7 @@ classdef Mission < Config
             filePath = fullfile(fileparts(mfilename("fullpath")), '..', 'missions');
             filePath = trimPath(filePath);
 
+            obj.changeMatlabPath = options.changeMatlabPath;
             obj.currentPath = filePath;
             if isa(mission, 'char') || isa(mission, 'string')
                 obj.name = mission;
@@ -70,20 +73,11 @@ classdef Mission < Config
                 obj.name = name;
             end
             obj.updatePath();
+            obj.updateMatlabPath();
         end
     end
 
     methods(Access = protected)
-        function updatePath(obj)
-            if isempty(obj.name) || isempty(obj.currentPath), return; end
-
-            obj.configPath = fullfile(obj.currentPath, obj.name, 'config');
-            obj.dataPath = fullfile(obj.currentPath, obj.name, 'data');
-            obj.msaPath = trimPath( ...
-                fullfile(obj.currentPath, '..', '..', '..', 'msa-toolkit') ...
-                );
-        end
-
         function loadConfig(obj)
             fileName = obj.configName;
             filePath = obj.currentPath;
@@ -97,6 +91,39 @@ classdef Mission < Config
             configObj = eval(varName);
             obj.name = configObj.name;
         end
+
+        function updatePath(obj)
+            if isempty(obj.name) || isempty(obj.currentPath), return; end
+
+            obj.configPath = fullfile(obj.currentPath, obj.name, 'config');
+            obj.dataPath = fullfile(obj.currentPath, obj.name, 'data');
+            obj.msaPath = trimPath( ...
+                fullfile(obj.currentPath, '..', '..', '..', 'msa-toolkit') ...
+                );
+        end
+
+        function updateMatlabPath(obj)
+            if isempty(obj.name) || isempty(obj.currentPath), return; end
+            if ~obj.changeMatlabPath, return; end
+            matlabPath = path;
+            % Checking if other missions are in path
+            pattern = 'missions\\[^\\/]*;'; % Matches any config folder under mission in path
+            missionsInPath = regexp(matlabPath, pattern);
+            % Checking if right mission is in path
+            pattern = strcat('missions[\\/]', obj.name,';');
+            missionInPath = regexp(matlabPath, pattern, 'once');
+
+            if length(missionsInPath) > 1 || isempty(missionInPath)
+                warning('off');
+                rmpath( ...
+                    genpath(obj.currentPath));
+                addpath( ...
+                    obj.currentPath, ...
+                    genpath( ...
+                    fullfile(obj.currentPath, obj.name)));
+                warning('on');
+            end
+        end
     end
 
     methods(Static, Access = protected)
-- 
GitLab