From d4b5e4d3e263b5546baf4ba552e97c61565c0911 Mon Sep 17 00:00:00 2001 From: Michael Buckley Date: Mon, 24 Jun 2019 15:38:35 -0400 Subject: [PATCH] Inline scripts and cleanup --- apex-source-control | 373 ++++++++++++++++++++++++++++++++++++ package.json | 2 +- scripts/apex-source-control | 36 ---- scripts/apex-to-file | 4 - scripts/check_conf_file.sh | 36 ---- scripts/install_apex.sql | 26 --- scripts/new-conf-file | 72 ------- scripts/read-conf-file | 18 -- scripts/switch-conf-file | 34 ---- scripts/uninstall_apex.sql | 18 -- 10 files changed, 374 insertions(+), 245 deletions(-) create mode 100755 apex-source-control delete mode 100755 scripts/apex-source-control delete mode 100755 scripts/apex-to-file delete mode 100755 scripts/check_conf_file.sh delete mode 100755 scripts/install_apex.sql delete mode 100755 scripts/new-conf-file delete mode 100755 scripts/read-conf-file delete mode 100755 scripts/switch-conf-file delete mode 100644 scripts/uninstall_apex.sql diff --git a/apex-source-control b/apex-source-control new file mode 100755 index 0000000..ff17a0e --- /dev/null +++ b/apex-source-control @@ -0,0 +1,373 @@ +#!/bin/bash + +function die { + echo $@ + exit 1 +} + +function run_sql_cmd { + local connect_string="${username}/${password}@${database_connection}" + local sql_cmd + if which sql > /dev/null; then + echo Running PWD $PWD + sql_cmd="sql -S" + else + echo WARNING: could not find SQLcl \(sql\). Falling back to sqlplus + sql_cmd="sqlplus -S" + fi + + NLS_LANG=.AL32UTF8 + export NLS_LANG + + ${sql_cmd} ${connect_string} "$@" || die Failed: ${sql_cmd} ${connect_string} "$@" +} + +function check_conf_file { + if [ ! -e ./config/asc.conf ]; then + echo "Missing or broken symbolic link: ${PWD}/config/asc.conf" + echo "Please use the command 'npm run switch-conf-file' to create the symbolic link ./config/asc.conf to your config file" + echo "If you don't have a config file set up you can create one using 'npm run new-config-file'" + exit 1 + fi + + echo "Using the config file at: $(readlink config/asc.conf)" + + source ./config/asc.conf + + [ -z "${apexappid}" ] && die "Missing config: ${apexappid} apexappid" + [ -z "${workspace_name}" ] && die "Missing config: ${workspace_name} workspace_name" + [ -z "${database_connection}" ] && die "Missing config: ${database_connection} database_connection" + [ -z "${username}" ] && die "Missing config: ${username} username" + [ -z "${password}" ] && die "Missing config: ${password} password" + + echo "Config file looks good! Moving on to the next step..." +} + +function legacy_apex_export { + + [ -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" + [ -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" + [ ! -e "${ORACLE_HOME}"/jdbc/lib/ojdbc6.jar ] && die "Missing ojdbc6.jar: please download from oracle and put in ${ORACLE_HOME}/jdbc/lib directory" + [ ! -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" + [ ! -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" + + source ./config/asc.conf + + export CLASSPATH="${APEX_HOME}"/utilities:"${ORACLE_HOME}"/jdbc/lib/ojdbc6.jar + + export_file="f${apexappid}.sql" + + if [ -d apex/ ]; then + rm -r apex/ + fi + if [ -e "${export_file}" ]; then + rm "${export_file}" + fi + + java oracle.apex.APEXExport -db "${database_connection}" -user "${username}" -password "${password}" -applicationid "${apexappid}" -skipExportDate -expOriginalIds || + 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." + + java oracle.apex.APEXExportSplitter "${export_file}" || + 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" + + rm "${export_file}" + + mv "f${apexappid}" apex # + + sed -i '' s^@application^@apex/application^ apex/install.sql +} + +function apex_export { + if which sql > /dev/null; then + source ./config/asc.conf + echo Using sqlcl to export app ${apexappid} + export_file="f${apexappid}.sql" + + if [ -d apex/ ]; then + rm -r apex/ + fi + if [ -e "${export_file}" ]; then + rm "${export_file}" + fi + + run_sql_cmd @/dev/stdin < p_workspace_name); + APEX_APPLICATION_INSTALL.SET_WORKSPACE_ID ( l_workspace_id ); + --apex_application_install.set_offset( p_offset_num ); + apex_application_install.generate_offset; +end; +/ +@apex/install.sql +/ +quit +EOF + +} + +function generate_app_id { + source config/asc.conf || die + + tmpfile=$(mktemp -t generate_app_id) + + run_sql_cmd @/dev/stdin < ${tmpfile} || exit 1 +set serveroutput on +set feedback off +begin + apex_application_install.generate_application_id; + dbms_output.put_Line(apex_application_install.get_application_id); +end; +/ +exit +ENDSQL + + app_id=$(cat $tmpfile) + + rm $tmpfile + +if [ -h config/asc.conf ]; then + conf_file=$(readlink ./config/asc.conf) + echo -n "The id '${app_id}' will be written to '${conf_file}'. Is this alright? [y/N]: " + read can_write + + if [ "${can_write}" == "y" ]; then + sed -i "s/^apexappid=.*/apexappid=${app_id}/" config/${conf_file} + echo "apexappid in '${conf_file}' has been replaced with '${app_id}'. The new config file looks like this: " + cat config/${conf_file} + else + echo -n "The generated app id was not written to file. " + echo "You can change this manually by changing the apexappid of your config to '${app_id}'" + fi +fi + +} + +function new_conf_file { +echo "Creating new config file..." + +echo -n "Please enter the file name of your config file: " +read conf_file +if [ -z "${conf_file}" ]; then + echo "Please input a value for your config file name before pressing enter"; exit 1 +fi + +if [ -e ./config/"${conf_file}" ]; then + echo "Sorry, the file ./scripts/${conf_file} already exists. Either delete the existing file or choose a different name and try again."; exit 1 +fi + +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: " +read apexappid + +echo -n "Please enter the name of your workspace: " +read workspace_name + +echo -n "Please enter the parsing schema for the app you are using. Note that parsing schema should be all caps: " +read parsing_schema + +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" +echo -n "Please enter the app_alias for your app: " +read app_alias + +echo -n "Please enter your Apex database connection in the following format [Hostname:port/SID]: " +read database_connection + +echo -n "Please enter your username for the given database: " +read username + +echo -n "Please enter your password: " +read password + +if [ ! -d ./config/ ]; then + mkdir config +fi + +echo "apexappid=${apexappid}" > ./config/"${conf_file}" +echo "workspace_name=${workspace_name}" >> ./config/"${conf_file}" +echo "parsing_schema=${parsing_schema}" >> ./config/"${conf_file}" +echo "app_alias=${app_alias}" >> ./config/"${conf_file}" +echo "database_connection=${database_connection}" >> ./config/"${conf_file}" +echo "username=${username}" >> ./config/"${conf_file}" +echo "password=${password}" >> ./config/"${conf_file}" + +echo "Config file successfully generated! It looks like this:" + +cat ./config/"${conf_file}" + +echo "If anything looks wrong you can simply edit the file yourself at ./config/${conf_file}" + +echo + +echo -n "Would you like to switch to the new config file now [y/n]: " +read switch_bool + +if [ "${switch_bool}" == "y" ]; then + cd config + if [ -h asc.conf ]; then + rm asc.conf + fi + ln -s "${conf_file}" asc.conf + cd .. + echo "./config/asc.conf now points to ./config/$( readlink ./config/asc.conf )" +else + echo "Config file was not switched. Run 'npm run switch-conf-file' if you would like to change this." +fi +} + +function switch_conf_file { +#!/bin/bash +# +# This script is designed to be called from the top level directory of your project and to be placed in the ./scripts/ directory + +echo "The availible config files are:" + +ls ./config/ | sed s/asc.conf// | sed -n '1!p' #print all conf files + +cd config + +if [ -z "${1}" ]; then + echo -n "Please enter the config file you would like to use: " + read conf_file +else + conf_file="${1}" +fi + +if [ ! -e "${conf_file}" ]; then + 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 +fi + +if [ -h asc.conf ]; then + rm asc.conf +fi + +ln -s "${conf_file}" asc.conf + +cd .. + +echo "./config/asc.conf now points to ./config/$( readlink ./config/asc.conf )" + +echo "The new config file looks like this: " + +cat ./config/asc.conf +} + +function unistall_apex { + check_conf_file || die + source config/asc.conf || exit 1 + + run_sql_cmd @/dev/stdin "${apexappid}" "${workspace_name}" "${parsing_schema}" < p_workspace_name ); + apex_application_install.set_schema( p_parsing_schema ); + APEX_APPLICATION_INSTALL.SET_WORKSPACE_ID ( l_workspace_id ); +end; +/ +@apex/application/init.sql +@apex/application/set_environment.sql +@apex/application/delete_application.sql +@apex/application/end_environment.sql +/ +quit + +EOF +} + +# WIPMB is this useful? Can we delete it? +function read_conf_file { + +if [ ! -e ./config/asc.conf ]; then + echo "Missing or broken symbolic link: ${PWD}/config/asc.conf" + echo "Please use the command 'npm run switch-conf-file' to create the symbolic link ./config/asc.conf to your config file" + echo "If you don't have a config file set up you can create one using 'npm run new-config-file'" + exit 1 +fi + +echo "The config file currently being used is: " +readlink ./config/asc.conf + +echo + +echo "The config file looks like this: " +cat ./config/asc.conf + +echo "If any of the data looks wrong you can simply edit the file yourself at ./config/$(readlink ./config/asc.conf)" +} + +function main { +case "${1}" in + 'test') + ./scripts/check_conf_file.sh + ;; + 'apex-to-file') + apex_to_file + ;; + 'file-to-apex') + file_to_apex + ;; + 'new-conf-file') + new_conf_file + ;; + 'switch-conf-file') + switch_conf_file + ;; + 'read-conf-file') + read_conf_file + ;; + 'generate-app-id') + generate_app_id + ;; + 'uninstall-apex') + uninstall_apex + ;; + *) + echo "apex-source-control: bad command" + ;; +esac +} + +main "$@" diff --git a/package.json b/package.json index 24de614..a0be05b 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.2.0", "description": "Scripts for bringing Oracle Application Express apps into source control", "bin": { - "apex-source-control": "scripts/apex-source-control" + "apex-source-control": "apex-source-control" }, "scripts": { "apex-to-file": "apex-source-control apex-to-file", diff --git a/scripts/apex-source-control b/scripts/apex-source-control deleted file mode 100755 index ec27335..0000000 --- a/scripts/apex-source-control +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash - -temp=$(readlink "${0}") #reads the symlink in node_modules/.bin which gives the relative path to the scripts/frontrunner script of this module -script_folder=$(dirname "${temp}") #gives the relative path from node_modules/.bin/ to the scripts dir of this module -bin_folder=$(dirname "${0}") #gives the absolute path of node_modules/.bin. We will need to call bin_folder/script_folder/some_script to call some_script of this module - - -case "${1}" in - 'test') - ./scripts/check_conf_file.sh - ;; - 'apex-to-file') - "${bin_folder}"/"${script_folder}"/apex-to-file - ;; - 'file-to-apex') - "${bin_folder}"/"${script_folder}"/file-to-apex - ;; - 'new-conf-file') - "${bin_folder}"/"${script_folder}"/new-conf-file - ;; - 'switch-conf-file') - "${bin_folder}"/"${script_folder}"/switch-conf-file - ;; - 'read-conf-file') - "${bin_folder}"/"${script_folder}"/read-conf-file - ;; - 'generate-app-id') - "${bin_folder}"/"${script_folder}"/generate-app-id - ;; - 'uninstall-apex') - "${bin_folder}"/"${script_folder}"/uninstall-apex - ;; - *) - echo "apex-source-control: bad command" - ;; -esac diff --git a/scripts/apex-to-file b/scripts/apex-to-file deleted file mode 100755 index 26e813f..0000000 --- a/scripts/apex-to-file +++ /dev/null @@ -1,4 +0,0 @@ -# designed to be called from frontrunner script -script_folder=$(dirname "${0}") -"${script_folder}"/check_conf_file.sh || exit 1 -"${script_folder}"/apexupdate.sh || exit 1 diff --git a/scripts/check_conf_file.sh b/scripts/check_conf_file.sh deleted file mode 100755 index a964028..0000000 --- a/scripts/check_conf_file.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash -# -# This script is designed to be run from the top level directory of your project and will break if called from anywhere else - -if [ ! -e ./config/asc.conf ]; then - echo "Missing or broken symbolic link: ${PWD}/config/asc.conf" - echo "Please use the command 'npm run switch-conf-file' to create the symbolic link ./config/asc.conf to your config file" - echo "If you don't have a config file set up you can create one using 'npm run new-config-file'" - exit 1 -fi - -echo "Using the config file at: $(readlink config/asc.conf)" - -source ./config/asc.conf - -if [ -z "${apexappid}" ]; then - echo "Missing config: ${apexappid} apexappid"; exit 1 -fi - -if [ -z "${workspace_name}" ]; then - echo "Missing config: ${workspace_name} workspace_name"; exit 1 -fi - -if [ -z "${database_connection}" ]; then - echo "Missing config: ${database_connection} database_connection"; exit 1 -fi - -if [ -z "${username}" ]; then - echo "Missing config: ${username} username"; exit 1 -fi - -if [ -z "${password}" ]; then - echo "Missing config: ${password} password"; exit 1 -fi - -echo "Config file looks good! Moving on to the next step..." diff --git a/scripts/install_apex.sql b/scripts/install_apex.sql deleted file mode 100755 index e11878a..0000000 --- a/scripts/install_apex.sql +++ /dev/null @@ -1,26 +0,0 @@ ---this script is designed to be called from the apex directory of your project with ./apex/install.sql being a valid file generated by APEXExportSplitter -declare - p_application_id NUMBER := '&1'; - p_workspace_name varchar2(255) := '&2'; - p_parsing_schema varchar2(255) := '&3'; - p_app_alias varchar2(255) := '&4'; - l_workspace_id NUMBER; -begin - apex_application_install.set_application_id ( p_application_id ); - apex_application_install.set_schema( p_parsing_schema ); - - IF p_app_alias is null THEN - apex_application_install.set_application_alias( 'F' || p_application_id ); - ELSE - apex_application_install.set_application_alias( p_app_alias ); - END IF; - - l_workspace_id := apex_util.find_security_group_id (p_workspace => p_workspace_name); - APEX_APPLICATION_INSTALL.SET_WORKSPACE_ID ( l_workspace_id ); - --apex_application_install.set_offset( p_offset_num ); - apex_application_install.generate_offset; -end; -/ -@apex/install.sql -/ -quit diff --git a/scripts/new-conf-file b/scripts/new-conf-file deleted file mode 100755 index 7dad97e..0000000 --- a/scripts/new-conf-file +++ /dev/null @@ -1,72 +0,0 @@ -#!/bin/bash -# -# This script is designed to be called from the top level directory of your project and must be placed in the $PROJECT_HOME/scripts/ folder - -echo "Creating new config file..." - -echo -n "Please enter the file name of your config file: " -read conf_file -if [ -z "${conf_file}" ]; then - echo "Please input a value for your config file name before pressing enter"; exit 1 -fi - -if [ -e ./config/"${conf_file}" ]; then - echo "Sorry, the file ./scripts/${conf_file} already exists. Either delete the existing file or choose a different name and try again."; exit 1 -fi - -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: " -read apexappid - -echo -n "Please enter the name of your workspace: " -read workspace_name - -echo -n "Please enter the parsing schema for the app you are using. Note that parsing schema should be all caps: " -read parsing_schema - -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" -echo -n "Please enter the app_alias for your app: " -read app_alias - -echo -n "Please enter your Apex database connection in the following format [Hostname:port/SID]: " -read database_connection - -echo -n "Please enter your username for the given database: " -read username - -echo -n "Please enter your password: " -read password - -if [ ! -d ./config/ ]; then - mkdir config -fi - -echo "apexappid=${apexappid}" > ./config/"${conf_file}" -echo "workspace_name=${workspace_name}" >> ./config/"${conf_file}" -echo "parsing_schema=${parsing_schema}" >> ./config/"${conf_file}" -echo "app_alias=${app_alias}" >> ./config/"${conf_file}" -echo "database_connection=${database_connection}" >> ./config/"${conf_file}" -echo "username=${username}" >> ./config/"${conf_file}" -echo "password=${password}" >> ./config/"${conf_file}" - -echo "Config file successfully generated! It looks like this:" - -cat ./config/"${conf_file}" - -echo "If anything looks wrong you can simply edit the file yourself at ./config/${conf_file}" - -echo - -echo -n "Would you like to switch to the new config file now [y/n]: " -read switch_bool - -if [ "${switch_bool}" == "y" ]; then - cd config - if [ -h asc.conf ]; then - rm asc.conf - fi - ln -s "${conf_file}" asc.conf - cd .. - echo "./config/asc.conf now points to ./config/$( readlink ./config/asc.conf )" -else - echo "Config file was not switched. Run 'npm run switch-conf-file' if you would like to change this." -fi diff --git a/scripts/read-conf-file b/scripts/read-conf-file deleted file mode 100755 index 3e0bed6..0000000 --- a/scripts/read-conf-file +++ /dev/null @@ -1,18 +0,0 @@ -# designed to be called from to level project dir - -if [ ! -e ./config/asc.conf ]; then - echo "Missing or broken symbolic link: ${PWD}/config/asc.conf" - echo "Please use the command 'npm run switch-conf-file' to create the symbolic link ./config/asc.conf to your config file" - echo "If you don't have a config file set up you can create one using 'npm run new-config-file'" - exit 1 -fi - -echo "The config file currently being used is: " -readlink ./config/asc.conf - -echo - -echo "The config file looks like this: " -cat ./config/asc.conf - -echo "If any of the data looks wrong you can simply edit the file yourself at ./config/$(readlink ./config/asc.conf)" diff --git a/scripts/switch-conf-file b/scripts/switch-conf-file deleted file mode 100755 index f7d201a..0000000 --- a/scripts/switch-conf-file +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash -# -# This script is designed to be called from the top level directory of your project and to be placed in the ./scripts/ directory - -echo "The availible config files are:" - -ls ./config/ | sed s/asc.conf// | sed -n '1!p' #print all conf files - -cd config - -if [ -z "${1}" ]; then - echo -n "Please enter the config file you would like to use: " - read conf_file -else - conf_file="${1}" -fi - -if [ ! -e "${conf_file}" ]; then - 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 -fi - -if [ -h asc.conf ]; then - rm asc.conf -fi - -ln -s "${conf_file}" asc.conf - -cd .. - -echo "./config/asc.conf now points to ./config/$( readlink ./config/asc.conf )" - -echo "The new config file looks like this: " - -cat ./config/asc.conf diff --git a/scripts/uninstall_apex.sql b/scripts/uninstall_apex.sql deleted file mode 100644 index c3e3e56..0000000 --- a/scripts/uninstall_apex.sql +++ /dev/null @@ -1,18 +0,0 @@ -declare - p_application_id NUMBER := '&1'; - p_workspace_name varchar2(255) := '&2'; - p_parsing_schema varchar2(255) := '&3'; - l_workspace_id NUMBER; -begin - apex_application_install.set_application_id ( p_application_id ); - l_workspace_id := apex_util.find_security_group_id (p_workspace => p_workspace_name ); - apex_application_install.set_schema( p_parsing_schema ); - APEX_APPLICATION_INSTALL.SET_WORKSPACE_ID ( l_workspace_id ); -end; -/ -@apex/application/init.sql -@apex/application/set_environment.sql -@apex/application/delete_application.sql -@apex/application/end_environment.sql -/ -quit