Thursday, 24 September 2015

Compiling Oracle code from Atom text editor

Just yesterday I was watching a webinar titled: "Fill the Glass: Measuring Software Performance" which featured Jorge Rimblas. You can check out the video on Vimeo at the following URL: https://vimeo.com/140068961. It's just giving an insight into a project Jorge is working on, giving various tips here and there - so if you have a minute (rather, an hour) to spare, go and check it out.

One of the sections that particularly caught my attention was the fact Jorge was able to compile packages from his editor of choice (which is Sublime text editor) (this happens at around the 18:20 mark). Because I like free (and open source) software, I actually use Atom text editor. Being centred around a plugin ecosystem, I was curious how he was able to do this - and if in fact I'd be able to accomplish the same, in Atom.

So, first of all I did some hunting to see how it was done in Sublime. This led me to this great post by Tim St. Hilaire - http://wphilltech.com/sublime-text-for-oracle-apex-developers/ - which covers all the plugins he uses with Sublime. The section of interest is titled "Building Code" - where I discovered it used a build system embedded into the application itself; Tim also provides some sample scripts that he uses for the actual compilation, so I grabbed them (it's actually a zip file with a couple of examples) as a reference point.

The next thing was to find how to build code in Atom. I did a search for "atom editor build system". This immediately led me to the package - https://atom.io/packages/build. I installed that and begun hacking away to get this build process working! As per the documentation, depending on the files in your project directory, will determine which build process gets initiated. If your desired build process isn't built in (which it isn't for Oracle), you can add a file to your project .atom-build.json.

Now, because I'm on Ubuntu, my script is a `sh` script, and I'm going to place it in /usr/local/bin - which goes without saying, this file should be executable. Ideally, this will all be packaged into a plugin that extends from the build package, but that's for a future date.

It's worth mentioning here, that of course you will need an Oracle client set up on your system.

So, my .atom-build.json, in the root of my project folder, will look like this:

{
  "cmd": "/usr/local/bin/atom_compileOracle.sh",
  "name": "Build Oracle code file",
  "args": [ "${host}", "${port}", "${sid}", "${user}", "${password}", "{FILE_ACTIVE}" ],
  "sh": true,
  "cwd": "{FILE_ACTIVE_PATH}",
  "env": {
    "user": "hr",
    "password" : "hr",
    "host" : "example.com",
    "port" : "1521",
    "sid" : "XE"
  },
  "errorMatch": [
  ],
  "keymap": "",
  "targets": {
  }
}



So, for each project, you will just update the environment settings accordingly. To support for different environments (e.g dev and prod) I would guess the targets could be used here - I haven't got that far yet.

Then, the actual script would look like (again, script based from Tim St. Hilaire's examples):

#!/bin/bash
echo "Running"
echo "host: $1"
echo "port: $2"
echo "sid: $3"
echo "user: $4"
# echo "password: $5"
echo "Compiling file: $6"

sqlplus -S -L $4/$5@$1:$2/$3 << EOF

    set linesize 200
    set tab off
    set serveroutput on

    COLUMN MY_USER FORMAT A20
    COLUMN DB FORMAT A20
    COLUMN SID FORMAT A10
    COLUMN NOW FORMAT A35
    --Show the details of the connection for confirmation
    select
        user as MY_USER
      , ora_database_name as DB
      --, '$3' as SID
      , systimestamp as NOW
    from dual;

    @$6

    COLUMN MY_USER  CLEAR
    COLUMN DB      CLEAR
    COLUMN SID      CLEAR
    COLUMN NOW      CLEAR

    show error

EOF


So, in SQL Developer I confirm I have no packages:



Then, I have the config file for database settings:


So, this is a good template that can be copied and updated to your other Oracle projects, so you can compile directly. You of course would likely want to ignore it from being committed to version control, since the password is included.

Now, I can create a code file and compile it with the keyboard shortcut ctrl+alt+b (cmd + alt+ b for Mac).


And, just to verify, in SQL Developer I refresh the package list and see my code file was in fact compiled to the database.


6 comments:

  1. How have you gone with Atom? Is there a syntax highlighter for Oracle stuff?

    ReplyDelete
    Replies
    1. Hi Scott, I was going to reply to you on slack about this. I haven't seen any yet syntax highlighting just yet, but apparently you can convert text mate bundles to an atom extension. That should be the next job!

      Delete
  2. If you could give a little more details that could help me to setup the environment too. I didn't understand where to put the bash script.

    ReplyDelete
    Replies
    1. Hi M,
      The bash script can go anywhere on your system that your user can access. Just make sure it's executable. In this example, I placed it at: `/usr/local/bin/atom_buildOracle.sh`, which you can see I specified in the `.atom-build.json` in the root of my project so the `atom-build` package knows what to run.

      Hope it helps.

      Delete
  3. Hi Trent. Thanks for sharing this. I'm a long-time Emacs users looking at Atom and this was a firm requirement for me and it works great.

    PS. Did you go further and configure the 'errorMatch entry in .atom-build.json' so ctl-atl-g scrolls through the compilation errors and moves to the offending line and column ? The line/col info is there in SQL*Plus but I can't work out what RegEx is required.

    ReplyDelete
    Replies
    1. Hi Andy,
      I haven't attempted the errorMatch regex yet. I also started working on a package to encapsulate all this a while back - I got some reports of issues with SQLPlus in OS X, so I was playing around with sqlcl running in the background (due to high start up times) using a named FIFO.
      Anyway, if you are interested, you can check it here: https://github.com/tschf/atom-build-oracle
      I haven't published it to the Atom package repository yet as I have only really tested on my system (Ubuntu).
      Cheers,

      Delete