|
| 1 | +*! version 1.0 |
| 2 | +version 12.0 |
| 3 | +capture program drop git |
| 4 | +program define git |
| 5 | +syntax anything(name=gitArgs id="git command and arguments") |
| 6 | + |
| 7 | + /** |
| 8 | + * This requires that you have git installed and accessible through the command line. |
| 9 | + * For instructions on installing git, see: http://git-scm.com/downloads |
| 10 | + */ |
| 11 | + di as red "WARNING:" |
| 12 | + di as red "This program requires git to be accessible through the command line." |
| 13 | + di as red "See http://git-scm.com/downloads" |
| 14 | + /* |
| 15 | + OS-dependent vars |
| 16 | + */ |
| 17 | + local os = "`c(os)'" |
| 18 | + local adoPlusDir = "`c(sysdir_plus)'" |
| 19 | + if("`os'" != "Windows"){ |
| 20 | + local adoPlusDir = subinstr("`adoPlusDir'","~","/Users/`c(username)'",.) |
| 21 | + } |
| 22 | + local adoDir = trim(subinstr("`adoPlusDir'","ado/plus/","",.)) |
| 23 | + local gitDir = "`adoDir'git/" |
| 24 | + local copyCmd = "cp" /* Defaults to *nix command */ |
| 25 | + local deleteCmd = "rm" /* Defaults to *nix command */ |
| 26 | + local origDir = "`c(pwd)'" |
| 27 | + local lsCmd = "ls" /* Defaults to *nix command */ |
| 28 | + local rmdirCmd = "rm -rf" /* Defaults to *nix command */ |
| 29 | + if("`os'" == "Windows"){ |
| 30 | + local copyCmd = "copy" |
| 31 | + local deleteCmd = "erase" |
| 32 | + local lsCmd = "dir" |
| 33 | + local rmdirCmd = "rmdir /Q /S" |
| 34 | + } |
| 35 | + |
| 36 | + /* |
| 37 | + Make the git program dir if not there and cd to it |
| 38 | + */ |
| 39 | + capture confirm file "`gitDir'" |
| 40 | + if _rc!=0{ |
| 41 | + mkdir "`gitDir'" |
| 42 | + } |
| 43 | + qui cd "`gitDir'" |
| 44 | + |
| 45 | + /* |
| 46 | + Command parsing |
| 47 | + */ |
| 48 | + if(strpos("`gitArgs'"," ") >0){ |
| 49 | + local gitCommand = trim(substr("`gitArgs'",1, strpos("`gitArgs'"," "))) |
| 50 | + } |
| 51 | + else{ |
| 52 | + local gitCommand = trim("`gitArgs'") |
| 53 | + } |
| 54 | + local gitParam1 = trim(subinstr("`gitArgs'","`gitCommand'","",1)) |
| 55 | + |
| 56 | + /* Translate "install" to "clone". "install" is used to be consistent with the ssc command */ |
| 57 | + if("`gitCommand'" == "install"){ |
| 58 | + |
| 59 | + /* Get the name of the repo - Stata has no equivalent to JS (or others) of string split */ |
| 60 | + local repoName = "`gitParam1'" |
| 61 | + local separatorPos = strpos("`repoName'","/") |
| 62 | + while `separatorPos' > 0 { |
| 63 | + local repoName = trim(substr("`repoName'", `separatorPos'+1, length("`repoName'")-`separatorPos')) |
| 64 | + local separatorPos = strpos("`repoName'","/") |
| 65 | + } |
| 66 | + |
| 67 | + /* Get the name of the command snake-cased */ |
| 68 | + local commandSnakeName = "`repoName'" |
| 69 | + local dashPos = strpos("`commandSnakeName'", "-") |
| 70 | + while `dashPos' > 0 { |
| 71 | + local commandSnakeName = trim(substr("`commandSnakeName'", `dashPos'+1, length("`commandSnakeName'")-`dashPos')) |
| 72 | + local dashPos = strpos("`commandSnakeName'", "-") |
| 73 | + } |
| 74 | + |
| 75 | + /* Create the repo */ |
| 76 | + local repoDir = "`gitDir'`repoName'/" |
| 77 | + capture confirm file "`repoDir'" |
| 78 | + if _rc!=0{ |
| 79 | + mkdir "`repoDir'" |
| 80 | + } |
| 81 | + else{ |
| 82 | + /* Return to old directory */ |
| 83 | + di as green "`repoName' already installed" |
| 84 | + di as green "type - git update `repoName' - to get the latest and greatest." |
| 85 | + qui cd "`origDir'" |
| 86 | + exit |
| 87 | + } |
| 88 | + shell git clone "`gitParam1'" |
| 89 | + |
| 90 | + /* Copy all .ado and .sthlp files to the right directory */ |
| 91 | + local programFirstLetter = lower(substr(subinstr("`repoName'","stata-","",.),1,1)) |
| 92 | + local programDir = "../ado/plus/`programFirstLetter'/" |
| 93 | + capture confirm file "`programDir'" |
| 94 | + if _rc!=0{ |
| 95 | + mkdir "`programDir'" |
| 96 | + } |
| 97 | + qui cd "`repoName'" |
| 98 | + shell `copyCmd' *.ado "`adoPlusDir'`programFirstLetter'" |
| 99 | + capture shell `copyCmd' *.sthlp "`adoPlusDir'`programFirstLetter'" |
| 100 | + capture shell `copyCmd' *.mmat "`adoPlusDir'`programFirstLetter'" |
| 101 | + |
| 102 | + /* Return to old directory */ |
| 103 | + qui cd "`origDir'" |
| 104 | + |
| 105 | + di as green "Done installing `commandSnakeName' from the `repoName' git repository" |
| 106 | + di as green "Type -help `commandSnakeName'-" |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + /* Translate "uninstall" to just removing the repo */ |
| 111 | + if("`gitCommand'" == "uninstall" | "`gitCommand'" == "update"){ |
| 112 | + |
| 113 | + /* Convert from camel case to snake case*/ |
| 114 | + local commandCamelCase = "`gitParam1'" |
| 115 | + local repoName = "stata-`commandCamelCase'" |
| 116 | + local lengthOfName = length("`repoName'") |
| 117 | + local programFirstLetter = substr("`commandCamelCase'",1,1) |
| 118 | + if(length("`repoName'") > 1){ |
| 119 | + forvalues pos = 2/`lengthOfName'{ |
| 120 | + local charAtPos = substr("`repoName'",`pos',1) |
| 121 | + if(lower("`charAtPos'") != "`charAtPos'"){ |
| 122 | + local repoName = substr("`repoName'",1,`pos'-1) + "-" + lower("`charAtPos'") + substr("`repoName'",`pos'+1,length("`repoName'")-`pos') |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /* Translate "update" to -git pull origin- */ |
| 128 | + if("`gitCommand'" == "update"){ |
| 129 | + di as green "Checking for updates for `commandCamelCase' (git repository name: `repoName')..." |
| 130 | + qui cd "`repoName'" |
| 131 | + shell git pull origin |
| 132 | + shell `copyCmd' `commandCamelCase'.ado "`adoPlusDir'`programFirstLetter'" |
| 133 | + capture shell `copyCmd' `commandCamelCase'.sthlp "`adoPlusDir'`programFirstLetter'" |
| 134 | + capture shell `copyCmd' `commandCamelCase'.mmat "`adoPlusDir'`programFirstLetter'" |
| 135 | + qui cd "`origDir'" |
| 136 | + } |
| 137 | + |
| 138 | + /* Uninstall nukes all */ |
| 139 | + if("`gitCommand'" == "uninstall"){ |
| 140 | + capture confirm file "`repoName'" |
| 141 | + if _rc!=0{ |
| 142 | + di as red "-`commandCamelCase'- not installed via -git- command" |
| 143 | + } |
| 144 | + else{ |
| 145 | + qui shell `rmdirCmd' `repoName' |
| 146 | + qui cd "`adoPlusDir'`programFirstLetter'" |
| 147 | + qui erase "`commandCamelCase'.ado" |
| 148 | + qui capture erase "`commandCamelCase'.sthlp" |
| 149 | + qui capture erase "`commandCamelCase'.mmat" |
| 150 | + qui cd "`origDir'" |
| 151 | + di as green "Git repository `repoName' and command -`commandCamelCase'- removed." |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + /* "list" lists all git-managed repos */ |
| 158 | + if("`gitCommand'" == "list"){ |
| 159 | + di as yellow "Git repositories installed using -git- command in" |
| 160 | + di as yellow "`gitDir':" |
| 161 | + di as green "note: when used in Stata, the stata- prefix is stripped and the " |
| 162 | + di as green " program name is snake-cased" |
| 163 | + di as green " e.g. repository stata-my-program becomes -myProgram- when used in Stata" |
| 164 | + shell `lsCmd' |
| 165 | + qui cd "`origDir'" |
| 166 | + } |
| 167 | + |
| 168 | +end |
0 commit comments