Skip to content

Commit 636ba76

Browse files
committed
refactor the .onLoad and .onAttach functions
1 parent 58c7a47 commit 636ba76

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed
Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
.GRASS_CACHE <- new.env(FALSE, parent = globalenv())
22

33
.onLoad <- function(lib, pkg) {
4-
assign(".GRASS_old.GRASS_PAGER", Sys.getenv("GRASS_PAGER"), envir = .GRASS_CACHE)
5-
Sys.setenv("GRASS_PAGER" = "cat")
6-
assign(".GRASS_old.GRASS_MESSAGE_FORMAT", Sys.getenv("GRASS_MESSAGE_FORMAT"),
4+
# backup original environment variables
5+
assign(
6+
".GRASS_old.GRASS_PAGER",
7+
Sys.getenv("GRASS_PAGER"),
78
envir = .GRASS_CACHE
89
)
9-
assign("INIT_USED", FALSE, envir = .GRASS_CACHE)
10-
assign("remove_GISRC", FALSE, envir = .GRASS_CACHE)
1110

12-
Sys.setenv("GRASS_MESSAGE_FORMAT" = "text")
11+
assign(
12+
".GRASS_old.GRASS_MESSAGE_FORMAT",
13+
Sys.getenv("GRASS_MESSAGE_FORMAT"),
14+
envir = .GRASS_CACHE
15+
)
1316

14-
gisrc <- Sys.getenv("GISRC")
15-
loc <- Sys.getenv("LOCATION_NAME")
17+
Sys.setenv("GRASS_PAGER" = "cat")
18+
Sys.setenv("GRASS_MESSAGE_FORMAT" = "text")
1619

20+
# set up the GRASS_CACHE environment
21+
assign("INIT_USED", FALSE, envir = .GRASS_CACHE)
22+
assign("remove_GISRC", FALSE, envir = .GRASS_CACHE)
1723
assign("cmdCACHE", list(), envir = .GRASS_CACHE)
1824
assign("override_encoding", "", envir = .GRASS_CACHE)
19-
SYS <- ""
25+
26+
# add platform type to .GRASS_CACHE
2027
if (.Platform$OS.type == "windows") {
21-
SYS <- "WinNat"
28+
platform <- "WinNat"
2229
} else if (.Platform$OS.type == "unix") {
23-
SYS <- "unix"
30+
platform <- "unix"
2431
} else {
25-
SYS <- "unknown"
32+
platform <- "unknown"
2633
}
27-
assign("SYS", SYS, envir = .GRASS_CACHE)
28-
res <- ""
29-
if (SYS == "WinNat") res <- ".exe"
34+
assign("SYS", platform, envir = .GRASS_CACHE)
35+
36+
# store the platform-specific executable extension
37+
res <- ifelse(platform == "WinNat", "exe", "")
3038
assign("addEXE", res, envir = .GRASS_CACHE)
3139
assign("WN_bat", "", envir = .GRASS_CACHE)
3240

41+
# set up other GRASS_CACHE environment variables
3342
assign("ignore.stderr", FALSE, envir = .GRASS_CACHE)
3443
assign("stop_on_no_flags_paras", TRUE, envir = .GRASS_CACHE)
3544
assign("echoCmd", FALSE, envir = .GRASS_CACHE)
@@ -42,31 +51,36 @@
4251
}
4352

4453
.onAttach <- function(lib, pkg) {
54+
# display the GRASS version and location when the package is attached
4555
gisrc <- Sys.getenv("GISRC")
4656
loc <- Sys.getenv("LOCATION_NAME")
57+
4758
if (nchar(gisrc) == 0) {
4859
gv <- "(GRASS not running)"
4960
} else {
5061
gv <- .grassVersion()
5162
comp <- .compatibleGRASSVersion(gv)
63+
5264
if (!is.na(comp) && !comp) {
5365
stop(attr(comp, "message"))
5466
}
67+
5568
assign("GV", gv, envir = .GRASS_CACHE)
5669
if (nchar(loc) == 0) {
5770
loc <- read.dcf(gisrc)[1, "LOCATION_NAME"]
5871
}
5972
}
6073

61-
Smess <- paste("GRASS GIS interface loaded ",
74+
startup_message <- paste0(
75+
"GRASS GIS interface loaded ",
6276
"with GRASS version: ", gv, "\n",
63-
ifelse(nchar(loc) == 0, "", paste("and location: ", loc, "\n", sep = "")),
64-
sep = ""
77+
ifelse(nchar(loc) == 0, "", paste0("and location: ", loc, "\n"))
6578
)
66-
packageStartupMessage(Smess, appendLF = FALSE)
79+
packageStartupMessage(startup_message, appendLF = FALSE)
6780
}
6881

6982
.onUnload <- function(lib, pkg) {
83+
# unset the GISRC environment variable and remove gisrc file
7084
if (get("INIT_USED", envir = .GRASS_CACHE)) {
7185
if (get("remove_GISRC", envir = .GRASS_CACHE)) {
7286
gisrc <- Sys.getenv("GISRC")
@@ -76,9 +90,16 @@
7690
unlink_.gislock()
7791
unset.GIS_LOCK()
7892
}
79-
Sys.setenv("GRASS_PAGER" = get(".GRASS_old.GRASS_PAGER", envir = .GRASS_CACHE))
80-
Sys.setenv("GRASS_MESSAGE_FORMAT" = get(".GRASS_old.GRASS_MESSAGE_FORMAT",
81-
envir = .GRASS_CACHE
82-
))
93+
94+
# restore original environment variables
95+
Sys.setenv(
96+
"GRASS_PAGER" = get(".GRASS_old.GRASS_PAGER", envir = .GRASS_CACHE)
97+
)
98+
99+
Sys.setenv(
100+
"GRASS_MESSAGE_FORMAT" =
101+
get(".GRASS_old.GRASS_MESSAGE_FORMAT", envir = .GRASS_CACHE)
102+
)
103+
83104
rm(.GRASS_CACHE)
84105
}

0 commit comments

Comments
 (0)