forked from maartenteaches/mkproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkproject_mptools.mata
287 lines (258 loc) · 7.09 KB
/
mkproject_mptools.mata
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
mata:
mata set matastrict on
string scalar mptools::gettoken(string scalar toparse)
{
string scalar first
transmorphic scalar t
t = tokeninit()
tokenset(t, toparse)
first = tokenget(t)
return(first)
}
void mptools::write_header(real scalar fh )
{
real scalar i
mpfput(fh, "<header>")
mpfput(fh, "<mkproject> " + reading.type)
mpfput(fh, "<version> " + invtokens(strofreal(reading.fversion), "."))
mpfput(fh, "<label> " + reading.label)
for (i = 1 ; i<=rows(reading.reqs); i++) {
mpfput(fh, "<reqs> " + reading.reqs[i])
}
if (rows(reading.description) > 0) {
mpfput(fh, "<description>")
}
for(i = 1 ; i<=rows(reading.description); i++ ) {
mpfput(fh, reading.description[i])
}
if (rows(reading.description) > 0) {
mpfput(fh, "</description>")
}
mpfput(fh, "</header>")
}
void mptools::header_ok(string scalar what, string scalar type)
{
string scalar fn
fn = find_file(what, type)
mpfread(fn)
read_header(type)
mpfclose(reading.fh)
}
void mptools::collect_header_info()
{
real scalar header, descopen
string scalar line, EOF, first, second
transmorphic scalar t
EOF = J(0,0,"")
t = tokeninit(" ", "", "<>")
header = 0
descopen = 0
while ((line=mpfget())!= EOF) {
reading.lnr = reading.lnr + 1
tokenset(t,line)
first = tokenget(t)
if (first == "<header>") {
if (header == 1) {
where_err()
errprintf("{p}A header is started when one was already open; not a valide mkproject file {p_end}")
mpfclose(reading.fh)
exit(198)
}
header = 1
}
else if (first == "</header>") {
if (header == 0) {
where_err()
errprintf("{p}A header was closed when none was open; not a valide mkproject file {p_end}")
mpfclose(reading.fh)
exit(198)
}
return
}
else if (first == "<body>" & header == 1) {
where_err()
errprintf("{p}A body was started while a header was still open; not a valid mkproject file{p_end}")
mpfclose(reading.fh)
exit(198)
}
else if (header) {
second = ustrtrim(tokenrest(t))
parse_header(first, second, descopen)
}
}
mpfclose(reading.fh)
if (header == 1) {
where_err()
errprintf("{p}Started a header but never closed it{p_end}")
exit(198)
}
if (descopen == 1) {
where_err()
errprintf("{p}Started a description but never closed it{p_end}")
exit(198)
}
}
void mptools::read_header(| string scalar what)
{
collect_header_info()
if (args()==1) {
chk_header(what)
}
}
void mptools::chk_header(string scalar what)
{
string scalar errmsg
if (reading.type != what) {
errmsg = "{p}Expected to find a mkproject file of type " + what +
" but found a mkproject file of type " + reading.type + "{p_end}"
errprintf(errmsg)
exit(198)
}
header_version(reading.sversion)
}
void mptools::descopenerr(real scalar descopen) {
if (descopen == 1) {
where_err()
errprintf("cannot enter other tags when a description is open")
exit(198)
}
}
void mptools::parse_header(string scalar first, string scalar second, real scalar descopen )
{
if (first == "<mkproject>") {
descopenerr(descopen)
reading.type = second
}
if (first == "<version>") {
descopenerr(descopen)
reading.sversion = second
}
if (first == "<label>") {
descopenerr(descopen)
reading.label = second
}
if (first == "<reqs>") {
descopenerr(descopen)
reading.reqs = reading.reqs \ second
}
if (first == "</description>") {
if (descopen == 0) {
where_err()
errprintf("{p}Tried to close a description when none was open{p_end}")
exit(198)
}
descopen = 0
}
if (descopen == 1) {
reading.description =
reading.description \ (first + " " + second)
}
if (first == "<description>") {
if (descopen == 1) {
where_err()
errprintf("{p}Tried to open a description when one was already open{p_end}")
exit(198)
}
descopen = 1
}
}
real scalar mptools::_chkreq(string scalar req)
{
string rowvector parts
real scalar ok, totest
ok = 1
parts = tokens(req)
if (parts[1] == "Stata") {
if (cols(parts) !=2) {
errprintf(`"{p}You specified a requirement as "<reqs> "' + req + `""{p_end}"')
errprintf(`"{p}The correct form of such a requirement is "<reqs> Stata #", where # is a number{p_end}"')
exit(198)
}
totest = strtoreal(parts[2])
if (totest == . ) {
errprintf(`"{p}You specified a requirement as "<reqs> "' + req + `""{p_end}"')
errprintf(`"{p}The correct form of such a requirement is "<reqs> Stata #", where # is a number{p_end}"')
exit(198)
}
ok = (st_numscalar("c(stata_version)") >= totest)
}
else if (parts[1] == "git") {
if (cols(parts) !=1) {
errprintf(`"{p}You specified a requirement as "<reqs> "' + req + `""{p_end}"')
errprintf(`"{p}The correct form of such a requirement is "<reqs> git"{p_end}"')
exit(198)
}
ok = -1
}
else {
if (cols(parts) !=1) {
errprintf(`"{p}You specified a requirement as "<reqs> "' + req + `""{p_end}"')
errprintf(`"{p}The correct form of such a requirement is "<reqs> cmd", where cmd is the command that should exist{p_end}"')
exit(198)
}
stata("capture which " + parts[1])
totest = st_numscalar("c(rc)")
if (!(totest ==0 | totest == 111)) {
errprintf(`"{p}You specified a requirement as "<reqs> "' + req + `""{p_end}"')
errprintf(`"{p}The correct form of such a requirement is "<reqs> git"{p_end}"')
exit(198)
}
ok = (totest == 0)
}
return(ok)
}
void mptools::chkreqs()
{
real scalar i, problem
problem = 0
for(i=1 ; i <= rows(reading.reqs) ; i++) {
if(_chkreq(reading.reqs[i])==0) {
errprintf("{p}failed requirement: " + reading.reqs[i] + "{p_end}")
problem = 1
}
}
if (problem) {
exit(198)
}
}
string scalar mptools::type2ext(string scalar type)
{
string scalar extension
string vector allowed
allowed = "boilerplate", "project", "default"
if (!anyof(allowed,type)) {
errprintf("{p}Only types boilerplate, project, or default allowed in type2ext(){p_end}")
exit(198)
}
extension = (type == "boilerplate" ? ".mpb" : (type == "project" ? ".mpp" : ".mpd"))
return(extension)
}
string scalar mptools::find_file(string scalar what, string scalar type, | string scalar plus)
{
string scalar path , extension
extension = type2ext(type)
path = pathjoin(pathsubsysdir("PERSONAL"), "m/mp_" + what + extension)
if (!fileexists(path) | plus != "") {
path = pathjoin(pathsubsysdir("PLUS"), "m/mp_" + what + extension)
if (!fileexists(path)) {
errprintf("{p}" + type + " " + what + " cannot be found{p_end}")
exit(601)
}
}
return(path)
}
string scalar mptools::mppathgetparent(string scalar path) {
real scalar pos
pos = max((strrpos(path,"/"),strrpos(path,"\")))
return(substr(path,1,pos))
}
void mptools::graceful_exit()
{
mpfclose_all()
chdir(odir)
}
void mptools::new()
{
odir = pwd()
}
end