Created
January 13, 2021 14:34
-
-
Save Guevara-chan/300b46fe477e270ca63783c3525077fe to your computer and use it in GitHub Desktop.
.NET re-implementation of 'OS' stdlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import clr | |
let | |
core = load "mscorlib" | |
diag = load "System.Diagnostics.Process" | |
ipath = @(core.GetType "System.IO.Path") | |
ifile = @(core.GetType "System.IO.File") | |
idir = @(core.GetType "System.IO.Directory") | |
ithrd = @(core.GetType "System.Threading.Thread") | |
ienv = @(core.GetType "System.Environment") | |
iproc = @(diag.GetType "System.Diagnostics.Process") | |
ispec = @(core.GetType "System.Environment+SpecialFolder") | |
DirSep: char = ipath.DirectorySeparatorChar | |
AltSep: char = ipath.AltDirectorySeparatorChar | |
PathSep: char = ipath.PathSeparator | |
CurDir: char = '.' | |
ParDir: string = ".." | |
proc sleep(milsecs: int) = ithrd.Sleep(milsecs) | |
proc getAppFilename(): string = iproc.GetCurrentProcess().MainModule.FileName | |
proc getAppDir(): string = | |
ipath.GetDirectoryName iproc.GetCurrentProcess().MainModule.FileName | |
proc paramCount(): int = ienv.GetCommandLineArgs().clrVariantToCOMArray1D.len - 1 | |
proc commandLineParams(): seq[TaintedString] = | |
for arg in ienv.GetCommandLineArgs().clrVariantToCOMArray1D[1..^1]: result.add arg | |
proc putEnv(key, val: string) = ienv.SetEnvironmentVariable(key, val) | |
proc delEnv(key: string) = ienv.SetEnvironmentVariable(key, Null) | |
proc existsEnv(key: string): bool = not ienv.GetEnvironmentVariable(key).isNull | |
proc getEnv(key: string; default = ""): TaintedString = | |
let env = ienv.GetEnvironmentVariable(key) | |
return if env.isNull: default else: env | |
iterator envPairs(): tuple[key, value: TaintedString] = | |
for de in ienv.GetEnvironmentVariables(): yield (key: de[].Key.TaintedString, value: de[].Value.TaintedString) | |
proc moveDir(source, dest: string) = idir.Move(source, dest) | |
proc dirExists(dir: string): bool = idir.Exists(dir) | |
proc getCurrentDir(): string = idir.GetCurrentDirectory() | |
proc setCurrentDir(newDir: string) = idir.SetCurrentDirectory(newDir) | |
proc createDir(dir: string) = idir.CreateDirectory(dir) | |
proc removeDir(dir: string; checkDir = false) = (if idir.Exists(dir) or checkDir: idir.Delete(dir, true)) | |
proc moveFile(source, dest: string) = ifile.Move(source, dest, true) | |
proc copyFile(source, dest: string) = ifile.Copy(source, dest, true) | |
proc fileExists(filename: string): bool = ifile.Exists(filename) | |
proc removeFile(file: string) = ifile.Delete(file) | |
proc tryRemoveFile(file: string) = (if ifile.exists(file): ifile.Delete(file)) | |
proc getFileSize(file: string): BiggestInt = core.new("System.IO.FileInfo", file).Length | |
proc isHidden(path: string): bool = (ifile.GetAttributes(path).int and 2).bool | |
proc changeFileExt(filename, ext: string): string = ipath.ChangeExtension(filename, ext) | |
proc extractFilename(path: string): string = ipath.GetFileName(path) | |
proc getTempDir(): string = ipath.GetTempPath() | |
proc getHomeDir(): string = ienv.GetFolderPath(ispec.UserProfile) | |
proc expandFilename(path: string): string = ipath.GetFullPath(path) | |
proc absolutePath(path: string; root = getCurrentDir()) = ipath.GetFullPath(path, root) | |
proc joinPath(head, tail: string): string = ipath.Combine(head, tail) | |
proc joinPath(parts: varargs[string]): string = ipath.Combine(parts) | |
proc `/`(head, tail: string): string = ipath.Combine(head, tail) | |
proc splitFile(path: string): tuple[dir, name, ext: string] = | |
(dir: $ipath.GetDirectoryName(path), name: $ipath.GetFileNameWithoutExtension(path), ext: $ipath.GetExtension(path)) | |
iterator walkDirs(pattern: string): string = | |
for de in idir.EnumerateDirectories($CurDir, pattern): yield de.`$`[2..^1] | |
iterator walkFiles(pattern: string): string = | |
for de in idir.EnumerateFiles($CurDir, pattern): yield de.`$`[2..^1] | |
iterator walkPattern(pattern: string): string = | |
for de in idir.EnumerateFileSystemEntries($CurDir, pattern): yield de.`$`[2..^1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment