From 537fab79bb2c86f43007bd94f020c0b50577eaca Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 11 Apr 2024 10:09:41 +0100 Subject: [PATCH 01/11] Add download script --- scripts/download.sh | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100755 scripts/download.sh diff --git a/scripts/download.sh b/scripts/download.sh new file mode 100755 index 0000000..4a356c9 --- /dev/null +++ b/scripts/download.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +FOLDER=${1:-} +if [[ -z "$FOLDER" ]]; then + echo "Missing folder name" + echo "Usage: download.sh " + exit 1 +fi + +curl -L https://codeload.github.com/hummingbird-project/template/tar.gz/refs/tags/2.0.0-beta.1 | tar xvz -s /template-2.0.0-beta.1/"$FOLDER"/ \ No newline at end of file From 40b2a335043fcb4230439aaf6492294622e0b08a Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 11 Apr 2024 10:14:53 +0100 Subject: [PATCH 02/11] Update script, add to README --- README.md | 5 +++++ scripts/download.sh | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2cb29a7..c46d433 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ # Template Template for Hummingbird App + +Run the following to clone locally +```bash +curl -L https://raw.githubusercontent.com/hummingbird-project/template/configure/scripts/download.sh | bash -s +``` \ No newline at end of file diff --git a/scripts/download.sh b/scripts/download.sh index 4a356c9..45b597f 100755 --- a/scripts/download.sh +++ b/scripts/download.sh @@ -1,9 +1,11 @@ #!/usr/bin/env bash FOLDER=${1:-} +TEMPLATE_VERSION=2.0.0-beta.1 + if [[ -z "$FOLDER" ]]; then echo "Missing folder name" echo "Usage: download.sh " exit 1 fi -curl -L https://codeload.github.com/hummingbird-project/template/tar.gz/refs/tags/2.0.0-beta.1 | tar xvz -s /template-2.0.0-beta.1/"$FOLDER"/ \ No newline at end of file +curl -L https://codeload.github.com/hummingbird-project/template/tar.gz/refs/tags/"$TEMPLATE_VERSION" | tar xvz -s /template-2.0.0-beta.1/"$FOLDER"/ \ No newline at end of file From 3bade28e2e34035c130fb8d9dab3164b3179acb5 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 11 Apr 2024 12:17:55 +0100 Subject: [PATCH 03/11] Update template for 5.10 --- .github/workflows/ci.yml | 2 +- .gitignore | 1 - Dockerfile | 6 +++--- scripts/download.sh | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4d334c4..ae071c5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,6 +22,6 @@ jobs: image: ${{ matrix.image }} steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Test run: swift test diff --git a/.gitignore b/.gitignore index a7aa374..6a2360b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,3 @@ /Packages /*.xcodeproj xcuserdata/ -/Package.resolved \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 4bb787f..a6f2d33 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # ================================ # Build image # ================================ -FROM swift:5.9 as build +FROM swift:5.10 as build # Install OS updates RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \ @@ -23,7 +23,7 @@ RUN swift package resolve COPY . . # Build everything, with optimizations and test discovery -RUN swift build --enable-test-discovery -c release +RUN swift build -c release # Switch to the staging area WORKDIR /staging @@ -38,7 +38,7 @@ RUN [ -d /build/public ] && { mv /build/public ./public && chmod -R a-w ./public # ================================ # Run image # ================================ -FROM swift:5.9-slim +FROM swift:5.10-slim # Make sure all system packages are up to date. RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && \ diff --git a/scripts/download.sh b/scripts/download.sh index 45b597f..b5786de 100755 --- a/scripts/download.sh +++ b/scripts/download.sh @@ -8,4 +8,4 @@ if [[ -z "$FOLDER" ]]; then exit 1 fi -curl -L https://codeload.github.com/hummingbird-project/template/tar.gz/refs/tags/"$TEMPLATE_VERSION" | tar xvz -s /template-2.0.0-beta.1/"$FOLDER"/ \ No newline at end of file +curl -sSL https://github.com/hummingbird-project/template/archive/refs/tags/"$TEMPLATE_VERSION".tar.gz | tar xvz -s /template-2.0.0-beta.1/"$FOLDER"/ \ No newline at end of file From 46dc3705e3258c6b4d07098cff7a3200dacdc685 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 11 Apr 2024 12:44:59 +0100 Subject: [PATCH 04/11] Add configure.sh with package and executable name --- Dockerfile | 4 +- Package.swift | 14 +++--- Tests/AppTests/AppTests.swift | 2 +- configure.sh | 85 +++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 9 deletions(-) create mode 100755 configure.sh diff --git a/Dockerfile b/Dockerfile index a6f2d33..bb991e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,7 +29,7 @@ RUN swift build -c release WORKDIR /staging # Copy main executable to staging area -RUN cp "$(swift build --package-path /build -c release --show-bin-path)/App" ./ +RUN cp "$(swift build --package-path /build -c release --show-bin-path)/{{HB_EXECUTABLE_NAME}}" ./ # Copy any resouces from the public directory and views directory if the directories exist # Ensure that by default, neither the directory nor any of its contents are writable. @@ -60,5 +60,5 @@ USER hummingbird:hummingbird EXPOSE 8080 # Start the Vapor service when the image is run, default to listening on 8080 in production environment -ENTRYPOINT ["./App"] +ENTRYPOINT ["./{{HB_EXECUTABLE_NAME}}"] CMD ["--hostname", "0.0.0.0", "--port", "8080"] diff --git a/Package.swift b/Package.swift index a0c0b62..4a484bd 100644 --- a/Package.swift +++ b/Package.swift @@ -4,21 +4,22 @@ import PackageDescription let package = Package( - name: "hummingbird-template", + name: "{{HB_PACKAGE_NAME}}", platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17)], products: [ - .executable(name: "App", targets: ["App"]), + .executable(name: "{{HB_EXECUTABLE_NAME}}", targets: ["{{HB_EXECUTABLE_NAME}}"]), ], dependencies: [ .package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.1"), .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0") ], targets: [ - .executableTarget(name: "App", + .executableTarget(name: "{{HB_EXECUTABLE_NAME}}", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser"), .product(name: "Hummingbird", package: "hummingbird"), ], + path: "Sources/App", swiftSettings: [ // Enable better optimizations when building in Release configuration. Despite the use of // the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release @@ -26,11 +27,12 @@ let package = Package( .unsafeFlags(["-cross-module-optimization"], .when(configuration: .release)) ] ), - .testTarget(name: "AppTests", + .testTarget(name: "{{HB_EXECUTABLE_NAME}}Tests", dependencies: [ - .byName(name: "App"), + .byName(name: "{{HB_EXECUTABLE_NAME}}"), .product(name: "HummingbirdTesting", package: "hummingbird") - ] + ], + path: "Tests/AppTests" ) ] ) diff --git a/Tests/AppTests/AppTests.swift b/Tests/AppTests/AppTests.swift index cc559c2..26ea441 100644 --- a/Tests/AppTests/AppTests.swift +++ b/Tests/AppTests/AppTests.swift @@ -3,7 +3,7 @@ import HummingbirdTesting import Logging import XCTest -@testable import App +@testable import {{HB_EXECUTABLE_NAME}} final class AppTests: XCTestCase { struct TestArguments: AppArguments { diff --git a/configure.sh b/configure.sh new file mode 100755 index 0000000..dd55d0a --- /dev/null +++ b/configure.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash + +PWD=$(pwd) +BASE_FOLDER=$(basename "$PWD") +CLEAN_BASE_FOLDER=$(echo "$BASE_FOLDER" | sed -e 's/[^a-zA-Z0-9_]/_/g') +TARGET_FOLDER=${1:-.} + +TEMP_FOLDER=$(mktemp -d) +MO="$TEMP_FOLDER"/mo + +cleanup() +{ + rm -rf "$TEMP_FOLDER" +} + +# Download MO bash mustache renderer +download_mo() +{ + curl -sSL https://raw.githubusercontent.com/tests-always-included/mo/master/mo -o "$MO" + chmod a+x "$MO" +} + +read_input_with_default () { + echo -n "[$1] > " + read -r READ_INPUT_RETURN + + if [ -z "$READ_INPUT_RETURN" ]; then + READ_INPUT_RETURN="$1" + fi +} + +run_mustache() +{ + FILES=$1 + for FILE in $FILES; do + $MO "$FILE" > "$TEMP_FOLDER"/tempfile + mv -f "$TEMP_FOLDER"/tempfile "$TARGET_FOLDER/$FILE" + done +} + +exitWithError() +{ + echo "Error: $1" + exit 1 +} + +check_valid() { + if [[ "$HB_PACKAGE_NAME" =~ [^a-zA-Z0-9_] ]]; then + exitWithError "Invalid package name: $HB_PACKAGE_NAME" + fi +} +trap cleanup EXIT $? + +download_mo +echo "Outputting to $TARGET_FOLDER" + +echo -n "Enter your package name: " +read_input_with_default "$CLEAN_BASE_FOLDER" +export HB_PACKAGE_NAME=$READ_INPUT_RETURN +if [[ "$HB_PACKAGE_NAME" =~ [^a-zA-Z0-9_-] ]]; then + exitWithError "Invalid package name: $HB_PACKAGE_NAME" +fi + +echo -n "Enter your executable name: " +read_input_with_default "App" +export HB_EXECUTABLE_NAME=$READ_INPUT_RETURN +if [[ "$HB_EXECUTABLE_NAME" =~ [^a-zA-Z0-9_] ]]; then + exitWithError "Invalid executable name: $HB_EXECUTABLE_NAME" +fi + +if [[ "$TARGET_FOLDER" != "." ]]; then + mkdir -p "$TARGET_FOLDER"/Sources/App + mkdir -p "$TARGET_FOLDER"/Tests/AppTests +fi + +# Root level files +FILES=$(find . -maxdepth 1 ! -type d ! -name "*.sh") +run_mustache "$FILES" +# Files in Sources and Tests folder +FILES=$(find Sources Tests ! -type d) +run_mustache "$FILES" + + + + From 5509847fb349bff2a6401b9c5be627b42950babb Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 11 Apr 2024 12:47:09 +0100 Subject: [PATCH 05/11] Outputting to text --- configure.sh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/configure.sh b/configure.sh index dd55d0a..eee0882 100755 --- a/configure.sh +++ b/configure.sh @@ -51,8 +51,18 @@ check_valid() { } trap cleanup EXIT $? +# Download Bash Mustache download_mo -echo "Outputting to $TARGET_FOLDER" + +if [[ "$TARGET_FOLDER" != "." ]]; then + echo "Outputting to $TARGET_FOLDER" + mkdir -p "$TARGET_FOLDER"/Sources/App + mkdir -p "$TARGET_FOLDER"/Tests/AppTests +else + echo "Outputting to current folder" +fi + + echo -n "Enter your package name: " read_input_with_default "$CLEAN_BASE_FOLDER" @@ -68,11 +78,6 @@ if [[ "$HB_EXECUTABLE_NAME" =~ [^a-zA-Z0-9_] ]]; then exitWithError "Invalid executable name: $HB_EXECUTABLE_NAME" fi -if [[ "$TARGET_FOLDER" != "." ]]; then - mkdir -p "$TARGET_FOLDER"/Sources/App - mkdir -p "$TARGET_FOLDER"/Tests/AppTests -fi - # Root level files FILES=$(find . -maxdepth 1 ! -type d ! -name "*.sh") run_mustache "$FILES" From 574d89a74ca10dd4b8ef4e8669abdb04a09ccbc1 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 11 Apr 2024 14:06:11 +0100 Subject: [PATCH 06/11] Update README --- README.md | 4 +++- configure.sh | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c46d433..1438f82 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,6 @@ Template for Hummingbird App Run the following to clone locally ```bash curl -L https://raw.githubusercontent.com/hummingbird-project/template/configure/scripts/download.sh | bash -s -``` \ No newline at end of file +``` + +Then enter the folder created, run `./configure` and follow the instructions. diff --git a/configure.sh b/configure.sh index eee0882..d91eb3a 100755 --- a/configure.sh +++ b/configure.sh @@ -38,6 +38,13 @@ run_mustache() done } +run_mustache-template() +{ + echo $1 + #| $MO > "$TEMP_FOLDER"/$2 + echo $2 +} + exitWithError() { echo "Error: $1" @@ -85,6 +92,8 @@ run_mustache "$FILES" FILES=$(find Sources Tests ! -type d) run_mustache "$FILES" - - - +# README file +cat < $TARGET_FOLDER/README.md +# $HB_PACKAGE_NAME +My awesome project +EOF From 433d91d6177536fdc5ff395862dde66684443d6e Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 11 Apr 2024 14:09:50 +0100 Subject: [PATCH 07/11] Update README to include `Use this template` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1438f82..9871f20 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Template for Hummingbird App -Run the following to clone locally +Either click on `Use this template` or run the following to clone locally ```bash curl -L https://raw.githubusercontent.com/hummingbird-project/template/configure/scripts/download.sh | bash -s ``` From 46b6338db812ccff72da952284cab889145c5105 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 11 Apr 2024 14:15:25 +0100 Subject: [PATCH 08/11] Fix README, configure outputs more info --- README.md | 2 +- configure.sh | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9871f20..4ec0dbd 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,4 @@ Either click on `Use this template` or run the following to clone locally curl -L https://raw.githubusercontent.com/hummingbird-project/template/configure/scripts/download.sh | bash -s ``` -Then enter the folder created, run `./configure` and follow the instructions. +Then enter the folder created, run `./configure.sh` and follow the instructions. diff --git a/configure.sh b/configure.sh index d91eb3a..7ead243 100755 --- a/configure.sh +++ b/configure.sh @@ -58,6 +58,8 @@ check_valid() { } trap cleanup EXIT $? +echo "Configuring your Hummingbird project" + # Download Bash Mustache download_mo From 0bc418902e18c9ab41c8bde17ebb3872dae72b2e Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 11 Apr 2024 14:26:09 +0100 Subject: [PATCH 09/11] Use executable name in logger label, server name --- Sources/App/Application+build.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/App/Application+build.swift b/Sources/App/Application+build.swift index 839c8c2..cd7a1fe 100644 --- a/Sources/App/Application+build.swift +++ b/Sources/App/Application+build.swift @@ -13,7 +13,7 @@ public protocol AppArguments { public func buildApplication(_ arguments: some AppArguments) -> some ApplicationProtocol { let logger = { - var logger = Logger(label: "HB") + var logger = Logger(label: "{{HB_EXECUTABLE_NAME}}") logger.logLevel = arguments.logLevel ?? .info return logger }() @@ -26,7 +26,7 @@ public func buildApplication(_ arguments: some AppArguments) -> some Application router: router, configuration: .init( address: .hostname(arguments.hostname, port: arguments.port), - serverName: "Hummingbird" + serverName: "{{HB_EXECUTABLE_NAME}}" ), logger: logger ) From 1b37ac726b255630916d58d84f05f7c366f3f5dc Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 11 Apr 2024 14:29:11 +0100 Subject: [PATCH 10/11] Use package name not executable name --- Sources/App/Application+build.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/App/Application+build.swift b/Sources/App/Application+build.swift index cd7a1fe..69ffe22 100644 --- a/Sources/App/Application+build.swift +++ b/Sources/App/Application+build.swift @@ -13,7 +13,7 @@ public protocol AppArguments { public func buildApplication(_ arguments: some AppArguments) -> some ApplicationProtocol { let logger = { - var logger = Logger(label: "{{HB_EXECUTABLE_NAME}}") + var logger = Logger(label: "{{HB_PACKAGE_NAME}}") logger.logLevel = arguments.logLevel ?? .info return logger }() @@ -26,7 +26,7 @@ public func buildApplication(_ arguments: some AppArguments) -> some Application router: router, configuration: .init( address: .hostname(arguments.hostname, port: arguments.port), - serverName: "{{HB_EXECUTABLE_NAME}}" + serverName: "{{HB_PACKAGE_NAME}}" ), logger: logger ) From b28b5473782b6f9b0a1feba65dc9ddabee1e680d Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 11 Apr 2024 14:37:16 +0100 Subject: [PATCH 11/11] Get correct base folder --- configure.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/configure.sh b/configure.sh index 7ead243..e4ca130 100755 --- a/configure.sh +++ b/configure.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash PWD=$(pwd) -BASE_FOLDER=$(basename "$PWD") +TARGET_FOLDER=${1:-$PWD} +BASE_FOLDER=$(basename $TARGET_FOLDER) CLEAN_BASE_FOLDER=$(echo "$BASE_FOLDER" | sed -e 's/[^a-zA-Z0-9_]/_/g') -TARGET_FOLDER=${1:-.} TEMP_FOLDER=$(mktemp -d) MO="$TEMP_FOLDER"/mo @@ -63,7 +63,7 @@ echo "Configuring your Hummingbird project" # Download Bash Mustache download_mo -if [[ "$TARGET_FOLDER" != "." ]]; then +if [[ "$TARGET_FOLDER" != "$PWD" ]]; then echo "Outputting to $TARGET_FOLDER" mkdir -p "$TARGET_FOLDER"/Sources/App mkdir -p "$TARGET_FOLDER"/Tests/AppTests