|
| 1 | +class Teslamate < Formula |
| 2 | + desc "Self-hosted data logger for your Tesla" |
| 3 | + homepage "https://docs.teslamate.org" |
| 4 | + url "https://github.com/teslamate-org/teslamate/archive/refs/tags/v2.0.0.tar.gz" |
| 5 | + sha256 "fe29dab0dd0b96bafe003b063a7f6f95338d64b26c7187a68bacd2023e423f82" |
| 6 | + license "MIT" |
| 7 | + |
| 8 | + depends_on "node" => :build |
| 9 | + depends_on "postgresql@17" => :test |
| 10 | + depends_on "elixir" |
| 11 | + depends_on "erlang" |
| 12 | + depends_on "openssl@3" |
| 13 | + |
| 14 | + uses_from_macos "ncurses" |
| 15 | + uses_from_macos "zlib" |
| 16 | + |
| 17 | + def install |
| 18 | + # See https://docs.teslamate.org/docs/installation/debian/ |
| 19 | + system "mix", "local.hex", "--force" |
| 20 | + system "mix", "local.rebar", "--force" |
| 21 | + system "mix", "deps.get", "--only", "prod" |
| 22 | + system "npm", "install", "--prefix", "./assets", *std_npm_args(prefix: false) |
| 23 | + system "npm", "run", "deploy", "--prefix", "./assets" |
| 24 | + |
| 25 | + with_env("MIX_ENV" => "prod") do |
| 26 | + system "mix", "do", "phx.digest,", "release", "--overwrite" |
| 27 | + end |
| 28 | + |
| 29 | + touch buildpath/"teslamate.env" |
| 30 | + etc.install "teslamate.env" |
| 31 | + libexec.install Dir["_build/prod/rel/teslamate/*"] |
| 32 | + bin.install_symlink Dir["#{libexec}/bin/teslamate"] |
| 33 | + |
| 34 | + # Corresponds to https://github.com/teslamate-org/teslamate/blob/main/entrypoint.sh |
| 35 | + (bin/"teslamate_brew_services").write <<~EOS |
| 36 | + #!/bin/bash |
| 37 | + set -e |
| 38 | + source #{etc}/teslamate.env |
| 39 | + #{bin}/teslamate eval "TeslaMate.Release.migrate" |
| 40 | + exec #{bin}/teslamate start |
| 41 | + EOS |
| 42 | + end |
| 43 | + |
| 44 | + service do |
| 45 | + run opt_bin/"teslamate_brew_services" |
| 46 | + keep_alive true |
| 47 | + log_path var/"log/teslamate.log" |
| 48 | + error_log_path var/"log/teslamate.log" |
| 49 | + working_dir var |
| 50 | + end |
| 51 | + |
| 52 | + test do |
| 53 | + ENV["LC_ALL"] = "C" |
| 54 | + |
| 55 | + pg_port = free_port |
| 56 | + pg_bin = Formula["postgresql@17"].opt_bin |
| 57 | + pg_ctl = pg_bin/"pg_ctl" |
| 58 | + datadir = testpath/"postgres" |
| 59 | + system pg_ctl, "init", "-D", datadir |
| 60 | + |
| 61 | + (datadir/"postgresql.conf").write <<~EOS, mode: "a+" |
| 62 | + port = #{pg_port} |
| 63 | + unix_socket_directories = '#{datadir}' |
| 64 | + EOS |
| 65 | + |
| 66 | + system pg_ctl, "start", "-D", datadir, "-l", testpath/"postgres.log" |
| 67 | + begin |
| 68 | + system pg_bin/"createdb", "-h", datadir, "-p", pg_port.to_s, "teslamate" |
| 69 | + system pg_bin/"createuser", "-h", datadir, "-p", pg_port.to_s, "-s", "teslamate" |
| 70 | + |
| 71 | + # Run Teslamate with the test database |
| 72 | + ENV["DATABASE_USER"] = "teslamate" |
| 73 | + ENV["DATABASE_PASS"] = "" |
| 74 | + ENV["DATABASE_NAME"] = "teslamate" |
| 75 | + ENV["DATABASE_HOST"] = "127.0.0.1" |
| 76 | + ENV["DATABASE_PORT"] = pg_port.to_s |
| 77 | + ENV["DISABLE_MQTT"] = "true" |
| 78 | + log_file = testpath/"teslamate_test.log" |
| 79 | + File.open(log_file, "w") do |file| |
| 80 | + pid = spawn(opt_bin/"teslamate_brew_services", out: file, err: file) |
| 81 | + sleep 20 |
| 82 | + system opt_bin/"teslamate", "stop" |
| 83 | + Process.kill("KILL", pid) |
| 84 | + Process.wait(pid) |
| 85 | + end |
| 86 | + output = log_file.read |
| 87 | + assert_match "Access TeslaMateWeb.Endpoint at http://localhost", output |
| 88 | + ensure |
| 89 | + system pg_ctl, "stop", "-D", datadir |
| 90 | + end |
| 91 | + end |
| 92 | +end |
0 commit comments