Skip to content

Commit

Permalink
Sub-class for CLIConfigurator that specializes in conda Python tools.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tinevez committed Jun 23, 2024
1 parent 2dfecd9 commit b885d1b
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit b885d1b

Please sign in to comment.