Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
pertsevds committed Mar 10, 2024
1 parent fd5cc25 commit c835998
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 107 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/credo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ jobs:
contents: read # for actions/checkout to fetch code
security-events: write # for github/codeql-action/upload-sarif to upload SARIF results
actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
otp: [26.2.3]
elixir: [1.16.1]
otp: ['26.2.3']
elixir: ['1.16.2']
steps:
- uses: actions/checkout@v3
- uses: erlef/setup-beam@988e02bfe678367a02564f65ca2e37726dc0268f
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
otp-version: ${{matrix.otp}}
elixir-version: ${{matrix.elixir}}
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ With `Nbpm` you don't need EPMD daemon. No additional daemons, no additional con

## Features

- `Mix.Release` compatibility.
- Ability to specify port number in the node name.
- Hash-Based Port from node name.
- `Mix.Release` compatibility.
- Ability to specify port number in the node name.
- Hash-Based Port from node name.

## Installation

Expand All @@ -29,11 +29,13 @@ end
```

Run:

```shell
mix nbpm.install
```

This will add `-start_epmd false -epmd_module Elixir.Nbpm` to `ELIXIR_ERL_OPTIONS` in files `rel/env.sh.eex` and `rel/env.bat.eex`.
This will add `-start_epmd false -epmd_module Elixir.Nbpm` to
`ELIXIR_ERL_OPTIONS` in files `rel/env.sh.eex` and `rel/env.bat.eex`.

## TODO

Expand Down
6 changes: 1 addition & 5 deletions coveralls.json
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
{
"skip_files": [
"lib/mix/tasks/nbpm.install.ex"
]
}
{}
84 changes: 18 additions & 66 deletions lib/mix/tasks/nbpm.get_port_number.ex
Original file line number Diff line number Diff line change
@@ -1,76 +1,28 @@
# TODO Без параметров - порт текущего приложения.
# С параметром - порт с названием параметра.

defmodule Mix.Tasks.Nbpm.GetPortNumber do
@shortdoc "Get port number for current mix project"

@moduledoc "Get port number for current mix project"
use Mix.Task

# defp init_script(script) do
# IO.puts("#{script} was not found.")
# run_init = IO.gets("Run `mix release.init`? [Y/n]")

# if run_init not in ["Y\n", "y\n"] do
# IO.puts("""
# Script was not found and you don't want to create it.
# Exiting.
# """)

# System.halt(0)
# end

# Mix.Task.run("release.init")
# end

# defp modify_script(script, contents, string, pattern) do
# exist = String.contains?(contents, string)

# if not exist do
# new_contents = Regex.replace(pattern, contents, "\\1\n" <> string <> "\n\n", global: false)
# :ok = File.write(script, new_contents)
# end
@shortdoc "Get port number"

# :ok
# end
@moduledoc """
Get port number.
# defp modify_linux_script do
# script = "rel/env.sh.eex"
Without arguments
`mix nbpm.get_port_number`
prints current project port.
# string =
# "export ELIXIR_ERL_OPTIONS=\"-start_epmd false -epmd_module Elixir.Nbpm $ELIXIR_ERL_OPTIONS\""
With one argument
`mix nbpm.get_port_number my_app`
prints port number for that string.
# pattern = ~r/(#!\/bin\/sh\n\n)/

# case File.read(script) do
# {:ok, contents} ->
# modify_script(script, contents, string, pattern)

# {:error, :enoent} ->
# :ok = init_script(script)
# modify_linux_script()
# end
# end

# defp modify_windows_script do
# script = "rel/env.bat.eex"

# string =
# "set ELIXIR_ERL_OPTIONS=-start_epmd false -epmd_module Elixir.Nbpm %ELIXIR_ERL_OPTIONS%"

# pattern = ~r/(@echo off\n)/

# case File.read(script) do
# {:ok, contents} ->
# modify_script(script, contents, string, pattern)
"""
use Mix.Task

# {:error, :enoent} ->
# :ok = init_script(script)
# modify_linux_script()
# end
# end
@impl Mix.Task
def run([]) do
app = Application.get_application(__MODULE__)
{:ok, port_number} = Nbpm.name_to_port("#{app}")
IO.puts(port_number)
end

@doc false
@impl Mix.Task
def run(args) do
{:ok, port_number} = Nbpm.name_to_port(hd(args))
IO.puts(port_number)
Expand Down
2 changes: 1 addition & 1 deletion lib/mix/tasks/nbpm.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule Mix.Tasks.Nbpm.Install do
end
end

@doc false
@impl Mix.Task
def run(_) do
:ok = modify_linux_script()
:ok = modify_windows_script()
Expand Down
6 changes: 5 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ defmodule Nbpm.MixProject do
[
app: :nbpm,
version: "0.2.1",
elixir: "~> 1.11",
elixir: "~> 1.12",
elixirc_paths: elixirc_paths(Mix.env()),
description: description(),
package: package(),
test_coverage: [tool: ExCoveralls],
Expand All @@ -30,6 +31,9 @@ defmodule Nbpm.MixProject do
]
end

defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]

defp description do
"Nbpm is a custom EPMD module that maps Erlang node names to network ports."
end
Expand Down
22 changes: 22 additions & 0 deletions test/nbpm.get_port_number_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Mix.Tasks.Nbpm.GetPortNumberTest do
use ExUnit.Case

import ExUnit.CaptureIO
import NbpmTest.Support

alias Mix.Tasks.Nbpm.GetPortNumber

def assert_node_to_listen_port({input, expected}) do
assert "#{expected}\n" == capture_io(fn -> GetPortNumber.run([input]) end)
end

describe "test get port number" do
test "get listen port by arg" do
Enum.each(nodes_ports(), &assert_node_to_listen_port/1)
end

test "get listen port by project app" do
assert "2937\n" == capture_io(fn -> GetPortNumber.run([]) end)
end
end
end
3 changes: 3 additions & 0 deletions test/nbpm.install_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Mix.Tasks.Nbpm.InstallTest do
use ExUnit.Case
end
27 changes: 2 additions & 25 deletions test/nbpm_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule NbpmTest do
use ExUnit.Case

import NbpmTest.Support

doctest Nbpm

def assert_name_to_port({input, expected}) do
Expand All @@ -15,31 +17,6 @@ defmodule NbpmTest do
assert {:ok, ^expected} = Nbpm.listen_port_please(input, ~c"127.0.0.1")
end

def nodes_ports do
[
{"88197my-random-name", 1_024},
{"18736my-random-name", 65_535},
{"my-random-name", 41_600},
{"my-random-name-12345", 12_345},
{"my-random-name12345", 12_345},
{"12345", 12_345},
{"rpc-random-name", 0},
{"rpc-random-name-12345", 0},
{"rpc-my-random-name12345", 0},
{"rpc-rem-my-random-name-12345", 0},
{"rpc-rem-my-random-name12345", 0},
{"rpc-12345", 0},
{"rpc12345", 12_345},
{"rem-random-sname", 0},
{"rem-random-sname-12345", 0},
{"rem-random-sname12345", 0},
{"rem-rpc-my-random-name-12345", 0},
{"rem-rpc-my-random-name12345", 0},
{"rem-12345", 0},
{"rem12345", 12_345}
]
end

describe "name_to_port/1" do
test "converts valid node names to ports" do
Enum.each(nodes_ports(), &assert_name_to_port/1)
Expand Down
27 changes: 27 additions & 0 deletions test/support/nbpm_test_support.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule NbpmTest.Support do
@moduledoc false
def nodes_ports do
[
{"88197my-random-name", 1_024},
{"18736my-random-name", 65_535},
{"my-random-name", 41_600},
{"my-random-name-12345", 12_345},
{"my-random-name12345", 12_345},
{"12345", 12_345},
{"rpc-random-name", 0},
{"rpc-random-name-12345", 0},
{"rpc-my-random-name12345", 0},
{"rpc-rem-my-random-name-12345", 0},
{"rpc-rem-my-random-name12345", 0},
{"rpc-12345", 0},
{"rpc12345", 12_345},
{"rem-random-sname", 0},
{"rem-random-sname-12345", 0},
{"rem-random-sname12345", 0},
{"rem-rpc-my-random-name-12345", 0},
{"rem-rpc-my-random-name12345", 0},
{"rem-12345", 0},
{"rem12345", 12_345}
]
end
end

0 comments on commit c835998

Please sign in to comment.