File tree Expand file tree Collapse file tree 2 files changed +86
-0
lines changed Expand file tree Collapse file tree 2 files changed +86
-0
lines changed Original file line number Diff line number Diff line change 1+ defmodule Mix.Gleam do
2+ def load_config ( dir ) do
3+ config_path = Path . join ( dir , "gleam.toml" )
4+
5+ case File . read ( config_path ) do
6+ { :ok , config } ->
7+ parse_config ( config )
8+
9+ { :error , :enoent } ->
10+ [ ]
11+
12+ { :error , error } ->
13+ raise ( "Error reading gleam.toml config #{ inspect ( config_path ) } : #{ error } " )
14+ end
15+ end
16+
17+ defp parse_config ( config ) do
18+ dependencies =
19+ config
20+ |> table ( "dependencies" )
21+ |> parse_deps ( )
22+
23+ dev_dependencies =
24+ config
25+ |> table ( "dev-dependencies" )
26+ |> parse_deps
27+ |> Enum . map ( & Tuple . insert_at ( & 1 , 2 , only: :dev ) )
28+
29+ % {
30+ deps: dependencies ++ dev_dependencies
31+ }
32+ end
33+
34+ # Given a string of deps, returns them as a list of tuples
35+ defp parse_deps ( string ) do
36+ ~r/ ^([a-z][a-z0-9_]+)\s *=\s *"([^"]+)"\s */ m
37+ |> Regex . scan ( string )
38+ |> Enum . map ( fn dep ->
39+ [ _ , name , version ] = dep
40+ { String . to_atom ( name ) , version }
41+ end )
42+ end
43+
44+ # Grabs a TOML table by name and returns its contents as a string
45+ def table ( config , name ) do
46+ ~r/ ^\[ #{ name } \] $[\r \n ]+((?:[a-z][a-z0-9_]*\s *=\s *"[^"]+"\r ?\n ?)+)/ m
47+ |> Regex . run ( config )
48+ |> List . last ( )
49+ end
50+ end
Original file line number Diff line number Diff line change 1+ # SPDX-License-Identifier: Apache-2.0
2+ # SPDX-FileCopyrightText: 2021 The Elixir Team
3+ # SPDX-FileCopyrightText: 2012 Plataformatec
4+
5+ Code . require_file ( "../test_helper.exs" , __DIR__ )
6+
7+ defmodule Mix.GleamTest do
8+ use MixTest.Case
9+
10+ @ compile { :no_warn_undefined , [ :gleam_dep , :gleam@int ] }
11+
12+ defmodule GleamAsDep do
13+ def project do
14+ [
15+ app: :gleam_as_dep ,
16+ version: "0.1.0" ,
17+ deps: [
18+ { :gleam_dep , path: MixTest.Case . tmp_path ( "gleam_dep" ) , app: false }
19+ ]
20+ ]
21+ end
22+ end
23+
24+ describe "load_config/1" do
25+ test "loads gleam.toml" do
26+ path = MixTest.Case . fixture_path ( "gleam_dep" )
27+ config = Mix.Gleam . load_config ( path )
28+
29+ assert config [ :deps ] == [
30+ { :gleam_stdlib , ">= 0.44.0 and < 2.0.0" } ,
31+ { :gleam_otp , ">= 0.16.1 and < 1.0.0" } ,
32+ { :gleeunit , ">= 1.0.0 and < 2.0.0" , only: :dev }
33+ ]
34+ end
35+ end
36+ end
You can’t perform that action at this time.
0 commit comments