#/bin/bash function default_workspace() { local dir="" is_set 'workspace' if [ $? -eq 1 ]; then dir=$(get_parameter 'workspace'); fi is_set 'w' if [ $? -eq 1 ]; then dir=$(get_parameter 'w'); fi if [ -z "$dir" ]; then dir="."; fi if [[ ! "$dir" =~ ^/ ]]; then dir=$(echo "$(pwd)/$dir" | sed 's?/\.$??'); fi echo $dir } function check_workspace() { local dir=$1 if [ -r "$dir/$FILE_WORKSPACE_ROOT" ]; then return 1 else return 0 fi } function locate_nearest_workspace() { local next_dir=$1 if [ -d $next_dir ]; then next_dir=$(realpath $next_dir); fi for i in $(echo 1 2 3 4); do check_workspace $next_dir if [ $? -eq 1 ]; then # Workspace found echo $next_dir return else next_dir=$next_dir/../ if [ -d $next_dir ]; then next_dir=$(realpath $next_dir); fi fi done echo "~none~" return } function assert_workspace() { local mode=$1 if [ -n "$DIR_WORKSPACE" ]; then return 0; fi local start_from=$(default_workspace) DIR_WORKSPACE=$(locate_nearest_workspace $start_from) if [ "$DIR_WORKSPACE" == "~none~" ]; then DIR_WORKSPACE=""; if [ "$mode" != "quiet" ]; then error_echo "No valid workspace found around here..."; fi return 1 fi debug "DIR_WORKSPACE is $DIR_WORKSPACE" return 0 } function action_init() { # Detect any existing worspace EXISTING_WORKSPACE=$(locate_nearest_workspace .) if [ "$EXISTING_WORKSPACE" != "~none~" ]; then error_echo "You cannot create a workspace within a workspace!" error_echo "Detected workspace location: '$EXISTING_WORKSPACE'" return 1 fi # Set workspace directory DIR_WORKSPACE=$(default_workspace) confirm "Do you want to initialize workspace: '${DIR_WORKSPACE}'?" if [ $? -eq 0 ]; then return 0; fi # Create directory color_echo_n "Creating directory '${DIR_WORKSPACE}'... " mkdir -p $DIR_WORKSPACE check_rc_echo $? if [ $? -ne 0 ]; then return 1; fi color_echo_n "Adding workspace flag file... " touch $DIR_WORKSPACE/$FILE_WORKSPACE_ROOT check_rc_echo $? if [ $? -ne 0 ]; then return 1; fi color_echo_n "Importing skeleton files... " cp -rp $DIR_WORKSPACE_TEMPLATE/* $DIR_WORKSPACE/ check_rc_echo $? if [ $? -ne 0 ]; then return 1; fi return 0 }