General developer forum

Adding environment dependency checking to custom plugin

 
Picture of Zoran Jeremic
Adding environment dependency checking to custom plugin
 
Hi,

I'm developing plugin for Moodle 2.6.3, and I have a dependency php_bcmath that has to be installed for plugin to work properly. I would like to have dependency check that will warn users about missing plugin, but as far as I found from documentation https://docs.moodle.org/dev/Environment_checking environment.xml file in plugin root is supported after Moodle 2.6.5. I don't want to change Moodle core code.

Is there any workaround for this?

Thanks,
Zoran


 
Average of ratings: -
Picture of Darko Miletić
Re: Adding environment dependency checking to custom plugin
Core developersParticularly helpful Moodlers

There is nothing out of the box so we have to resort to hacks. In general moodle plugin install fails when version is not set so this might work:

// Inside [plugindir]/version.php
if (extension_loaded('myextension')) {
$plugin->version = myversion;
}



 
Average of ratings: Useful (2)
Picture of Zoran Jeremic
Re: Adding environment dependency checking to custom plugin
 

This is great. Thanks a lot.

Zoran



 
Average of ratings: -
Picture of Matteo Scaramuccia
Re: Adding environment dependency checking to custom plugin - HACK working but when using CI
Core developersParticularly helpful MoodlersPlugin developers

TNX Darko, it is actually an useful tip! Yes

I was in need to prevent a plugin branch to be used under onwards Moodle versions i.e. a MOODLE_XX_STABLE plugin branch should not be installed, by mistake, in a Moodle version YY where YY is a minor/major update compared to XX e.g. Moodle 34 vs plugin 33).

It works as described but issues come when adopting the HACK and you want to keep running the previous CI infrastructure via moodle-plugin-ci.

The current almost working snippet - I do not want the plugin to be installed in a Moodle branch version different from 33 - is:

...
$plugin->component = '********';
// HACK - Do not install this plugin version on Moodle 3.4+.
global $CFG, $branch;
$currentbranch = isset($CFG->branch) ? $CFG->branch : $branch;
if ($currentbranch === '33') {
    $plugin->version = ********;
    $plugin->release = '********';
}
$plugin->requires = 2017051500;
...

Today I'll try to fix my current CI issue:

...
$ moodle-plugin-ci phpunit
 RUN  PHPUnit tests for ********
Moodle 3.3.5+ (Build: 20180405), 14fc7f597a3abcdb827e644ee925594147dfa3b8
Php: 7.0.25, pgsql: 9.3.20, OS: Linux 4.14.12-041412-generic x86_64
Moodle PHPUnit environment was initialised for different version, please use:
 php admin/tool/phpunit/cli/init.php
The command "moodle-plugin-ci phpunit" exited with 1. ...

guessing that for some reason at some time the admin/tool/phpunit/cli/init.php has already done its job in a previous stage w/o installing the plugin and now the phpunit setup should be redone due to the new plugin being installed.

It worth noting that moodle-plugin-ci is mostly a wrapper to native moodle commands like:

  • php -d log_errors=1 -d error_log=NULL /home/travis/build/moodle/admin/tool/behat/cli/util_single_run.php --install
  • php -d log_errors=1 -d error_log=NULL /home/travis/build/moodle/admin/tool/phpunit/cli/util.php --buildconfig <= this will be the starting point for my last (? Hopefully...) issue

HTH,
Matteo

 
Average of ratings: -
Picture of Darko Miletić
Re: Adding environment dependency checking to custom plugin - HACK working but when using CI
Core developersParticularly helpful Moodlers

You just need to be creative here. Try setting the Moodle version to something far in the future.

$plugin->requires = 2017051500;
$branch = (int)$CFG->branch;
if ($branch > 33) {
    $plugin->requires = 2045051500;
}



 
Average of ratings: Useful (2)