forked from florianschanda/miss_hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
miss_hit.m
119 lines (77 loc) · 3.03 KB
/
miss_hit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
% (c) Copyright 2019-2020 Zenuity AB
classdef miss_hit < handle
properties (Constant, Access = private)
here = fileparts(mfilename('fullpath'))
end
methods (Static)
function init()
end
function currentFile()
% Run miss_hit on the currently opened file.
doc = matlab.desktop.editor.getActive();
assert(doc.Editable, 'File is not editable.');
assert(~doc.Modified, 'Save file before you proceed.');
miss_hit.file(doc.Filename);
matlab.desktop.editor.openDocument(doc.Filename);
end
function cached()
% Run miss_hit on cached files in repository.
files = git_retrieve_files('git --no-pager diff --name-only');
miss_hit.run(files);
end
function lastCommit()
% Run miss_hit on files changed in the last commit.
files = git_retrieve_files( ...
'git --no-pager diff HEAD^ HEAD --name-only');
miss_hit.run(files);
end
function file(file)
% Run miss_hit on given file.
miss_hit.run(file);
end
function currentFolder()
% Run miss_hit on the current folder.
miss_hit.folder(cd());
end
function folder(folder)
% Run miss_hit on the given folder.
miss_hit.run(folder);
end
end
methods (Static, Access = private)
function run(file_or_folder)
AssertFileOrFolder = @(f) ...
assert(ischar(f) && (isfolder(f) || isfile(f)), ...
'Not a file or folder.');
if iscell(file_or_folder)
cellfun(AssertFileOrFolder, file_or_folder);
file_or_folder = strjoin(file_or_folder);
else
AssertFileOrFolder(file_or_folder);
end
% Do the auto-fixes
python_command = strjoin({ ...
'cd', miss_hit.here, '&&', ...
'python3', 'mh_style', file_or_folder, ...
'--fix', '--no-style'});
system(python_command, '-echo');
% Generate report for remaining issues.
report_name = fullfile(miss_hit.here, 'report.html');
python_command = strjoin({ ...
'cd', miss_hit.here, '&&', ...
'python3', 'mh_style', file_or_folder, ...
'--html', report_name});
system(python_command);
web(report_name);
end
end
end
function files = git_retrieve_files(msg)
fldr = cd();
[s, m] = unix(strjoin({'cd', fldr, '&&', msg}));
assert(s == 0, 'Could not execute git command.');
f = strsplit(m);
f(cellfun(@isempty, f)) = [];
assert(~isempty(f), 'No files changed.');
files = cellfun(@(e) fullfile(fldr, e), f, 'un', false)';
end