Mercurial > cgi-bin > hgweb.cgi > ImagePrep
annotate src/name/blackcap/imageprep/Files.kt @ 5:884f1415a330
Rationalized directory management.
author | David Barts <n5jrn@me.com> |
---|---|
date | Fri, 17 Jul 2020 17:11:43 -0700 |
parents | 5234e4500d45 |
children | 098c4f5507c7 |
rev | line source |
---|---|
0 | 1 /* |
2 * For dealing with files. | |
3 */ | |
4 package name.blackcap.imageprep | |
5 | |
6 import java.io.BufferedReader | |
7 import java.io.File | |
8 import java.io.FileInputStream | |
1 | 9 import java.io.FileOutputStream |
0 | 10 import java.io.InputStreamReader |
11 import java.util.Properties | |
12 import java.util.logging.FileHandler | |
13 import java.util.logging.Level | |
14 import java.util.logging.Logger | |
15 import java.util.logging.SimpleFormatter | |
16 | |
17 /* OS Type */ | |
18 | |
19 enum class OS { | |
20 MAC, UNIX, WINDOWS, OTHER; | |
21 companion object { | |
22 private val rawType = System.getProperty("os.name")?.toLowerCase() | |
23 val type = if (rawType == null) { | |
24 OTHER | |
25 } else if (rawType.contains("win")) { | |
26 WINDOWS | |
27 } else if (rawType.contains("mac")) { | |
28 MAC | |
29 } else if (rawType.contains("nix") || rawType.contains("nux") || rawType.contains("aix") || rawType.contains("sunos")) { | |
30 UNIX | |
31 } else { | |
32 OTHER | |
33 } | |
34 } | |
35 } | |
36 | |
37 /* joins path name components to java.io.File */ | |
38 | |
39 fun joinPath(base: String, vararg rest: String) = rest.fold(File(base), ::File) | |
40 | |
41 /* file names */ | |
42 | |
4
5234e4500d45
Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents:
1
diff
changeset
|
43 private val SHORTNAME = Application.MYNAME.toLowerCase() |
0 | 44 private val LONGNAME = "name.blackcap." + SHORTNAME |
45 private val HOME = System.getenv("HOME") | |
46 val PF_DIR = when (OS.type) { | |
47 OS.MAC -> joinPath(HOME, "Library", "Application Support", LONGNAME) | |
48 OS.WINDOWS -> joinPath(System.getenv("APPDATA"), LONGNAME) | |
49 else -> joinPath(HOME, "." + SHORTNAME) | |
50 } | |
51 val LF_DIR = when (OS.type) { | |
52 OS.MAC -> joinPath(HOME, "Library", "Application Support", LONGNAME) | |
53 OS.WINDOWS -> joinPath(System.getenv("LOCALAPPDATA"), LONGNAME) | |
54 else -> joinPath(HOME, "." + SHORTNAME) | |
55 } | |
56 val PROP_FILE = File(PF_DIR, SHORTNAME + ".properties") | |
57 val LOG_FILE = File(LF_DIR, SHORTNAME + ".log") | |
58 | |
59 /* make some needed directories */ | |
60 | |
61 private fun File.makeIfNeeded() = if (exists()) { true } else { mkdirs() } | |
62 | |
63 /* make some usable objects */ | |
64 | |
65 val CHARSET = "UTF-8" | |
66 | |
67 private val DPNAME = "default.properties" | |
68 val DPROPERTIES = Properties().apply { | |
69 val rawStream = OS::class.java.getResourceAsStream(DPNAME) | |
70 BufferedReader(InputStreamReader(rawStream, CHARSET)).use { | |
71 load(it) | |
72 } | |
73 } | |
74 | |
75 val PROPERTIES = Properties(DPROPERTIES).apply { | |
76 PF_DIR.makeIfNeeded() | |
77 if (PROP_FILE.exists()) { | |
78 BufferedReader(InputStreamReader(FileInputStream(PROP_FILE), CHARSET)).use { | |
79 load(it) | |
80 } | |
81 } else { | |
82 FileOutputStream(PROP_FILE).use { output -> | |
83 OS::class.java.getResourceAsStream(DPNAME).use { input -> | |
84 input.copyTo(output) | |
85 } | |
86 } | |
87 } | |
88 } | |
89 | |
90 val LOGGER = run { | |
91 System.setProperty("java.util.logging.SimpleFormatter.format", | |
92 "%1\$tFT%1\$tT%1\$tz %2\$s%n%4\$s: %5\$s%6\$s%n") | |
93 LF_DIR.makeIfNeeded() | |
94 Logger.getLogger(LONGNAME).apply { | |
95 addHandler(FileHandler(LOG_FILE.toString()).apply { | |
96 formatter = SimpleFormatter() }) | |
97 level = Level.CONFIG | |
98 useParentHandlers = false | |
99 } | |
100 } |