James Walker

James Walker

Web Developer

ipm dev blog, Part 2 – Refining the app initialisation

Published on Thursday October 19 2017 at 20:34 in ipm dev blog

In the first part of the ipm dev blog series, I outlined the basic purpose of the app and completed initial implementation of the app shell. This week, I refined the work by improving the app initialisation around the detection of ipm project files.

I've only managed to spend a small amount of time on ipm this week after starting the series last week. Today I'm documenting the single small change I've made since last week, which concerns the way the apps gets initialised with a project definition.

The original app shell published last week always looked for an ipm project file (.ipm) at startup and would abort execution if it couldn't find one in the working directory. It didn't consider any other factors that could influence whether having an ipm project file available is actually necessary. The lack of existence of a project file isn't necessarily an issue, if we are executing a global command or creating a new project.

The init method is now updated to only abort execution if the project file doesn't exist and we're not trying to run the --init command to initialise ipm in a new project directory.

#FUNCTION ipm.init(self, wd, args_cli)
#Initialise the app instance for the working directory and CLI arguments .
def init(self, wd, args_cli):
#Assign class vars
self.wd = wd
self.args_cli = args_cli

#Check ipm is initialised in the directory
if (pfile.project_file_exists(self.wd) != True) and (self.args_cli[0] != "--init"):
sys.exit("ipm aborting: ipm file not found.")

Similarly, the main method has had to be updated so we don't try to load a project file when we're running the --init command.

#FUNCTION ipm.main(self, wd, args_cli)
#App main runtime.
def main(self, wd, args_cli):
#Initialise the app
self.init(wd, args_cli)

#Load the project
if self.args_cli[0] != "--init":
self.project = pfile.load(wd)

if len(args_cli) == 0:
sys.exit("ipm aborting: no command specified.")
command = args_cli[0]
command_args = args_cli[1:]

#Load the main runtime
applet = main.runtime(command, command_args)

Naturally there are further improvements to be made here. One immediate step is to remove that hardcoded --init whitelist and instead create a centralised store of global commands that can always be run, even if ipm is being run from a working directory not initialised with an ipm project. We'd then just need to check whether the command being run is in the whitelist to determine whether we can ignore a missing ipm project file.

I'll be implementing this in the next post, hopefully alongside more substantial features including the first few built-in global commands. This should be published in two weeks' time.