Skip to content

Commit aa6a49c

Browse files
committed
[CACHE] add support for restoring specific folders
1 parent 1636570 commit aa6a49c

File tree

5 files changed

+77
-20
lines changed

5 files changed

+77
-20
lines changed

Diff for: bin/cache

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
source ~/.bashful.filesys/utils/parse-args
4+
35
cache_dir_base=~/.folder-cache
46
repo=`basename $(pwd)`;
57
cache_dir="$cache_dir_base/$repo"
@@ -8,30 +10,38 @@ if [ ! -d "$cache_dir_base" ]; then
810
mkdir "$cache_dir_base"
911
fi
1012

11-
if [ "$1" == "--clear" ]; then
12-
if [ "$2" != "all" ]; then
13+
if [ ! -z "$__clear" ]; then
14+
if [ "$__clear_VALUE" != "all" ]; then
1315
rm -rf "$cache_dir_base"
1416
else
1517
rm -rf "$cache_dir"
1618
fi
1719

18-
elif [ "$1" == "--show" ]; then
20+
elif [ ! -z "$__show" ]; then
1921
if [ -f "$cache_dir/.cache-timestamp" ]; then
2022
echo "Files for $repo were cached on" $(cat "$cache_dir/.cache-timestamp")
2123
ls "$cache_dir";
2224
else
2325
echo "No cached files found for $repo"
2426
fi
2527

26-
elif [ "$1" == "--restore" ]; then
27-
for folder in `find $cache_dir -type d -maxdepth 1 -mindepth 1`; do
28-
echo "RESTORING: $(basename $folder)"
29-
mv "$folder" "./$(basename $folder)"
30-
rm "$cache_dir/.cache-timestamp" 2>/dev/null
31-
done
28+
elif [ ! -z "$__restore" ]; then
29+
if [ ! -z "$positional_args" ]; then
30+
for folder in "${positional_args[@]}"; do
31+
echo "RESTORING: $folder"
32+
mv "$cache_dir/$folder" "./$folder"
33+
rm "$cache_dir/.cache-timestamp" 2>/dev/null
34+
done
35+
else
36+
for folder in `find $cache_dir -type d -maxdepth 1 -mindepth 1`; do
37+
echo "RESTORING: $(basename $folder)"
38+
mv "$folder" "./$(basename $folder)"
39+
rm "$cache_dir/.cache-timestamp" 2>/dev/null
40+
done
41+
fi
3242

33-
elif [ "$1" != "" ]; then
34-
for folder in ${@}; do
43+
elif [ "$args" != "" ]; then
44+
for folder in "${args[@]}"; do
3545
if [ ! -d "$cache_dir" ]; then
3646
mkdir "$cache_dir"
3747
fi

Diff for: docs/cache.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ cache <option>
2121

2222
`cache` supports the following options:
2323

24-
- `cache --restore`: Restores _all_ folders currently cached from the current working directory. (support soon coming to be able to restore a specific folder or set of folders by name, but not currently available).
24+
- `cache --restore [<folder 1> <...>]`: Restores folders cached from the current working directory. If no folder names are given as arguments, it will restore _all_ folders in the cache for the current directory.
2525
- `cache --show`: Lists the cached folders from the current working directory and the timestamp of last caching.
2626
- `cache --clear`: Permanently deletes all the cached folders from the current working directory. If specified `--clear all` then **_all_** of the cached folders from _every_ workspace will be removed.
2727

@@ -43,7 +43,7 @@ We can now do whatever tasks need to be done without that folder there. When we
4343
are ready to have the node_modules back in the repo, we can simply invoke:
4444

4545
```sh
46-
cache --restore
46+
cache --restore node_modules
4747
```
4848

4949
This is _much_ quicker than running `npm install`, and a lot more efficient on

Diff for: test/cache_test.sh

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ source test/_tools.sh
44
mkdir test_tmp
55
cp -a test/ test_tmp/
66
cd test_tmp
7-
ls
7+
run ls
88

99
run cache snapshots
10-
ls
10+
run ls
1111

1212
run cache parse-args results
13-
ls
13+
run ls
1414

1515
# Fails on diff for "Files for test_tmp were cached on <DATE>" because it was always different - hence sed
1616
run cache --show | sed -e "s/Files for test_tmp were cached on .*/Files for test_tmp were cached on <DATE>/g"
17-
ls
17+
run ls
18+
19+
run cache --restore parse-args results
20+
run ls
1821

1922
run cache --restore
20-
ls
23+
run ls
2124

2225
run cache --show
2326

Diff for: test/index.sh

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ testFiles="""
44
cache_test.sh
55
"""
66

7+
[ ! -z "$1" ] && [ "$1" != "--update-snapshots" ] && testFiles="$1_test.sh";
8+
79
testProg () {
810
echo "#####################################################################################################"
911
echo "# TESTING $1"

Diff for: test/snapshots/cache_test.sh.snapshot

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#####################################################################################################
22
# TESTING cache_test.sh
33
#####################################################################################################
4+
________________________________________________________________________________________________
5+
RUNNING > 'ls'
6+
................................................................
47
_tools.sh
58
bashful_test.sh
69
cache_test.sh
@@ -9,20 +12,27 @@ parse-args
912
parse-args_test.sh
1013
results
1114
snapshots
15+
................................................................
16+
________________________________________________________________________________________________
1217
________________________________________________________________________________________________
1318
RUNNING > 'cache snapshots'
1419
................................................................
1520
CACHED FOLDERS:
1621
snapshots
1722
................................................................
1823
________________________________________________________________________________________________
24+
________________________________________________________________________________________________
25+
RUNNING > 'ls'
26+
................................................................
1927
_tools.sh
2028
bashful_test.sh
2129
cache_test.sh
2230
index.sh
2331
parse-args
2432
parse-args_test.sh
2533
results
34+
................................................................
35+
________________________________________________________________________________________________
2636
________________________________________________________________________________________________
2737
RUNNING > 'cache parse-args results'
2838
................................................................
@@ -32,11 +42,16 @@ results
3242
snapshots
3343
................................................................
3444
________________________________________________________________________________________________
45+
________________________________________________________________________________________________
46+
RUNNING > 'ls'
47+
................................................................
3548
_tools.sh
3649
bashful_test.sh
3750
cache_test.sh
3851
index.sh
3952
parse-args_test.sh
53+
................................................................
54+
________________________________________________________________________________________________
4055
________________________________________________________________________________________________
4156
RUNNING > 'cache --show'
4257
................................................................
@@ -46,19 +61,44 @@ results
4661
snapshots
4762
................................................................
4863
________________________________________________________________________________________________
64+
________________________________________________________________________________________________
65+
RUNNING > 'ls'
66+
................................................................
4967
_tools.sh
5068
bashful_test.sh
5169
cache_test.sh
5270
index.sh
5371
parse-args_test.sh
72+
................................................................
5473
________________________________________________________________________________________________
55-
RUNNING > 'cache --restore'
74+
________________________________________________________________________________________________
75+
RUNNING > 'cache --restore parse-args results'
5676
................................................................
57-
RESTORING: snapshots
5877
RESTORING: parse-args
5978
RESTORING: results
6079
................................................................
6180
________________________________________________________________________________________________
81+
________________________________________________________________________________________________
82+
RUNNING > 'ls'
83+
................................................................
84+
_tools.sh
85+
bashful_test.sh
86+
cache_test.sh
87+
index.sh
88+
parse-args
89+
parse-args_test.sh
90+
results
91+
................................................................
92+
________________________________________________________________________________________________
93+
________________________________________________________________________________________________
94+
RUNNING > 'cache --restore'
95+
................................................................
96+
RESTORING: snapshots
97+
................................................................
98+
________________________________________________________________________________________________
99+
________________________________________________________________________________________________
100+
RUNNING > 'ls'
101+
................................................................
62102
_tools.sh
63103
bashful_test.sh
64104
cache_test.sh
@@ -67,6 +107,8 @@ parse-args
67107
parse-args_test.sh
68108
results
69109
snapshots
110+
................................................................
111+
________________________________________________________________________________________________
70112
________________________________________________________________________________________________
71113
RUNNING > 'cache --show'
72114
................................................................

0 commit comments

Comments
 (0)