🗂

PkgTemplates.jlを使ったパッケージ作り

2021/02/04に公開

PkgTemplates.jlを使ったパッケージ作り

GitHubと連携したパッケージの作り方。GitHub Actionsを使ったCIも利用する。
このページを参考に、Travis CIではなくGitHub Actionsを使う。
以下、パッケージ名はPkgTest.jl、GitHubのユーザ名はshinaokaとします。

必要環境

  • PkgTemplates.jl
  • 予め、GitHubのアカウント名をgitに登録してくこと (以下sshアクセスが可能なことを仮定)。
$ git config --global user.name "Hiroshi Shinaoka" # もし設定していなければ
$ git config --global github.user shinaoka

パッケージの作成

以下Juliaの操作。PkgTestという名前で、GitHub Actionsと連携したパッケージを作成。

julia> using PkgTemplates
julia> t = Template(;
           ssh=true,
           plugins=[
              GitHubActions(),
              Documenter{GitHubActions}(),
           ]
           ,
       )
julia> generate("PkgTest", t)

正常に終了すれば、``~/.julia/dev/PkgTest`以下にファイルができる。

$ tree ~/.julia/dev/PkgTest/
~/.julia/dev/PkgTest/
├── LICENSE
├── Manifest.toml
├── Project.toml
├── README.md
├── docs
│   ├── Manifest.toml
│   ├── Project.toml
│   ├── make.jl
│   └── src
│       └── index.md
├── src
│   └── PkgTest.jl
└── test
    └── runtests.jl

4 directories, 10 files

Documentの作成

$ cd ~/.julia/dev/PkgTest/
$ julia --project=docs -e '
          using Pkg;
          Pkg.develop(PackageSpec(path=pwd()));
          Pkg.instantiate();
          include("docs/make.jl");
'

Buildされたドキュメントを、HTTPサーバを起動して確認する。
今回は8001番のポートで起動した。

$ cd docs/build/
$ python3 -m http.server --bind localhost 8001

GitHubへのpush

PkgTemplates.jlによってすでにgit initや最初のコミットが済んだ状態になっている。また、originも適切に設定済み。

$ cd ~/.julia/dev/PkgTest
$ git log
commit 66af6ba96f733ade0939b3b17b237f92d746b205 (HEAD -> master)
Author: Hiroshi Shinaoka <h.shinaoka@gmail.com>
Date:   Thu Feb 4 09:57:29 2021 +0900

    Files generated by PkgTemplates
    
    PkgTemplates version: 0.7.13

commit 9986eb0515f1c2ec860ac4f4c176db13fd2ad3d2
Author: Hiroshi Shinaoka <h.shinaoka@gmail.com>
Date:   Thu Feb 4 09:57:08 2021 +0900

    Initial commit
$ git remote -v
origin  https://github.com/h.shinaoka@gmail.com/PkgTest.jl (fetch)
origin  https://github.com/h.shinaoka@gmail.com/PkgTest.jl (push)

GitHubのページでPkgTest.jlという名前でレポジトリを作成したあと、
通常通りpushすれば良い。

$ git push -u origin master

ドキュメントの公開

このページを参考にしつつ、差分を中心に説明する。
PkgTemplates.jlによって、~/.julia/dev/PkgTest/.github/workflows/ci.ymlにすでに、GitHub Actionsによるテストとドキュメントのビルド用の設定が生成されている。
設定の詳細は、Documeter.jlのドキュメントを参考にすること。

まずは、 SSH Depoly keyを作る。このページを参考にして、以下のようなコマンドを実行。

pkg> add DocumenterTools
julia> using DocumenterTools
julia> DocumenterTools.genkeys(user="shinaoka", repo="PkgTest.jl")

https://github.com/shinaoka/PkgTest.jl/settings/keysへアクセスして、deploy keysにDocumenterという名前でRSA公開鍵を登録。Allow write accessにチェックを入れること。次に、GitHubのドキュメントを参考に、秘密鍵をレポジトリに登録する (titleは、DOCUMENTER_KEY)。
あとは、適当にファイルを更新してpushすればよい。暫く待つと、dev以下にドキュメントが公開されているのがわかる。

Discussion