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

Adding in setup scripts for Windows powershell users #867

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions src/generators/web.cr
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ class LuckyCli::Generators::Web
#{"Done generating your Lucky project".colorize.bold}

#{green_arrow} cd into #{project_location.colorize(:green)}
#{green_arrow} check database settings in #{"config/database.cr".colorize(:green)}
#{green_arrow} run #{"script/setup".colorize(:green)}
#{green_arrow} check database settings in #{Path.new("config", "database.cr").colorize(:green)}
{% if flag?(:windows) %}
#{green_arrow} run #{Path.new("script", "setup.ps1").colorize(:green)}
{% else %}
#{green_arrow} run #{Path.new("script", "setup").colorize(:green)}
{% end %}
#{green_arrow} run #{"lucky dev".colorize(:green)} to start the server

TEXT
Expand Down
63 changes: 48 additions & 15 deletions src/lucky_cli/src_template.cr
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,58 @@ class SrcTemplate
ECR.embed("#{__DIR__}/../web_app_skeleton/docker/wait-for-it.sh.ecr", io)
end
end
root_dir.add_folder("script") do |script_dir|
script_dir.add_file("setup", 0o755) do |io|
ECR.embed("#{__DIR__}/../web_app_skeleton/script/setup.ecr", io)
root_dir.insert_folder("script", script_folder)
root_dir.insert_folder("spec", spec_folder)
root_dir.insert_folder("src", src_folder)
root_dir.insert_folder("tasks", tasks_folder)
end
end

private def script_folder
{% if flag?(:windows) %}
create_powershell_scripts_folder
{% else %}
create_bash_scripts_folder
{% end %}
end

# Generate bash scripts for *nix platforms
private def create_bash_scripts_folder
LuckyTemplate.create_folder do |script_dir|
script_dir.add_file("setup", 0o755) do |io|
ECR.embed("#{__DIR__}/../web_app_skeleton/script/setup.ecr", io)
end
script_dir.add_file("system_check", 0o755) do |io|
ECR.embed("#{__DIR__}/../web_app_skeleton/script/system_check.ecr", io)
end
script_dir.add_folder("helpers") do |helpers_dir|
helpers_dir.add_file("function_helpers") do |io|
ECR.embed("#{__DIR__}/../web_app_skeleton/script/helpers/function_helpers.ecr", io)
end
script_dir.add_file("system_check", 0o755) do |io|
ECR.embed("#{__DIR__}/../web_app_skeleton/script/system_check.ecr", io)
helpers_dir.add_file("text_helpers") do |io|
ECR.embed("#{__DIR__}/../web_app_skeleton/script/helpers/text_helpers.ecr", io)
end
script_dir.add_folder("helpers") do |helpers_dir|
helpers_dir.add_file("function_helpers") do |io|
ECR.embed("#{__DIR__}/../web_app_skeleton/script/helpers/function_helpers.ecr", io)
end
helpers_dir.add_file("text_helpers") do |io|
ECR.embed("#{__DIR__}/../web_app_skeleton/script/helpers/text_helpers.ecr", io)
end
end
end
end

# Generate PowerShell scripts for Windows
private def create_powershell_scripts_folder
LuckyTemplate.create_folder do |script_dir|
script_dir.add_file("setup.ps1", 0o755) do |io|
ECR.embed("#{__DIR__}/../web_app_skeleton/script/setup.ps1.ecr", io)
end
script_dir.add_file("system_check.ps1", 0o755) do |io|
ECR.embed("#{__DIR__}/../web_app_skeleton/script/system_check.ps1.ecr", io)
end
script_dir.add_folder("helpers") do |helpers_dir|
helpers_dir.add_file("function_helpers.ps1") do |io|
ECR.embed("#{__DIR__}/../web_app_skeleton/script/helpers/function_helpers.ps1.ecr", io)
end
helpers_dir.add_file("text_helpers.ps1") do |io|
ECR.embed("#{__DIR__}/../web_app_skeleton/script/helpers/text_helpers.ps1.ecr", io)
end
end
root_dir.insert_folder("spec", spec_folder)
root_dir.insert_folder("src", src_folder)
root_dir.insert_folder("tasks", tasks_folder)
end
end

Expand Down
42 changes: 42 additions & 0 deletions src/web_app_skeleton/script/helpers/function_helpers.ps1.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This file contains a set of functions used as helpers
# for various tasks. Read the examples for each one for
# more information. Feel free to put any additional helper
# functions you may need for your app


# Returns true if the command $Command is not found
# example:
# if (CommandNotFound "yarn") {
# echo "no yarn"
# }
function CommandNotFound($Command) {
try {
Get-Command -Name $Command -ErrorAction | Out-Null
return $false
} catch {
return $true
}
}

# Returns true if the command $Command is not running
# You must supply the full command to check as an argument
# example:
# if (CommandNotRunning "redis-cli ping") {
# PrintError "Redis is not running"
# }
function CommandNotRunning($Command) {
Invoke-Expression $Command
if ($LASTEXITCODE -ne 0) {
return $true
} else {
return $false
}
}

# Prints error and exit.
# example:
# PrintError "Redis is not running. Run it with some_command"
function PrintError($Message) {
Write-Host -ForegroundColor Red "There is a problem with your system setup:`n`n"
Write-Host -ForegroundColor Red "$Message `n`n" | Indent
}
32 changes: 32 additions & 0 deletions src/web_app_skeleton/script/helpers/text_helpers.ps1.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This file contains a set of functions used to format text,
# and make printing text a little easier. Feel free to put
# any additional functions you need for formatting your shell
# output text.

# Colors
$BOLD_RED_COLOR="`e[1m`e[31m"

# Indents the text 2 spaces
# example:
# Write-Host "Hello" | Indent
function Indent {
process {
foreach($Line in $_) {
Write-Host " $Line"
}
}
}

# Prints out an arrow to your custom notice
# example:
# Notice "Installing new magic"
function Notice($Message) {
Write-Host "`n▸ $Message`n"
}

# Prints out a check mark and Done.
# example:
# PrintDone
function PrintDone {
Write-Host "✔ Done`n" | Indent
}
44 changes: 44 additions & 0 deletions src/web_app_skeleton/script/setup.ps1.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Exit if any subcommand fails
$ErrorActionPreferences = "Stop"

.\script\helpers\text_helpers.ps1


Notice "Running System Check"
& .\script\system_check.ps1
PrintDone

<%- if browser? -%>
Notice "Installing node dependencies"
yarn install --no-progress | Indent

Notice "Compiling assets"
yarn dev | Indent

PrintDone
<%- end -%>

Notice "Installing shards"
shards install --ignore-crystal-version | Indent

if (!Test-Path ".env") {
Notice "No .env found. Creating one."
New-Item ".env"
PrintDone
}

Notice "Creating the database"
lucky db.create | Indent

Notice "Verifying postgres connection"
lucky db.verify_connection | Indent

Notice "Migrating the database"
lucky db.migrate | Indent

Notice "Seeding the database with required and sample records"
lucky db.seed.required_data | Indent
lucky db.seed.sample_data | Indent

PrintDone
Notice "Run 'lucky dev' to start the app"
30 changes: 30 additions & 0 deletions src/web_app_skeleton/script/system_check.ps1.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.\script\helpers\text_helpers.ps1
.\script\helpers\function_helpers.ps1

# Use this script to check the system for required tools and process that your app needs.
# A few helper functions are provided to make writing bash a little easier. See the
# script/helpers/function_helpers file for more examples.
#
# A few examples you might use here:
# * 'lucky db.verify_connection' to test postgres can be connected
# * Checking that elasticsearch, redis, or postgres is installed and/or booted
# * Note: Booting additional processes for things like mail, background jobs, etc...
# should go in your Procfile.dev.

<%- if browser? -%>
if (CommandNotFound "yarn") {
PrintError "Yarn is not installed\n See https://yarnpkg.com/lang/en/docs/install/ for install instructions."
}
<%- end -%>

if (CommandNotFound "createdb") {
PrintError "Please install the postgres CLI tools, then try again.\nSee https://www.postgresql.org/docs/current/tutorial-install.html for install instructions."
}

## CUSTOM PRE-BOOT CHECKS ##
# example:
# if (CommandNotFound "redis-cli ping") {
# PrintError "Redis is not running."
# }


Loading