From b885d1ba790a5c9efdcede6de3b4494653845b68 Mon Sep 17 00:00:00 2001 From: Jean-Yves TINEVEZ Date: Sun, 23 Jun 2024 18:25:33 +0200 Subject: [PATCH] Sub-class for CLIConfigurator that specializes in conda Python tools. For many tools we want to integrate are Python tools that can be run from a specific conda environment. If the implementing CLI config knows already what is the Python command, the user should just have to specify in what conda env it is installed. It is more convenient than to browse to a Python executable and less prone to error. --- .../util/cli/CondaCLIConfigurator.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/main/java/fiji/plugin/trackmate/util/cli/CondaCLIConfigurator.java diff --git a/src/main/java/fiji/plugin/trackmate/util/cli/CondaCLIConfigurator.java b/src/main/java/fiji/plugin/trackmate/util/cli/CondaCLIConfigurator.java new file mode 100644 index 000000000..58354fb56 --- /dev/null +++ b/src/main/java/fiji/plugin/trackmate/util/cli/CondaCLIConfigurator.java @@ -0,0 +1,59 @@ +package fiji.plugin.trackmate.util.cli; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CondaCLIConfigurator extends CLIConfigurator +{ + + public static final String KEY_CONDA_ENV = "CONDA_ENV"; + + private final ChoiceArgument condaEnv; + + protected CondaCLIConfigurator() + { + super(); + + // Make a UI-only arg configuring the conda env. + // Default is last one (base is not interesting as a default). + final List< String > envList = CLIUtils.getEnvList(); + this.condaEnv = addChoiceArgument() + .name( "Conda environment" ) + .help( "In what conda environment is the tool installed." ) + .addChoiceAll( envList ) + .defaultValue( envList.get( envList.size() - 1 ) ) + .key( KEY_CONDA_ENV ) + .visible( true ) + .inCLI( false ) + .get(); + + // Don't show the executable arg in the UI: must be set by subclass, and + // only conda env required configurating. + getExecutableArg().visible( false ); + + // Add the translator to make a proper cmd line calling conda first. + setTranslator( getExecutableArg(), s -> { + final String executableName = ( String ) s; + // Split by spaces + final String[] split = executableName.split( " " ); + final List< String > cmd = new ArrayList<>(); + final String envname = condaEnv.getValue(); + cmd.addAll( Arrays.asList( "cmd.exe", "/c", "conda", "activate", envname ) ); + cmd.add( "&" ); + cmd.addAll( Arrays.asList( split ) ); + return cmd; + } ); + } + + public ChoiceArgument getCondaEnv() + { + return condaEnv; + } + + @Override + public String check() + { + return checkArguments(); + } +}