Skip to content

Commit

Permalink
Merge pull request #3 from optimumBA/remove_fast_yaml
Browse files Browse the repository at this point in the history
Remove demanding dependency fast_yaml
  • Loading branch information
almirsarajcic authored Jun 21, 2024
2 parents a1575b3 + b48263b commit 9474171
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 71 deletions.
8 changes: 4 additions & 4 deletions .github/github_workflows.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,16 @@ defmodule GithubWorkflows do
"fail-fast": false,
matrix: [
versions: [
%{
[
elixir: "1.11",
otp: "21.3",
"runner-image": "ubuntu-20.04"
},
%{
],
[
elixir: "1.16",
otp: "26.2",
"runner-image": "ubuntu-latest"
}
]
]
]
],
Expand Down
127 changes: 77 additions & 50 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: CI

on:
pull_request: []
pull_request:
push:
branches:
- main

jobs:
compile:
name: Install deps and compile
Expand All @@ -12,11 +14,11 @@ jobs:
fail-fast: false
matrix:
versions:
- otp: 21.3
elixir: 1.11
- elixir: 1.11
otp: 21.3
runner-image: ubuntu-20.04
- otp: 26.2
elixir: 1.16
- elixir: 1.16
otp: 26.2
runner-image: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -28,9 +30,12 @@ jobs:
otp-version: ${{ matrix.versions.otp }}
- uses: actions/cache@v3
with:
path: "_build\ndeps"
path: |
_build
deps
key: mix-${{ matrix.versions.runner-image }}-${{ matrix.versions.otp }}-${{ matrix.versions.elixir }}-${{ github.sha }}
restore-keys: mix-${{ matrix.versions.runner-image }}-
restore-keys: |
mix-${{ matrix.versions.runner-image }}-
- name: Install Elixir dependencies
env:
MIX_ENV: test
Expand All @@ -47,11 +52,11 @@ jobs:
fail-fast: false
matrix:
versions:
- otp: 21.3
elixir: 1.11
- elixir: 1.11
otp: 21.3
runner-image: ubuntu-20.04
- otp: 26.2
elixir: 1.16
- elixir: 1.16
otp: 26.2
runner-image: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -63,9 +68,12 @@ jobs:
otp-version: ${{ matrix.versions.otp }}
- uses: actions/cache@v3
with:
path: "_build\ndeps"
path: |
_build
deps
key: mix-${{ matrix.versions.runner-image }}-${{ matrix.versions.otp }}-${{ matrix.versions.elixir }}-${{ github.sha }}
restore-keys: mix-${{ matrix.versions.runner-image }}-
restore-keys: |
mix-${{ matrix.versions.runner-image }}-
- name: Check code style
env:
MIX_ENV: test
Expand All @@ -78,11 +86,11 @@ jobs:
fail-fast: false
matrix:
versions:
- otp: 21.3
elixir: 1.11
- elixir: 1.11
otp: 21.3
runner-image: ubuntu-20.04
- otp: 26.2
elixir: 1.16
- elixir: 1.16
otp: 26.2
runner-image: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -94,9 +102,12 @@ jobs:
otp-version: ${{ matrix.versions.otp }}
- uses: actions/cache@v3
with:
path: "_build\ndeps"
path: |
_build
deps
key: mix-${{ matrix.versions.runner-image }}-${{ matrix.versions.otp }}-${{ matrix.versions.elixir }}-${{ github.sha }}
restore-keys: mix-${{ matrix.versions.runner-image }}-
restore-keys: |
mix-${{ matrix.versions.runner-image }}-
- name: Check for vulnerable Mix dependencies
env:
MIX_ENV: test
Expand All @@ -109,11 +120,11 @@ jobs:
fail-fast: false
matrix:
versions:
- otp: 21.3
elixir: 1.11
- elixir: 1.11
otp: 21.3
runner-image: ubuntu-20.04
- otp: 26.2
elixir: 1.16
- elixir: 1.16
otp: 26.2
runner-image: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -125,15 +136,19 @@ jobs:
otp-version: ${{ matrix.versions.otp }}
- uses: actions/cache@v3
with:
path: "_build\ndeps"
path: |
_build
deps
key: mix-${{ matrix.versions.runner-image }}-${{ matrix.versions.otp }}-${{ matrix.versions.elixir }}-${{ github.sha }}
restore-keys: mix-${{ matrix.versions.runner-image }}-
restore-keys: |
mix-${{ matrix.versions.runner-image }}-
- name: Restore PLT cache
uses: actions/cache@v3
with:
path: priv/plts
key: plt-${{ matrix.versions.runner-image }}-${{ matrix.versions.otp }}-${{ matrix.versions.elixir }}-${{ github.sha }}
restore-keys: plt-${{ matrix.versions.runner-image }}-
restore-keys: |
plt-${{ matrix.versions.runner-image }}-
- name: Create PLTs
env:
MIX_ENV: test
Expand All @@ -150,11 +165,11 @@ jobs:
fail-fast: false
matrix:
versions:
- otp: 21.3
elixir: 1.11
- elixir: 1.11
otp: 21.3
runner-image: ubuntu-20.04
- otp: 26.2
elixir: 1.16
- elixir: 1.16
otp: 26.2
runner-image: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -166,9 +181,12 @@ jobs:
otp-version: ${{ matrix.versions.otp }}
- uses: actions/cache@v3
with:
path: "_build\ndeps"
path: |
_build
deps
key: mix-${{ matrix.versions.runner-image }}-${{ matrix.versions.otp }}-${{ matrix.versions.elixir }}-${{ github.sha }}
restore-keys: mix-${{ matrix.versions.runner-image }}-
restore-keys: |
mix-${{ matrix.versions.runner-image }}-
- name: Check Elixir formatting
env:
MIX_ENV: test
Expand All @@ -181,11 +199,11 @@ jobs:
fail-fast: false
matrix:
versions:
- otp: 21.3
elixir: 1.11
- elixir: 1.11
otp: 21.3
runner-image: ubuntu-20.04
- otp: 26.2
elixir: 1.16
- elixir: 1.16
otp: 26.2
runner-image: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -197,9 +215,12 @@ jobs:
otp-version: ${{ matrix.versions.otp }}
- uses: actions/cache@v3
with:
path: "_build\ndeps"
path: |
_build
deps
key: mix-${{ matrix.versions.runner-image }}-${{ matrix.versions.otp }}-${{ matrix.versions.elixir }}-${{ github.sha }}
restore-keys: mix-${{ matrix.versions.runner-image }}-
restore-keys: |
mix-${{ matrix.versions.runner-image }}-
- name: Check for retired Hex packages
env:
MIX_ENV: test
Expand Down Expand Up @@ -229,11 +250,11 @@ jobs:
fail-fast: false
matrix:
versions:
- otp: 21.3
elixir: 1.11
- elixir: 1.11
otp: 21.3
runner-image: ubuntu-20.04
- otp: 26.2
elixir: 1.16
- elixir: 1.16
otp: 26.2
runner-image: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -245,9 +266,12 @@ jobs:
otp-version: ${{ matrix.versions.otp }}
- uses: actions/cache@v3
with:
path: "_build\ndeps"
path: |
_build
deps
key: mix-${{ matrix.versions.runner-image }}-${{ matrix.versions.otp }}-${{ matrix.versions.elixir }}-${{ github.sha }}
restore-keys: mix-${{ matrix.versions.runner-image }}-
restore-keys: |
mix-${{ matrix.versions.runner-image }}-
- name: Run tests
env:
MIX_ENV: test
Expand All @@ -262,11 +286,11 @@ jobs:
fail-fast: false
matrix:
versions:
- otp: 21.3
elixir: 1.11
- elixir: 1.11
otp: 21.3
runner-image: ubuntu-20.04
- otp: 26.2
elixir: 1.16
- elixir: 1.16
otp: 26.2
runner-image: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -278,9 +302,12 @@ jobs:
otp-version: ${{ matrix.versions.otp }}
- uses: actions/cache@v3
with:
path: "_build\ndeps"
path: |
_build
deps
key: mix-${{ matrix.versions.runner-image }}-${{ matrix.versions.otp }}-${{ matrix.versions.elixir }}-${{ github.sha }}
restore-keys: mix-${{ matrix.versions.runner-image }}-
restore-keys: |
mix-${{ matrix.versions.runner-image }}-
- name: Check for unused Mix dependencies
env:
MIX_ENV: test
Expand Down
9 changes: 7 additions & 2 deletions lib/github_workflows_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ defmodule GithubWorkflowsGenerator do
{:ok, workflows} <- get_workflows(source_path) do
for {filename, data} <- workflows do
path = Path.join(dir_path, filename)
yml = YmlEncoder.encode(data)

yml =
data
|> List.first()
|> YmlEncoder.encode()

File.write!(path, yml)
end

Expand Down Expand Up @@ -41,7 +46,7 @@ defmodule GithubWorkflowsGenerator do
%{} ->
{:error, "File #{source_path} does not contain any workflows."}

_ ->
_other ->
{:error, "File #{source_path} does not have a valid structure."}
end
end
Expand Down
74 changes: 62 additions & 12 deletions lib/github_workflows_generator/yml_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,68 @@ defmodule GithubWorkflowsGenerator.YmlEncoder do
@spec encode(term()) :: String.t()
def encode(data) do
data
|> Enum.at(0)
|> :fast_yaml.encode()
|> to_string()
# Trim trailing space
|> String.replace("\s\n", "\n")
# Avoid putting list element in new line after -
|> String.replace(~r/-\n\s+/, "-\s")
# Trim trailing newline inside strings
|> String.replace(~S(\n"), ~S("))
# Unquote strings
|> String.replace(~r<"([a-z0-9@_\-\.,: \{\}\$/\*\(\)'!=&\<\>~]+)">i, "\\1")
# Add newline to the EOF
|> to_yml()
|> String.trim()
|> Kernel.<>("\n")
end

defp to_yml(data, level \\ 0, indent? \\ false)

defp to_yml(data, level, indent?) when is_list(data) do
if Keyword.keyword?(data) do
handle_keyword_list(data, level, indent?)
else
Enum.map_join(
data,
"\n",
&(String.duplicate(" ", level) <> "- " <> to_yml(&1, level + 1, false))
)
end
end

defp to_yml(data, level, _indent?) do
cond do
!is_binary(data) ->
to_string(data)

String.contains?(data, "\n") ->
values =
data
|> String.split("\n", trim: true)
|> Enum.map_join("\n", &(String.duplicate(" ", level) <> &1))

"|\n#{values}"

true ->
data
end
end

defp handle_keyword_list(data, level, indent?) do
data
|> Enum.map_reduce(indent?, fn {key, value}, indent? ->
indentation =
if indent? do
String.duplicate(" ", level)
else
""
end

{delimiter, string_value} =
case value do
[] ->
{"", ""}

value when is_list(value) ->
{"\n", to_yml(value, level + 1, true)}

value ->
{" ", to_yml(value, level + 1, false)}
end

{"#{indentation}#{key}:#{delimiter}#{string_value}", true}
end)
|> elem(0)
|> Enum.join(if(level == 0, do: "\n\n", else: "\n"))
end
end
Loading

0 comments on commit 9474171

Please sign in to comment.