Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to LuckyTemplate - Part 4 of ... #810

Merged
merged 1 commit into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions src/generators/web.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "../lucky_cli/generator_helpers"

class LuckyCli::Generators::Web
include LuckyCli::GeneratorHelpers

Expand Down Expand Up @@ -113,42 +115,46 @@ class LuckyCli::Generators::Web
end
end

# TODO: migrate to lucky_template
private def add_default_lucky_structure_to_src
SrcTemplate.new(project_name, generate_auth: generate_auth?, api_only: api_only?, with_sec_tester: with_sec_tester?)
SrcTemplate.new(
project_name,
generate_auth: generate_auth?,
api_only: api_only?,
with_sec_tester: with_sec_tester?
)
.render(project_dir, force: true)
end

# TODO: migrate to lucky_template
private def add_browser_app_structure_to_src
BrowserSrcTemplate.new(generate_auth: generate_auth?)
.render(project_dir, force: true)
end

private def add_base_auth_to_src
BaseAuthenticationSrcTemplate.new.render(Path[project_dir])
BaseAuthenticationSrcTemplate.new
.render(Path[project_dir])
end

private def add_api_authentication_to_src
ApiAuthenticationTemplate.new.render(Path[project_dir])
ApiAuthenticationTemplate.new
.render(Path[project_dir])
end

private def add_browser_authentication_to_src
BrowserAuthenticationSrcTemplate.new.render(project_dir, force: true)
BrowserAuthenticationSrcTemplate.new
.render(Path[project_dir])
end

private def add_sec_tester_to_src
AppWithSecTesterTemplate.new(generate_auth: generate_auth?, browser: browser?)
AppWithSecTesterTemplate.new(
generate_auth: generate_auth?,
browser: browser?
)
.render(Path[project_dir])
end

private def install_shards
puts "Installing shards"
run_command "shards install"
end

private def generate_default_project : Nil
Dir.mkdir_p(project_dir)
end

private def generate_shard_yml : Nil
ShardFileGenerator.new(
project_name,
Expand All @@ -159,6 +165,15 @@ class LuckyCli::Generators::Web
.render(Path[project_dir])
end

private def install_shards
puts "Installing shards"
run_command "shards install"
end

private def generate_default_project : Nil
Dir.mkdir_p(project_dir)
end

private def ensure_directory_does_not_exist
if Dir.exists?(project_dir)
puts "Folder named #{project_dir} already exists, please use a different name".colorize.red.bold
Expand Down
168 changes: 166 additions & 2 deletions src/lucky_cli/browser_authentication_src_template.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,167 @@
class BrowserAuthenticationSrcTemplate < Teeplate::FileTree
directory "#{__DIR__}/../browser_authentication_app_skeleton"
class BrowserAuthenticationSrcTemplate
def render(path : Path)
LuckyTemplate.write!(path, template_folder)
end

def template_folder
LuckyTemplate.create_folder do |root_dir|
root_dir.add_folder("spec") do |spec_dir|
spec_dir.add_folder("flows") do |flows_dir|
flows_dir.add_file("authentication_spec.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/spec/flows/authentication_spec.cr.ecr", io)
end
flows_dir.add_file("reset_password_spec.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/spec/flows/reset_password_spec.cr.ecr", io)
end
end
spec_dir.add_folder("support") do |support_dir|
support_dir.add_file(".keep")
support_dir.add_folder("flows") do |flows_dir|
flows_dir.add_file("authentication_flow.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/spec/support/flows/authentication_flow.cr.ecr", io)
end
flows_dir.add_file("reset_password_flow.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/spec/support/flows/reset_password_flow.cr.ecr", io)
end
end
end
end
root_dir.add_folder("src") do |src_dir|
src_dir.insert_folder("actions", actions_folder)
src_dir.insert_folder("emails", emails_folder)
src_dir.insert_folder("pages", pages_folder)
end
end
end

private def actions_folder
LuckyTemplate.create_folder do |actions_dir|
actions_dir.add_folder("me") do |me_dir|
me_dir.add_file("show.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/me/show.cr.ecr", io)
end
end
actions_dir.add_folder("mixins") do |mixins_dir|
mixins_dir.add_file(".keep")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can knock out #778 too while you're in here? This issue is basically saying no .keep inside folders that will always have files generated. We can also come back to that later once everything is in place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only issue I have removing .keep files are that it removes the intention of whether a particular folder structure should always exist, e.g. mixins is a folder name you expect to see in a project. Or support is a folder name you expect to see in spec. That's my opinion, otherwise I agree you don't need to include them (although, they don't cause any harm in having them).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could always add an option to the CLI that doesn't add those files, which seems like the better option here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm cool with that too. The files being there don't really bother me. We can just leave it for now and worry about that in a future PR.

mixins_dir.add_folder("auth") do |auth_dir|
auth_dir.add_file("allow_guests.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/mixins/auth/allow_guests.cr.ecr", io)
end
auth_dir.add_file("redirect_signed_in_users.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/mixins/auth/redirect_signed_in_users.cr.ecr", io)
end
auth_dir.add_file("require_sign_in.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/mixins/auth/require_sign_in.cr.ecr", io)
end
auth_dir.add_file("test_backdoor.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/mixins/auth/test_backdoor.cr.ecr", io)
end
auth_dir.add_folder("password_resets") do |password_resets_dir|
password_resets_dir.add_file("base.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/mixins/auth/password_resets/base.cr.ecr", io)
end
password_resets_dir.add_file("find_user.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/mixins/auth/password_resets/find_user.cr.ecr", io)
end
password_resets_dir.add_file("require_token.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/mixins/auth/password_resets/require_token.cr.ecr", io)
end
password_resets_dir.add_file("token_from_session.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/mixins/auth/password_resets/token_from_session.cr.ecr", io)
end
end
end
end
actions_dir.add_folder("password_reset_requests") do |password_reset_requests_dir|
password_reset_requests_dir.add_file("create.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/password_reset_requests/create.cr.ecr", io)
end
password_reset_requests_dir.add_file("new.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/password_reset_requests/new.cr.ecr", io)
end
end
actions_dir.add_folder("password_resets") do |password_resets_dir|
password_resets_dir.add_file("create.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/password_resets/create.cr.ecr", io)
end
password_resets_dir.add_file("edit.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/password_resets/edit.cr.ecr", io)
end
password_resets_dir.add_file("new.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/password_resets/new.cr.ecr", io)
end
end
actions_dir.add_folder("sign_ins") do |sign_ins_dir|
sign_ins_dir.add_file("create.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/sign_ins/create.cr.ecr", io)
end
sign_ins_dir.add_file("delete.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/sign_ins/delete.cr.ecr", io)
end
sign_ins_dir.add_file("new.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/sign_ins/new.cr.ecr", io)
end
end
actions_dir.add_folder("sign_ups") do |sign_ups_dir|
sign_ups_dir.add_file("create.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/sign_ups/create.cr.ecr", io)
end
sign_ups_dir.add_file("new.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/actions/sign_ups/new.cr.ecr", io)
end
end
end
end

private def emails_folder
LuckyTemplate.create_folder do |emails_dir|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's interesting that this already knows what directory its in. Pretty neat!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not that it already knows, it's more that I'm making it clear that I expect it to be this way. You still need to add it to Folder#insert_folder and provide a name (which could be something other than "emails"). This is just so you can separate out the folders for better code organization or for reuse.

emails_dir.add_file("password_reset_request_email.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/emails/password_reset_request_email.cr.ecr", io)
end
emails_dir.add_folder("templates/password_reset_request_email") do |email_templates_dir|
email_templates_dir.add_file("html.ecr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/emails/templates/password_reset_request_email/html.ecr.ecr", io)
end
email_templates_dir.add_file("text.ecr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/emails/templates/password_reset_request_email/text.ecr.ecr", io)
end
end
end
end

private def pages_folder
LuckyTemplate.create_folder do |pages_dir|
pages_dir.add_folder("me") do |me_dir|
me_dir.add_file("show_page.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/pages/me/show_page.cr.ecr", io)
end
end
pages_dir.add_folder("password_reset_requests") do |password_reset_requests_dir|
password_reset_requests_dir.add_file("new_page.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/pages/password_reset_requests/new_page.cr.ecr", io)
end
end
pages_dir.add_folder("password_resets") do |password_resets_dir|
password_resets_dir.add_file("new_page.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/pages/password_resets/new_page.cr.ecr", io)
end
end
pages_dir.add_folder("sign_ins") do |sign_ins_dir|
sign_ins_dir.add_file("new_page.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/pages/sign_ins/new_page.cr.ecr", io)
end
end
pages_dir.add_folder("sign_ups") do |sign_ups_dir|
sign_ups_dir.add_file("new_page.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/pages/sign_ups/new_page.cr.ecr", io)
end
end
pages_dir.add_file("auth_layout.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/pages/auth_layout.cr.ecr", io)
end
pages_dir.add_file("main_layout.cr") do |io|
ECR.embed("#{__DIR__}/../browser_authentication_app_skeleton/src/pages/main_layout.cr.ecr", io)
end
end
end
end