aboutsummaryrefslogtreecommitdiff
path: root/src/installer/lombok/installer/IdeFinder.java
blob: f68a0e4c862fb5354d14e22e7d493fe9f4d3263b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2009-2011 The Project Lombok Authors.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package lombok.installer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import lombok.core.Version;

/**
 * Implement and provide this class to add auto-finding a certain brand of IDEs to the lombok installer.
 */
public abstract class IdeFinder {
	private static final AtomicBoolean windowsDriveInfoLibLoaded = new AtomicBoolean(false);
	
	private static void loadWindowsDriveInfoLib() throws IOException {
		if (!windowsDriveInfoLibLoaded.compareAndSet(false, true)) return;
		
		final String prefix = "lombok-" + Version.getVersion() + "-";
		
		File temp = File.createTempFile("lombok", ".mark");
		File dll1 = new File(temp.getParentFile(), prefix + "WindowsDriveInfo-i386.dll");
		File dll2 = new File(temp.getParentFile(), prefix + "WindowsDriveInfo-x86_64.dll");
		temp.delete();
		dll1.deleteOnExit();
		dll2.deleteOnExit();
		try {
			if (unpackDLL("WindowsDriveInfo-i386.dll", dll1)) {
				System.load(dll1.getAbsolutePath());
				return;
			}
		} catch (Throwable ignore) {}
		
		try {
			if (unpackDLL("WindowsDriveInfo-x86_64.dll", dll2)) {
				System.load(dll2.getAbsolutePath());
			}
		} catch (Throwable ignore) {}
	}
	
	private static boolean unpackDLL(String dllName, File target) throws IOException {
		InputStream in = IdeFinder.class.getResourceAsStream(dllName);
		try {
			try {
				FileOutputStream out = new FileOutputStream(target);
				try {
					byte[] b = new byte[32000];
					while (true) {
						int r = in.read(b);
						if (r == -1) break;
						out.write(b, 0, r);
					}
				} finally {
					out.close();
				}
			} catch (IOException e) {
				//Fall through - if there is a file named lombok-WindowsDriveInfo-arch.dll, we'll try it.
				return target.exists() && target.canRead();
			}
		} finally {
			in.close();
		}
		
		return true;
	}
	
	/**
	 * Returns all drive letters on windows that represent fixed disks.
	 * 
	 * Floppy drives, optical drives, USB sticks, and network drives should all be excluded.
	 * 
	 * @return A List of drive letters, such as ["C", "D", "X"].
	 */
	public static List<String> getDrivesOnWindows() throws Throwable {
		loadWindowsDriveInfoLib();
		
		List<String> drives = new ArrayList<String>();
		
		WindowsDriveInfo info = new WindowsDriveInfo();
		for (String drive : info.getLogicalDrives()) {
			if (info.isFixedDisk(drive)) drives.add(drive);
		}
		
		return drives;
	}
	
	public static enum OS {
		MAC_OS_X("\n"), WINDOWS("\r\n"), UNIX("\n");
		
		private final String lineEnding;
		
		OS(String lineEnding) {
			this.lineEnding = lineEnding;
		}
		
		public String getLineEnding() {
			return lineEnding;
		}
	}
	
	public static OS getOS() {
		String prop = System.getProperty("os.name", "").toLowerCase();
		if (prop.matches("^.*\\bmac\\b.*$")) return OS.MAC_OS_X;
		if (prop.matches("^.*\\bdarwin\\b.*$")) return OS.MAC_OS_X;
		if (prop.matches("^.*\\bwin(dows|32|64)?\\b.*$")) return OS.WINDOWS;
		
		return OS.UNIX;
	}
	
	/**
	 * Look for installations of your IDE in the usual places.
	 * 
	 * @param locations Add to this list any valid locations that you found.
	 * @param problems
	 *     Add to this list any locations that look like installations,
	 *     but have problems that prevent you from installing/uninstalling from them. DONT add to this list
	 *     any common locations that have no installation at all - only add near misses.
	 */
	public abstract void findIdes(List<IdeLocation> locations, List<CorruptedIdeLocationException> problems);
}