Skip to content

Commit d4b5e4d

Browse files
author
Michael Buckley
committed
Inline scripts and cleanup
1 parent 0acf8aa commit d4b5e4d

File tree

10 files changed

+374
-245
lines changed

10 files changed

+374
-245
lines changed

apex-source-control

Lines changed: 373 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,373 @@
1+
#!/bin/bash
2+
3+
function die {
4+
echo $@
5+
exit 1
6+
}
7+
8+
function run_sql_cmd {
9+
local connect_string="${username}/${password}@${database_connection}"
10+
local sql_cmd
11+
if which sql > /dev/null; then
12+
echo Running PWD $PWD
13+
sql_cmd="sql -S"
14+
else
15+
echo WARNING: could not find SQLcl \(sql\). Falling back to sqlplus
16+
sql_cmd="sqlplus -S"
17+
fi
18+
19+
NLS_LANG=.AL32UTF8
20+
export NLS_LANG
21+
22+
${sql_cmd} ${connect_string} "$@" || die Failed: ${sql_cmd} ${connect_string} "$@"
23+
}
24+
25+
function check_conf_file {
26+
if [ ! -e ./config/asc.conf ]; then
27+
echo "Missing or broken symbolic link: ${PWD}/config/asc.conf"
28+
echo "Please use the command 'npm run switch-conf-file' to create the symbolic link ./config/asc.conf to your config file"
29+
echo "If you don't have a config file set up you can create one using 'npm run new-config-file'"
30+
exit 1
31+
fi
32+
33+
echo "Using the config file at: $(readlink config/asc.conf)"
34+
35+
source ./config/asc.conf
36+
37+
[ -z "${apexappid}" ] && die "Missing config: ${apexappid} apexappid"
38+
[ -z "${workspace_name}" ] && die "Missing config: ${workspace_name} workspace_name"
39+
[ -z "${database_connection}" ] && die "Missing config: ${database_connection} database_connection"
40+
[ -z "${username}" ] && die "Missing config: ${username} username"
41+
[ -z "${password}" ] && die "Missing config: ${password} password"
42+
43+
echo "Config file looks good! Moving on to the next step..."
44+
}
45+
46+
function legacy_apex_export {
47+
48+
[ -z "${ORACLE_HOME}" ] && die "Missing environment variable: ORACLE_HOME. Please add the path of your oracle installation to your environment variables under the variable name ORACLE_HOME"
49+
[ -z "${APEX_HOME}" ] && die "Missing environment variable: APEX_HOME. Please add the path of your apex installation to your environment variables under the variable name APEX_HOME. Note: You should be using APEX version 5 or above"
50+
[ ! -e "${ORACLE_HOME}"/jdbc/lib/ojdbc6.jar ] && die "Missing ojdbc6.jar: please download from oracle and put in ${ORACLE_HOME}/jdbc/lib directory"
51+
[ ! -e "${APEX_HOME}"/utilities/oracle/apex/APEXExport.class ] && die "Missing APEXExport class. Please ensure you are using Apex 5 or above and have the APEXExport and APEXExportSplitter classes are in the $APEX_HOME/utilities/oracle/apex/ directory"
52+
[ ! -e "${APEX_HOME}"/utilities/oracle/apex/APEXExportSplitter.class ] && die "Missing APEXExportSplitter class. Please ensure you are using Apex 5 or above and have the APEXExport and APEXExportSplitter classes are in the $APEX_HOME/utilities/oracle/apex/ directory"
53+
54+
source ./config/asc.conf
55+
56+
export CLASSPATH="${APEX_HOME}"/utilities:"${ORACLE_HOME}"/jdbc/lib/ojdbc6.jar
57+
58+
export_file="f${apexappid}.sql"
59+
60+
if [ -d apex/ ]; then
61+
rm -r apex/
62+
fi
63+
if [ -e "${export_file}" ]; then
64+
rm "${export_file}"
65+
fi
66+
67+
java oracle.apex.APEXExport -db "${database_connection}" -user "${username}" -password "${password}" -applicationid "${apexappid}" -skipExportDate -expOriginalIds ||
68+
die "Exit code #: $?. An error has occured while trying to use APEXExport. Please check that your database_connection, username, password, and apexappid variables are all set correctly."
69+
70+
java oracle.apex.APEXExportSplitter "${export_file}" ||
71+
die "Exit code #: $?. An error has occured while trying to use APEXExportSplitter. Please check that your apexappid variable is set correctly and that the application exists in the workspace you are trying to export from"
72+
73+
rm "${export_file}"
74+
75+
mv "f${apexappid}" apex #
76+
77+
sed -i '' s^@application^@apex/application^ apex/install.sql
78+
}
79+
80+
function apex_export {
81+
if which sql > /dev/null; then
82+
source ./config/asc.conf
83+
echo Using sqlcl to export app ${apexappid}
84+
export_file="f${apexappid}.sql"
85+
86+
if [ -d apex/ ]; then
87+
rm -r apex/
88+
fi
89+
if [ -e "${export_file}" ]; then
90+
rm "${export_file}"
91+
fi
92+
93+
run_sql_cmd @/dev/stdin <<EOF
94+
apex export -skipExportDate -expOriginalIds -split -splitNoCheckSum -applicationid $apexappid
95+
EOF
96+
if [ -e "${export_file}" ]; then
97+
rm "${export_file}"
98+
fi
99+
100+
mv "f${apexappid}" apex
101+
102+
# fixup install paths. The tool generates absolute paths
103+
sed -E -i '' 's^@(.*/)?application/^@apex/application/^' apex/install.sql
104+
else
105+
echo WARNING: Did not find SQLcl. Using legacy APEXExportSplitter instead
106+
legacy_apex_export
107+
fi
108+
}
109+
110+
function apex_to_file {
111+
check_conf_file || die
112+
apex_export || exit 1
113+
}
114+
115+
function file_to_apex {
116+
source config/asc.conf || die No config file at config/asc.conf
117+
118+
run_sql_cmd @/dev/stdin "${apexappid}" "${workspace_name}" "${parsing_schema}" "${app_alias}" <<EOF || die Failed to install app
119+
declare
120+
p_application_id NUMBER := '&1';
121+
p_workspace_name varchar2(255) := '&2';
122+
p_parsing_schema varchar2(255) := '&3';
123+
p_app_alias varchar2(255) := '&4';
124+
l_workspace_id NUMBER;
125+
begin
126+
apex_application_install.set_application_id ( p_application_id );
127+
apex_application_install.set_schema( p_parsing_schema );
128+
129+
IF p_app_alias is null THEN
130+
apex_application_install.set_application_alias( 'F' || p_application_id );
131+
ELSE
132+
apex_application_install.set_application_alias( p_app_alias );
133+
END IF;
134+
135+
l_workspace_id := apex_util.find_security_group_id (p_workspace => p_workspace_name);
136+
APEX_APPLICATION_INSTALL.SET_WORKSPACE_ID ( l_workspace_id );
137+
--apex_application_install.set_offset( p_offset_num );
138+
apex_application_install.generate_offset;
139+
end;
140+
/
141+
@apex/install.sql
142+
/
143+
quit
144+
EOF
145+
146+
}
147+
148+
function generate_app_id {
149+
source config/asc.conf || die
150+
151+
tmpfile=$(mktemp -t generate_app_id)
152+
153+
run_sql_cmd @/dev/stdin <<ENDSQL > ${tmpfile} || exit 1
154+
set serveroutput on
155+
set feedback off
156+
begin
157+
apex_application_install.generate_application_id;
158+
dbms_output.put_Line(apex_application_install.get_application_id);
159+
end;
160+
/
161+
exit
162+
ENDSQL
163+
164+
app_id=$(cat $tmpfile)
165+
166+
rm $tmpfile
167+
168+
if [ -h config/asc.conf ]; then
169+
conf_file=$(readlink ./config/asc.conf)
170+
echo -n "The id '${app_id}' will be written to '${conf_file}'. Is this alright? [y/N]: "
171+
read can_write
172+
173+
if [ "${can_write}" == "y" ]; then
174+
sed -i "s/^apexappid=.*/apexappid=${app_id}/" config/${conf_file}
175+
echo "apexappid in '${conf_file}' has been replaced with '${app_id}'. The new config file looks like this: "
176+
cat config/${conf_file}
177+
else
178+
echo -n "The generated app id was not written to file. "
179+
echo "You can change this manually by changing the apexappid of your config to '${app_id}'"
180+
fi
181+
fi
182+
183+
}
184+
185+
function new_conf_file {
186+
echo "Creating new config file..."
187+
188+
echo -n "Please enter the file name of your config file: "
189+
read conf_file
190+
if [ -z "${conf_file}" ]; then
191+
echo "Please input a value for your config file name before pressing enter"; exit 1
192+
fi
193+
194+
if [ -e ./config/"${conf_file}" ]; then
195+
echo "Sorry, the file ./scripts/${conf_file} already exists. Either delete the existing file or choose a different name and try again."; exit 1
196+
fi
197+
198+
echo -n "Please enter the apexappid you would like to use. This should be chosen very carefully to avoid conflicts with other developers' app ids: "
199+
read apexappid
200+
201+
echo -n "Please enter the name of your workspace: "
202+
read workspace_name
203+
204+
echo -n "Please enter the parsing schema for the app you are using. Note that parsing schema should be all caps: "
205+
read parsing_schema
206+
207+
echo "NOTE: The app_alias variable should only be set for well known versions of the app (i.e. production or some dev versions) in order to avoid potentially damaging conflicts. Press [ENTER] to leave the variable unset"
208+
echo -n "Please enter the app_alias for your app: "
209+
read app_alias
210+
211+
echo -n "Please enter your Apex database connection in the following format [Hostname:port/SID]: "
212+
read database_connection
213+
214+
echo -n "Please enter your username for the given database: "
215+
read username
216+
217+
echo -n "Please enter your password: "
218+
read password
219+
220+
if [ ! -d ./config/ ]; then
221+
mkdir config
222+
fi
223+
224+
echo "apexappid=${apexappid}" > ./config/"${conf_file}"
225+
echo "workspace_name=${workspace_name}" >> ./config/"${conf_file}"
226+
echo "parsing_schema=${parsing_schema}" >> ./config/"${conf_file}"
227+
echo "app_alias=${app_alias}" >> ./config/"${conf_file}"
228+
echo "database_connection=${database_connection}" >> ./config/"${conf_file}"
229+
echo "username=${username}" >> ./config/"${conf_file}"
230+
echo "password=${password}" >> ./config/"${conf_file}"
231+
232+
echo "Config file successfully generated! It looks like this:"
233+
234+
cat ./config/"${conf_file}"
235+
236+
echo "If anything looks wrong you can simply edit the file yourself at ./config/${conf_file}"
237+
238+
echo
239+
240+
echo -n "Would you like to switch to the new config file now [y/n]: "
241+
read switch_bool
242+
243+
if [ "${switch_bool}" == "y" ]; then
244+
cd config
245+
if [ -h asc.conf ]; then
246+
rm asc.conf
247+
fi
248+
ln -s "${conf_file}" asc.conf
249+
cd ..
250+
echo "./config/asc.conf now points to ./config/$( readlink ./config/asc.conf )"
251+
else
252+
echo "Config file was not switched. Run 'npm run switch-conf-file' if you would like to change this."
253+
fi
254+
}
255+
256+
function switch_conf_file {
257+
#!/bin/bash
258+
#
259+
# This script is designed to be called from the top level directory of your project and to be placed in the ./scripts/ directory
260+
261+
echo "The availible config files are:"
262+
263+
ls ./config/ | sed s/asc.conf// | sed -n '1!p' #print all conf files
264+
265+
cd config
266+
267+
if [ -z "${1}" ]; then
268+
echo -n "Please enter the config file you would like to use: "
269+
read conf_file
270+
else
271+
conf_file="${1}"
272+
fi
273+
274+
if [ ! -e "${conf_file}" ]; then
275+
echo "Sorry, the file ./scripts/"${conf_file}" does not exist. Either create the file using npm run new-conf-file or choose a pre-existing config file."; exit 1
276+
fi
277+
278+
if [ -h asc.conf ]; then
279+
rm asc.conf
280+
fi
281+
282+
ln -s "${conf_file}" asc.conf
283+
284+
cd ..
285+
286+
echo "./config/asc.conf now points to ./config/$( readlink ./config/asc.conf )"
287+
288+
echo "The new config file looks like this: "
289+
290+
cat ./config/asc.conf
291+
}
292+
293+
function unistall_apex {
294+
check_conf_file || die
295+
source config/asc.conf || exit 1
296+
297+
run_sql_cmd @/dev/stdin "${apexappid}" "${workspace_name}" "${parsing_schema}" <<EOF || die Failed to uninstall
298+
declare
299+
p_application_id NUMBER := '&1';
300+
p_workspace_name varchar2(255) := '&2';
301+
p_parsing_schema varchar2(255) := '&3';
302+
l_workspace_id NUMBER;
303+
begin
304+
apex_application_install.set_application_id ( p_application_id );
305+
l_workspace_id := apex_util.find_security_group_id (p_workspace => p_workspace_name );
306+
apex_application_install.set_schema( p_parsing_schema );
307+
APEX_APPLICATION_INSTALL.SET_WORKSPACE_ID ( l_workspace_id );
308+
end;
309+
/
310+
@apex/application/init.sql
311+
@apex/application/set_environment.sql
312+
@apex/application/delete_application.sql
313+
@apex/application/end_environment.sql
314+
/
315+
quit
316+
317+
EOF
318+
}
319+
320+
# WIPMB is this useful? Can we delete it?
321+
function read_conf_file {
322+
323+
if [ ! -e ./config/asc.conf ]; then
324+
echo "Missing or broken symbolic link: ${PWD}/config/asc.conf"
325+
echo "Please use the command 'npm run switch-conf-file' to create the symbolic link ./config/asc.conf to your config file"
326+
echo "If you don't have a config file set up you can create one using 'npm run new-config-file'"
327+
exit 1
328+
fi
329+
330+
echo "The config file currently being used is: "
331+
readlink ./config/asc.conf
332+
333+
echo
334+
335+
echo "The config file looks like this: "
336+
cat ./config/asc.conf
337+
338+
echo "If any of the data looks wrong you can simply edit the file yourself at ./config/$(readlink ./config/asc.conf)"
339+
}
340+
341+
function main {
342+
case "${1}" in
343+
'test')
344+
./scripts/check_conf_file.sh
345+
;;
346+
'apex-to-file')
347+
apex_to_file
348+
;;
349+
'file-to-apex')
350+
file_to_apex
351+
;;
352+
'new-conf-file')
353+
new_conf_file
354+
;;
355+
'switch-conf-file')
356+
switch_conf_file
357+
;;
358+
'read-conf-file')
359+
read_conf_file
360+
;;
361+
'generate-app-id')
362+
generate_app_id
363+
;;
364+
'uninstall-apex')
365+
uninstall_apex
366+
;;
367+
*)
368+
echo "apex-source-control: bad command"
369+
;;
370+
esac
371+
}
372+
373+
main "$@"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.2.0",
44
"description": "Scripts for bringing Oracle Application Express apps into source control",
55
"bin": {
6-
"apex-source-control": "scripts/apex-source-control"
6+
"apex-source-control": "apex-source-control"
77
},
88
"scripts": {
99
"apex-to-file": "apex-source-control apex-to-file",

0 commit comments

Comments
 (0)