Skip to content

Commit fbe972a

Browse files
committed
Install: Use DRY method for markers in startup.m
1 parent c8c7a7f commit fbe972a

1 file changed

Lines changed: 31 additions & 47 deletions

File tree

mhkit/package/+mhkit/+configure_environment/uninstall.m

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
function success = uninstall()
2-
% Remove MHKiT environment fixes from MATLAB startup script
2+
% Remove MHKiT environment fixes from MATLAB startup script using shared utilities
33

4-
[startup_file, ~] = get_startup_file_location();
4+
% Get spec for constants
5+
spec = mhkit.spec();
56

6-
if ~exist(startup_file, 'file') || ~has_mhkit_fixes(startup_file)
7+
% Get startup file location using shared utility
8+
[~, startup_file] = mhkit.matlab.get_matlab_user_directory(spec);
9+
10+
if ~has_mhkit_fixes(startup_file, spec)
711
fprintf('No MHKiT fixes found to remove.\n');
812
success = true;
913
return;
1014
end
1115

12-
success = remove_mhkit_fixes(startup_file);
16+
success = remove_mhkit_fixes(startup_file, spec);
1317

1418
if success
1519
fprintf('✓ Removed MHKiT fixes from: %s\n', startup_file);
@@ -19,57 +23,37 @@
1923
end
2024
end
2125

22-
function [startup_file, matlab_user_dir] = get_startup_file_location()
23-
matlab_user_dir = userpath;
24-
if isempty(matlab_user_dir)
25-
if ispc
26-
matlab_user_dir = fullfile(getenv('USERPROFILE'), 'Documents', 'MATLAB');
27-
else
28-
matlab_user_dir = fullfile(getenv('HOME'), 'Documents', 'MATLAB');
29-
end
30-
else
31-
matlab_user_dir = strtrim(matlab_user_dir);
32-
if endsWith(matlab_user_dir, pathsep)
33-
matlab_user_dir = matlab_user_dir(1:end-1);
34-
end
35-
end
36-
startup_file = fullfile(matlab_user_dir, 'startup.m');
37-
end
26+
function has_fixes = has_mhkit_fixes(startup_file, spec)
27+
% Check if startup.m contains MHKiT fixes using shared utilities
3828

39-
function has_fixes = has_mhkit_fixes(startup_file)
40-
has_fixes = false;
41-
try
42-
fid = fopen(startup_file, 'r');
43-
if fid == -1, return; end
44-
content = fread(fid, '*char')';
45-
fclose(fid);
46-
has_fixes = contains(content, '%% MHKiT Environment Setup - START');
47-
catch
29+
[success, content] = mhkit.io.read_file_safe(startup_file);
30+
if ~success
31+
has_fixes = false;
32+
return;
4833
end
49-
end
5034

51-
function success = remove_mhkit_fixes(startup_file)
52-
success = false;
53-
try
54-
fid = fopen(startup_file, 'r');
55-
if fid == -1, return; end
56-
content = fread(fid, '*char')';
57-
fclose(fid);
35+
start_marker = spec.constants.startup_script.start_marker;
36+
has_fixes = contains(content, start_marker);
37+
end
5838

59-
cleaned_content = remove_mhkit_section(content);
39+
function success = remove_mhkit_fixes(startup_file, spec)
40+
% Remove MHKiT fixes using shared utilities
6041

61-
fid = fopen(startup_file, 'w');
62-
if fid == -1, return; end
63-
fprintf(fid, '%s', cleaned_content);
64-
fclose(fid);
65-
success = true;
66-
catch
42+
[read_success, content] = mhkit.io.read_file_safe(startup_file);
43+
if ~read_success
44+
success = false;
45+
return;
6746
end
47+
48+
cleaned_content = remove_mhkit_section(content, spec);
49+
success = mhkit.io.write_file_safe(startup_file, cleaned_content);
6850
end
6951

70-
function cleaned_content = remove_mhkit_section(content)
71-
start_marker = '%% MHKiT Environment Setup - START';
72-
end_marker = '%% MHKiT Environment Setup - END';
52+
function cleaned_content = remove_mhkit_section(content, spec)
53+
% Remove MHKiT section using constants from spec
54+
55+
start_marker = spec.constants.startup_script.start_marker;
56+
end_marker = spec.constants.startup_script.end_marker;
7357

7458
start_pos = strfind(content, start_marker);
7559
end_pos = strfind(content, end_marker);

0 commit comments

Comments
 (0)