Skip to content

Instantly share code, notes, and snippets.

@LanderlYoung

LanderlYoung/ramdisk

Last active Aug 22, 2018
Embed
What would you like to do?
Create a RAM disk on macOS to boost up android project building.
#!/bin/bash
#########################################################################
# File Name: ramdisk.sh
# Author: Landerl Young
# e-Mail: LanderlYoung@gmail.com
# Created Time: Wed Aug 22 15:08:28 2018
#########################################################################
VOLUME=RAM_DISK
RAMDISK_LOCATION=/Volumes/$VOLUME
RAMDISK_CONFIG=/Volumes/$VOLUME/.ramdisk_config
#@param size in MegaBytes
function ramdisk_create() {
if [[ -d $RAMDISK_LOCATION ]]; then
echo "RAM_DISK already existed $RAMDISK_LOCATION"
return
fi
RAMDISK_SIZE_MB=$1
if [[ $RAMDISK_SIZE_MB -le 0 ]]; then
echo "invalid size $RAMDISK_SIZE_MB"
return
fi
echo "crate RAM DISK size ${RAMDISK_SIZE_MB}MB"
RAMDISK_SECTORS=$((2048 * $RAMDISK_SIZE_MB))
DISK_ID=$(hdiutil attach -nomount ram://$RAMDISK_SECTORS)
echo "Disk ID is :$DISK_ID"
diskutil erasevolume HFS+ "$VOLUME" ${DISK_ID}
echo $DISK_ID > $RAMDISK_CONFIG
}
function ramdisk_delete() {
if [[ ! -d $RAMDISK_LOCATION ]]; then
echo "RAM_DISK does not exist $RAMDISK_LOCATION"
return
fi
DISK_ID=$(<$RAMDISK_CONFIG)
if [[ -z $DISK_ID ]];then
echo "invalid RAM_DISK abort"
return
fi
echo "Disk ID is :$DISK_ID"
umount -f $RAMDISK_LOCATION
hdiutil detach $DISK_ID
}
function softlink() {
CWD=$(pwd)
NAME=$(basename $CWD)
FROM=$1
SRC=$CWD/$FROM
TARGET=$RAMDISK_LOCATION/$NAME/$FROM
echo "ln $TARGET -> $NAME/$FROM"
rm -rf $FROM
mkdir -p $(basename $SRC)
mkdir -p $TARGET
ln -s $TARGET $SRC
}
function ramdisk_build_cache() {
if [[ ! -d $RAMDISK_LOCATION ]]; then
echo "RAMDISK doesn't exist, please new first"
return 1
fi
CWD=$(pwd)
NAME=$(basename $CWD)
echo "link ramdisk for gralde project $NAME"
for dir in *;do
if [[ -d $dir/build || -f $dir/build.gradle ]]; then
softlink $dir/build
fi
done
softlink build
return
}
function ramdisk_build_cache_resotre() {
if [[ ! -d $RAMDISK_LOCATION ]]; then
echo "RAMDISK doesn't exist, please new first"
return 1
fi
CWD=$(pwd)
NAME=$(basename $CWD)
rm -rf build
for dir in *; do
if [[ -d $dir/build ]]; then
rm -rf $dir/build
fi
done
}
function ramdisk_trim() {
CWD=$(pwd)
NAME=$(basename $CWD)
for dir in ${RAMDISK_LOCATION}/*; do
if [[ -d $dir && $(basename $dir) != $NAME ]]; then
echo "rm $dir"
rm -rf $dir
fi
done
return
}
COMMAND=$1
shift
case $COMMAND in
"new")
ramdisk_create $@
;;
"delete" )
ramdisk_delete $@
;;
"link" )
ramdisk_build_cache $@
;;
"unlink")
ramdisk_build_cache_resotre $@
;;
"trim" )
ramdisk_trim $@
;;
* )
name=$(basename $0)
echo "$name new|delete|link|unlink|trim"
echo " new [size in MB] -> create ramdisk"
echo " delete -> delete ramdisk"
echo " link -> link gradle project build cache to ramdisk"
echo " unlink -> unlink gradle project build cache to ramdisk"
echo " trim -> delete other projects' build cache in ramdisk"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment