Add task for build a production image

This commit is contained in:
connor rigby 2018-01-04 11:12:11 -08:00
parent 9594e84f9d
commit c26743612d
3 changed files with 62 additions and 44 deletions

View file

@ -37,6 +37,43 @@ defmodule Mix.Tasks.Farmbot.Env do
to_string(Farmbot.Project.env())
end
@doc false
def format_date_time(%{ctime: {{yr, m, day}, {hr, min, sec}}}) do
dt =
%DateTime{
hour: hr,
year: yr,
month: m,
day: day,
minute: min,
second: sec,
time_zone: "Etc/UTC",
zone_abbr: "UTC",
std_offset: 0,
utc_offset: 0
}
|> Timex.local()
"#{dt.year}-#{pad(dt.month)}-#{pad(dt.day)}_#{pad(dt.hour)}#{pad(dt.minute)}"
end
defp pad(int) do
if int < 10, do: "0#{int}", else: "#{int}"
end
@doc false
def build_comment(time, comment) do
"""
*New Farmbot Firmware!*
> *_Env_*: `#{env()}`
> *_Target_*: `#{target()}`
> *_Version_*: `#{mix_config(:version)}`
> *_Commit_*: `#{mix_config(:commit)}`
> *_Time_*: `#{time}`
#{String.trim(comment)}
"""
end
@doc false
def slack_token do
System.get_env("SLACK_TOKEN") || Mix.raise "No $SLACK_TOKEN environment variable."

View file

@ -0,0 +1,25 @@
defmodule Mix.Tasks.Farmbot.ProdImage do
@moduledoc "Build a production firmware image file"
@shortdoc @moduledoc
use Mix.Task
import Mix.Tasks.Farmbot.Env
def run(opts) do
{keywords, _, _} =
opts |> OptionParser.parse(switches: [signed: :boolean])
signed? = Keyword.get(keywords, :signed, false)
Application.ensure_all_started(:timex)
fw_file_to_upload = if signed?, do: signed_fw_file(), else: fw_file()
time = format_date_time(File.stat!(fw_file_to_upload))
filename =
"#{mix_config(:app)}-#{target()}-#{env()}-#{mix_config(:commit)}#{
if signed?, do: "-signed-", else: "-"
}#{time}.img"
Mix.shell().info(build_comment(time, ""))
Mix.Tasks.Firmware.Image.run([filename])
end
end

View file

@ -54,51 +54,7 @@ defmodule Mix.Tasks.Farmbot.Firmware.Slack do
end
end
defp build_comment(time, comment) do
# %{
# env: env(),
# target: target(),
# version: mix_config(:version),
# commit: mix_config(:commit),
# time: time,
# comment: comment
# } |> Poison.encode!(pretty: true)
"""
*New Farmbot Firmware!*
> *_Env_*: `#{env()}`
> *_Target_*: `#{target()}`
> *_Version_*: `#{mix_config(:version)}`
> *_Commit_*: `#{mix_config(:commit)}`
> *_Time_*: `#{time}`
#{String.trim(comment)}
"""
end
defp error(msg) do
Mix.raise("Upload failed! " <> msg)
end
defp format_date_time(%{ctime: {{yr, m, day}, {hr, min, sec}}}) do
dt =
%DateTime{
hour: hr,
year: yr,
month: m,
day: day,
minute: min,
second: sec,
time_zone: "Etc/UTC",
zone_abbr: "UTC",
std_offset: 0,
utc_offset: 0
}
|> Timex.local()
"#{dt.year}-#{pad(dt.month)}-#{pad(dt.day)}_#{pad(dt.hour)}#{pad(dt.minute)}"
end
defp pad(int) do
if int < 10, do: "0#{int}", else: "#{int}"
end
end