Skip to content

Commit

Permalink
VSCode Task to create new packages #36
Browse files Browse the repository at this point in the history
  • Loading branch information
martindsouza committed Aug 27, 2020
1 parent d4bd80a commit a9cb97c
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 18 deletions.
14 changes: 10 additions & 4 deletions .vscode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
- [Setup](#setup)
- [`tasks.json`](#tasksjson)
- [Tasks](#tasks)
- [Compiling Code](#compiling-code)
- [APEX Export](#apex-export)
- [Compiling Code](#compiling-code)
- [Generate Object](#generate-object)

[Microsoft Visual Studio Code (VSC)](https://code.visualstudio.com/) is a code editor and is recommended for most PL/SQL work. VSC can compile PL/SQL code directly from VSC (see [this blog](https://ora-00001.blogspot.ca/2017/03/using-vs-code-for-plsql-development.html)) for more information. Opening this project folder in VSC will automatically give you the ability to compile PL/SQL code and do APEX backups

Expand All @@ -27,10 +28,15 @@ Tasks can be executed with `⌘+shift+B` and selecting the desired task.

![Task Compile Demo](img/task-compile.gif)

### APEX Export

If you want to export your APEX applications (defined in `scripts/project-config.sh`) execute the `apex export: <project name>`

### Compiling Code

To compile the current file you're editing execute `compile: <project name>` task.
To compile the current file you're editing execute the `compile: <project name>` task.

### APEX Export

If you want to export your APEX applications (defined in `scripts/project-config.sh`) execute the `apex export: <project name>`
### Generate Object

To quickly create a new package, view, or data file execute the `generate object: <project name>` task.
10 changes: 10 additions & 0 deletions .vscode/scripts/gen_object.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Env variables $1, $2, etc are from the tasks.json args array

# Directory of this file
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Load helper
source $SCRIPT_DIR/../../scripts/helper.sh

# Generate object
gen_object $1 $2
34 changes: 34 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@
},
"options": {},
"problemMatcher": []
},
{
"label": "generate object: CHANGEME",
"type": "shell",
"command": ".vscode/scripts/gen_object.sh",
"args": [
"${input:newObjectType}",
"${input:newObjectName}"
],
"group": "build",
"presentation": {
"reveal": "always",
"panel": "dedicated"
},
"options": {},
"problemMatcher": []
},
],
"inputs": [
{
"type": "pickString",
"id": "newObjectType",
"description": "Testing types to include",
// Note: these options must match what's in scripts/project-config.sh
"options": [
"package",
"view",
"data"
]
},
{
"type": "promptString",
"id": "newObjectName",
"description": "Object name"
}
]
}
51 changes: 38 additions & 13 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ Files in this folder can be used for multiple purposes such as VSCode compilatio
- [Environment Variables](#environment-variables)
- [Functions](#functions)
- [`export_apex_app`](#export_apex_app)
- [`reset_release`](#reset_release)
- [`gen_object`](#gen_object)
- [`list_all_files`](#list_all_files)
- [`reset_release`](#reset_release)
- [`project-config.sh`](#project-configsh)
- [`user-config.sh`](#user-configsh)

Expand Down Expand Up @@ -100,30 +101,31 @@ export_apex_app
export_apex_app 1.2.3
```

#### `gen_object`

#### `reset_release`

Resets the project's root release folder. Because resetting will erase everything in the `release/code` folder and reset `release/code/_run_code.sql` this function requires that an additional parameter is passed in to ensure that nothing is deleted by mistake.
Generates a file based on template file. (see example below for more description)

**Parameters**
Position | Required | Description
--- | --- | ---
`$1` | Required | project root directory name. If this root folder is `/Users/martin/git/insum/starter-project-template/` then this parameter will be `starter-project-template`
`$1` | Required | Object type. By default this is `package, view, data`)
`$2` | Required | Object name. Will be new file name along with replacing all reference of `CHANGEME` in file


**Example**

```bash
# Show what happens when no parameter is passed in
# Note the error message will show what call to make
reset_release
Suppose you wanted to quickly create a new package (`pkg_emp`). By default in the [`templates`](../templates) folder there exists two files [`template_pkg.pks`](../templates/template_pkg.pks) and [`template_pkg.pkb`](../templates/template_pkg.pkb). In the past you'd need to copy these two files, rename them, then modify the `CHANGEME`s and replace with your package name. Now you can simply:

Error: confirmation directory missing or not matching. Run: reset_release starter-project-template
```bash
source ./scripts/helper.sh
gen_object package pkg_emp
```

# Correct run
This will then automatically create two new files `packages/pkg_emp.pks` and `packages/pkg_emp.pkb`. In VSCode there's also a task for this to avoid any command line

reset_release starter-project-template
```
**Configuration**

To modify the different types of available templates modify [`scripts/project-config.sh`](project-config.sh) and look for `OBJECT_FILE_TEMPLATE_MAP` (it is self documenting)

#### `list_all_files`

Expand All @@ -146,6 +148,29 @@ list_all_files views release/all_views.sql sql
list_all_files packages release/all_packages.sql pks,pkb
```

#### `reset_release`

Resets the project's root release folder. Because resetting will erase everything in the `release/code` folder and reset `release/code/_run_code.sql` this function requires that an additional parameter is passed in to ensure that nothing is deleted by mistake.

**Parameters**
Position | Required | Description
--- | --- | ---
`$1` | Required | project root directory name. If this root folder is `/Users/martin/git/insum/starter-project-template/` then this parameter will be `starter-project-template`

**Example**

```bash
# Show what happens when no parameter is passed in
# Note the error message will show what call to make
reset_release

Error: confirmation directory missing or not matching. Run: reset_release starter-project-template

# Correct run

reset_release starter-project-template
```

## `project-config.sh`
This file contains information about your project (such as schema name, APEX applications, etc.). It is common for all developers and changes are saved in git. **Do not** put any sensitive information in this file (`user-config.sh` is for sensitive information).

Expand Down
61 changes: 61 additions & 0 deletions scripts/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,67 @@ from dual;



# #36 Create new files quickly based on template files
#
# See scripts/project-config.sh on how to define the various object types
#
# Actions:
# - Create a new file in defined destination folder
# - Based on template
# - Replace all referneces to CHANGEME with the object name
#
# Parameters
# $1 Object type
# $2 Object Name
gen_object(){
# Parameters
local p_object_type=$1
local p_object_name=$2

# Loop variables
local object_type_arr
local object_type
local object_template
local object_dest_folder
local object_dest_file

# OBJECT_FILE_TEMPLATE_MAP is defined in scripts/project-config.sh
for object_type in $(echo $OBJECT_FILE_TEMPLATE_MAP | sed "s/,/ /g"); do

object_type_arr=(`echo "$object_type" | sed 's/:/ /g'`)

# In bash arrays start at 0 whereas in zsh they start at 1
# Only way to make array reference compatible with both is to specify the offset and length
# See: https://stackoverflow.com/questions/50427449/behavior-of-arrays-in-bash-scripting-and-zsh-shell-start-index-0-or-1/50433774
object_type=${object_type_arr[@]:0:1}
object_template=${object_type_arr[@]:1:1}
object_file_exts=${object_type_arr[@]:2:1}
object_dest_folder=${object_type_arr[@]:3:1}

if [[ "$p_object_type" == "$object_type" ]]; then

for file_ext in $(echo $object_file_exts | sed "s/;/ /g"); do
object_dest_file=$PROJECT_DIR/$object_dest_folder/$p_object_name.$file_ext

if [[ -f $object_dest_file ]]; then
echo "${COLOR_ORANGE}File already exists:${COLOR_RESET} $object_dest_file"
else
cp $object_template.$file_ext $object_dest_file
sed -i -bak "s/CHANGEME/$p_object_name/g" $object_dest_file
# Remove backup versin of file
rm $object_dest_file-bak
echo "Created: $object_dest_file"
fi
done

break # No longer need to loop through other definitions
fi

done # OBJECT_FILE_TEMPLATE_MAP

} # gen_object


# Initialize
init(){
local PROJECT_DIR_FOLDER_NAME=$(basename $PROJECT_DIR)
Expand Down
18 changes: 17 additions & 1 deletion scripts/project-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,20 @@ APEX_APP_IDS=CHANGEME
# Will be used throughought the scripts to generate lists of packages, views, etc from the filesystem
EXT_PACKAGE_SPEC=pks
EXT_PACKAGE_BODY=pkb
EXT_VIEW=sql
EXT_VIEW=sql


# File Mappings
# This will be used in VSCode to allow for quick generate of a given file based on template data
# Format:
# <name>:<template_file prefix (no extension)>:<file extensions (; delimited)>:<destination directory>
#
# Definitions:
# - name: Name that will be mapped to VSCode task
# - template file: Template file prefix to use (no extension)
# - file extensions: ";" delimited list of file extensions to reference each template file
# - destination directory: where to store the new file
OBJECT_FILE_TEMPLATE_MAP=""
OBJECT_FILE_TEMPLATE_MAP="$OBJECT_FILE_TEMPLATE_MAP,package:templates/template_pkg:$EXT_PACKAGE_SPEC;$EXT_PACKAGE_BODY:packages"
OBJECT_FILE_TEMPLATE_MAP="$OBJECT_FILE_TEMPLATE_MAP,view:templates/template_view:$EXT_VIEW:views"
OBJECT_FILE_TEMPLATE_MAP="$OBJECT_FILE_TEMPLATE_MAP,data:templates/template_data:sql:data"
3 changes: 3 additions & 0 deletions templates/template_view.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
create or replace force view CHANGEME as

;

0 comments on commit a9cb97c

Please sign in to comment.