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