From 671c8326f3d6bc33a6148cf116efd2bc02a5ae92 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Wed, 11 Mar 2015 10:16:11 -0400 Subject: [PATCH] add pipe (fix #47) --- README.md | 2 ++ src/Compat.jl | 23 +++++++++++++++++++++++ test/runtests.jl | 3 +++ 3 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 0473bc513b224..48e504a86db6f 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,8 @@ Currently, the `@compat` macro supports the following syntaxes: * `beginswith` is now `startswith` [#9583](https://github.com/JuliaLang/julia/pull/9583) +* `|>`, `>>`, `.>`, and `.>>` are now `pipe` [#10211](https://github.com/JuliaLang/julia/pull/10211) + ## New macros * `@inline` and `@noinline` have been added. On 0.3, these are "no-ops," meaning they don't actually do anything. diff --git a/src/Compat.jl b/src/Compat.jl index b60634b1e4cf3..2f4c33355b84f 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -80,6 +80,29 @@ if VERSION < v"0.4.0-dev+2014" export sizehint! end +if VERSION < v"0.4.0-dev+3413" + # based on pipe in base/process.jl: + function pipe(src::AbstractCmd; stdin=nothing, stdout=nothing, stderr=nothing, append::Bool=false) + if append && stdout === nothing && stderr === nothing + error("append set to true, but no output redirections specified") + end + if stdin !== nothing + cmd = stdin |> cmd + end + if stdout !== nothing + cmd = append ? cmd >> stdout : cmd |> stdout + end + if stderr !== nothing + cmd = append ? cmd .>> stderr : cmd .> stderr + end + return cmd + end + pipe(cmd::AbstractCmd, dest) = pipe(cmd, stdout=dest) + pipe(src::Union(Redirectable,AbstractString), cmd::AbstractCmd) = pipe(cmd, stdin=src) + pipe(a, b, c, d...) = pipe(pipe(a,b), c, d...) + export pipe +end + function rewrite_dict(ex) length(ex.args) == 1 && return ex diff --git a/test/runtests.jl b/test/runtests.jl index 26d13115a5cbe..19439e657ecd3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -93,3 +93,6 @@ end @test CartesianTest.f(1,2,3) == (1,2,3) @test CartesianTest.f(1,2,3,4) == (1,2,3,4) @test CartesianTest.f(1,2,3,4,5) == (1,2,3,4,5) + +@test readall(pipe(`echo hello`, `sort`)) == "hello\n" +@test success(pipe(`true`, `true`))