diff --git a/Dockerfile b/Dockerfile index 89df7ed..3862fe8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 @@ -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 diff --git a/apps/backend/src/api/hex_repo.gleam b/apps/backend/src/api/hex_repo.gleam index a3b3b7b..eea473f 100644 --- a/apps/backend/src/api/hex_repo.gleam +++ b/apps/backend/src/api/hex_repo.gleam @@ -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) @@ -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) diff --git a/apps/backend/src/gloogle_hex_ffi.erl b/apps/backend/src/gloogle_hex_ffi.erl index 7907d34..a878b98 100644 --- a/apps/backend/src/gloogle_hex_ffi.erl +++ b/apps/backend/src/gloogle_hex_ffi.erl @@ -1,5 +1,5 @@ -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/">>, @@ -7,19 +7,31 @@ package_interface_path(ContentDest, BaseName) -> Path = <>, unicode:characters_to_binary(Path). +save_file(ContentDest, HttpBody) -> + PackageInterfacePath = <>, + 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 = <>, Content = <>, 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(<>), {ok, {PackageInterface, GleamToml, unicode:characters_to_binary(Result)}} end.