From c160d55e01c2d133492965c566e2d03f71cc392c Mon Sep 17 00:00:00 2001 From: James Harton Date: Mon, 28 Dec 2020 13:18:09 +1300 Subject: [PATCH] feat(project_info_dotenv_format): Add the `dotenv` format for project info output. This is handy because you can use `dotenv` files to share outputs between jobs on Gitlab CI. --- lib/mix/tasks/git_ops.project_info.ex | 14 ++++++++++++-- test/project_info_test.exs | 17 +++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/mix/tasks/git_ops.project_info.ex b/lib/mix/tasks/git_ops.project_info.ex index 96c1c5f..badd792 100644 --- a/lib/mix/tasks/git_ops.project_info.ex +++ b/lib/mix/tasks/git_ops.project_info.ex @@ -13,7 +13,7 @@ defmodule Mix.Tasks.GitOps.ProjectInfo do ## Switches: * `--format|-f` selects the output format. Currently suported output formats - are `json`, `toml`, `github-actions` and `shell`. + are `json`, `toml`, `github-actions`, `shell` and `dotenv`. """ alias GitOps.Config @@ -30,6 +30,7 @@ defmodule Mix.Tasks.GitOps.ProjectInfo do opts |> Keyword.get(:format) + |> String.downcase() |> case do "toml" -> format_toml(project, opts) @@ -43,8 +44,11 @@ defmodule Mix.Tasks.GitOps.ProjectInfo do "shell" -> format_shell(project, opts) + "dotenv" -> + format_dotenv(project, opts) + format -> - raise "Invalid format `#{inspect(format)}`. Valid formats are `json`, `toml`, `github-actions` and `shell`." + raise "Invalid format `#{inspect(format)}`. Valid formats are `json`, `toml`, `github-actions`, `shell` and `dotenv`." end end @@ -81,6 +85,12 @@ defmodule Mix.Tasks.GitOps.ProjectInfo do IO.write(~s|export APP_NAME="#{name}"\nexport APP_VERSION="#{version}"\n|) end + defp format_dotenv(project, _opts) do + {name, version} = extract_name_and_version_from_project(project) + + IO.write(~s|APP_NAME="#{name}"\nAPP_VERSION="#{version}"\n|) + end + defp extract_name_and_version_from_project(project) do %{app: name, version: version} = project diff --git a/test/project_info_test.exs b/test/project_info_test.exs index e472800..6e02f93 100644 --- a/test/project_info_test.exs +++ b/test/project_info_test.exs @@ -47,7 +47,7 @@ defmodule GitOps.Mix.Tasks.Test.ProjectInfoTest do end end - describe "Github Actions actual format" do + describe "Github Actions format" do test "it is correctly formatted", %{name: name, version: version} do actual = run(["--format", "github-actions"]) @@ -60,7 +60,7 @@ defmodule GitOps.Mix.Tasks.Test.ProjectInfoTest do end end - describe "Shell actual format" do + describe "Shell format" do test "it is correctly formatted", %{name: name, version: version} do actual = run(["--format", "shell"]) @@ -73,6 +73,19 @@ defmodule GitOps.Mix.Tasks.Test.ProjectInfoTest do end end + describe "Dotenv format" do + test "it is correctly formatted", %{name: name, version: version} do + actual = run(["--format", "dotenv"]) + + expected = """ + APP_NAME="#{name}" + APP_VERSION="#{version}" + """ + + assert actual == expected + end + end + def run(args) do capture_io(fn -> ProjectInfo.run(args)