LoginSignup
1
0

More than 1 year has passed since last update.

Pony 試乗記(8) Corral で依存管理

Posted at

はじめに

正規表現を使えるようになれば日頃 Python で書いているツールを Pony で書くこともできそうです。
正規表現ライブラリを Corral を使って依存ライブラリとして登録できるということなので試してみます。

Corral インストール

https://github.com/ponylang/corral の記述に従って、 Corral をインストールします:

$ ponyup update corral release

以下のようにインストールされました:

$ ponyup show corral
corral-release-0.5.6-x86_64-linux *

プロジェクトの作成

さらに https://github.com/ponylang/corral の記述に従って、プロジェクトを作成します。

プロジェクトディレクトリ作成と corral init 実行

プロジェクトを置くディレクトリを作成し、その上で corral init を実行します:

$ mkdir プロジェクト名
$ cd プロジェクト名
$ corral init

corral init 実行によって、ファイル corral.json および lock.json が作成されました。

依存の追加

libpcre2 を使った正規表現ライブラリ https://github.com/ponylang/regex を依存ライブラリとして追加しました(libpcre2-devが入っていない場合、https://github.com/ponylang/regex の記述に従ってインストールが必要です):

corral add github.com/ponylang/regex.git

指定した URL が corral.json に追加されました:

$ grep regex corral.json
      "locator": "github.com/ponylang/regex.git",

corral update を行っておきます:

$ corral update

プログラム作成

追加したライブラリを使用するプログラムを書いて、作成したディレクトリに置きます。

標準入力を行単位で読み取り、引数に与えられたパターンにマッチする行のみを表示する、grep 的なものを作って見ました。


"""
grep 的なプログラム
"""

use "buffered"
use "regex"

class RegexFilter is InputNotify
  let _env: Env
  let _rb: Reader
  let _r: Regex
  new create(env: Env, pattern: String) ? =>
    _env = env
    _rb = Reader
    _r = Regex(pattern)?
  fun ref apply(data: Array[U8] iso) =>
    """
    与えられたバイト列を行単位の文字列として切り出したうえで、パターン検索を行う。
    パターンにマッチする行を表示する。
    """
    _rb.append(consume data)  // 入力バイト列をバッファに追加していく
    try
      while true do
        let line: String = _rb.line()?  // バッファから行単位で取り出す
        _print_line_if_match_regex(line)  // 取り出した行を検査
      end
    end  // バッファに行がなくなったら戻る
  fun ref _print_line_if_match_regex(line: String): None =>
    """
    与えられた行文字列がパターンにマッチしたら表示する。マッチしなければ無視する。
    """
    let iterator = _r.matches(line)
    if iterator.has_next() == true then
      _env.out.print(line)
    end

actor Main
  new create(env: Env) =>
    try
      let pattern = env.args(1)?
      try
        env.input(recover RegexFilter(env, pattern)? end)
      else
        env.out.print("pattern not acceptable: " + pattern)
      end
    else
      try
        env.out.print("usage: " + env.args(0)? + " pattern")
      end
    end

プロジェクトディレクトリ上で以下を実行すると、依存ライブラリを解決したうえでコンパイルが行われます:

$ corral run -- ponyc

ディレクトリ名が生成されるバイナリの名前として用いられるのは ponyc コマンド投入でのコンパイルと同じです。

まとめ

corral を使って依存ライブラリを含むプログラムをコンパイルしてみました。
Maven や Gradle みたいなものを想像していたのですが、簡単に使用することができました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0