   """
   defmacro environment(name, do: block) do
     quote do
-      unless is_atom(unquote(name)) do
-        raise "environment name must be an atom! got #{inspect(unquote(name))}"
-      end
-
-      env = Environment.new(unquote(name))
-
-      Mix.Config.Agent.merge(
-        var!(config_agent, Mix.Releases.Config),
-        environments: [{unquote(name), env}]
-      )
-
-      var!(current_env, Mix.Releases.Config) = unquote(name)
+      name = unquote(name)
+
+      unless is_atom(name) do
+        raise "environment name must be an atom! got #{inspect(name)}"
+      end
+
+      env = Environment.new(name)
+      conf = var!(config, Mix.Releases.Config)
+      var!(config, Mix.Releases.Config) = put_in(conf, [:environments, name], env)
+      var!(current_env, Mix.Releases.Config) = name
       unquote(block)
       var!(current_env, Mix.Releases.Config) = nil
     end

          :ok
      end

      config = Mix.Config.Agent.get(var!(config_agent, Mix.Releases.Config))

      cond do
        current_env != nil ->
          env = get_in(config, [:environments, current_env])
          profile = env.profile
          plugins = profile.plugins ++ [{unquote(name), unquote(opts)}]
          env = %{env | :profile => %{profile | :plugins => plugins}}

          Mix.Config.Agent.merge(
            var!(config_agent, Mix.Releases.Config),
            environments: [{current_env, env}]
          )

        current_rel != nil ->
          rel = get_in(config, [:releases, current_rel])
          profile = rel.profile
          plugins = profile.plugins ++ [{unquote(name), unquote(opts)}]
          rel = %{rel | :profile => %{profile | :plugins => plugins}}

          Mix.Config.Agent.merge(
            var!(config_agent, Mix.Releases.Config),
            releases: [{current_rel, rel}]
          )
      end
    end
  end


  """
  defmacro current_version(app) do
    quote do
      app = unquote(app)

      unless is_atom(app) do
        raise "current_version argument must be an atom! got #{inspect(app)}"
      end

      Application.load(app)

      case Application.spec(app) do
        nil ->
          raise "could not get current version of #{app}, app could not be loaded"

        spec ->
          "#{Keyword.get(spec, :vsn)}"
      end
    end
  end
