2017-03-24 56 views
1

私はxargsが使用するよう指示されているコマンドラインパラメータを基本的に "find stuff | xargs grep"複数のプロセス。これはProcess.runを介して実行され、出力とエラーはカスタムIOオブジェクトにリダイレクトされ、grepから出てくるものをフィルタリングし、フィルタリングされていないものはすべてSTDOUTに書き出します。シェルコマンドのgrepへのパイプ出力の結果 "grep:write error"

これを通常通り実行すると、大部分は正常に動作しているようです。検索が完了する前に出力が途切れてしまったように見えるので、結果を完全に信頼できるかどうかはわかりません。しかし、このコマンドの出力をgrepにパイプすると、検索が早く終了し、 "grep:write error"と表示されます。なぜこれが起こっているのか分かりませんし、助けが大好きです。最終的に純粋なCrystalのすべてを行うためにこれを書き直す予定ですが、これは私が作業しているコードベースを検索するための迅速な解決策です。ここで

は実行を取得しているコードです:

class FindFilterIO 
include IO 

@@generic_filter = [".diff", ".iml", "/target/"] 
@@web_filter = [".css", ".js", ".jsp", ".ftl"] 

def initialize(@web_search : Bool = false) 
end 

def read(slice : Bytes) 
    raise "FindFilterIO does not support reading!" 
end 

def write(slice : Bytes) 
    str = String.new slice 
    if @@generic_filter.any? { |e| str.includes? e } 
    return 
    end 

    if @web_search 
    if [email protected]@web_filter.any? { |e| str.includes? e } 
     return 
    end 
    end 

    STDOUT.write(slice) 
end 
end 

    cmd = "find . -not \\(-path ./.svn -prune \\) " \ 
     "-not \\(-path ./.idea -prune \\) " \ 
     "-type f -print0 " \ 
     "| xargs -0 -P 1 -n 100 grep -E -n --color=always " 
    cmd += if @html_id 
      "'id=['\"'\"'\"]#{@search_text}['\"'\"'\"]|\##{@search_text}'" 
     elsif @html_class 
      "'class=['\"'\"'\"]#{@search_text}['\"'\"'\"]|\\.#{@search_text}'" 
     else 
      "'#{@search_text}'" 
     end 
    io = FindFilterIO.new web_search: (@html_id || @html_class) 
    Process.run(cmd, output: io, error: io, shell: true, chdir: File.join(@env.home_dir, @env.branch, "repodir")) 
+2

これはよく知られているバグです:https://github.com/crystal-lang/crystal/issues/2065 – asterite

答えて

0

これはhttps://github.com/crystal-lang/crystal/issues/2065に問題が閉じられたことを今に固定されているようです。それは完全に修正されていることを確認するためにいくつかのテストを行う必要がありますが、私の古いコードを使用して今うまく動作しているようだ。

関連する問題