Skip to content

Commit

Permalink
fix: 🚸 improved reset_template
Browse files Browse the repository at this point in the history
code deduplication, MATLAB codes reset, license setup, self-deletion
  • Loading branch information
djmaxus committed Dec 3, 2024
1 parent 76e369d commit 3d05d76
Showing 1 changed file with 65 additions and 68 deletions.
133 changes: 65 additions & 68 deletions reset_template.m
Original file line number Diff line number Diff line change
@@ -1,76 +1,73 @@
function [] = reset_template()
% Define the paths for CHANGELOG.md, version.txt, and README.md
changelogPath = fullfile(pwd, 'CHANGELOG.md');
versionPath = fullfile(pwd, 'version.txt');
readmePath = fullfile(pwd, 'README.md');

% Reset the content of CHANGELOG.md
fid = fopen(changelogPath, 'w');
if fid ~= -1
fclose(fid);
fprintf('CHANGELOG.md has been reset.\n');
else
error('Could not open CHANGELOG.md for writing.');
function [] = reset_template(year,full_name,self_delete)
arguments
year (1,1) int32
full_name char
self_delete (1,1) logical = true;
end

% Write "0.0.1" to version.txt
fid = fopen(versionPath, 'w');
if fid ~= -1
fprintf(fid, '0.0.1\n');
fclose(fid);
fprintf('version has been set to "0.0.1".\n');
else
error('Could not open version.txt for writing.');
end
% Reset MATLAB code examples
reset_file('main.m','function varargout = main(varargin)\nend\n');
rmdir('src','s');
mkdir('src');

% Reset the content of README.md
fid = fopen(readmePath, 'w');
if fid ~= -1
fprintf(fid, '# README\n\n');
fprintf(fid, '![MATLAB checks](../../actions/workflows/matlab-ci.yml/badge.svg)\n');
fclose(fid);
fprintf('README.md has been reset.\n');
else
error('Could not open README.md for writing.');
end
% Reset repository files
reset_file('CHANGELOG.md');
reset_file('version.txt', '0.0.1\n');
reset_file('README.md', ['# README\n\n',...
'![MATLAB checks](../../actions/workflows/matlab-ci.yml/badge.svg)\n']);

% Delete unneeded workflow file
syncMirrorWorkflowPath = fullfile(pwd, '.github/workflows/sync-mirror.yml');
delete(syncMirrorWorkflowPath)

% Create a figure for the UI
hFig = figure('Name', 'Licenses to Keep', 'Position', [300, 300, 300, 150], 'MenuBar', 'none', 'NumberTitle', 'off');

% Create checkboxes for MIT and APACHE 2.0 licenses
mitCheckbox = uicontrol('Style', 'checkbox', 'String', 'MIT License', 'Position', [50, 80, 100, 30]);
apacheCheckbox = uicontrol('Style', 'checkbox', 'String', 'Apache 2.0 License', 'Position', [50, 50, 150, 30]);

% Create Confirm button
uicontrol('Style', 'pushbutton', 'String', 'Confirm', 'Position', [100, 20, 100, 30], 'Callback', @confirmCallback);

% Callback function for the Confirm button
function confirmCallback(~, ~)
% Determine the selected licenses
mitSelected = get(mitCheckbox, 'Value');
apacheSelected = get(apacheCheckbox, 'Value');

% Define the license file paths
mitLicensePath = fullfile(pwd, 'LICENSE-MIT');
apacheLicensePath = fullfile(pwd, 'LICENSE-APACHE');

% Delete the LICENSE-MIT file if MIT is not selected
if ~mitSelected && exist(mitLicensePath, 'file')
delete(mitLicensePath);
fprintf('LICENSE-MIT has been deleted.\n');
end

% Delete the LICENSE-APACHE file if Apache 2.0 is not selected
if ~apacheSelected && exist(apacheLicensePath, 'file')
delete(apacheLicensePath);
fprintf('LICENSE-APACHE has been deleted.\n');
end

% Close the figure after confirmation
close(hFig);
end
% Set license details
set_license('LICENSE',year,full_name);

if ~self_delete
return;
end

% self-delete template resetter
this_file = mfilename();
delete(which(this_file));

end

function reset_file(name,lines)
arguments
name char
lines char = ''
end

full_file = fullfile(pwd, name);
[fid, err_msg] = fopen(full_file, 'w');

if fid == -1
error(['Could not open ', name,' for writing: ',err_msg]);
end

fprintf(fid, lines);
fclose(fid);
fprintf([name, ' has been reset.\n']);
end


function set_license(file_name,year,full_name)
full_file = fullfile(pwd, file_name);

[fid, err_msg] = fopen(full_file,'r');
if fid == -1
error(['Could not open ', name,' for reading: ',err_msg]);
end
license_text=fread(fid,'*char')';
fclose(fid);

upd_license_text = regexprep(license_text,'Copyright \(c\) (.*?)\n', ['Copyright (c) ',num2str(year),', ', full_name,'\n']);

[fid, err_msg] = fopen(full_file,'w');
if fid == -1
error(['Could not open ', name,' for writing: ',err_msg]);
end
fprintf(fid,'%s',upd_license_text);
fclose(fid);
end

0 comments on commit 3d05d76

Please sign in to comment.