Skip to content

Commit

Permalink
https://github.com/bonfire-networks/bonfire-app/issues/849
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Feb 5, 2025
1 parent 8a71990 commit eefd176
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 27 deletions.
8 changes: 5 additions & 3 deletions lib/mix/mixer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -508,12 +508,14 @@ if not Code.ensure_loaded?(Bonfire.Mixer) do
def dep_paths(dep, extra) when is_binary(extra) do
dep_path =
dep_path(dep, true)

# |> IO.inspect()
|> IO.inspect()

if dep_path do
# path =
Path.join(dep_path, extra) |> Path.wildcard()
Path.join(dep_path, extra)
|> IO.inspect()
|> Path.wildcard()

# if path, do: [path], else: []
else
[]
Expand Down
17 changes: 17 additions & 0 deletions lib/mix_tasks/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ defmodule Bonfire.Common.Mix.Tasks.Helpers do
end
end

def list_extensions do
extensions_pattern =
Bonfire.Common.Utils.maybe_apply(Bonfire.Mixer, :multirepo_prefixes, [],
fallback_return: []
) ++ ["bonfire"] ++ Bonfire.Common.Config.get([:extensions_pattern], [])

(Bonfire.Common.Utils.maybe_apply(Bonfire.Mixer, :deps_tree_flat, [], fallback_return: nil) ||
Bonfire.Common.Extensions.loaded_deps_names())
|> IO.inspect(label: "all deps")
|> Enum.map(&to_string/1)
|> Enum.filter(fn
#  FIXME: make this configurable
"bonfire_" <> _ -> true
name -> String.starts_with?(name, extensions_pattern)
end)
end

def igniter_path_for_module(
igniter,
module_name,
Expand Down
99 changes: 99 additions & 0 deletions lib/mix_tasks/install/copy_configs.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
defmodule Mix.Tasks.Bonfire.Install.CopyConfigs do
use Igniter.Mix.Task
alias Bonfire.Common.Mix.Tasks.Helpers

# TODO: turn into an escript so it can be run without compiling the whole app

@shortdoc "Copies configs for the extension into the parent app"
@doc """
Usage:
`just mix bonfire.install.copy_migrations my_extension`
or
`just mix bonfire.install.copy_migrations`
NOTE: if you don't specify what extension(s) to include, it will automatically include all extensions which:
- start with `bonfire_`
- and are included in the top-level app (not dependencies of dependencies)
Optional args:
--force (to not ask for confirmation before copying, or to overwrite existing config files - only applies when not using Igniter)
--from config (to change the source repo paths, relative to each extension path)
--to config/ (to change the target repo path (defaults to current flavour's configs) relative to working directory)
"""

@switches [from: :string, to: :string, force: :boolean]
@default_config_path "config"

def igniter(igniter, args) do
IO.inspect(args, label: "Args")

case OptionParser.parse(args, switches: @switches) do
{opts, [], _} ->
# None specified, will simply try copying any available extension.
copy_all(igniter, opts)

{opts, extensions, _} ->
copy_for_extensions(igniter, extensions, opts)
end
end

def copy_all(igniter, opts) do
extensions = Helpers.list_extensions()

copy_for_extensions(igniter, extensions, opts)
end

def copy_for_extensions(igniter, extensions, opts) do
IO.inspect(opts, label: "Options")

path = opts[:to] || Path.expand(@default_config_path, Bonfire.Mixer.flavour_path())

dest_path =
Path.expand(path, File.cwd!())
|> IO.inspect(label: "to path")

from = opts[:from] || @default_config_path

extension_paths =
extensions
|> IO.inspect(label: "deps to include")
|> Enum.flat_map(&Bonfire.Mixer.dep_paths(&1, Path.join(from, "#{&1}.exs")))
|> IO.inspect(label: "paths to copy")

if igniter do
Igniter.include_glob(igniter, Path.join(dest_path, "*"))
|> Helpers.igniter_copy(extension_paths, dest_path, opts)
else
simple_copy(extension_paths, dest_path, opts)
end
end

def simple_copy(extension_paths, dest_path, opts) when is_list(extension_paths),
do: Enum.each(extension_paths, &simple_copy(&1, dest_path, opts))

def simple_copy(source_path, dest_path, opts) do
source_path
|> IO.inspect()

if opts[:force] do
IO.puts(
"\nCopying the following config from #{source_path} to #{dest_path}: \n#{inspect(File.ls!(source_path))}\n"
)

with {:error, reason, file} <- File.cp_r(source_path, dest_path) do
IO.puts("\nERROR: Could not copy #{file} : #{inspect(reason)}\n")
end
else
if IO.gets(
"Will copy the following config from #{source_path} to #{dest_path}: \n#{inspect(File.ls!(source_path))}\n\nType y to confirm: "
) == "y\n" do
File.cp_r(source_path, dest_path,
on_conflict: fn source, destination ->
IO.gets("Overwriting #{destination} by #{source}. Type y to confirm. ") == "y\n"
end
)
end
end
end
end
25 changes: 3 additions & 22 deletions lib/mix_tasks/install/copy_migrations.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
defmodule Mix.Tasks.Bonfire.Install.CopyMigrations do
use Igniter.Mix.Task
alias Bonfire.Common.Mix.Tasks.Helpers
# import Macro, only: [camelize: 1, underscore: 1]
# import Mix.Generator
# import Mix.Ecto, except: [migrations_path: 1]

# TODO: turn into an escript so it can be run without compiling the whole app

@shortdoc "Generates migrations for the extension"
@shortdoc "Copies migrations for the extension into the parent app"
@doc """
Usage:
`just mix bonfire.install.copy_migrations my_extension`
Expand All @@ -22,7 +17,7 @@ defmodule Mix.Tasks.Bonfire.Install.CopyMigrations do
--force (to not ask for confirmation before copying, or to overwrite existing migration files - only applies when not using Igniter)
--from priv/repo/migrations (to change the source repo paths, relative to each extension path)
--to priv/repo/ (to change the target repo path (defaults to current flavour's migrations) relative to working directory)
--to repo/ (to change the target repo path (defaults to current flavour's migrations) relative to working directory)
"""

@switches [from: :string, to: :string, force: :boolean]
Expand All @@ -43,21 +38,7 @@ defmodule Mix.Tasks.Bonfire.Install.CopyMigrations do
end

def copy_all(igniter, opts) do
extensions_pattern =
Bonfire.Common.Utils.maybe_apply(Bonfire.Mixer, :multirepo_prefixes, [],
fallback_return: []
) ++ ["bonfire"] ++ Bonfire.Common.Config.get([:extensions_pattern], [])

extensions =
(Bonfire.Common.Utils.maybe_apply(Bonfire.Mixer, :deps_tree_flat, [], fallback_return: nil) ||
Bonfire.Common.Extensions.loaded_deps_names())
|> IO.inspect(label: "all deps")
|> Enum.map(&to_string/1)
|> Enum.filter(fn
#  FIXME: make this configurable
"bonfire_" <> _ -> true
name -> String.starts_with?(name, extensions_pattern)
end)
extensions = Helpers.list_extensions()

copy_for_extensions(igniter, extensions, opts)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mix_tasks/install/copy_migrations.old.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Mix.Tasks.Bonfire.Extension.CopyMigrations do

# TODO: turn into an escript so it can be run without compiling the whole app?

@shortdoc "Generates migrations for the extension"
@shortdoc "Copies migrations for the extension into the parent app"
@doc """
Usage:
`just mix bonfire.extension.copy_migrations my_extension`
Expand All @@ -21,7 +21,7 @@ defmodule Mix.Tasks.Bonfire.Extension.CopyMigrations do
--force (to not ask for confirmation before copying, or to overwrite existing migration files)
--from priv/repo/migrations (to change the source repo paths, relative to each extension path)
--to priv/repo/migrations (to change the target repo path (defaults to current flavour's migrations) relative to working directory)
--to repo/migrations (to change the target repo path (defaults to current flavour's migrations) relative to working directory)
--repo MyRepo (to specify what repo to migrate after)
"""

Expand Down

0 comments on commit eefd176

Please sign in to comment.