-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJDKmanager.R
131 lines (102 loc) · 3.86 KB
/
JDKmanager.R
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
# Quickly & forcefully manage extra JDKs in base R
## (e.g. simplify rJava reconf)
# by Jess Sullivan
# default JVM location on osx:
osxJDKpath <- "/Library/Java/JavaVirtualMachines/"
# move extra JDKs here:
removedJDKpath <- '~/disabledJDKs/'
### check for removedJDKpath: ###
if (!dir.exists(removedJDKpath)) {
dir.create(removedJDKpath)
}
installedJDKs <- list.files(osxJDKpath)
removedJDKs <- list.files(removedJDKpath)
allJDKs <- list(installedJDKs, removedJDKs)
halt <- function(...) {
# verbatim, sweet function seen here:
# https://www.mail-archive.com/r-help@r-project.org/msg117700.html
# and again here:
# https://stackoverflow.com/a/33916009
blankMsg <- sprintf("\r%s\r", paste0(rep(" ", getOption("width")-1L), collapse=" "));
stop(simpleError(blankMsg));
}
cmd <- 'echo ... '
helpString <- function() {
writeLines(paste0('\n JDKmanager help: \n',
'(args are not case sensitive) \n',
'(usage: `sudo rscript JDKmanager.R help`) \n\n',
'list :: prints contents of default JDK path and removed JDK path \n',
'reset :: move all JDKs in removed JDK path back to default JDK path \n',
'config :: configure rJava. equivalent to `R CMD javareconf` in shell \n\n',
'specific JDK, such as 11.0.1, 1.8,openjdk-12.0.2, etc: \n',
' searches through both default and removed pathes for the specific JDK. \n',
' if found in the default path, any other JDKs will be moved to the `removed JDKs` directory. \n',
' the specified JDK will be configured for rJava.'))
}
main <- function() {
if (grepl('help', commandArgs()[5], ignore.case = TRUE)) {
helpString()
halt()
}
if (length(commandArgs()) != 6) {
print("\n please use sudo and supply one argument! supply 'help' for options")
halt()
}
arg <- as.character(commandArgs()[6])
if (grepl('list', arg, ignore.case = TRUE)) {
writeLines(paste0('\n listing contents of default JDK dir [ ', osxJDKpath, ' ] ... \n'))
print(installedJDKs)
writeLines(paste0('\n listing contents of removed JDK dir [ ', removedJDKpath, ' ] ... \n'))
print(removedJDKs)
halt()
}
if (grepl('help', arg, ignore.case = TRUE)) {
helpString()
halt()
}
if (grepl('config', arg, ignore.case = TRUE)) {
if (length(installedJDKs) > 0) {
system('R CMD javareconf -n')
writeLines('\n completed reconf')
halt()
} else {
writeLines('\n no JDK found, default path is empty!')
}
halt()
}
if (grepl('reset', arg, ignore.case = TRUE)) {
writeLines('\n got reset arg, attempting to reset JDKs to default path ...')
for (type in removedJDKs) {
cmd = paste0(cmd,
paste0(' && mv ', removedJDKpath, type,
paste0(' ', osxJDKpath, type)))
}
# commit changes:
system(cmd) # must be sudo user
writeLines('\n reset JDKs to default path successfully!')
halt()
}
if (!grepl('reset', arg, ignore.case = TRUE)) {
writeLines(paste0('\n attempting to isolate JDK ', arg))
writeLines('\n checking removed JDK path ... ')
for (type in removedJDKs) {
if (grepl(arg, type)) {
print(paste0('\n not complete- JDK ',
arg, ' found in ', removedJDKpath,
' please run run RESET arg before continuing'))
halt()
}
}
print('\n checking default JDK path')
for (type in installedJDKs) {
if (!grepl(arg, type)) {
system(paste0('mv ', osxJDKpath, type,
paste0(' ', removedJDKpath, type)))
halt()
}
}
}
}
### RUN: ###
main()
### END ###