defmodule Meld do @moduledoc """ Meld is a command-line utility (and library) that makes it easy to provide a mix task as a global command, enabling __real__ CLIs. ## Usage Meld.link ".", "mix_task" Meld.unlink "mix_task" Where `File.cwd()` is a directory containing an Elixir app with a mix task that can be run. 'mix_task' is the mix task name, that means that it can be run with 'mix mix_task'. Meld will create a shell script in its directory at ~/.meld. This shell script will cd into the specified directory and run the mix task. By adding the Meld directory to the global PATH, a simulated global executable is achieved. """ alias Meld.Util @doc """ Links a mix task to a newly created shell script in `~/.meld`. ## Usage Meld.link ".", "mix_task", "script_name" ## Arguments * `dir` - A directory containing a mix project. * `mix_task` - The name of the mix task to link. * `script_name` - Custom name for the generated script. """ def link(dir, mix_task, script_name) do Util.check_dir_not_exists "Please run 'meld setup' first, aborting." if !File.exists? dir do raise "The specified project directory does not exist, aborting." end content = " # Generated by Meld (http://github.com/conflate/meld) cd #{Path.expand(dir)} mix #{mix_task} " filename = Path.basename(Path.expand(dir)) path = if script_name do Path.join([Util.path, script_name]) else Path.join([Util.path, "#{filename}_#{mix_task}"]) end File.write! path, content {_msg, _result} = System.cmd "chmod", ["755", path], cd: Util.path end @doc """ Removes a script from the ~/.meld directory. ## Usage Meld.unlink "script_name" ## Arguments * `script_name` - The filename of the script to remove. """ def unlink(script_name) do filepath = Path.join(Util.path, script_name) if !File.exists? filepath do raise "The specified script doesn't exist." exit "" end File.rm! filepath end end