Skip to content

Instantly share code, notes, and snippets.

@elventear
Created May 29, 2013 14:22
Show Gist options
  • Save elventear/5670630 to your computer and use it in GitHub Desktop.
Save elventear/5670630 to your computer and use it in GitHub Desktop.
#shell script to #move #types
#!/usr/bin/env zsh
local SCRIPT_NAME=$(basename $0)
local SCRIPT_DIR=$(dirname $0)
. $SCRIPT_DIR/../dependencies/zsh-functional/functional.plugin.zsh
function usage {
test ${#1} -gt 0 && echo ERROR: $1
echo $SCRIPT_NAME SOURCE_TYPE DEST_TYPE
}
function get_type {
test -z $1 &&\
echo No Type found &&\
return 1
local tokens
echo ${(A)tokens:=${(s:.:)1}} > /dev/null
local integer num_tokens=${#tokens}
test $num_tokens -lt 2 &&\
echo Two few tokens &&\
return 1
local typename=$tokens[-1]
test -z $typename &&\
echo Empty Type &&\
return 1
echo $typename
}
function verify_source_dest_path {
local SOURCE_TYPE=$1
local DEST_TYPE=$2
local TYPE_NAME=$(get_type $SOURCE_TYPE)
test $? -eq 1 && echo $TYPE_NAME && return 1
test $TYPE_NAME != $(get_type $DEST_TYPE) &&\
echo Source and Destination Types do not match &&\
return 1
return 0
}
function replace_explicit_references {
local SOURCE_TYPE=$1
local DEST_TYPE=$2
verify_source_dest_path $SOURCE_TYPE $DEST_TYPE
local TYPE_NAME=$(get_type $SOURCE_TYPE)
local source_escaped=${SOURCE_TYPE//./\\.}
gsed -i -e "s/$source_escaped/$DEST_TYPE/g" $(list_files_with_type $TYPE_NAME)
}
function replace_implicit_references {
local SOURCE_TYPE=$1
local DEST_TYPE=$2
verify_source_dest_path $SOURCE_TYPE $DEST_TYPE
local TYPE_NAME=$(get_type $SOURCE_TYPE)
local -a files_missing
files_missing=${=$(list_files_missing_explicit $DEST_TYPE)}
test ${#files_missing} -gt 0 || return
local exclude_gsp='echo $1 | egrep -qv "\.gsp$"'
gsed -i "0,/import/s//import $DEST_TYPE\\nimport/" $(filter $exclude_gsp $files_missing)
local only_gsp='echo $1 | egrep -q "\.gsp$"'
gsed -ri "0,/import/s/(.*)/<%@ page import="$DEST_TYPE" %>\\n\1/" $(filter $only_gsp $files_missing)
}
function list_files_with_type {
local TYPE_NAME=$1
ack-5.12 -w --groovy --java -l $TYPE_NAME | sort | uniq
}
function list_files_missing_explicit {
local FULL_TYPE=$1
local TYPE_NAME=$(get_type $FULL_TYPE)
local -a files_w_type
files_w_type=$(list_files_with_type $TYPE_NAME)
test ${#files_w_type} -gt 0 || return
ack-5.12 -L ${FULL_TYPE//./\\.} ${=files_w_type} | sort | uniq
}
function move_type {
local SOURCE_TYPE=$1
test -z $SOURCE_TYPE && usage 'Missing SOURCE_TYPE' && return 1
local DEST_TYPE=$2
test -z $DEST_TYPE && usage 'Missing DEST_TYPE' && return 1
verify_source_dest_path $SOURCE_TYPE $DEST_TYPE
replace_explicit_references $SOURCE_TYPE $DEST_TYPE
replace_implicit_references $SOURCE_TYPE $DEST_TYPE
return 0
}
#setopt -x
move_type $1 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment