-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.ado
174 lines (155 loc) · 6.84 KB
/
git.ado
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
*! version 1.1
version 12.0
capture program drop git
program define git
syntax anything(name=gitArgs id="git command and arguments")
/**
* This requires that you have git installed and accessible through the command line.
* For instructions on installing git, see: http://git-scm.com/downloads
*/
di as yellow "------ WARNING:---------"
di as yellow " This program requires git to be accessible through the command line."
di as yellow " See http://git-scm.com/downloads"
di as yellow "------------------------"
/*
OS-dependent vars
*/
local os = "`c(os)'"
local adoPlusDir = "`c(sysdir_plus)'"
if("`os'" != "Windows"){
local adoPlusDir = subinstr("`adoPlusDir'","~","/Users/`c(username)'",.)
}
local adoDir = trim(subinstr("`adoPlusDir'","ado/plus/","",.))
local gitDir = "`adoDir'git/"
local copyCmd = "cp" /* Defaults to *nix command */
local deleteCmd = "rm" /* Defaults to *nix command */
local origDir = "`c(pwd)'"
local lsCmd = "ls" /* Defaults to *nix command */
local rmdirCmd = "rm -rf" /* Defaults to *nix command */
if("`os'" == "Windows"){
local copyCmd = "copy"
local deleteCmd = "erase"
local lsCmd = "dir"
local rmdirCmd = "rmdir /Q /S"
}
/* Make the git program dir if not there and cd to it */
/* use /nul to deal with the directory as it would be a file (for stata) */
capture confirm file "`gitDir'/nul"
if _rc!=0{
capture mkdir "`gitDir'"
}
qui cd "`gitDir'"
/*
Command parsing
*/
if(strpos("`gitArgs'"," ") >0){
local gitCommand = trim(substr("`gitArgs'",1, strpos("`gitArgs'"," ")))
}
else{
local gitCommand = trim("`gitArgs'")
}
local gitParam1 = trim(subinstr("`gitArgs'","`gitCommand'","",1))
if("`gitCommand'" != "install" & "`gitCommand'" != "uninstall" & "`gitCommand'" != "update" & "`gitCommand'" != "list"){
di as red "`gitCommand' is not a valid git command"
di as yellow "Expected one of: install, uninstal, update, list."
exit
}
/* Translate "install" to "clone". "install" is used to be consistent with the ssc command */
if("`gitCommand'" == "install"){
/* Get the name of the repo - Stata has no equivalent to JS (or others) of string split */
local repoName = subinstr("`gitParam1'",".git","",.)
local separatorPos = strpos("`repoName'","/")
while `separatorPos' > 0 {
local repoName = trim(substr("`repoName'", `separatorPos'+1, length("`repoName'")-`separatorPos'))
local separatorPos = strpos("`repoName'","/")
}
/* Get the name of the command snake-cased */
local commandSnakeName = "`repoName'"
local dashPos = strpos("`commandSnakeName'", "-")
while `dashPos' > 0 {
local commandSnakeName = trim(substr("`commandSnakeName'", `dashPos'+1, length("`commandSnakeName'")-`dashPos'))
local dashPos = strpos("`commandSnakeName'", "-")
}
/* Create the repo */
local repoDir = "`gitDir'`repoName'/"
capture confirm file "`repoDir'/nul"
if _rc!=0{
capture mkdir "`repoDir'"
}
else{
/* Return to old directory */
di as green "`repoName' already installed"
di as green "type - git update `repoName' - to get the latest and greatest."
qui cd "`origDir'"
exit
}
shell git clone "`gitParam1'"
/* Copy all .ado and .sthlp files to the right directory */
local programFirstLetter = lower(substr(subinstr("`repoName'","stata-","",.),1,1))
local programDir = "../ado/plus/`programFirstLetter'/"
capture confirm file "`programDir'/nul"
if _rc!=0{
capture mkdir "`programDir'"
}
qui cd "`repoName'"
shell `copyCmd' *.ado "`adoPlusDir'`programFirstLetter'"
capture shell `copyCmd' *.sthlp "`adoPlusDir'`programFirstLetter'"
capture shell `copyCmd' *.mmat "`adoPlusDir'`programFirstLetter'"
/* Return to old directory */
qui cd "`origDir'"
di as green "Done installing `commandSnakeName' from the `repoName' git repository"
di as green "Type -help `commandSnakeName'-"
}
/* Translate "uninstall" to just removing the repo */
if("`gitCommand'" == "uninstall" | "`gitCommand'" == "update"){
/* Convert from camel case to snake case*/
local commandCamelCase = "`gitParam1'"
local repoName = "stata-`commandCamelCase'"
local lengthOfName = length("`repoName'")
local programFirstLetter = substr("`commandCamelCase'",1,1)
if(length("`repoName'") > 1){
forvalues pos = 2/`lengthOfName'{
local charAtPos = substr("`repoName'",`pos',1)
if(lower("`charAtPos'") != "`charAtPos'"){
local repoName = substr("`repoName'",1,`pos'-1) + "-" + lower("`charAtPos'") + substr("`repoName'",`pos'+1,length("`repoName'")-`pos')
}
}
}
/* Translate "update" to -git pull origin- */
if("`gitCommand'" == "update"){
di as green "Checking for updates for `commandCamelCase' (git repository name: `repoName')..."
qui cd "`repoName'"
shell git pull origin
shell `copyCmd' `commandCamelCase'.ado "`adoPlusDir'`programFirstLetter'"
capture shell `copyCmd' `commandCamelCase'.sthlp "`adoPlusDir'`programFirstLetter'"
capture shell `copyCmd' `commandCamelCase'.mmat "`adoPlusDir'`programFirstLetter'"
qui cd "`origDir'"
}
/* Uninstall nukes all */
if("`gitCommand'" == "uninstall"){
capture confirm file "`repoName'/nul"
if _rc!=0{
di as red "-`commandCamelCase'- not installed via -git- command"
}
else{
qui shell `rmdirCmd' `repoName'
qui cd "`adoPlusDir'`programFirstLetter'"
qui erase "`commandCamelCase'.ado"
qui capture erase "`commandCamelCase'.sthlp"
qui capture erase "`commandCamelCase'.mmat"
qui cd "`origDir'"
di as green "Git repository `repoName' and command -`commandCamelCase'- removed."
}
}
}
/* "list" lists all git-managed repos */
if("`gitCommand'" == "list"){
di as yellow "Git repositories installed using -git- command in"
di as yellow "`gitDir':"
di as green "note: when used in Stata, the stata- prefix is stripped and the "
di as green " program name is snake-cased"
di as green " e.g. repository stata-my-program becomes -myProgram- when used in Stata"
shell `lsCmd'
qui cd "`origDir'"
}
end