-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow generating versions for a project
- Loading branch information
Showing
10 changed files
with
269 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
elixir 1.13.3 | ||
erlang 24.3.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule Mix.Tasks.Scapa.Gen.Versions do | ||
@moduledoc false | ||
|
||
@doc false | ||
def run(_argv) do | ||
for {path, content} <- Scapa.CLI.generate_versions(), do: File.write(path, content) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule Scapa.CLI do | ||
@moduledoc false | ||
|
||
alias Scapa.FunctionDefinition | ||
alias Scapa.VersionCalculator | ||
|
||
@doc """ | ||
Receives a pattern for files to look into and generates versions for those | ||
""" | ||
def generate_versions(files_pattern \\ "lib/**/*.ex") do | ||
files_to_versionate(files_pattern) | ||
|> Enum.map(&{&1, add_versions_to_file(&1)}) | ||
|> Enum.filter(&elem(&1, 1)) | ||
end | ||
|
||
defp files_to_versionate(files_pattern) do | ||
files_pattern | ||
|> Path.wildcard() | ||
|> Enum.map(&Path.expand/1) | ||
|> MapSet.new() | ||
end | ||
|
||
defp add_versions_to_file(file_path) do | ||
file_content = File.read!(file_path) | ||
|
||
case funtions_to_versionate(file_content) do | ||
[] -> | ||
nil | ||
|
||
function_definitions -> | ||
Enum.reduce(function_definitions, file_content, fn function_definition, content -> | ||
Scapa.Code.upsert_doc_version( | ||
content, | ||
function_definition, | ||
VersionCalculator.calculate(function_definition) | ||
) | ||
end) | ||
end | ||
end | ||
|
||
defp funtions_to_versionate(file_contents) do | ||
file_contents | ||
|> Code.string_to_quoted!() | ||
|> Scapa.Code.defined_modules() | ||
|> Enum.flat_map(&Scapa.Code.functions_with_doc({:module, &1, file_contents})) | ||
|> Enum.sort_by(&FunctionDefinition.line_number/1) | ||
|> Enum.reverse() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
defmodule Scapa.CLITest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Scapa.CLI | ||
|
||
describe "generate_versions/1" do | ||
test "returns the file location and new source code wih versions" do | ||
[module_with_doc, module_with_hidden_doc] = CLI.generate_versions("test/support/*.ex") | ||
|
||
assert String.ends_with?(elem(module_with_doc, 0), "/support/module_with_doc.ex") | ||
|
||
assert elem(module_with_doc, 1) == """ | ||
defmodule Scapa.ModuleWithDoc do | ||
@moduledoc \""" | ||
Test module used to test the returned function definitions and | ||
the corresponding version. | ||
\""" | ||
@doc "Public with doc" | ||
@doc version: "75335224" | ||
def public_with_doc, do: nil | ||
@doc "Public with version" | ||
@doc version: "27952351" | ||
def public_with_version, do: nil | ||
@doc "Multiple def" | ||
@doc version: "30685952" | ||
def multiple_def(1), do: 2 | ||
def multiple_def("2"), do: 4 | ||
@doc "Multiple def with default" | ||
@doc version: "119275990" | ||
def multiple_def_with_default(num \\\\ 42) | ||
def multiple_def_with_default(1), do: 2 | ||
def multiple_def_with_default(2), do: 4 | ||
def public_no_doc, do: nil | ||
defp private_fun, do: nil | ||
end | ||
""" | ||
|
||
assert String.ends_with?( | ||
elem(module_with_hidden_doc, 0), | ||
"/support/module_with_hidden_doc.ex" | ||
) | ||
|
||
assert elem(module_with_hidden_doc, 1) == """ | ||
defmodule Scapa.ModuleWithHiddenDoc do | ||
@moduledoc false | ||
@doc "Public with doc" | ||
@doc version: "67474296" | ||
def public_with_doc, do: nil | ||
end | ||
""" | ||
end | ||
end | ||
end |
Oops, something went wrong.