Skip to content

Commit

Permalink
feat: use package-interface.json from Hex if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
ghivert committed Jul 9, 2024
1 parent 2a1f467 commit 7635107
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM --platform=x86_64 ghcr.io/gleam-lang/gleam:v1.3.0-erlang-alpine AS builder

RUN apk add ca-certificates
RUN apk add build-base ca-certificates
RUN mkdir -p /build/backend/src

COPY apps/backend/gleam.toml /build/backend
Expand All @@ -13,7 +13,7 @@ RUN cd /build/backend && gleam export erlang-shipment
FROM --platform=x86_64 ghcr.io/gleam-lang/gleam:v1.3.0-erlang-alpine as runner
LABEL org.opencontainers.image.source https://github.com/ghivert/gloogle

RUN apk add ca-certificates inotify-tools
RUN apk add build-base ca-certificates inotify-tools

WORKDIR /app

Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/api/hex_repo.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn get_home() -> Result(String, Nil)
fn extract_tar(
tarbin: BitArray,
base_name: String,
version: String,
slug: String,
) -> Result(#(String, String, String), Nil)

Expand Down Expand Up @@ -136,7 +137,7 @@ fn extract_package_infos(name: String, version: String) {
use body <- result.try(req)
use #(interface_s, toml_s, res) <- result.try({
body
|> extract_tar(name, slug)
|> extract_tar(name, version, slug)
|> result.map_error(fn(_) {
let content = "Impossible to extract tar for " <> package_name
wisp.log_warning(content)
Expand Down
22 changes: 17 additions & 5 deletions apps/backend/src/gloogle_hex_ffi.erl
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
-module(gloogle_hex_ffi).
-export([extract_tar/3, remove_tar/1, is_match/2, get_home/0, set_level/1]).
-export([extract_tar/4, remove_tar/1, is_match/2, get_home/0, set_level/1]).

package_interface_path(ContentDest, BaseName) ->
BuildFolder = <<"/build/dev/docs/">>,
Package = <<"/package-interface.json">>,
Path = <<ContentDest/binary, BuildFolder/binary, BaseName/binary, Package/binary>>,
unicode:characters_to_binary(Path).

save_file(ContentDest, HttpBody) ->
PackageInterfacePath = <<ContentDest/binary, "/package-interface.json">>,
file:write_file(PackageInterfacePath, HttpBody),
{unicode:characters_to_binary(PackageInterfacePath), <<"">>}.

% Get the tarball package from Hex, build the package, extract the
% package-interface.json and the gleam.toml.
extract_tar(Binary, BaseName, Slug) ->
extract_tar(Binary, BaseName, Version, Slug) ->
PackagePath = <<"/tmp/", Slug/binary>>,
ContentDest = <<PackagePath/binary, "/contents">>,
Content = <<PackagePath/binary, "/contents.tar.gz">>,
case erl_tar:extract({binary, Binary}, [{cwd, PackagePath}]) of
{error, _} -> {error, nil};
_ ->
Url = <<"https://hexdocs.pm/", BaseName/binary, "/", Version/binary, "/package-interface.json">>,
erl_tar:extract(Content, [{cwd, ContentDest}, compressed]),
BuildCmd = <<"cd ", ContentDest/binary, " && gleam docs build">>,
Result = os:cmd(binary_to_list(BuildCmd)),
PackageInterface = package_interface_path(ContentDest, BaseName),
{PackageInterface, Result} =
case httpc:request(Url) of
{ok, {_, _, HttpBody}} -> save_file(ContentDest, HttpBody);
{ok, {_, HttpBody}} -> save_file(ContentDest, HttpBody);
{error, _} ->
BuildCmd = <<"cd ", ContentDest/binary, " && gleam docs build">>,
Res = os:cmd(binary_to_list(BuildCmd)),
{package_interface_path(ContentDest, BaseName), Res}
end,
GleamToml = unicode:characters_to_binary(<<ContentDest/binary, "/gleam.toml">>),
{ok, {PackageInterface, GleamToml, unicode:characters_to_binary(Result)}}
end.
Expand Down

0 comments on commit 7635107

Please sign in to comment.