Generate APEX plug-ins on the command line
Instead of uploading new files to a plug-in, we generate the plug-in via a template on the command line and install it
Table of Contents
Introduction
Everyone who develops plug-ins for APEX knows the problem: Once a plug-in is finished, future maintenance is usually just a matter of uploading updated JavaScript files to the plug-in and finally downloading the plug-in to make it available to others. This can be a very time consuming process when troubleshooting or implementing a new feature.
So for my latest open source project, I thought about how to shorten the whole process. I already had a file watcher and a build script running anyway, which, when source files are changed, merges the entire project into a single SQL install script. The plug-in was just about updating a JavaScript file (normal and minified). The PL/SQL code for the plug-in is in a package and only referenced - this is also a good approach for performance reasons (Blog post by Daniel Hochleitner on the topic).
Creating a template from the existing plug-in
So I downloaded the finished plug-in once and stored it as a template in the project directory. Then I looked at how the files are stored in the SQL script of the plug-in:
It looks like we have to encode the files somehow - but how? If you take a closer look at the characters, you will notice at some point that only the digits 0 to 9 and the letters a to f are used. So it seems to be a hexadecimal encoding - with 200 characters in each block.
Ok, then in the template we replace the first anonymous PL/SQL block with a placeholder:
Now we need some code to replace the placeholder with the converted source file.
JavaScript helper functions for conversion
Since I implemented the file watcher and other scripts with Node.js/npm, it was obvious to solve this in JavaScript as well. First the needed modules and a md5 hash function:
const fs = require('fs');
const UglifyJS = require('uglify-js');
const crypto = require('crypto');
const toMd5Hash = function (string) { return crypto.createHash('md5').update(string).digest('hex') };
let consoleJsCode, minified, version, md5Hash, conf;
Then we need a helper function that breaks a string into blocks of 200 characters and returns it as an array:
const toChunks = function (text, size) {
const numChunks = Math.ceil(text.length / size);
const chunks = new Array(numChunks);
for (let i = 0, start = 0; i < numChunks; ++i, start += size) {
chunks[i] = text.substr(start, size);
}
return chunks;
};
Finally, our actual function, which hexadecimally encodes a string (read file) and puts it into the required SQL script form:
const toApexPluginFile = function (text) {
const hexString = new Buffer.from(text).toString('hex');
const chunks = toChunks(hexString, 200);
let apexLoadFile = 'begin\n' +
' wwv_flow_api.g_varchar2_table := wwv_flow_api.empty_varchar2_table;\n';
for (let i = 0; i < chunks.length; ++i) {
apexLoadFile += ` wwv_flow_api.g_varchar2_table(${(i + 1)}) := '${chunks[i]}';\n`;
}
apexLoadFile += 'end;\n/';
return apexLoadFile;
};
Build script to build the plug-in when changes are made in the source code.
Everything is assembled by the build script - here is the relevant snippet:
It should be noted that we will also have to manage the file version itself - APEX will install it as we create it. For that purpose, we check by means of hash-function whether JavaScript source file has changed and only then we regenerate the plug-in (including minification). This way, we avoid unnecessarily regenerating the plug-in every time we change source code in the repository and thus incrementing the file version.In the build script snippet above, the current file version and hash value is stored in a JSON file (apexplugin.json
, last line of code) so that we can use this as a reference in the next build.
Here is the relevant place in the plug-in template where the file version is entered by the build script (placeholder #FILE_VERSION#
, last parameter):
In addition, the version number of the plug-in itself is also updated here (placeholder #CONSOLE_VERSION#
, third last parameter) - but this depends on the respective circumstances of the project. In my case I write the version number of the PL/SQL package CONSOLE also into the plug-in depending on it - so if the version number of the package changes, the plug-in will be rebuilt as well. But here everybody has to see what is needed in the project.
When saving source code changes, the file watcher starts and triggers the build script. This then builds two installation SQL scripts together: That of the logging tool itself (not the focus of this article) and then that for the plug-in. Following this, both scripts are then installed in a dev environment. For this, of course, the default workspace stored in the plug-in and the default application number must be present in the dev environment.
NPM scripts for orchestration
I now often manage build and install scripts with the node package manager npm. The reason for this is the good integration with Visual Studio Code, the operating system independence and the possibility to orchestrate the scripts a bit - ok, you have to install Node.js, but don’t you always? With one click you can either start the watch task, which does everything automatically with every save of a source file, or you can conveniently call the scripts individually. If someone else looks into the repository, it is also clearer with the npm scripts what works here and how.
It’s also clear that you don’t write usernames and passwords into the scripts here anymore - that’s already forbidden in principle when the code is checked into version control. On the screenshot you can (hopefully) see that here a wallet with the alias playground
is used. For those who only use the instant client and have problems setting up a wallet: How to use mkstore and orapki with Oracle Instant Client.
The scripts are managed in the file package.json
on the top level of the project directory. If you want to read more about npm scripts, you can start here:
A complete example
For those who want to see how everything can work together: I took the examples here in the article from my open source project Oracle Instrumentation Console.
Another way
My focus with this solution was to be able to supply a plug-in locally on the PC with new files. But there is another way: You can upload files or whole directories via script into an installed plug-in of an APEX application and then download the plug-in afterwards. For this purpose there is the Open Souce Project APEX Publish Static Files by Vincent Morneau. This project is also integrated with APEX Nitro which can speed up frontend development in APEX.
Have fun generating plug-ins :-)
Ottmar