Skip to content

Commit 033b0bd

Browse files
authored
Merge pull request #33 from sjmh/master
Fix git import for packages
2 parents c1872aa + e15295a commit 033b0bd

File tree

2 files changed

+102
-40
lines changed

2 files changed

+102
-40
lines changed

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,41 @@ Pushes a file system path onto the end of `BAKEPATH`.
9696
9797
### `bake_require libname`
9898
99-
Searches `BAKEPATH` for the library and sources it, loading the file (executing its contents). Libraries should (generally) only contain declarations, as any imperative code will be executed when the library is loaded. Libraries may load other libraries.
99+
Searches `BAKEPATH` for the library and sources it, loading the file (executing its contents). Libraries should (generally) only contain declarations, as any imperative code will be executed when the library is loaded. Libraries may load other libraries.
100+
101+
You can also load libraries from Github or Enterprise Github instances. See the [Remote Libraries](#remote_libraries) section for more details.
100102
101103
## Look Ma, no `Bakefile` aka lets use a `./bake` directory
102104
103105
`bake` can also be used without a `Bakefile`, if you make a directory called `bake` and place shell files within it, `bake` will treat everything in that directory as library and require it automatically. See the section on libraries below.
104106
105-
# Libraries!
107+
108+
# Libraries
106109
107110
Some of the goals I had for for `bake` are for it to encourage best practices for shell scripting and to encourage re-use by encouraging the creation of small re-useable parts including libraries. Bake encourages small re-useable functions essentially by requiring the use of shell functions. It's up to you to break your functions into libraries that can be shared across your projects. Have a look at the [Best Practices](#best-practices) section below.
108111

112+
## Remote Libraries
113+
114+
Loading via Github via http/https
115+
```
116+
bake_require github.com/john/bakelib/my_bake_funcs
117+
bake_require https://github.com/john/bakelib/my_bake_funcs
118+
```
119+
120+
Or via ssh
121+
```
122+
bake_require ssh://github.acme.com/john/bakelib/my_bake_funcs
123+
bake_require git@github.acme.com:john/bakelib.git/my_bake_funcs
124+
```
125+
126+
You can also load from branches or tags
127+
```
128+
bake_require ssh://github.acme.com/john/bakelibs/my_bake_funcs develop
129+
bake_require github.com/steve/examples/lib1 1.0.2
130+
```
131+
132+
If you want to update your locally installed version of the libraries, you can run `bake update`
133+
109134
### `BAKEPATH`
110135

111136
This is a colon separated list of paths that `bake_require` uses to locate libraries.
@@ -334,6 +359,7 @@ bake make-release
334359
335360
* Kyle Burton <kyle.burton@gmail.com>
336361
* Isaac Schaaf <zeekus99@gmail.com>
362+
* Steve Hajducko <hajducko@gmail.com>
337363
338364
# License
339365

bake

Lines changed: 74 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -Eeu -o pipefail
44

55

66
# TODO: detect if we're being sourced or if we're being executed
7-
BAKE_VERSION="2014-11-05"
7+
BAKE_VERSION="1.0.15"
88
BAKE_STDOUT_IS_TERMINAL=""
99
BAKE_COLOR_NORMAL=""
1010
BAKE_COLOR_RED=""
@@ -77,6 +77,15 @@ function bake_bakefile_dir () {
7777

7878
BAKE_ROOT_DIR="$(bake_root_dir)"
7979

80+
function bake_looks_like_git () {
81+
local thing="$1"
82+
83+
if [[ $thing == git@* ]]; then
84+
return 0
85+
fi
86+
return 1
87+
}
88+
8089
function bake_looks_like_url () {
8190
local thing="$1"
8291

@@ -92,6 +101,10 @@ function bake_looks_like_url () {
92101
return 0
93102
fi
94103

104+
if [[ $thing == ssh://* ]]; then
105+
return 0
106+
fi
107+
95108
return 1
96109
}
97110

@@ -140,46 +153,38 @@ function bake_require_from_fs () {
140153

141154
function bake_url_to_package_path () {
142155
local url="$1"
143-
# strip the http:// or https:// if present
156+
# strip the *:// if present
144157
# then see if it exists as $BAKE_PACKAGES_PATH/$url
145158
local fpath
146-
fpath="$(echo "$url" | sed 's/^https\?:\/\///')"
159+
fpath=${url##*://}
160+
fpath=${fpath##git@}
161+
162+
# Nuke the .git/ if left in path
163+
fpath=${fpath/.git\///}
164+
147165
echo "$BAKE_PACKAGES_PATH/$fpath"
148166
}
149167

150-
function bake_require_git () {
168+
function bake_git_to_url () {
151169
local url="$1"
152-
local libpath="$2"
153-
local tag="${3:-master}"
170+
local host
171+
local path
154172

155-
bake_ensure_bake_packages_path
156-
157-
pushd "$BAKE_PACKAGES_PATH" >/dev/null
173+
[[ $url =~ git@(.*):(.*) ]] && host=${BASH_REMATCH[1]} && path=${BASH_REMATCH[2]}
174+
echo ssh://git@"${host}"/"${path}"
175+
}
158176

159-
# git@github.com:kyleburton/bake-recipes.git
160-
# https://github.com/kyleburton/bake-recipes.git
161-
# strip the leading git@, http:// or https://
162-
local full_package_path
163-
full_package_path="$(echo "$url" | sed 's/^git@//' | sed 's/https:\/\///' | sed 's/http:\/\///' | sed 's/.git$//' | sed 's/:/\//')"
164-
local package_path
165-
package_path="$(dirname "$full_package_path")"
177+
function bake_sanitize_url () {
178+
local url="$1"
166179

167-
test -d "$package_path" || mkdir -p "$package_path"
168-
cd "$package_path"
169-
local dname
170-
dname="$(basename "$full_package_path")"
171-
if [ ! -d "$dname" ]; then
172-
git clone "$url"
173-
cd "$dname"
174-
git fetch
175-
git co "$tag"
180+
# If no schema, assume it's https
181+
if [[ $url != *://* ]]; then
182+
url=https://${url}
176183
fi
177184

178-
popd > /dev/null
179-
180-
# translate the require path to local fs path
181-
# source it!
182-
source "$BAKE_PACKAGES_PATH/$full_package_path/$libpath"
185+
# Remove any .git in the url
186+
url=${url/.git\///}
187+
echo "$url"
183188
}
184189

185190
function bake_ensure_bake_packages_path () {
@@ -189,20 +194,46 @@ function bake_ensure_bake_packages_path () {
189194
function bake_package_install () {
190195
local url="$1"
191196
local tag="${2:-master}"
197+
local schema=${url%%://*}
198+
199+
if ! command -v git > /dev/null; then
200+
bake_echo_red "Error[bake_package_install] git command not found in PATH" EXIT
201+
exit 1
202+
fi
192203

193204
bake_ensure_bake_packages_path
194-
pushd "$BAKE_PACKAGES_PATH"
195-
local git_project_name
196-
git_project_name="$(echo "$url" | awk -F/ '{print $3 }')"
197-
local git_host_and_user
198-
git_host_and_user="$(echo "$url" | awk -F/ '{print $1 "/" $2 }')"
205+
pushd "$BAKE_PACKAGES_PATH" > /dev/null
206+
207+
local git_project_name=
208+
local git_host_and_user=
209+
local bake_library_file=
210+
211+
[[ $url =~ [a-zA-Z]+://([a-zA-Z0-9@-_.]+/[a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/(.*) ]] && \
212+
git_host_and_user=${BASH_REMATCH[1]} && \
213+
git_project_name=${BASH_REMATCH[2]} && \
214+
bake_library_file=${BASH_REMATCH[3]}
215+
216+
if [[ -z "$git_host_and_user" ]] || [[ -z "$git_project_name" ]]; then
217+
bake_echo_red "Error[bake_package_install] Could not parse url '$url'"
218+
exit 1
219+
fi
220+
221+
if [[ -z "$bake_library_file" ]]; then
222+
bake_echo_red "Error[bake_package_install] No library file specified"
223+
exit 1
224+
fi
225+
199226
local git_url
200-
git_url="$(echo "$url" | awk -F/ '{print $1 "/" $2 "/" $3}')"
227+
git_url=${git_host_and_user}/${git_project_name}
228+
git_host_and_user=${git_host_and_user##git@}
201229
test -d "$git_host_and_user" || mkdir -p "$git_host_and_user"
202230
cd "$git_host_and_user"
203-
if [ ! -e "$git_project_name" ]; then
204-
git clone "https://$git_url"
231+
232+
if [[ ! -e "$git_project_name" ]]; then
233+
git clone "$schema://$git_url"
205234
fi
235+
236+
# Checkout the project and branch/tag
206237
cd "$git_project_name"
207238
git checkout .
208239
git pull
@@ -241,6 +272,11 @@ function bake_require_from_url () {
241272
function bake_require () {
242273
local module="$1"
243274
if bake_looks_like_url "$module"; then
275+
module="$(bake_sanitize_url "$module")"
276+
bake_require_from_url "$module"
277+
elif bake_looks_like_git "$module"; then
278+
module="$(bake_git_to_url "$module")"
279+
module="$(bake_sanitize_url "$module")"
244280
bake_require_from_url "$module"
245281
else
246282
bake_require_from_fs "$module"

0 commit comments

Comments
 (0)