View Source hex_tarball (hex_core v0.10.3)

Functions for creating and unpacking Hex tarballs.

Summary

Functions

Creates a package tarball.

Creates a docs tarball.

Returns base16-encoded representation of checksum.

Converts an error reason term to a human-readable error message string.

Unpacks a package tarball.

Unpacks a package tarball.

Unpacks a documentation tarball.

Types

checksum/0

-type checksum() :: binary().

contents/0

-type contents() :: #{filename() => binary()}.

filename/0

-type filename() :: string().

files/0

-type files() :: [{filename(), filename() | binary()}].

metadata/0

-type metadata() :: map().

tarball/0

-type tarball() :: binary().

Functions

create(Metadata, Files)

-spec create(metadata(), files()) ->
                {ok, #{tarball => tarball(), outer_checksum => checksum(), inner_checksum => tarball()}} |
                {error, term()}.

create(Metadata, Files, Config)

-spec create(metadata(), files(), hex_core:config()) ->
                {ok, #{tarball => tarball(), outer_checksum => checksum(), inner_checksum => tarball()}} |
                {error, term()}.

Creates a package tarball.

Returns the binary of the tarball the "inner checksum" and "outer checksum". The inner checksum is deprecated in favor of the outer checksum.

Examples:

  > Metadata = #{<<"name">> => <<"foo">>, <<"version">> => <<"1.0.0">>},
  > Files = [{"src/foo.erl", <<"-module(foo).">>}],
  > hex_tarball:create(Metadata, Files).
  {ok, #{tarball => <<86,69,...>>,
         outer_checksum => <<40,32,...>>,
         inner_checksum => <<178,12,...>>}}

create_docs(Files)

-spec create_docs(files()) -> {ok, tarball()} | {error, term()}.

create_docs(Files, Config)

-spec create_docs(files(), hex_core:config()) -> {ok, tarball()} | {error, term()}.

Creates a docs tarball.

Examples:

  > Files = [{"doc/index.html", <<"Docs">>}],
  > hex_tarball:create_docs(Files).
  {ok, <<86,69,...>>}

format_checksum(Checksum)

-spec format_checksum(checksum()) -> binary().

Returns base16-encoded representation of checksum.

format_error(_)

-spec format_error(term()) -> string().

Converts an error reason term to a human-readable error message string.

unpack(Tarball, Output)

-spec unpack(tarball(), memory) ->
                {ok,
                 #{outer_checksum => checksum(),
                   inner_checksum => checksum(),
                   metadata => metadata(),
                   contents => contents()}} |
                {error, term()};
            (tarball(), filename()) ->
                {ok,
                 #{outer_checksum => checksum(), inner_checksum => checksum(), metadata => metadata()}} |
                {error, term()}.

Unpacks a package tarball.

See also: unpack/3.

unpack(Tarball, Output, Config)

-spec unpack(tarball(), memory, hex_core:config()) ->
                {ok,
                 #{outer_checksum => checksum(),
                   inner_checksum => checksum(),
                   metadata => metadata(),
                   contents => contents()}} |
                {error, term()};
            (tarball(), filename(), hex_core:config()) ->
                {ok,
                 #{outer_checksum => checksum(), inner_checksum => checksum(), metadata => metadata()}} |
                {error, term()}.

Unpacks a package tarball.

Remember to verify the outer tarball checksum against the registry checksum returned from hex_repo:get_package(Config, Package).

Examples:

  > hex_tarball:unpack(Tarball, memory).
  {ok,#{outer_checksum => <<...>>,
        contents => [{"src/foo.erl",<<"-module(foo).">>}],
        metadata => #{<<"name">> => <<"foo">>, ...}}}
 
  > hex_tarball:unpack(Tarball, "path/to/unpack").
  {ok,#{outer_checksum => <<...>>,
        metadata => #{<<"name">> => <<"foo">>, ...}}}

unpack_docs(Tarball, Output)

-spec unpack_docs(tarball(), memory) -> {ok, contents()} | {error, term()};
                 (tarball(), filename()) -> ok | {error, term()}.

unpack_docs(Tarball, Output, Config)

-spec unpack_docs(tarball(), memory, hex_core:config()) -> {ok, contents()} | {error, term()};
                 (tarball(), filename(), hex_core:config()) -> ok | {error, term()}.

Unpacks a documentation tarball.

Examples:

  > hex_tarball:unpack_docs(Tarball, memory).
  {ok, [{"index.html", <<"<!doctype>">>}, ...]}
 
  > hex_tarball:unpack_docs(Tarball, "path/to/unpack").
  ok