Debian Codegraph @

vicious/tools/headergen

raw symbols SCIP
1 #!/usr/bin/env lua
2 -- copyright header generator
3 -- Copyright (C) 2019 Nguyễn Gia Phong <[email protected]m>
4 --
5 -- This file is part of Vicious.
6 --
7 -- Vicious is free software: you can redistribute it and/or modify
8 -- it under the terms of the GNU General Public License as
9 -- published by the Free Software Foundation, either version 2 of the
10 -- License, or (at your option) any later version.
11 --
12 -- Vicious is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU General Public License for more details.
16 --
17 -- You should have received a copy of the GNU General Public License
18 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
19
20 local ipairs = ipairs
21 local pairs = pairs
22 local tonumber = tonumber
23 local io = { open = io.open, popen = io.popen }
24 local os = { exit = os.exit }
25 local table = {
26 concat = table.concat,
27 insert = table.insert,
28 sort = table.sort
29 }
30
31 local HEADER = [[-- %s
32 %s
33 --
34 -- This file is part of Vicious.
35 --
36 -- Vicious is free software: you can redistribute it and/or modify
37 -- it under the terms of the GNU General Public License as
38 -- published by the Free Software Foundation, either version 2 of the
39 -- License, or (at your option) any later version.
40 --
41 -- Vicious is distributed in the hope that it will be useful,
42 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
43 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 -- GNU General Public License for more details.
45 --
46 -- You should have received a copy of the GNU General Public License
47 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
48
49 %s]]
50 local COMMAND = "git log --date=format:%Y --format='%ad %an <%ae>' "
51
52 if #arg == 0 then
53 print'usage: headergen lua-source-files'
54 os.exit(1)
55 end
56
57 for _, filename in ipairs(arg) do
58 local fi = io.open(filename)
59 local content = fi:read'*a'
60 fi:close()
61
62 local holders = {}
63 local output = io.popen(COMMAND .. filename)
64 for line in output:lines() do
65 local year, author = line:match'(%d+)%s+(.+)'
66 if holders[author] == nil then holders[author] = {} end
67 holders[author][year] = true
68 end
69 output:close()
70
71 local copyrights = {}
72 for author, years in pairs(holders) do
73 local time = {}
74 for year, _ in pairs(years) do table.insert(time, tonumber(year)) end
75 table.sort(time)
76
77 local copyright = { 'Copyright (C) ' }
78 for _, current in ipairs(time) do
79 local prev = tonumber(copyright[#copyright])
80 if prev == nil then
81 table.insert(copyright, current)
82 elseif current - 1 ~= prev then
83 table.insert(copyright, ',')
84 table.insert(copyright, current)
85 elseif copyright[#copyright - 1] == '-' then
86 copyright[#copyright] = current
87 else
88 table.insert(copyright, '-')
89 table.insert(copyright, current)
90 end
91 end
92 table.insert(copyrights,
93 ('-- %s %s'):format(table.concat(copyright), author))
94 end
95
96 local fo = io.open(filename, 'w')
97 table.sort(copyrights)
98 fo:write(HEADER:format(filename, table.concat(copyrights, '\n'), content))
99 fo:close()
100 end