|
| 1 | +classdef (Hidden) PathSynchronizerHandler < matlabls.handlers.FeatureHandler |
| 2 | + % PATHSYNCHRONIZERHANDLER The feature handler to support synchronizing the MATLAB path |
| 3 | + % with the client's workspace. This provides access points to add and remove from the path, |
| 4 | + % as well as get/set MATLAB's current working directory. |
| 5 | + |
| 6 | + % Copyright 2024 The MathWorks, Inc. |
| 7 | + |
| 8 | + properties (Access = private) |
| 9 | + CdRequestChannel = "/matlabls/pathSynchronizer/cd/request" |
| 10 | + |
| 11 | + PwdRequestChannel = "/matlabls/pathSynchronizer/pwd/request" |
| 12 | + PwdResponseChannel = "/matlabls/pathSynchronizer/pwd/response" |
| 13 | + |
| 14 | + AddPathRequestChannel = "/matlabls/pathSynchronizer/addpath/request" |
| 15 | + RmPathRequestChannel = "/matlabls/pathSynchronizer/rmpath/request" |
| 16 | + end |
| 17 | + |
| 18 | + methods |
| 19 | + function this = PathSynchronizerHandler () |
| 20 | + this.RequestSubscriptions(1) = matlabls.internal.CommunicationManager.subscribe(this.CdRequestChannel, @this.handleCdRequest); |
| 21 | + this.RequestSubscriptions(2) = matlabls.internal.CommunicationManager.subscribe(this.PwdRequestChannel, @this.handlePwdRequest); |
| 22 | + this.RequestSubscriptions(3) = matlabls.internal.CommunicationManager.subscribe(this.AddPathRequestChannel, @this.handleAddPathRequest); |
| 23 | + this.RequestSubscriptions(4) = matlabls.internal.CommunicationManager.subscribe(this.RmPathRequestChannel, @this.handleRmPathRequest); |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + methods (Access = private) |
| 28 | + function handleCdRequest (~, msg) |
| 29 | + path = msg.path; |
| 30 | + |
| 31 | + try |
| 32 | + cd(path) |
| 33 | + catch e |
| 34 | + disp('Error during `cd` operation:') |
| 35 | + disp(e.message) |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + function handlePwdRequest (this, msg) |
| 40 | + try |
| 41 | + currentPath = pwd(); |
| 42 | + |
| 43 | + responseChannel = strcat(this.PwdResponseChannel, '/', msg.channelId); |
| 44 | + matlabls.internal.CommunicationManager.publish(responseChannel, currentPath); |
| 45 | + catch e |
| 46 | + disp('Error during `pwd` operation:') |
| 47 | + disp(e.message) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + function handleAddPathRequest (~, msg) |
| 52 | + paths = msg.paths; |
| 53 | + paths = strjoin(paths, pathsep); |
| 54 | + |
| 55 | + try |
| 56 | + addpath(paths) |
| 57 | + catch e |
| 58 | + disp('Error during `addpath` operation:') |
| 59 | + disp(e.message) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + function handleRmPathRequest (~, msg) |
| 64 | + paths = msg.paths; |
| 65 | + paths = strjoin(paths, pathsep); |
| 66 | + |
| 67 | + try |
| 68 | + rmpath(paths) |
| 69 | + catch e |
| 70 | + disp('Error during `rmpath` operation:') |
| 71 | + disp(e.message) |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | +end |
0 commit comments