Skip to content

Commit

Permalink
improve generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Jan 20, 2025
1 parent 2d92cc1 commit ba31eb3
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions lib/mix_tasks/extension/extension.new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,51 @@ defmodule Mix.Tasks.Bonfire.Extension.New do
use Mix.Task

def run([extension_name]) do
snake_name = Macro.underscore(extension_name)

camel_name =
extension_name
|> String.replace("bonfire_", "bonfire/")
|> Macro.camelize()

if File.exists?("extensions/bonfire_extension_template") do
File.cp_r!("extensions/bonfire_extension_template", "extensions/#{extension_name}")
File.cp_r!("extensions/bonfire_extension_template", "extensions/#{snake_name}")
else
System.cmd(
"git",
[
"clone",
"https://github.com/bonfire-networks/bonfire_extension_template.git",
extension_name
snake_name
],
cd: "extensions"
)
end

rename_modules(extension_name)
rename_config_file(extension_name)
reset_git(extension_name)
rename_modules(snake_name, camel_name)
rename_config_file(snake_name)
reset_git(snake_name)

IO.puts(
"Done! You can now start developing your extension in ./extensions/#{extension_name}/"
)
IO.puts("Done! You can now start developing your extension in ./extensions/#{snake_name}/")
end

defp rename_modules(extension_name) do
defp rename_modules(snake_name, camel_name) do
# Get all .ex, .exs, and .md files in the extension directory
["**/*.ex", "**/*.exs", "**/*.md", "**/*.sface"]
|> Enum.flat_map(&Path.wildcard("extensions/#{extension_name}/" <> &1))
|> Enum.flat_map(&Path.wildcard("extensions/#{snake_name}/" <> &1))
|> Enum.each(fn path ->
# Read the file
file_content = File.read!(path)

# Replace the module names
new_content =
String.replace(file_content, "bonfire_extension_template", extension_name)
String.replace(file_content, "bonfire_extension_template", snake_name)

new_content =
String.replace(
new_content,
"Bonfire.ExtensionTemplate",
extension_name
|> String.replace("bonfire_", "bonfire/")
|> Macro.camelize()
camel_name
)

# Write the new content to the file
Expand Down

0 comments on commit ba31eb3

Please sign in to comment.