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 Lucky Template - Part 5 of ... #812

Merged
merged 6 commits into from
Sep 21, 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
5 changes: 1 addition & 4 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ crystal: ">= 1.6.0"
license: MIT

dependencies:
teeplate:
github: luckyframework/teeplate
version: ~> 0.8.5
lucky_template:
github: luckyframework/lucky_template
version: ~> 0.1.0
version: ~> 0.2.0
lucky_task:
github: luckyframework/lucky_task
version: ~> 0.1.1
Expand Down
6 changes: 2 additions & 4 deletions src/generators/web.cr
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,19 @@ 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?
)
.render(project_dir, force: true)
.render(Path[project_dir])
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)
.render(Path[project_dir])
end

private def add_base_auth_to_src
Expand Down
1 change: 0 additions & 1 deletion src/lucky_cli.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "teeplate"
require "lucky_template"
require "lucky_task"
require "nox"
Expand Down
117 changes: 115 additions & 2 deletions src/lucky_cli/browser_src_template.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,122 @@
require "random/secure"

class BrowserSrcTemplate < Teeplate::FileTree
directory "#{__DIR__}/../browser_app_skeleton"
class BrowserSrcTemplate
getter? generate_auth

def initialize(@generate_auth : Bool)
end

def render(path : Path)
LuckyTemplate.write!(path, template_folder)
end

def template_folder
LuckyTemplate.create_folder do |root_dir|
root_dir.add_file("bs-config.js") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/bs-config.js.ecr", io)
end
root_dir.add_file("package.json") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/package.json.ecr", io)
end
root_dir.add_file("webpack.mix.js") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/webpack.mix.js.ecr", io)
end
root_dir.add_folder("config") do |config_dir|
config_dir.add_file("html_page.cr") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/config/html_page.cr.ecr", io)
end
end
root_dir.add_folder("db") do |db_dir|
db_dir.add_file("migrations/.keep")
end
root_dir.add_folder("public") do |public_dir|
public_dir.add_file("favicon.ico") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/public/favicon.ico.ecr", io)
end
public_dir.add_file("mix-manifest.json") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/public/mix-manifest.json.ecr", io)
end
public_dir.add_file("robots.txt") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/public/robots.txt.ecr", io)
end
public_dir.add_file("assets/images/.keep")
end
root_dir.insert_folder("spec", spec_folder)
root_dir.insert_folder("src", src_folder)
end
end

private def spec_folder
LuckyTemplate.create_folder do |spec_dir|
spec_dir.add_file("flows/.keep")
spec_dir.add_folder("setup") do |setup_dir|
setup_dir.add_file(".keep")
setup_dir.add_file("configure_lucky_flow.cr") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/spec/setup/configure_lucky_flow.cr.ecr", io)
end
end
spec_dir.add_folder("support") do |support_dir|
support_dir.add_file(".keep")
support_dir.add_file("factories/.keep")
support_dir.add_file("flows/base_flow.cr") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/spec/support/flows/base_flow.cr.ecr", io)
end
end
end
end

private def src_folder
LuckyTemplate.create_folder do |src_dir|
src_dir.add_file("actions/browser_action.cr") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/src/actions/browser_action.cr.ecr", io)
end
src_dir.add_folder("components") do |components_dir|
components_dir.add_file(".keep")
components_dir.add_file("base_component.cr") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/src/components/base_component.cr.ecr", io)
end
components_dir.add_folder("shared") do |shared_dir|
shared_dir.add_file("field.cr") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/src/components/shared/field.cr.ecr", io)
end
shared_dir.add_file("field_errors.cr") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/src/components/shared/field_errors.cr.ecr", io)
end
shared_dir.add_file("flash_messages.cr") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/src/components/shared/flash_messages.cr.ecr", io)
end
shared_dir.add_file("layout_head.cr") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/src/components/shared/layout_head.cr.ecr", io)
end
end
end
src_dir.add_folder("css") do |css_dir|
css_dir.add_file("app.scss") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/src/css/app.scss.ecr", io)
end
css_dir.add_folder("components/.keep")
css_dir.add_folder("mixins/.keep")
css_dir.add_folder("variables/.keep")
end
src_dir.add_file("emails/.keep")
src_dir.add_file("js/app.js") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/src/js/app.js.ecr", io)
end
src_dir.add_folder("models") do |models_dir|
models_dir.add_file("mixins/.keep")
end
src_dir.add_folder("operations") do |ops_dir|
ops_dir.add_file(".keep")
ops_dir.add_file("mixins/.keep")
end
src_dir.add_folder("pages") do |pages_dir|
pages_dir.add_file("main_layout.cr") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/src/pages/main_layout.cr.ecr", io)
end
pages_dir.add_file("errors/show_page.cr") do |io|
ECR.embed("#{__DIR__}/../browser_app_skeleton/src/pages/errors/show_page.cr.ecr", io)
end
end
end
end
end
28 changes: 11 additions & 17 deletions src/lucky_cli/shard_file_generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,42 @@ class ShardFileGenerator

def template_folder
LuckyTemplate.create_folder do |top_dir|
top_dir.add_file("shard.yml", <<-YAML)
#{shard_info}
#{shard_deps}
#{shard_dev_deps}
YAML
top_dir.add_file("shard.yml", shard_yml.to_yaml)
end
end

def shard_info : String
normalize_yaml({
def shard_yml
{
"name" => project_name,
"version" => "0.1.0",
"targets" => {
project_name => {
"main" => Path.new("src", "#{project_name}.cr").to_s,
},
},
"crystal" => ">= #{Crystal::VERSION}",
})
"crystal" => ">= #{Crystal::VERSION}",
"dependencies" => shard_deps,
"development_dependencies" => shard_dev_deps,
}
end

def shard_deps
private def shard_deps
deps = project_base_deps
if generate_auth?
deps = deps.merge(project_auth_deps)
end
normalize_yaml({"dependencies" => deps})
deps
end

def shard_dev_deps
private def shard_dev_deps
deps = {} of String => String
if browser?
deps = deps.merge(project_browser_dev_deps)
end
if with_sec_tester?
deps = deps.merge(project_additional_dev_deps)
end
normalize_yaml({"development_dependencies" => deps})
end

private def normalize_yaml(hash) : String
hash.to_yaml.lines[1..].join('\n')
deps
end

private def project_base_deps
Expand Down
Loading
Loading