|
| 1 | +defmodule Mix.Tasks.Membrane.Demo do |
| 2 | + @shortdoc "Download Membrane demos and examples" |
| 3 | + @moduledoc """ |
| 4 | + Download Membrane demos and examples. Requires `git` installed. |
| 5 | +
|
| 6 | + $ mix membrane.demo [-a] [-l] [-d <repo_dir>] [<demos> ...] |
| 7 | +
|
| 8 | + ## Options |
| 9 | + * `-l, --list` - List all demos available and their brief descriptions. |
| 10 | + * `-a, --all` - Pull the repository with all demos. |
| 11 | + * `-d, --directory` - Specify a directory where the demos should be placed in. By default they're placed in the current working directory |
| 12 | + """ |
| 13 | + use Mix.Task |
| 14 | + |
| 15 | + @switches [ |
| 16 | + list: :boolean, |
| 17 | + all: :boolean, |
| 18 | + directory: :string |
| 19 | + ] |
| 20 | + |
| 21 | + @aliases [ |
| 22 | + l: :list, |
| 23 | + a: :all, |
| 24 | + d: :directory |
| 25 | + ] |
| 26 | + |
| 27 | + @demos_search_list [".", "livebooks"] |
| 28 | + |
| 29 | + @demos_readme_url "https://raw.githubusercontent.com/membraneframework/membrane_demo/refs/heads/master/README.md" |
| 30 | + @demos_clone_url "https://github.com/membraneframework/membrane_demo.git" |
| 31 | + |
| 32 | + @impl true |
| 33 | + def run([]) do |
| 34 | + Mix.Tasks.Help.run(["membrane.demo"]) |
| 35 | + end |
| 36 | + |
| 37 | + def run(argv) do |
| 38 | + {opts, demos_names} = OptionParser.parse!(argv, aliases: @aliases, switches: @switches) |
| 39 | + |
| 40 | + if opts[:list] do |
| 41 | + list_available_demos() |
| 42 | + end |
| 43 | + |
| 44 | + target_dir = Path.expand(opts[:directory] || ".") |
| 45 | + |
| 46 | + cond do |
| 47 | + opts[:all] -> copy_all_demos(target_dir) |
| 48 | + demos_names != [] -> copy_specific_demos(target_dir, demos_names) |
| 49 | + true -> :ok |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + defp list_available_demos() do |
| 54 | + {:ok, _apps} = Application.ensure_all_started(:req) |
| 55 | + response = Req.get!(@demos_readme_url) |
| 56 | + |
| 57 | + demos_info = |
| 58 | + response.body |
| 59 | + |> String.split("\n") |
| 60 | + |> Enum.map(&Regex.named_captures(~r/^- \[(?<name>.*?)\]\(.*?\) - (?<description>.*)/, &1)) |
| 61 | + |> Enum.filter(&(&1 != nil)) |
| 62 | + |> Enum.map( |
| 63 | + &Map.update!( |
| 64 | + &1, |
| 65 | + "description", |
| 66 | + fn description -> Regex.replace(~r/\[(.*?)\]\(.*?\)/, description, "\\1") end |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + max_name_length = |
| 71 | + demos_info |
| 72 | + |> Enum.map(&String.length(&1["name"])) |
| 73 | + |> Enum.max() |
| 74 | + |
| 75 | + demos_table = |
| 76 | + demos_info |
| 77 | + |> Enum.map_join("\n", fn %{"name" => name, "description" => description} -> |
| 78 | + dots_line = String.duplicate(".", max_name_length - String.length(name) + 3) |
| 79 | + " | #{name} #{dots_line} | #{description}" |
| 80 | + end) |
| 81 | + |
| 82 | + Mix.shell().info(demos_table) |
| 83 | + end |
| 84 | + |
| 85 | + defp copy_all_demos(target_dir) do |
| 86 | + repo_dir = Path.join(target_dir, "membrane_demo") |
| 87 | + execute_git_command(["clone", "-q", "--depth", "1", @demos_clone_url, repo_dir]) |
| 88 | + end |
| 89 | + |
| 90 | + defp copy_specific_demos(target_dir, demos_names) do |
| 91 | + repo_dir = Path.join(target_dir, "membrane_demo") |
| 92 | + execute_git_command(["clone", "-q", "--depth", "1", "-n", @demos_clone_url, repo_dir]) |
| 93 | + |
| 94 | + Enum.each(demos_names, fn demo_name -> |
| 95 | + case copy_demo(target_dir, demo_name) do |
| 96 | + :error -> |
| 97 | + Mix.shell().error( |
| 98 | + "No demo named #{demo_name}, run this task with -l to list all available demos" |
| 99 | + ) |
| 100 | + |
| 101 | + :ok -> |
| 102 | + Mix.shell().info("`#{demo_name}` demo created at #{Path.join(target_dir, demo_name)}") |
| 103 | + end |
| 104 | + end) |
| 105 | + |
| 106 | + File.rm_rf!(repo_dir) |
| 107 | + end |
| 108 | + |
| 109 | + defp copy_demo(target_dir, demo_name) do |
| 110 | + repo_dir = Path.join(target_dir, "membrane_demo") |
| 111 | + |
| 112 | + Enum.reduce_while(@demos_search_list, :error, fn demo_dir, _status -> |
| 113 | + demo_path = Path.join([repo_dir, demo_dir, demo_name]) |
| 114 | + |
| 115 | + execute_git_command(["sparse-checkout", "set", Path.join(demo_dir, demo_name)], repo_dir) |
| 116 | + execute_git_command(["checkout"], repo_dir) |
| 117 | + |
| 118 | + case File.cp_r(demo_path, Path.join(target_dir, Path.basename(demo_path))) do |
| 119 | + {:ok, _files} -> {:halt, :ok} |
| 120 | + {:error, :enoent, _dir} -> {:cont, :error} |
| 121 | + end |
| 122 | + end) |
| 123 | + end |
| 124 | + |
| 125 | + defp execute_git_command(argv, dir \\ ".") do |
| 126 | + case System.cmd("git", argv, stderr_to_stdout: true, cd: dir) do |
| 127 | + {_output, 0} -> |
| 128 | + :ok |
| 129 | + |
| 130 | + {output, exit_code} -> |
| 131 | + Mix.shell().error(""" |
| 132 | + Git command `git #{Enum.join(argv, " ")} exited with code #{exit_code}. Output: |
| 133 | + #{output} |
| 134 | + """) |
| 135 | + end |
| 136 | + end |
| 137 | +end |
0 commit comments