|
3 | 3 | import java.nio.file.Files; |
4 | 4 | import java.nio.file.Path; |
5 | 5 |
|
6 | | -public class Version { |
| 6 | +public interface Version { |
7 | 7 |
|
8 | | - static final String VERSION_FILE = "/version.txt"; |
9 | | - static final Path[] FALLBACK_PATHS = { |
10 | | - Path.of("src/main/resources/version.txt"), |
11 | | - Path.of("zsmith/src/main/resources/version.txt") |
12 | | - }; |
| 8 | + String versionFile = "version.txt"; |
| 9 | + Path versionPath = Path.of(versionFile); |
13 | 10 |
|
14 | 11 | public static String current() { |
15 | 12 | var fromManifest = Version.class.getPackage().getImplementationVersion(); |
16 | 13 | if (fromManifest != null) { |
17 | 14 | return fromManifest.strip(); |
18 | 15 | } |
19 | | - try (var in = Version.class.getResourceAsStream(VERSION_FILE)) { |
| 16 | + try (var in = Version.class.getResourceAsStream(versionFile)) { |
20 | 17 | if (in != null) { |
21 | 18 | return new String(in.readAllBytes()).strip(); |
22 | 19 | } |
23 | 20 | } catch (Exception e) { |
24 | | - throw new IllegalStateException("Cannot read " + VERSION_FILE + " from classpath", e); |
| 21 | + throw new IllegalStateException("Cannot read " + versionPath + " from classpath", e); |
25 | 22 | } |
26 | 23 | return fromFilesystem(); |
27 | 24 | } |
28 | 25 |
|
29 | 26 | static String fromFilesystem() { |
30 | | - for (var path : FALLBACK_PATHS) { |
31 | | - if (Files.exists(path)) { |
32 | | - try { |
33 | | - return Files.readString(path).strip(); |
34 | | - } catch (Exception e) { |
35 | | - throw new IllegalStateException("Cannot read " + path, e); |
36 | | - } |
37 | | - } |
| 27 | + try { |
| 28 | + return Files.readString(versionPath).strip(); |
| 29 | + } catch (Exception e) { |
| 30 | + throw new IllegalStateException("Cannot read " + versionPath, e); |
38 | 31 | } |
39 | | - throw new IllegalStateException("version.txt not found on classpath or in " + java.util.Arrays.toString(FALLBACK_PATHS)); |
40 | 32 | } |
41 | 33 | } |
0 commit comments