|
1 | 1 | = OS-Lib
|
2 |
| -:version: 0.9.1 |
3 |
| -:toc-placement: preamble |
4 |
| -:toclevels: 3 |
5 |
| -:toc: |
| 2 | +:version: 0.10.0 |
6 | 3 | :link-geny: https://github.com/com-lihaoyi/geny
|
7 | 4 | :link-oslib: https://github.com/com-lihaoyi/os-lib
|
8 | 5 | :link-oslib-gitter: https://gitter.im/lihaoyi/os-lib
|
|
14 | 11 | image:{link-oslib}/actions/workflows/build.yml/badge.svg[Build Status,link={link-oslib}/actions]
|
15 | 12 | image:https://badges.gitter.im/Join%20Chat.svg[Gitter Chat,link={link-oslib-gitter}]
|
16 | 13 | image:https://img.shields.io/badge/patreon-sponsor-ff69b4.svg[Patreon,link=https://www.patreon.com/lihaoyi]
|
| 14 | +image:https://javadoc.io/badge2/com.lihaoyi/os-lib_3/scaladoc.svg[API Docs (Scala 3),link=https://javadoc.io/doc/com.lihaoyi/os-lib_3] |
17 | 15 |
|
18 | 16 | [source,scala]
|
19 | 17 | ----
|
@@ -86,6 +84,8 @@ ivy"com.lihaoyi::os-lib:{version}"
|
86 | 84 | "com.lihaoyi" %% "os-lib" % "{version}"
|
87 | 85 | ----
|
88 | 86 |
|
| 87 | +https://javadoc.io/doc/com.lihaoyi/os-lib_3[API Documentation (Scala 3)] |
| 88 | + |
89 | 89 | == Cookbook
|
90 | 90 |
|
91 | 91 | Most operation in OS-Lib take place on <<os-path>>s, which are
|
@@ -1128,7 +1128,7 @@ os.readLink(wd / "misc" / "broken-abs-symlink") ==> os.root / "doesnt" / "exist"
|
1128 | 1128 | ----
|
1129 | 1129 |
|
1130 | 1130 | Note that symbolic links can be either absolute ``os.Path``s or relative
|
1131 |
| -``os.RelPath``s, represented by `os.FilePath`. You can also use `os.readLink.all` |
| 1131 | +``os.RelPath``s, represented by `os.FilePath`. You can also use `os.readLink.absolute` |
1132 | 1132 | to automatically resolve relative symbolic links to their absolute destination:
|
1133 | 1133 |
|
1134 | 1134 | [source,scala]
|
@@ -1483,7 +1483,7 @@ the subprocess via `os.SubProcess#stdin`, and if used on its stdout it lets the
|
1483 | 1483 | parent process read from the subprocess via `os.SubProcess#stdout`
|
1484 | 1484 | and `os.SubProcess#stderr`.
|
1485 | 1485 | * `os.Inherit`: inherits the stream from the parent process. This lets the
|
1486 |
| -subprocess read directly from the paren process's standard input or write |
| 1486 | +subprocess read directly from the parent process's standard input or write |
1487 | 1487 | directly to the parent process's standard output or error
|
1488 | 1488 | * `os.Path`: connects the subprocess's stream to the given filesystem
|
1489 | 1489 | path, reading its standard input from a file or writing its standard
|
@@ -1691,6 +1691,48 @@ val sha = os.proc("shasum", "-a", "256").spawn(stdin = gzip.stdout)
|
1691 | 1691 | sha.stdout.trim ==> "acc142175fa520a1cb2be5b97cbbe9bea092e8bba3fe2e95afa645615908229e -"
|
1692 | 1692 | ----
|
1693 | 1693 |
|
| 1694 | +== Spawning Pipelines of Subprocesses |
| 1695 | + |
| 1696 | +After constructing a subprocess with `os.proc`, you can use the `pipeTo` method |
| 1697 | +to pipe its output to another subprocess: |
| 1698 | + |
| 1699 | +[source,scala] |
| 1700 | +---- |
| 1701 | +val wc = os.proc("ls", "-l") |
| 1702 | + .pipeTo(os.proc("wc", "-l")) |
| 1703 | + .call() |
| 1704 | + .out.text() |
| 1705 | +---- |
| 1706 | + |
| 1707 | +This is equivalent to the shell command `ls -l | wc -l`. You can chain together |
| 1708 | +as many subprocesses as you like. Note that by using this API you can utilize |
| 1709 | +the broken pipe behaviour of Unix systems. For example, you can take 10 first elements |
| 1710 | +of output from the `yes` command, and after the `head` command terminates, the `yes` |
| 1711 | +command will be terminated as well: |
| 1712 | + |
| 1713 | +[source,scala] |
| 1714 | +---- |
| 1715 | +val yes10 = os.proc("yes") |
| 1716 | + .pipeTo(os.proc("head", "-n", "10")) |
| 1717 | + .call() |
| 1718 | + .out.text() |
| 1719 | +---- |
| 1720 | + |
| 1721 | +This feature is implemented inside the library and will terminate any process reading the |
| 1722 | +stdin of other process in pipeline on every IO error. This behavior can be disabled via the |
| 1723 | +`handleBrokenPipe` flag on `call` and `spawn` methods. Note that Windows does not support |
| 1724 | +broken pipe behaviour, so a command like`yes` would run forever. `handleBrokenPipe` is set |
| 1725 | +to false by default on Windows. |
| 1726 | + |
| 1727 | +Both `call` and `spawn` correspond in their behavior to their counterparts in the `os.proc`, |
| 1728 | +but `spawn` returns the `os.ProcessPipeline` instance instead. It offers the same |
| 1729 | +`API` as `SubProcess`, but will operate on the set of processes instead of a single one. |
| 1730 | + |
| 1731 | +`Pipefail` is enabled by default, so if any of the processes in the pipeline fails, the whole |
| 1732 | +pipeline will have a non-zero exit code. This behavior can be disabled via the `pipefail` flag |
| 1733 | +on `call` and `spawn` methods. Note that the pipefail does not kill the processes in the pipeline, |
| 1734 | +it just sets the exit code of the pipeline to the exit code of the failed process. |
| 1735 | + |
1694 | 1736 | === Watching for Changes
|
1695 | 1737 |
|
1696 | 1738 | ==== `os.watch.watch`
|
@@ -1752,8 +1794,6 @@ paths changed: /Users/lihaoyi/Github/Ammonite/out/i am,/Users/lihaoyi/Github/Amm
|
1752 | 1794 | paths changed: /Users/lihaoyi/Github/Ammonite/out/version/log,/Users/lihaoyi/Github/Ammonite/out/version/meta.json,/Users/lihaoyi/Github/Ammonite/out/version
|
1753 | 1795 | ----
|
1754 | 1796 |
|
1755 |
| -`watch` currently only supports Linux and Mac-OSX, and not Windows. |
1756 |
| - |
1757 | 1797 | == Data Types
|
1758 | 1798 |
|
1759 | 1799 | === `os.Path`
|
@@ -2059,6 +2099,38 @@ Python, ...) do. Even in cases where it's uncertain, e.g. you're taking user
|
2059 | 2099 | input as a String, you have to either handle both possibilities with BasePath or
|
2060 | 2100 | explicitly choose to convert relative paths to absolute using some base.
|
2061 | 2101 |
|
| 2102 | +==== Roots and filesystems |
| 2103 | + |
| 2104 | +If you are using a system that supports different roots of paths, e.g. Windows, |
| 2105 | +you can use the argument of `os.root` to specify which root you want to use. |
| 2106 | +If not specified, the default root will be used (usually, C on Windows, / on Unix). |
| 2107 | + |
| 2108 | +[source,scala] |
| 2109 | +---- |
| 2110 | +val root = os.root('C:\') / "Users" / "me" |
| 2111 | +assert(root == os.Path("C:\Users\me")) |
| 2112 | +---- |
| 2113 | + |
| 2114 | +Additionally, custom filesystems can be specified by passing a `FileSystem` to |
| 2115 | +`os.root`. This allows you to use OS-Lib with non-standard filesystems, such as |
| 2116 | +jar filesystems or in-memory filesystems. |
| 2117 | + |
| 2118 | +[source,scala] |
| 2119 | +---- |
| 2120 | +val uri = new URI("jar", Paths.get("foo.jar").toURI().toString, null); |
| 2121 | +val env = new HashMap[String, String](); |
| 2122 | +env.put("create", "true"); |
| 2123 | +val fs = FileSystems.newFileSystem(uri, env); |
| 2124 | +val path = os.root("/", fs) / "dir" |
| 2125 | +---- |
| 2126 | + |
| 2127 | +Note that the jar file system operations suchs as writing to a file are supported |
| 2128 | +only on JVM 11+. Depending on the filesystem, some operations may not be supported - |
| 2129 | +for example, running an `os.proc` with pwd in a jar file won't work. You may also |
| 2130 | +meet limitations imposed by the implementations - in jar file system, the files are |
| 2131 | +created only after the file system is closed. Until that, the ones created in your |
| 2132 | +program are kept in memory. |
| 2133 | + |
2062 | 2134 | ==== `os.ResourcePath`
|
2063 | 2135 |
|
2064 | 2136 | In addition to manipulating paths on the filesystem, you can also manipulate
|
@@ -2186,6 +2258,31 @@ string, int or set representations of the `os.PermSet` via:
|
2186 | 2258 |
|
2187 | 2259 | == Changelog
|
2188 | 2260 |
|
| 2261 | +[#0-10-0] |
| 2262 | +=== 0.10.0 |
| 2263 | + |
| 2264 | +* Support for Scala-Native 0.5.0 |
| 2265 | +* Dropped support for Scala 2.11.x |
| 2266 | +* Minimum version of Scala 3 increased to 3.3.1 |
| 2267 | + |
| 2268 | + |
| 2269 | +[#0-9-3] |
| 2270 | +=== 0.9.3 - 2024-01-01 |
| 2271 | + |
| 2272 | +* Fix `os.watch` on Windows (#236) |
| 2273 | +* Fix propagateEnv = false to not propagate env (#238) |
| 2274 | +* Make os.home a def (#239) |
| 2275 | + |
| 2276 | +=== 0.9.2 - 2023-11-05 |
| 2277 | + |
| 2278 | +* Added new convenience API to create pipes between processes with `.pipeTo` |
| 2279 | +* Fixed issue with leading `..` / `os.up` in path segments created from a `Seq` |
| 2280 | +* Fixed Windows-specific issues with relative paths with leading (back)slashes |
| 2281 | +* Removed some internal use of deprecated API |
| 2282 | +* ScalaDoc now maps some external references to their online sites |
| 2283 | +* Dependency updates: sourcecode 0.3.1 |
| 2284 | +* Tooling updates: acyclic 0.3.9, Mill 0.11.5, mill-mima 0.0.24, mill-vcs-version 0.4.0, scalafmt 3.7.15 |
| 2285 | + |
2189 | 2286 | === 0.9.1 - 2023-03-07
|
2190 | 2287 |
|
2191 | 2288 | * Refined return types when constructing paths with `/` and get rid of long `ThisType#ThisType` cascades.
|
|
0 commit comments