get-lastest-tag.escript 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env escript
  2. %% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
  3. %% ex: ft=erlang ts=4 sw=4 et
  4. %% -------------------------------------------------------------------
  5. %%
  6. %% nodetool: Helper Script for interacting with live nodes
  7. %%
  8. %% -------------------------------------------------------------------
  9. main(_Args) ->
  10. io:format(getRef()).
  11. comparingFun([C1|R1], [C2|R2]) when is_list(C1), is_list(C2);
  12. is_integer(C1), is_integer(C2) -> C1 < C2 orelse comparingFun(R1, R2);
  13. comparingFun([C1|R1], [C2|R2]) when is_integer(C1), is_list(C2) -> comparingFun(R1, R2);
  14. comparingFun([C1|_R1], [C2|_R2]) when is_list(C1), is_integer(C2) -> true;
  15. comparingFun(_, _) -> false.
  16. sortFun(T1, T2) ->
  17. C = fun(T) ->
  18. [case catch list_to_integer(E) of
  19. I when is_integer(I) -> I;
  20. _ -> E
  21. end || E <- re:split(string:sub_string(T, 2), "[.-]", [{return, list}])]
  22. end,
  23. comparingFun(C(T1), C(T2)).
  24. latestTag(BranchName) ->
  25. Tag = os:cmd("git describe --abbrev=1 --tags --always") -- "\n",
  26. LatestTagCommitId = os:cmd(io_lib:format("git rev-parse --short ~s", [Tag])) -- "\n",
  27. case string:tokens(os:cmd(io_lib:format("git tag -l \"v*\" --points-at ~s", [LatestTagCommitId])), "\n") of
  28. [] -> BranchName ++ "-" ++ LatestTagCommitId;
  29. Tags ->
  30. lists:last(lists:sort(fun(T1, T2) -> sortFun(T1, T2) end, Tags))
  31. end.
  32. branch() ->
  33. case os:getenv("GITHUB_RUN_ID") of
  34. false -> os:cmd("git branch | grep -e '^*' | cut -d' ' -f 2") -- "\n";
  35. _ -> re:replace(os:getenv("GITHUB_REF"), "^refs/heads/|^refs/tags/", "", [global, {return ,list}])
  36. end.
  37. getRef() ->
  38. case re:run(branch(), "master|^dev/|^hotfix/", [{capture, none}]) of
  39. match -> branch();
  40. _ -> latestTag(branch())
  41. end.