Debian Codegraph @

proj_mgmt/updateVersion

raw
1 #!/usr/bin/env lua
2 -- -*- lua -*-
3
4 _G._DEBUG = false
5 local posix = require("posix")
6
7 Version = "1.1"
8
9 require("declare")
10 require("strict")
11 require("string_utils")
12 local Optiks = require("Optiks")
13 local dbg = require("Dbg"):dbg()
14 local s_optionTbl = {}
15 local load = (_VERSION == "Lua 5.1") and loadstring or load
16
17 function optionTbl()
18 return s_optionTbl
19 end
20
21 function main()
22 options()
23 local optionTbl = optionTbl()
24 local fn = optionTbl.pargs[1] or "UnknownFile"
25 local whole
26 local fh
27
28 declare("myFullVersion")
29 declare("myDate")
30 declare("myVersion")
31 myFullVersion = optionTbl.version
32 myDate = optionTbl.date
33
34 local vA = {}
35 for value in myFullVersion:split("%.") do
36 vA[#vA + 1 ] = value
37 end
38
39 myVersion = vA[1] .. "." .. (vA[2] or "0")
40
41 declare("patternA")
42 if (not isFile(optionTbl.patternFile)) then
43 io.stderr:write("missing patternFile\n")
44 os.exit(1)
45 end
46
47 fh = io.open(optionTbl.patternFile,"r")
48 whole = fh:read("*all")
49 fh:close()
50 local ok
51 local func, msg = load(whole)
52 if (func) then
53 ok, msg = pcall(func)
54 else
55 ok = false
56 end
57 if (not ok) then
58 io.stderr:write("Error: unable to load: ", optionTbl.patternFile,"\n", " ", msg, "\n");
59 os.exit(1)
60 end
61
62 fh = io.open(fn,"r")
63 if (not fh) then
64 io.stderr:write("unable to open ",fn,"\n")
65 end
66
67 whole = fh:read("*all")
68 fh:close()
69
70 fh = io.open(fn,"w")
71 for line in whole:split("\n") do
72 for i = 1,#patternA do
73 local pattern = patternA[i][1]
74 local value = patternA[i][2]
75 line = line:gsub(pattern,value)
76 end
77 fh:write(line,"\n")
78 end
79 fh:close()
80 end
81
82 function options()
83 local optionTbl = optionTbl()
84 local usage = "Usage: "
85 local cmdlineParser = Optiks:new{usage=usage, version=Version}
86
87 cmdlineParser:add_option{
88 name = {'-v','--verbose'},
89 dest = 'verbosityLevel',
90 action = 'count',
91 }
92 cmdlineParser:add_option{
93 name = {'-p','--patternFile'},
94 dest = 'patternFile',
95 default = '',
96 action = 'store',
97 }
98 cmdlineParser:add_option{
99 name = {'--version'},
100 dest = 'version',
101 default = '0.0.0',
102 action = 'store',
103 }
104 cmdlineParser:add_option{
105 name = {'--date'},
106 dest = 'date',
107 default = '',
108 action = 'store',
109 }
110 local optTbl, pargs = cmdlineParser:parse(arg)
111
112 for v in pairs(optTbl) do
113 optionTbl[v] = optTbl[v]
114 end
115 optionTbl.pargs = pargs
116
117 end
118
119 --------------------------------------------------------------------------
120 -- Return true if file exists is and is a file or link.
121 -- @param fn A file path
122 function isFile(fn)
123 if (fn == nil) then return false end
124 local t = posix.stat(fn,"type")
125
126 local result = ((t == "regular") or (t == "link"))
127
128 return result
129 end
130
131 main()