Skip to content

Commit 2d19110

Browse files
committed
move procs in os to appdirs
1 parent de5dce4 commit 2d19110

File tree

2 files changed

+149
-141
lines changed

2 files changed

+149
-141
lines changed

lib/pure/os.nim

Lines changed: 3 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export osdirs
4040
import std/private/ossymlinks
4141
export ossymlinks
4242

43+
import std/private/osappdirs
44+
export osappdirs
45+
4346
import std/private/oscommon
4447

4548
include system/inclrtl
@@ -100,148 +103,7 @@ export envvars
100103
import std/private/osseps
101104
export osseps
102105

103-
proc getHomeDir*(): string {.rtl, extern: "nos$1",
104-
tags: [ReadEnvEffect, ReadIOEffect].} =
105-
## Returns the home directory of the current user.
106-
##
107-
## This proc is wrapped by the `expandTilde proc`_
108-
## for the convenience of processing paths coming from user configuration files.
109-
##
110-
## See also:
111-
## * `getConfigDir proc`_
112-
## * `getTempDir proc`_
113-
## * `expandTilde proc`_
114-
## * `getCurrentDir proc`_
115-
## * `setCurrentDir proc`_
116-
runnableExamples:
117-
assert getHomeDir() == expandTilde("~")
118-
119-
when defined(windows): return getEnv("USERPROFILE") & "\\"
120-
else: return getEnv("HOME") & "/"
121-
122-
proc getConfigDir*(): string {.rtl, extern: "nos$1",
123-
tags: [ReadEnvEffect, ReadIOEffect].} =
124-
## Returns the config directory of the current user for applications.
125-
##
126-
## On non-Windows OSs, this proc conforms to the XDG Base Directory
127-
## spec. Thus, this proc returns the value of the `XDG_CONFIG_HOME` environment
128-
## variable if it is set, otherwise it returns the default configuration
129-
## directory ("~/.config/").
130-
##
131-
## An OS-dependent trailing slash is always present at the end of the
132-
## returned string: `\\` on Windows and `/` on all other OSs.
133-
##
134-
## See also:
135-
## * `getHomeDir proc`_
136-
## * `getTempDir proc`_
137-
## * `expandTilde proc`_
138-
## * `getCurrentDir proc`_
139-
## * `setCurrentDir proc`_
140-
when defined(windows):
141-
result = getEnv("APPDATA")
142-
else:
143-
result = getEnv("XDG_CONFIG_HOME", getEnv("HOME") / ".config")
144-
result.normalizePathEnd(trailingSep = true)
145-
146-
proc getCacheDir*(): string =
147-
## Returns the cache directory of the current user for applications.
148-
##
149-
## This makes use of the following environment variables:
150-
##
151-
## * On Windows: `getEnv("LOCALAPPDATA")`
152-
##
153-
## * On macOS: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")`
154-
##
155-
## * On other platforms: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")`
156-
##
157-
## **See also:**
158-
## * `getHomeDir proc`_
159-
## * `getTempDir proc`_
160-
## * `getConfigDir proc`_
161-
# follows https://crates.io/crates/platform-dirs
162-
when defined(windows):
163-
result = getEnv("LOCALAPPDATA")
164-
elif defined(osx):
165-
result = getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")
166-
else:
167-
result = getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")
168-
result.normalizePathEnd(false)
169-
170-
proc getCacheDir*(app: string): string =
171-
## Returns the cache directory for an application `app`.
172-
##
173-
## * On Windows, this uses: `getCacheDir() / app / "cache"`
174-
## * On other platforms, this uses: `getCacheDir() / app`
175-
when defined(windows):
176-
getCacheDir() / app / "cache"
177-
else:
178-
getCacheDir() / app
179-
180-
181-
when defined(windows):
182-
type DWORD = uint32
183-
184-
when defined(nimPreviewSlimSystem):
185-
import std/widestrs
186-
187-
proc getTempPath(
188-
nBufferLength: DWORD, lpBuffer: WideCString
189-
): DWORD {.stdcall, dynlib: "kernel32.dll", importc: "GetTempPathW".} =
190-
## Retrieves the path of the directory designated for temporary files.
191-
192-
template getEnvImpl(result: var string, tempDirList: openArray[string]) =
193-
for dir in tempDirList:
194-
if existsEnv(dir):
195-
result = getEnv(dir)
196-
break
197-
198-
template getTempDirImpl(result: var string) =
199-
when defined(windows):
200-
getEnvImpl(result, ["TMP", "TEMP", "USERPROFILE"])
201-
else:
202-
getEnvImpl(result, ["TMPDIR", "TEMP", "TMP", "TEMPDIR"])
203106

204-
proc getTempDir*(): string {.rtl, extern: "nos$1",
205-
tags: [ReadEnvEffect, ReadIOEffect].} =
206-
## Returns the temporary directory of the current user for applications to
207-
## save temporary files in.
208-
##
209-
## On Windows, it calls [GetTempPath](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppathw).
210-
## On Posix based platforms, it will check `TMPDIR`, `TEMP`, `TMP` and `TEMPDIR` environment variables in order.
211-
## On all platforms, `/tmp` will be returned if the procs fails.
212-
##
213-
## You can override this implementation
214-
## by adding `-d:tempDir=mytempname` to your compiler invocation.
215-
##
216-
## **Note:** This proc does not check whether the returned path exists.
217-
##
218-
## See also:
219-
## * `getHomeDir proc`_
220-
## * `getConfigDir proc`_
221-
## * `expandTilde proc`_
222-
## * `getCurrentDir proc`_
223-
## * `setCurrentDir proc`_
224-
const tempDirDefault = "/tmp"
225-
when defined(tempDir):
226-
const tempDir {.strdefine.}: string = tempDirDefault
227-
result = tempDir
228-
else:
229-
when nimvm:
230-
getTempDirImpl(result)
231-
else:
232-
when defined(windows):
233-
let size = getTempPath(0, nil)
234-
# If the function fails, the return value is zero.
235-
if size > 0:
236-
let buffer = newWideCString(size.int)
237-
if getTempPath(size, buffer) > 0:
238-
result = $buffer
239-
elif defined(android): result = "/data/local/tmp"
240-
else:
241-
getTempDirImpl(result)
242-
if result.len == 0:
243-
result = tempDirDefault
244-
normalizePathEnd(result, trailingSep=true)
245107

246108
proc expandTilde*(path: string): string {.
247109
tags: [ReadEnvEffect, ReadIOEffect].} =

lib/std/private/osappdirs.nim

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
include system/inclrtl
2+
import std/envvars
3+
import std/private/ospaths2
4+
5+
proc getHomeDir*(): string {.rtl, extern: "nos$1",
6+
tags: [ReadEnvEffect, ReadIOEffect].} =
7+
## Returns the home directory of the current user.
8+
##
9+
## This proc is wrapped by the `expandTilde proc`_
10+
## for the convenience of processing paths coming from user configuration files.
11+
##
12+
## See also:
13+
## * `getConfigDir proc`_
14+
## * `getTempDir proc`_
15+
## * `expandTilde proc`_
16+
## * `getCurrentDir proc`_
17+
## * `setCurrentDir proc`_
18+
runnableExamples:
19+
assert getHomeDir() == expandTilde("~")
20+
21+
when defined(windows): return getEnv("USERPROFILE") & "\\"
22+
else: return getEnv("HOME") & "/"
23+
24+
proc getConfigDir*(): string {.rtl, extern: "nos$1",
25+
tags: [ReadEnvEffect, ReadIOEffect].} =
26+
## Returns the config directory of the current user for applications.
27+
##
28+
## On non-Windows OSs, this proc conforms to the XDG Base Directory
29+
## spec. Thus, this proc returns the value of the `XDG_CONFIG_HOME` environment
30+
## variable if it is set, otherwise it returns the default configuration
31+
## directory ("~/.config/").
32+
##
33+
## An OS-dependent trailing slash is always present at the end of the
34+
## returned string: `\\` on Windows and `/` on all other OSs.
35+
##
36+
## See also:
37+
## * `getHomeDir proc`_
38+
## * `getTempDir proc`_
39+
## * `expandTilde proc`_
40+
## * `getCurrentDir proc`_
41+
## * `setCurrentDir proc`_
42+
when defined(windows):
43+
result = getEnv("APPDATA")
44+
else:
45+
result = getEnv("XDG_CONFIG_HOME", getEnv("HOME") / ".config")
46+
result.normalizePathEnd(trailingSep = true)
47+
48+
proc getCacheDir*(): string =
49+
## Returns the cache directory of the current user for applications.
50+
##
51+
## This makes use of the following environment variables:
52+
##
53+
## * On Windows: `getEnv("LOCALAPPDATA")`
54+
##
55+
## * On macOS: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")`
56+
##
57+
## * On other platforms: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")`
58+
##
59+
## **See also:**
60+
## * `getHomeDir proc`_
61+
## * `getTempDir proc`_
62+
## * `getConfigDir proc`_
63+
# follows https://crates.io/crates/platform-dirs
64+
when defined(windows):
65+
result = getEnv("LOCALAPPDATA")
66+
elif defined(osx):
67+
result = getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")
68+
else:
69+
result = getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")
70+
result.normalizePathEnd(false)
71+
72+
proc getCacheDir*(app: string): string =
73+
## Returns the cache directory for an application `app`.
74+
##
75+
## * On Windows, this uses: `getCacheDir() / app / "cache"`
76+
## * On other platforms, this uses: `getCacheDir() / app`
77+
when defined(windows):
78+
getCacheDir() / app / "cache"
79+
else:
80+
getCacheDir() / app
81+
82+
83+
when defined(windows):
84+
type DWORD = uint32
85+
86+
when defined(nimPreviewSlimSystem):
87+
import std/widestrs
88+
89+
proc getTempPath(
90+
nBufferLength: DWORD, lpBuffer: WideCString
91+
): DWORD {.stdcall, dynlib: "kernel32.dll", importc: "GetTempPathW".} =
92+
## Retrieves the path of the directory designated for temporary files.
93+
94+
template getEnvImpl(result: var string, tempDirList: openArray[string]) =
95+
for dir in tempDirList:
96+
if existsEnv(dir):
97+
result = getEnv(dir)
98+
break
99+
100+
template getTempDirImpl(result: var string) =
101+
when defined(windows):
102+
getEnvImpl(result, ["TMP", "TEMP", "USERPROFILE"])
103+
else:
104+
getEnvImpl(result, ["TMPDIR", "TEMP", "TMP", "TEMPDIR"])
105+
106+
proc getTempDir*(): string {.rtl, extern: "nos$1",
107+
tags: [ReadEnvEffect, ReadIOEffect].} =
108+
## Returns the temporary directory of the current user for applications to
109+
## save temporary files in.
110+
##
111+
## On Windows, it calls [GetTempPath](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppathw).
112+
## On Posix based platforms, it will check `TMPDIR`, `TEMP`, `TMP` and `TEMPDIR` environment variables in order.
113+
## On all platforms, `/tmp` will be returned if the procs fails.
114+
##
115+
## You can override this implementation
116+
## by adding `-d:tempDir=mytempname` to your compiler invocation.
117+
##
118+
## **Note:** This proc does not check whether the returned path exists.
119+
##
120+
## See also:
121+
## * `getHomeDir proc`_
122+
## * `getConfigDir proc`_
123+
## * `expandTilde proc`_
124+
## * `getCurrentDir proc`_
125+
## * `setCurrentDir proc`_
126+
const tempDirDefault = "/tmp"
127+
when defined(tempDir):
128+
const tempDir {.strdefine.}: string = tempDirDefault
129+
result = tempDir
130+
else:
131+
when nimvm:
132+
getTempDirImpl(result)
133+
else:
134+
when defined(windows):
135+
let size = getTempPath(0, nil)
136+
# If the function fails, the return value is zero.
137+
if size > 0:
138+
let buffer = newWideCString(size.int)
139+
if getTempPath(size, buffer) > 0:
140+
result = $buffer
141+
elif defined(android): result = "/data/local/tmp"
142+
else:
143+
getTempDirImpl(result)
144+
if result.len == 0:
145+
result = tempDirDefault
146+
normalizePathEnd(result, trailingSep=true)

0 commit comments

Comments
 (0)