-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/bonfire-networks/bonfire-app/issues/707
- Loading branch information
Showing
10 changed files
with
525 additions
and
153 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
defmodule Mix.Tasks.Bonfire.Gen.Component do | ||
@moduledoc """ | ||
`just mix bonfire.gen.component stateless Bonfire.MyUIExtension MyComponent` | ||
or | ||
`just mix bonfire.gen.component stateful Bonfire.MyUIExtension MyComponent` | ||
will present you with a diff and create new files | ||
""" | ||
use Igniter.Mix.Task | ||
alias Bonfire.Common.Mix.Tasks.Helpers | ||
|
||
def igniter(igniter, [state, extension, module_name | _] = _argv) do | ||
gen_component(igniter, extension, module_name, state) | ||
end | ||
|
||
def gen_component(igniter, extension, module_name, state) | ||
when state in ["stateful", "stateless"] do | ||
ext_module = | ||
extension | ||
|> Macro.camelize() | ||
|
||
snake_name = Macro.underscore(extension) | ||
|
||
module_name = | ||
String.trim_trailing(ext_module <> "." <> module_name, "Live") | ||
|> Kernel.<>("Live") | ||
|> Igniter.Project.Module.parse() | ||
|
||
# |> IO.inspect() | ||
|
||
lib_path_prefix = "lib/web/components" | ||
|
||
igniter | ||
|> Igniter.create_new_file( | ||
Helpers.igniter_path_for_module(igniter, module_name, lib_path_prefix), | ||
""" | ||
defmodule #{inspect(module_name)} do | ||
use Bonfire.UI.Common.Web, :#{state}_component | ||
prop name, :string, default: nil | ||
end | ||
""" | ||
) | ||
|> Igniter.create_new_file( | ||
Helpers.igniter_path_for_module(igniter, module_name, lib_path_prefix, "sface") | ||
|> IO.inspect(), | ||
""" | ||
<div> | ||
Hello, This is a new #{state} component for #{ext_module}. | ||
You can include a other components by uncommenting the line below and updating it with your other component module name and then passing the assigns you need: | ||
{!-- <#{ext_module}.SimpleComponentLive name="#{ext_module}" /> --} | ||
</div> | ||
""" | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
defmodule Mix.Tasks.Bonfire.Gen.Extension do | ||
@moduledoc """ | ||
`just mix bonfire.gen.extension Bonfire.MyExtension` | ||
will present you with a diff of new files to create your new extension and create a repo for it in `extensions/` | ||
""" | ||
|
||
use Igniter.Mix.Task | ||
|
||
@impl Igniter.Mix.Task | ||
def info(_argv, _composing_task) do | ||
%Igniter.Mix.Task.Info{ | ||
# description: "Creates a new Bonfire extension from a template", | ||
positional: [ | ||
extension_name: [ | ||
type: :string, | ||
required: true, | ||
doc: "Name of the extension to create" | ||
] | ||
] | ||
} | ||
end | ||
|
||
@impl Igniter.Mix.Task | ||
def igniter(igniter) do | ||
[extension_name] = igniter.args.argv | ||
snake_name = Macro.underscore(extension_name) | ||
|
||
camel_name = | ||
extension_name | ||
|> String.replace("bonfire_", "bonfire/") | ||
|> Macro.camelize() | ||
|
||
igniter | ||
|> clone_template(snake_name) | ||
|> rename_modules(snake_name, camel_name) | ||
|> rename_config_file(snake_name) | ||
|> reset_git(snake_name) | ||
|> Igniter.add_notice( | ||
"Done! You can now start developing your extension in ./extensions/#{snake_name}/" | ||
) | ||
end | ||
|
||
defp clone_template(igniter, snake_name) do | ||
if File.exists?("extensions/bonfire_extension_template") do | ||
System.cmd("sh", [ | ||
"-c", | ||
"cd extensions/bonfire_extension_template && find . -name '.git' -prune -o -print | cpio -pdm ../#{snake_name}" | ||
]) | ||
else | ||
System.cmd( | ||
"git", | ||
[ | ||
"clone", | ||
"--depth", | ||
"1", | ||
"https://github.com/bonfire-networks/bonfire_extension_template.git", | ||
snake_name | ||
], | ||
cd: "extensions" | ||
) | ||
end | ||
|
||
igniter | ||
end | ||
|
||
defp rename_modules(igniter, snake_name, camel_name) do | ||
patterns = ["**/*.ex", "**/*.exs", "**/*.md", "**/*.sface"] | ||
base_path = "extensions/#{snake_name}/" | ||
|
||
Enum.reduce(patterns, igniter, fn pattern, acc -> | ||
Path.wildcard(base_path <> pattern) | ||
|> Enum.reduce(acc, fn path, inner_acc -> | ||
inner_acc | ||
|> Igniter.include_existing_file(path) | ||
|> Igniter.update_file(path, fn source -> | ||
Rewrite.Source.update(source, :content, fn | ||
content when is_binary(content) -> | ||
content | ||
|> String.replace("bonfire_extension_template", snake_name) | ||
|> String.replace("Bonfire.ExtensionTemplate", camel_name) | ||
|
||
content -> | ||
content | ||
end) | ||
end) | ||
end) | ||
end) | ||
end | ||
|
||
defp rename_config_file(igniter, extension_name) do | ||
old_name = "extensions/#{extension_name}/config/bonfire_extension_template.exs" | ||
new_name = "extensions/#{extension_name}/config/#{extension_name}.exs" | ||
|
||
Igniter.move_file(igniter, old_name, new_name) | ||
end | ||
|
||
defp reset_git(igniter, extension_name) do | ||
cd_path = "extensions/#{extension_name}" | ||
|
||
System.cmd("rm", ["-rf", ".git"], cd: cd_path) | ||
System.cmd("git", ["init"], cd: cd_path) | ||
System.cmd("git", ["add", "."], cd: cd_path) | ||
System.cmd("git", ["commit", "-m", "new extension"], cd: cd_path) | ||
|
||
igniter | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule Mix.Tasks.Bonfire.Gen.Extension.Ui do | ||
@moduledoc """ | ||
`just mix bonfire.gen.extension.ui Bonfire.MyExtension` | ||
will present you with a diff of new files to create your new extension and create a repo for it in `extensions/` | ||
""" | ||
|
||
use Igniter.Mix.Task | ||
|
||
@impl Igniter.Mix.Task | ||
def info(_argv, _composing_task) do | ||
%Igniter.Mix.Task.Info{ | ||
# description: "Creates a new Bonfire extension from a template", | ||
positional: [ | ||
extension_name: [ | ||
type: :string, | ||
required: true, | ||
doc: "Name of the UI extension to create" | ||
] | ||
] | ||
} | ||
end | ||
|
||
@impl Igniter.Mix.Task | ||
def igniter(igniter) do | ||
[extension_name] = igniter.args.argv | ||
# snake_name = Macro.underscore(extension_name) | ||
|
||
camel_name = | ||
extension_name | ||
|> String.replace("bonfire_", "bonfire/") | ||
|> Macro.camelize() | ||
|
||
igniter | ||
|> Igniter.compose_task(Mix.Tasks.Bonfire.Gen.Extension, [camel_name]) | ||
|> Igniter.compose_task(Mix.Tasks.Bonfire.Gen.Ui, [camel_name]) | ||
end | ||
end |
Oops, something went wrong.