Skip to content

Commit 96a059d

Browse files
committed
Align API more consistently with appose-python
1 parent 0dbbffd commit 96a059d

9 files changed

Lines changed: 95 additions & 41 deletions

File tree

src/main/java/org/apposed/appose/Appose.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,17 @@ public static DynamicBuilder content(String content) {
447447

448448
// -- Direct Environment shortcuts --
449449

450+
/**
451+
* Wraps an existing environment directory, automatically detecting the environment type.
452+
*
453+
* @param envDir The path to the directory containing the environment.
454+
* @return An Environment configured for the detected type.
455+
* @throws BuildException If the directory doesn't exist or type cannot be determined.
456+
*/
457+
public static Environment wrap(String envDir) throws BuildException {
458+
return wrap(new File(envDir));
459+
}
460+
450461
/**
451462
* Wraps an existing environment directory, automatically detecting the environment type.
452463
* This method intelligently detects whether the directory contains a pixi, mamba/conda,
@@ -472,17 +483,6 @@ public static Environment wrap(File envDir) throws BuildException {
472483
return custom().wrap(envDir);
473484
}
474485

475-
/**
476-
* Wraps an existing environment directory, automatically detecting the environment type.
477-
*
478-
* @param envDir The path to the directory containing the environment.
479-
* @return An Environment configured for the detected type.
480-
* @throws BuildException If the directory doesn't exist or type cannot be determined.
481-
*/
482-
public static Environment wrap(String envDir) throws BuildException {
483-
return wrap(new File(envDir));
484-
}
485-
486486
/**
487487
* Creates a system environment with sensible defaults.
488488
* <p>

src/main/java/org/apposed/appose/BuildException.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,40 @@ public class BuildException extends Exception {
4040
@Nullable
4141
public final Builder<?> builder;
4242

43+
public BuildException() {
44+
this(null, null, null);
45+
}
46+
47+
public BuildException(Builder<?> builder) {
48+
this(builder, null, null);
49+
}
50+
4351
public BuildException(String message) {
44-
this(null, message);
52+
this(null, message, null);
4553
}
4654

4755
public BuildException(Throwable cause) {
48-
this(null, cause);
56+
this(null, null, cause);
4957
}
5058

51-
public BuildException(@Nullable Builder<?> builder, String message) {
52-
super(message);
53-
this.builder = builder;
59+
public BuildException(Builder<?> builder, String message) {
60+
this(builder, message, null);
61+
}
62+
63+
public BuildException(Builder<?> builder, Throwable cause) {
64+
this(builder, null, cause);
5465
}
5566

56-
public BuildException(@Nullable Builder<?> builder, Throwable cause) {
57-
this(builder, makeMessage(builder, cause), cause);
67+
public BuildException(String message, Throwable cause) {
68+
this(null, message, cause);
5869
}
5970

60-
public BuildException(@Nullable Builder<?> builder, String message, Throwable cause) {
61-
super(message, cause);
71+
public BuildException(
72+
@Nullable Builder<?> builder,
73+
@Nullable String message,
74+
@Nullable Throwable cause)
75+
{
76+
super(message == null ? makeMessage(builder, cause) : message, cause);
6277
this.builder = builder;
6378
}
6479

src/main/java/org/apposed/appose/BuilderFactory.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ public interface BuilderFactory {
7777
*/
7878
double priority();
7979

80+
/**
81+
* Checks if this builder can wrap the given existing environment directory.
82+
* Used by {@link Appose#wrap(File)} to auto-detect environment type.
83+
*
84+
* @param envDir The directory to check
85+
* @return true if this builder can wrap the directory
86+
*/
87+
default boolean canWrap(String envDir) {
88+
return canWrap(new File(envDir));
89+
}
90+
8091
/**
8192
* Checks if this builder can wrap the given existing environment directory.
8293
* Used by {@link Appose#wrap(File)} to auto-detect environment type.

src/main/java/org/apposed/appose/builder/Builders.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private Builders() {
4848
}
4949

5050
/** All known {@link BuilderFactory} implementations, in priority order. */
51-
private static final List<BuilderFactory> ALL = Plugins.discover(BuilderFactory.class,
51+
private static final List<BuilderFactory> BUILDERS = Plugins.discover(BuilderFactory.class,
5252
(a, b) -> Double.compare(b.priority(), a.priority()));
5353

5454
/**
@@ -58,7 +58,7 @@ private Builders() {
5858
* @return The factory with matching name, or null if not found.
5959
*/
6060
public static @Nullable BuilderFactory findFactoryByName(String name) {
61-
return Plugins.find(ALL, factory -> factory.name().equalsIgnoreCase(name));
61+
return Plugins.find(BUILDERS, factory -> factory.name().equalsIgnoreCase(name));
6262
}
6363

6464
/**
@@ -69,7 +69,18 @@ private Builders() {
6969
* @return The first factory that supports the scheme, or null if none found.
7070
*/
7171
public static @Nullable BuilderFactory findFactoryByScheme(String scheme) {
72-
return Plugins.find(ALL, factory -> factory.supportsScheme(scheme));
72+
return Plugins.find(BUILDERS, factory -> factory.supportsScheme(scheme));
73+
}
74+
75+
/**
76+
* Finds the first factory that can wrap the given environment directory.
77+
* Factories are checked in priority order (highest priority first).
78+
*
79+
* @param envDir The directory to find a factory for.
80+
* @return The first factory that can wrap the directory, or null if none found.
81+
*/
82+
public static @Nullable BuilderFactory findFactoryForWrapping(String envDir) {
83+
return findFactoryForWrapping(new File(envDir));
7384
}
7485

7586
/**
@@ -80,7 +91,18 @@ private Builders() {
8091
* @return The first factory that can wrap the directory, or null if none found.
8192
*/
8293
public static @Nullable BuilderFactory findFactoryForWrapping(File envDir) {
83-
return Plugins.find(ALL, factory -> factory.canWrap(envDir));
94+
return Plugins.find(BUILDERS, factory -> factory.canWrap(envDir));
95+
}
96+
97+
/**
98+
* Checks if the given directory can be wrapped as a known environment type.
99+
* This is a convenience method equivalent to {@code findFactoryForWrapping(envDir) != null}.
100+
*
101+
* @param envDir The directory to check.
102+
* @return true if the directory can be wrapped by any known builder, false otherwise.
103+
*/
104+
public static boolean canWrap(String envDir) {
105+
return canWrap(new File(envDir));
84106
}
85107

86108
/**

src/main/java/org/apposed/appose/builder/MambaBuilder.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ public String name() {
5757
return "mamba";
5858
}
5959

60+
/**
61+
* Adds conda channels to search for packages.
62+
*
63+
* @param channels Channel names (e.g., "conda-forge", "bioconda")
64+
* @return This builder instance, for fluent-style programming.
65+
*/
66+
@Override
67+
public MambaBuilder channels(String... channels) {
68+
return super.channels(channels);
69+
}
70+
6071
@Override
6172
public Environment build() throws BuildException {
6273
File envDir = envDir();
@@ -150,17 +161,6 @@ public Environment wrap(File envDir) throws BuildException {
150161
return build();
151162
}
152163

153-
/**
154-
* Adds conda channels to search for packages.
155-
*
156-
* @param channels Channel names (e.g., "conda-forge", "bioconda")
157-
* @return This builder instance, for fluent-style programming.
158-
*/
159-
@Override
160-
public MambaBuilder channels(String... channels) {
161-
return super.channels(channels);
162-
}
163-
164164
private Environment createEnvironment(File envDir) {
165165
Mamba mamba = new Mamba();
166166
String base = envDir.getAbsolutePath();

src/main/java/org/apposed/appose/scheme/Schemes.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private Schemes() {
5353
* since both are TOML files but {@code pyproject.toml} has more specific markers.
5454
* </p>
5555
*/
56-
private static final List<Scheme> ALL = Plugins.discover(Scheme.class,
56+
private static final List<Scheme> SCHEMES = Plugins.discover(Scheme.class,
5757
(a, b) -> Double.compare(b.priority(), a.priority()));
5858

5959
/**
@@ -64,7 +64,7 @@ private Schemes() {
6464
* @throws IllegalArgumentException If no scheme can handle the content
6565
*/
6666
public static Scheme fromContent(String content) {
67-
Scheme result = Plugins.find(ALL, scheme -> scheme.supportsContent(content));
67+
Scheme result = Plugins.find(SCHEMES, scheme -> scheme.supportsContent(content));
6868
if (result != null) return result;
6969
throw new IllegalArgumentException(
7070
"Cannot infer scheme from content. " +
@@ -79,7 +79,7 @@ public static Scheme fromContent(String content) {
7979
* @throws IllegalArgumentException If no scheme matches the name
8080
*/
8181
public static Scheme fromName(String name) {
82-
Scheme result = Plugins.find(ALL, scheme -> scheme.name().equals(name));
82+
Scheme result = Plugins.find(SCHEMES, scheme -> scheme.name().equals(name));
8383
if (result != null) return result;
8484
throw new IllegalArgumentException("Unknown scheme: " + name);
8585
}

src/main/java/org/apposed/appose/syntax/Syntaxes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private Syntaxes() {
4848
}
4949

5050
/** All known script syntax implementations. */
51-
private static final List<ScriptSyntax> ALL =
51+
private static final List<ScriptSyntax> SYNTAXES =
5252
Plugins.discover(ScriptSyntax.class, null);
5353

5454
/**
@@ -58,7 +58,7 @@ private Syntaxes() {
5858
* @return The matching script syntax object, or null if no syntax with the given name
5959
*/
6060
public static @Nullable ScriptSyntax get(String name) {
61-
return Plugins.find(ALL, syntax -> name.equals(syntax.name()));
61+
return Plugins.find(SYNTAXES, syntax -> name.equals(syntax.name()));
6262
}
6363

6464
/**

src/main/java/org/apposed/appose/util/Downloads.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ private Downloads() {
6969
// Prevent instantiation of utility class.
7070
}
7171

72+
public static File download(String name, String urlPath)
73+
throws IOException, InterruptedException, URISyntaxException
74+
{
75+
return download(name, urlPath, null);
76+
}
77+
7278
public static File download(String name, String urlPath, @Nullable BiConsumer<Long, Long> progressConsumer)
7379
throws IOException, InterruptedException, URISyntaxException
7480
{

src/test/java/org/apposed/appose/ServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void testScopeGroovy() throws Exception {
8989
@Test
9090
public void testServiceStartupFailure() throws Exception {
9191
// Create an environment with no binPaths to test startup failure.
92-
File tempDir = new File("no-pythons-to-be-found-here");
92+
File tempDir = new File("target", "no-pythons-to-be-found-here");
9393
tempDir.mkdirs();
9494
tempDir.deleteOnExit();
9595

0 commit comments

Comments
 (0)