#!/usr/bin/env bash # 새 업무 기능의 뼈대를 계층 leaf에 만든다. # # scripts/new-feature.sh [--path ] [--no-format] # # 예: scripts/new-feature.sh Invoice -> /invoices # scripts/new-feature.sh PurchaseOrder -> /purchaseOrders # scripts/new-feature.sh Person --path people -> /people # # 만드는 것(배치·이름 근거는 docs/manual/03-placement.md, 단계는 02-new-feature.md): # domain-core dev.caskeleton.domain. aggregate·id·IdFactory·Repository·불변식 예외 # testFixtures ...//testkit 포트당 fake 하나 + 공유 계약(XxxRepositoryContract) # application-core dev.caskeleton.application. Create/Get 유스케이스·command·query·view·오류 코드 # outbound:identifier dev.caskeleton.adapter.outbound.identifier. UuidIdFactory # inbound:web dev.caskeleton.adapter.inbound.web. 컨트롤러·요청/응답 DTO # app-bootstrap dev.caskeleton.bootstrap.autoconfigure. Config + AutoConfiguration.imports 한 줄 # 그리고 각 leaf의 테스트. # # 만들지 않는 것: 저장 adapter(JPA 엔티티·마이그레이션). 고를 저장소와 번호대가 서비스마다 달라 # 사람이 정한다(docs/manual/04-persistence.md). 그 adapter가 생기기 전까지 Config는 꺼져 있다. # # 생성 뒤 그대로 통과해야 하는 검사(scripts/verify-feature-scaffold.sh가 왕복으로 고정한다): # ./gradlew :domain-core:check :application-core:check :adapter:outbound:identifier:check \ # :adapter:inbound:web:check :app-bootstrap:check architectureCheck set -euo pipefail usage() { sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//' exit 2 } repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" templates="$repo_root/scripts/feature-scaffold/templates" name="" path_segment="" format=1 while [ $# -gt 0 ]; do case "$1" in --path) path_segment="${2:-}"; shift 2 ;; --no-format) format=0; shift ;; -h|--help) usage ;; -*) echo "알 수 없는 옵션: $1" >&2; usage ;; *) [ -z "$name" ] || usage; name="$1"; shift ;; esac done [ -n "$name" ] || usage # --- 이름 검사 ----------------------------------------------------------------------------- if ! [[ "$name" =~ ^[A-Z][a-z0-9]+([A-Z][a-z0-9]+)*$ ]]; then echo "기능 이름은 PascalCase여야 한다(예: Invoice, PurchaseOrder): $name" >&2 exit 2 fi package_name="$(printf '%s' "$name" | tr '[:upper:]' '[:lower:]')" camel="$(printf '%s' "${name:0:1}" | tr '[:upper:]' '[:lower:]')${name:1}" upper_snake="$(printf '%s' "$name" | sed -E 's/([a-z0-9])([A-Z])/\1_\2/g' | tr '[:lower:]' '[:upper:]')" kebab="$(printf '%s' "$name" | sed -E 's/([a-z0-9])([A-Z])/\1-\2/g' | tr '[:upper:]' '[:lower:]')" if [ -z "$path_segment" ]; then case "$camel" in *s|*x|*ch|*sh) path_segment="${camel}es" ;; *[!aeiou]y) path_segment="${camel%y}ies" ;; *) path_segment="${camel}s" ;; esac fi # AIP-122: 경로 조각은 소문자로 시작하는 영숫자(ForbiddenApiArchitectureTest.CONTROLLER_REQUEST_MAPPINGS_FOLLOW_AIP122). if ! [[ "$path_segment" =~ ^[a-z][a-zA-Z0-9]*$ ]]; then echo "--path는 AIP-122 모양([a-z][a-zA-Z0-9]*)이어야 한다: $path_segment" >&2 exit 2 fi # Java 예약어는 패키지 이름으로 쓸 수 없다. java_keywords=" abstract assert boolean break byte case catch char class const continue default do double else enum extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while record sealed permits var yield " java_keywords=" $(printf '%s' "$java_keywords" | tr -s ' \n' ' ') " if [[ "$java_keywords" == *" $package_name "* ]]; then echo "'$package_name'는 Java 예약어다. 다른 이름을 고른다." >&2 exit 2 fi # 기능 패키지는 다섯 루트 바로 아래에 생긴다. 어느 leaf에든 그 이름의 패키지가 이미 있으면 거절한다. # 템플릿 패키지(operation, lock, cache, secret, notification…)는 모두 여기 걸린다. 이름 목록을 따로 # 두지 않는 이유: 목록은 템플릿이 패키지를 더하거나 옮길 때 갈라진다(예전 목록에는 사라진 # `outbound`가 남고 새 `secret`이 없었다). 템플릿 소유 목록의 정본은 src/application-core/CLAUDE.md # 「Package ownership」이고, 생성 뒤에는 NamingConventionTest.FEATURE_PACKAGES_DO_NOT_REUSE_TEMPLATE_PACKAGE_NAMES가 # 같은 규칙을 다시 건다. feature_roots=(domain application adapter/inbound/web adapter/outbound/identifier bootstrap/autoconfigure) existing="" for root in "${feature_roots[@]}"; do # `| head`를 쓰지 않는다: pipefail 아래에서 find가 SIGPIPE로 끝나면 이 줄에서 스크립트가 멈춘다. found="$(cd "$repo_root" && find src -path '*/build' -prune -o -type d \ -path "*/src/main/java/dev/caskeleton/$root/$package_name" -print)" if [ -n "$found" ]; then existing="${found%%$'\n'*}" break fi done if [ -n "$existing" ]; then echo "'$package_name' 패키지가 이미 있다: $existing" >&2 echo "템플릿 패키지와 같은 이름의 업무 기능은 upstream 템플릿을 받을 때 같은 패키지 트리에 섞인다." >&2 echo "다른 이름을 고른다(템플릿 패키지 목록: src/application-core/CLAUDE.md 「Package ownership」)." >&2 exit 2 fi # --- 대상 경로 ----------------------------------------------------------------------------- substitute() { sed -e "s/__Feature__/$name/g" \ -e "s/__featureCamel__/$camel/g" \ -e "s/__feature_kebab__/$kebab/g" \ -e "s/__featurePath__/$path_segment/g" \ -e "s/__FEATURE__/$upper_snake/g" \ -e "s/__feature__/$package_name/g" } mapfile -t sources < <(cd "$templates" && find . -type f -name '*.tmpl' | sort) [ "${#sources[@]}" -gt 0 ] || { echo "템플릿이 없다: $templates" >&2; exit 1; } targets=() for source in "${sources[@]}"; do relative="${source#./}" target="$(printf '%s' "${relative%.tmpl}" | substitute)" if [ -e "$repo_root/$target" ]; then echo "이미 있다(덮어쓰지 않는다): $target" >&2 exit 1 fi targets+=("$target") done imports="$repo_root/src/app-bootstrap/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports" config_class="dev.caskeleton.bootstrap.autoconfigure.$package_name.${name}Config" [ -f "$imports" ] || { echo "없다: $imports" >&2; exit 1; } # --- 생성 ---------------------------------------------------------------------------------- for i in "${!sources[@]}"; do mkdir -p "$(dirname "$repo_root/${targets[$i]}")" substitute < "$templates/${sources[$i]#./}" > "$repo_root/${targets[$i]}" echo "생성 ${targets[$i]}" done # 파일 끝 줄바꿈이 없으면 붙이고 한 줄을 더한다. [ -z "$(tail -c1 "$imports")" ] || printf '\n' >> "$imports" printf '%s\n' "$config_class" >> "$imports" echo "추가 ${imports#"$repo_root/"}: $config_class" leaves=(:domain-core :application-core :adapter:outbound:identifier :adapter:inbound:web :app-bootstrap) # google-java-format은 Javadoc 줄바꿈을 이름 길이에 따라 다시 맞춘다. 템플릿만으로는 모든 이름에서 # spotlessCheck를 통과시킬 수 없어 생성 직후 한 번 적용한다. if [ "$format" -eq 1 ]; then spotless=() for leaf in "${leaves[@]}"; do spotless+=("$leaf:spotlessApply"); done (cd "$repo_root/src" && ./gradlew -q --console=plain "${spotless[@]}") echo "포맷 spotlessApply (${leaves[*]})" fi checks=() for leaf in "${leaves[@]}"; do checks+=("$leaf:check"); done cat < $kebab:create 를 준다(07-authorization.md). 주기 전까지 POST /$path_segment 는 모두 403이다. 3. 저장 adapter: ${name}Repository를 구현하고 ${name}RepositoryContract를 실제 저장소로 상속한다 (04-persistence.md). 그 전까지 ${name}Config는 꺼져 있어 /$path_segment 가 없다. 4. placeholder 필드 name을 실제 상태로 바꾸고 불변식을 ${name} 안에 둔다. EOF