Last active
January 9, 2020 20:33
-
-
Save jakeprem/70c49bbd6386a8276410b6eff4d35a4a to your computer and use it in GitHub Desktop.
Checks whether a tag already exists for the currently set version in mix.exs of app that have changed
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
defmodule Differ do | |
require Logger | |
def cli do | |
changed_app_versions = get_changed_apps() |> get_app_versions() | |
tags = get_tags() | |
changed_app_versions | |
|> Enum.filter(&(&1 in tags)) | |
|> Enum.map(fn {app, vsn} -> | |
"A tag already exists for #{String.capitalize(app)} version #{ | |
Enum.join([vsn.major, vsn.minor, vsn.patch], ".") | |
}. Please update 'apps/#{app}/mix.exs'." | |
end) | |
|> Enum.each(&Logger.warn/1) | |
end | |
def get_changed_apps do | |
with {raw, 0} <- get_raw_file_diff() do | |
extract_apps(raw) | |
else | |
error -> Logger.error(inspect(error)) | |
end | |
end | |
def get_app_versions(apps) do | |
Application.ensure_all_started(:mix) | |
Enum.map(apps, &get_app_version/1) | |
end | |
def get_tags do | |
System.cmd("git", ["tag"]) | |
|> elem(0) | |
|> String.split("\n") | |
|> Enum.map(&String.split(&1, "@")) | |
|> Enum.reject(&(length(&1) != 2)) | |
|> Enum.map(fn [app, vsn] -> {app, Version.parse!(vsn)} end) | |
end | |
defp get_app_version(app) do | |
with app_path = "apps/#{app}/mix.exs", | |
true <- File.exists?(app_path), | |
{{:module, module, _, _}, _} <- Code.eval_file(app_path) do | |
{app, module.project()[:version] |> Version.parse!()} | |
end | |
end | |
defp get_raw_file_diff do | |
System.cmd("git", ["diff", "--name-status", "master"]) | |
end | |
defp extract_apps(raw) do | |
raw | |
|> String.split(["\n", "\t"]) | |
|> Enum.map(&String.split(&1, "/")) | |
|> Enum.map(fn | |
["apps", app | _rest] -> app | |
_other -> [] | |
end) | |
|> List.flatten() | |
|> MapSet.new() | |
|> MapSet.to_list() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment