1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.migration.utils;
17
18 import java.io.File;
19 import java.io.InputStream;
20 import java.nio.file.Files;
21 import java.nio.file.Path;
22 import java.util.Properties;
23
24 public enum Util {
25 ;
26
27 private static final String MIGRATIONS_HOME = "MIGRATIONS_HOME";
28
29
30 private static final String MIGRATIONS_HOME_PROPERTY_DEPRECATED = "migrationHome";
31
32 private static final String MIGRATIONS_HOME_PROPERTY = "migrationsHome";
33
34 private static final String MIGRATIONS_PROPERTIES = "migration.properties";
35
36 public static String migrationsHome() {
37 String migrationsHome = System.getenv(MIGRATIONS_HOME);
38
39 if (migrationsHome == null) {
40 migrationsHome = System.getProperty(MIGRATIONS_HOME_PROPERTY);
41 if (migrationsHome == null) {
42 migrationsHome = System.getProperty(MIGRATIONS_HOME_PROPERTY_DEPRECATED);
43 }
44 }
45 return migrationsHome;
46 }
47
48 public static boolean getPropertyOptionAsBoolean(String key) {
49 return Boolean.parseBoolean(getPropertyOption(key));
50 }
51
52
53
54
55
56
57
58 public static String getPropertyOption(String key) {
59 String migrationsHome = migrationsHome();
60 if (migrationsHome == null || migrationsHome.isEmpty()) {
61 return null;
62 }
63 Properties properties = new Properties();
64 String path = migrationsHome + File.separator + MIGRATIONS_PROPERTIES;
65 try (InputStream stream = Files.newInputStream(Path.of(path))) {
66 properties.load(stream);
67 return properties.getProperty(key);
68 } catch (Exception e) {
69 return null;
70 }
71 }
72
73 public static boolean isOption(String arg) {
74 return arg.startsWith("--") && !arg.trim().endsWith("=");
75 }
76
77 public static File file(File path, String fileName) {
78 return Path.of(path.getAbsolutePath() + File.separator + fileName).toFile();
79 }
80
81 public static String horizontalLine(String caption, int length) {
82 StringBuilder builder = new StringBuilder();
83 builder.append("==========");
84 if (caption.length() > 0) {
85 caption = " " + caption + " ";
86 builder.append(caption);
87 }
88 for (int i = 0; i < length - caption.length() - 10; i++) {
89 builder.append("=");
90 }
91 return builder.toString();
92 }
93 }