fugafuga.write

日々のログ

Hex に自作ライブラリを登録する

この前とても役に立つコマンド(個人差有)

github.com

を作った。

Hex に登録して escript.install すると作ったコマンドをすぐ共有できてよさそうなのでやる。

環境

Elixir 1.5.2

Hex にユーザー登録する

mixコマンド は elixir に同梱されている。

> mix hex.user register
By registering an account on Hex.pm you accept all our policies and terms of service found at https://hex.pm/policies

Username: tokoyax
Email: tokoyax.dev@gmail.com
Password:
Password (confirm):
Registering...
Generating API key...
Encrypting API key with user password...
You are required to confirm your email to access your account, a confirmation email has been sent to tokoyax.dev@gmail.com

API keyは~/.hex/hex.configに保存される。
メールが届くので confirm するとアカウントが作成される。

mix.exsの編集

ライブラリのページに記載されるメタデータを編集する

defmodule No.Mixfile do
  use Mix.Project

  # 追加
  @description"""
  This is `no` command that outputs `n` implemented in Elixir.
  """

  def project do
    [
      app: :no,
      version: "0.1.0",
      elixir: "~> 1.5",
      name: "No",
      description: @description, # <= 追加
      package: package(), # <= 追加
      escript: escript_config(),
      start_permanent: Mix.env == :prod,
      deps: deps()
    ]
  end

...

  # 追加
  def package do
    [
      maintainers: ["tokoyax"],
      licenses: ["MIT"],
      links: %{ "Github": "https://github.com/tokoyax/no" }
    ]
  end

Hex へ登録

Passphrase を聞かれるのでアカウント作成時のパスワードを入力する。

> mix hex.publish
Passphrase:
Publishing no 0.1.0
  Files:
    lib/no.ex
    mix.exs
    README.md
  App: no
  Name: no
  Description: This is `no` command that outputs `n` implemented in Elixir.
  Version: 0.1.0
  Build tools: mix
  Licenses: MIT
  Maintainers: tokoyax
  Links:
    Github: https://github.com/tokoyax/no
  Elixir: ~> 1.5
Before publishing, please read the Code of Conduct: https://hex.pm/policies/codeofconduct
Proceed? [Yn] Y
Building docs...
Compiling 1 file (.ex)
Generated no app
The "docs" task is unavailable. Please add {:ex_doc, ">= 0.0.0", only: :dev} to your dependencies in your mix.exs. If ex_doc was already added, make sure you run the task in the same environment it is configured to
** (Mix) The task "docs" could not be found. Did you mean "do"?

なんかエラーが。

{:ex_doc, ">= 0.0.0", only: :dev}が無いので追加しないといけないっぽい。

mix.exsを編集する

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      {:ex_doc, ">= 0.0.0", only: :dev}, # <= 追加
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
    ]
  end

もう一度実行

> mix hex.publish
...
Proceed? [Yn] Y
Building docs...
Unchecked dependencies for environment dev:
* ex_doc (Hex package)
  the dependency is not available, run "mix deps.get"
** (Mix) Can't continue due to errors on dependencies

またエラーが。

依存関係のパッケージが無いのでmix deps.getしないといけませんよーとのこと。 次のアクションを促してくれるのは親切でとてもよい。

> mix deps.get
Running dependency resolution...
Dependency resolution completed:
  earmark 1.2.3
  ex_doc 0.18.1
* Getting ex_doc (Hex package)
  Checking package (https://repo.hex.pm/tarballs/ex_doc-0.18.1.tar)
  Fetched package
* Getting earmark (Hex package)
  Checking package (https://repo.hex.pm/tarballs/earmark-1.2.3.tar)
  Fetched package

Ok, 再度実行

> mix hex.publish
Passphrase:
Publishing no 0.1.0
  Files:
    lib/no.ex
    mix.exs
    README.md
  App: no
  Name: no
  Description: This is `no` command that outputs `n` implemented in Elixir.
  Version: 0.1.0
  Build tools: mix
  Licenses: MIT
  Maintainers: tokoyax
  Links:
    Github: https://github.com/tokoyax/no
  Elixir: ~> 1.5
Before publishing, please read the Code of Conduct: https://hex.pm/policies/codeofconduct
Proceed? [Yn] Y
Building docs...
==> earmark
Compiling 3 files (.erl)
Compiling 24 files (.ex)
Generated earmark app
==> ex_doc
Compiling 15 files (.ex)
Generated ex_doc app
==> no
Compiling 1 file (.ex)
Generated no app
Docs successfully generated.
View them at "doc/index.html".
Publishing package...
[#########################] 100%
Package published to https://hex.pm/packages/no/0.1.0 (1b7e3d36abb1e88b60b9fd3872cc251dc32ef3aa19a13ead26bd040644af42d3)
Publishing docs...
[#########################] 100%
Docs published to https://hexdocs.pm/no/0.1.0[f:id:tokoyax:20171118170418j:plain]

OK

hex.pm にアクセスしてみる。

f:id:tokoyax:20171118170418j:plain

https://hex.pm/packages/no

SUCCESS 😇 😇 😇 😇 😇

escript.install してみる

escript.install – Mix v1.6.0-dev

If an argument is provided, it should be a local path or a URL to a prebuilt escript, a Git repository, a GitHub repository, or a Hex package.

とあるように、github など Hex 以外からも install できるらしい。便利。

> mix escript.install hex no
Running dependency resolution...
Dependency resolution completed:
  no 0.1.0
* Getting no (Hex package)
  Checking package (https://repo.hex.pm/tarballs/no-0.1.0.tar)
  Using locally cached package
Compiling 1 file (.ex)
Generated no app
Generated escript no with MIX_ENV=prod
Found existing entry: /Users/takuya/.mix/escripts/no
Are you sure you want to replace it with "no"? [Yn] Y
* creating /Users/takuya/.mix/escripts/no

warning: you must append "/Users/takuya/.mix/escripts" to your PATH if you want to invoke escripts by name

実行可能ファイルは、~/.mix/escripts に install される。 warning にもあるように PATH 通さないと使えないので気をつけてね、とのこと。

まとめ

  • elixir さえインストールされていれば結構お手軽にコマンド配布できたので最高という感想
  • ドキュメントの整備はまた気が向いたらやる

(参考)
* ElixirのライブラリをHexに公開する | Developers.IO

プログラミングElixir

プログラミングElixir