forked from gvegayon/devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdt_create_pkg.mata
76 lines (60 loc) · 1.9 KB
/
dt_create_pkg.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
mata
/**
* @brief Creates a .pkg file
* @param pkgnb Name of the pakage and its description
* @param fns List of files to be included in the package
* @param replace whether to replace or not existing pkg files
* @param pkgdir Dir where the package files lie
* @demo
* /* Creating an empty file */
* dt_create_pkg("myexamplepkg",("a.ado","b.ado","a.sthlp","b.sthlp","lab.mlib"))
* stata("view myexamplepkg.pkg")
* dt_erase_file("myexamplepkg.pkg")
*/
void function dt_create_pkg(
string scalar pkgnb ,
| string rowvector fns,
real scalar replace,
string scalar auth,
string scalar description,
string scalar pkgdir
)
{
/* Parsing pkgnamed (name and description) */
pkgnb = strtrim(pkgnb)
string scalar pkgname, pkgbrief
pkgname = ""
pkgbrief = ""
if (regexm(pkgnb, "^([a-zA-Z0-9_]+) (.+)"))
{
pkgname = regexs(1)
pkgbrief = regexs(2)
}
/* Setting the folder */
string scalar olddir
olddir = c("pwd")
if (args() < 6) pkgdir = c("pwd")
if (dt_stata_capture("cd "+pkgdir))
_error(1, "Couldn't find the -"+pkgdir+"- dir")
/* Listing the files */
if (fns==J(1,1,"")) fns = (dir(".","files","*.mlib")\dir(".","files","*.ado")\dir(".","files","*.sthlp")\dir(".","files","*.hlp"))'
if (!length(fns)) return
real scalar fh, i
string scalar fn
/* Checking the replace */
if (replace ==J(1,1,.)) replace = 0
/* Creating the pkg file */
if (fileexists(pkgname+".pkg") & replace) unlink(pkgname+".pkg")
else if (fileexists(pkgname+".pkg") & !replace)
_error(1, "The file -"+pkgname+".pkg- already exists.")
fh = fopen(pkgname+".pkg","w")
fput(fh, "v 3")
fput(fh, "d "+pkgname+ " " + (pkgbrief == ""? "A package compiled by -devtools-." : pkgbrief) )
fput(fh, "d Distribution-Date:"+sprintf("%tdCYND",date(c("current_date"),"DMY")))
fput(fh, "d Author: "+(auth == J(1,1,"") ? c("username") : auth) )
for(i=1;i<=length(fns);i++)
fput(fh,"F "+fns[i])
fclose(fh)
return
}
end