Skip to content

Commit

Permalink
plugin install: create base env with git and run git commands there
Browse files Browse the repository at this point in the history
  • Loading branch information
emlys committed Jan 10, 2025
1 parent 595684b commit 44371cf
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions workbench/src/main/setupAddRemovePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,26 @@ export function setupAddPlugin() {
try {
logger.info('adding plugin at', pluginURL);
const micromamba = settingsStore.get('micromamba');
const rootPrefix = upath.join(process.resourcesPath, 'micromamba_envs')
const baseEnvPrefix = upath.join(rootPrefix, 'invest_base');
// Create invest_base environment, if it doesn't already exist
// The purpose of this environment is just to ensure that git is available
if (!fs.existsSync(baseEnvPrefix)) {
await spawnWithLogging(
micromamba,
['create', '--yes', '--prefix', `"${baseEnvPrefix}"`, '-c', 'conda-forge', 'git']
);
}
// Create a temporary directory and check out the plugin's pyproject.toml
const tmpPluginDir = fs.mkdtempSync(upath.join(tmpdir(), 'natcap-invest-'));
await spawnWithLogging(
'git',
['clone', '--depth', '1', '--no-checkout', pluginURL, tmpPluginDir]
micromamba,
['run', '--prefix', `"${baseEnvPrefix}"`,
'git', 'clone', '--depth', '1', '--no-checkout', pluginURL, tmpPluginDir]
);
await spawnWithLogging(
'git',
['checkout', 'HEAD', 'pyproject.toml'],
micromamba,
['run', '--prefix', `"${baseEnvPrefix}"`, 'git', 'checkout', 'HEAD', 'pyproject.toml'],
{ cwd: tmpPluginDir }
);
// Read in the plugin's pyproject.toml, then delete it
Expand All @@ -80,22 +91,24 @@ export function setupAddPlugin() {

// Create a conda env containing the plugin and its dependencies
const envName = `invest_plugin_${pluginID}`;
const pluginEnvPrefix = upath.join(rootPrefix, envName)
await spawnWithLogging(
micromamba,
['create', '--yes', '--name', envName, '-c', 'conda-forge', '"python<3.12"', '"gdal<3.6"']
['create', '--yes', '--prefix', `"${pluginEnvPrefix}"`,
'-c', 'conda-forge', '"python<3.12"', '"gdal<3.6"']
);
logger.info('created micromamba env for plugin');
await spawnWithLogging(
micromamba,
['run', '--name', envName, 'pip', 'install', `git+${pluginURL}`]
['run', '--prefix', `"${pluginEnvPrefix}"`, 'pip', 'install', `git+${pluginURL}`]
);
logger.info('installed plugin into its env');
// Write plugin metadata to the workbench's config.json
const envInfo = execSync(`${micromamba} info --name ${envName}`, { windowsHide: true }).toString();
logger.info(`env info:\n${envInfo}`);
const regex = /env location : (.+)/;
const envPath = envInfo.match(regex)[1];
logger.info(`env path:\n${envPath}`);
// const envInfo = execSync(`${micromamba} info --name ${envName}`, { windowsHide: true }).toString();
// logger.info(`env info:\n${envInfo}`);
// const regex = /env location : (.+)/;
// const envPath = envInfo.match(regex)[1];
// logger.info(`env path:\n${envPath}`);
logger.info('writing plugin info to settings store');
settingsStore.set(
`plugins.${pluginID}`,
Expand All @@ -104,7 +117,7 @@ export function setupAddPlugin() {
pyname: pluginPyName,
type: 'plugin',
source: pluginURL,
env: envPath,
env: pluginEnvPrefix,
}
);
logger.info('successfully added plugin');
Expand Down

0 comments on commit 44371cf

Please sign in to comment.