-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaven-profiler.plugin.zsh
126 lines (105 loc) · 3.73 KB
/
maven-profiler.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env zsh
_mvnp_load_env() {
Red='\033[0;31m'
Green='\033[0;32m'
BIBlue='\033[1;94m'
BIWhite='\033[1;97m'
NC='\033[0m'
MAVEN_PROFILER_PROMPT_TITLE='MAVEN'
MAVEN_PROFILER_DEFAULT_PROFILE='default'
if [[ -n "$MAVEN_PROFILE" ]]; then
# clear MAVEN_PROFILE at the loading phase
# if MAVEN_PROFILE is not cleared in this phase, then MAVEN_PROFILE config could only be reset after re-login
export MAVEN_PROFILE=''
fi
_mvnp_error() {
echo -e "[${BIBlue}${MAVEN_PROFILER_PROMPT_TITLE}${NC}] ${BIWhite}---${NC} ${Red}${1}${NC} ${BIWhite} ---${NC}"
}
_mvnp_prompt() {
echo -e "[${BIBlue}${MAVEN_PROFILER_PROMPT_TITLE}${NC}] ${BIWhite}---${NC} ${Green}${1}${NC} ${BIWhite}(${2}) ---${NC}"
}
_mvnp_show_profile() {
if [[ -n $MAVEN_PROFILE ]]; then
_mvnp_prompt "mvn profile" "${MAVEN_PROFILE}"
else
_mvnp_prompt "mvn profile" "${MAVEN_PROFILER_DEFAULT_PROFILE}"
fi
}
_mvnp_switch_profile() {
local TARGET_MAVEN_PROFILE=${1}
local TARGET_MAVEN_CONFIG_FILE
# validate if maven config file exist
TARGET_MAVEN_CONFIG_FILE=$(_mvnp_assemble_config_file_path "$TARGET_MAVEN_PROFILE")
if [[ ! -f "$TARGET_MAVEN_CONFIG_FILE" ]]; then
_mvnp_error "maven config file [$(basename "$TARGET_MAVEN_CONFIG_FILE")] not found, rollback to previous profile"
_mvnp_show_profile
return 1
fi
export MAVEN_PROFILE=$TARGET_MAVEN_PROFILE
_mvnp_show_profile
}
_mvnp_assemble_config_file_path() {
local MAVEN_PROFILE=${1}
# use default conf file for profile 'default'
if [[ "$MAVEN_PROFILE" == "$MAVEN_PROFILER_DEFAULT_PROFILE" ]]; then
MAVEN_CONFIG_FILE=$MAVEN_HOME/conf/settings.xml
else
MAVEN_CONFIG_FILE=$MAVEN_HOME/conf/settings-$MAVEN_PROFILE.xml
fi
echo "$MAVEN_CONFIG_FILE"
}
_mvnp_autoconfig() {
# locate mvn installed on system if MAVEN_HOME is not set
if [[ -n $MAVEN_HOME ]]; then
echo -e "[maven-profiler] MAVEN_HOME is not empty, abort duplicated setup action"
return 0
fi
if command -v mvn &>/dev/null; then
MAVEN_EXECUTABLE=$(which mvn)
export MAVEN_HOME="${MP_MAVEN_EXECUTABLE%/*/*}"
else
echo -e "[maven-profiler] maven is not found in PATH"
return 1
fi
}
_mvnp_apply_default_profile() {
# validations
if [[ -z $MAVEN_PROFILE ]]; then
# apply default maven profile
export MAVEN_PROFILE=$MAVEN_PROFILER_DEFAULT_PROFILE
fi
}
mvn-profiler() {
local JDK_VERSION_OUTPUT MAVEN_EXECUTABLE
MAVEN_EXECUTABLE=$MAVEN_HOME/bin/mvn
MAVEN_CONFIG_FILE=$MAVEN_HOME/conf/settings-$MAVEN_PROFILE.xml
# validations
_mvnp_apply_default_profile
MAVEN_CONFIG_FILE=$(_mvnp_assemble_config_file_path "$MAVEN_PROFILE")
if [[ ! -f $MAVEN_CONFIG_FILE ]]; then
_mvnp_error "maven config file [$(basename "$MAVEN_CONFIG_FILE")] not found"
return 1
fi
if [[ ! -f $MAVEN_EXECUTABLE ]]; then
_mvnp_error "maven executable not found, please check if \$MAVEN_HOME variable is properly configured"
return 1
fi
# shellcheck disable=SC2207
IFS=$'\n' JDK_VERSION_OUTPUT=($(java --version))
_mvnp_prompt "jdk version" "${JDK_VERSION_OUTPUT[1]}"
_mvnp_prompt "mvn profile" "${MAVEN_PROFILE}"
_mvnp_prompt "mvn home" "${MAVEN_HOME}"
_mvnp_prompt "mvn conf" "${MAVEN_CONFIG_FILE}"
# pause for a short time for human eye to catch up with the prompt message
sleep 1
# execute mvn with designate config file and passed options
"$MAVEN_EXECUTABLE" -s "$MAVEN_CONFIG_FILE" "$@"
}
# override default mvn command
alias mvn=mvn-profiler
alias mvnp=mvn-profiler
alias mvnp-switch=_mvnp_switch_profile
alias mvnp-show=_mvnp_show_profile
}
_mvnp_load_env
unfunction _mvnp_load_env