-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sh
More file actions
521 lines (446 loc) · 17.4 KB
/
init.sh
File metadata and controls
521 lines (446 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/env bash
# claude-dev-framework — init.sh
# Initializes a .dev/ directory with workflow, skills, and standards.
# Usage:
# bash init.sh # interactive mode
# bash init.sh --config config.yml # config file mode
# bash init.sh --target /path/to/project
# curl -sL <url>/init.sh | bash # pipe mode (downloads repo first)
set -euo pipefail
# ─── Defaults ─────────────────────────────────────────────────────────────────
PROJECT_NAME=""
LANGUAGE=""
AUTHOR=""
YEAR="$(date +%Y)"
LICENSE=""
PACKAGE=""
DBC="false"
TARGET_DIR="."
CONFIG_FILE=""
CDF_ROOT="${CDF_ROOT:-}"
# ─── Argument parsing ─────────────────────────────────────────────────────────
while [[ $# -gt 0 ]]; do
case "$1" in
--config) CONFIG_FILE="$2"; shift 2 ;;
--target) TARGET_DIR="$2"; shift 2 ;;
--help|-h)
echo "Usage: init.sh [--config config.yml] [--target /path/to/project]"
exit 0
;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done
# ─── Locate framework files ──────────────────────────────────────────────────
find_cdf_root() {
if [[ -n "$CDF_ROOT" && -d "$CDF_ROOT/templates" ]]; then
return
fi
# Running from cloned repo?
local script_dir
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [[ -d "$script_dir/templates" ]]; then
CDF_ROOT="$script_dir"
return
fi
# Pipe mode — download to temp dir
echo "Downloading claude-dev-framework..."
CDF_ROOT="$(mktemp -d)"
git clone --depth 1 https://github.com/YOUR_USERNAME/claude-dev-framework.git "$CDF_ROOT" 2>/dev/null \
|| { echo "Error: could not download framework. Clone it manually."; exit 1; }
trap "rm -rf '$CDF_ROOT'" EXIT
}
find_cdf_root
# ─── Config file parsing (simple YAML subset) ────────────────────────────────
parse_config() {
local file="$1"
[[ -f "$file" ]] || { echo "Config file not found: $file"; exit 1; }
while IFS=': ' read -r key value; do
# Skip comments and empty lines
[[ "$key" =~ ^#.*$ || -z "$key" ]] && continue
# Strip leading/trailing whitespace and quotes
value="$(echo "$value" | sed 's/^[ "]*//;s/[ "]*$//')"
case "$key" in
project_name) PROJECT_NAME="$value" ;;
language) LANGUAGE="$value" ;;
author) AUTHOR="$value" ;;
year) YEAR="$value" ;;
license) LICENSE="$value" ;;
package) PACKAGE="$value" ;;
dbc) DBC="$value" ;;
esac
done < "$file"
}
# ─── Interactive prompts ─────────────────────────────────────────────────────
prompt() {
local var_name="$1" prompt_text="$2" default="$3"
local current_val="${!var_name}"
if [[ -n "$current_val" ]]; then
return
fi
if [[ -n "$default" ]]; then
read -rp "$prompt_text [$default]: " input
eval "$var_name=\"${input:-$default}\""
else
read -rp "$prompt_text: " input
eval "$var_name=\"$input\""
fi
}
interactive_setup() {
echo ""
echo "╔══════════════════════════════════════════════════╗"
echo "║ claude-dev-framework — initialization ║"
echo "╚══════════════════════════════════════════════════╝"
echo ""
local default_name
default_name="$(basename "$(cd "$TARGET_DIR" && pwd)")"
prompt PROJECT_NAME "Project name" "$default_name"
prompt LANGUAGE "Language (java, python, typescript)" ""
prompt AUTHOR "Author name" ""
prompt LICENSE "License (MIT, LGPL-3.0, Apache-2.0, none)" "MIT"
if [[ "$LANGUAGE" == "java" ]]; then
prompt PACKAGE "Java package (e.g. com.example.mylib)" ""
fi
read -rp "Enable Design by Contract workflow? (y/N): " dbc_input
[[ "$dbc_input" =~ ^[Yy]$ ]] && DBC="true"
}
# ─── Load config or run interactive ──────────────────────────────────────────
if [[ -n "$CONFIG_FILE" ]]; then
parse_config "$CONFIG_FILE"
else
interactive_setup
fi
# ─── Derived variables ────────────────────────────────────────────────────────
PACKAGE_PATH="${PACKAGE//./\/}"
case "$LANGUAGE" in
java) SRC_MAIN="src/main/java"; SRC_TEST="src/test/java" ;;
python) SRC_MAIN="src"; SRC_TEST="tests" ;;
typescript) SRC_MAIN="src"; SRC_TEST="tests" ;;
*) SRC_MAIN="src"; SRC_TEST="tests" ;;
esac
# ─── License header generation ────────────────────────────────────────────────
generate_license_header() {
case "$LICENSE" in
MIT)
cat <<HEADER
/*
* \${CLASS_NAME}
*
* Version \${VERSION}
*
* Copyright (c) $YEAR $AUTHOR
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
HEADER
;;
LGPL-3.0)
cat <<HEADER
/*
* \${CLASS_NAME}
*
* Version \${VERSION}
*
* $PROJECT_NAME
* Copyright (C) $YEAR $AUTHOR
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <https://www.gnu.org/licenses/>.
*/
HEADER
;;
Apache-2.0)
cat <<HEADER
/*
* \${CLASS_NAME}
*
* Version \${VERSION}
*
* Copyright $YEAR $AUTHOR
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
HEADER
;;
none|"")
cat <<HEADER
/*
* \${CLASS_NAME}
*
* Version \${VERSION}
*
* Copyright (c) $YEAR $AUTHOR
*/
HEADER
;;
esac
}
LICENSE_HEADER="$(generate_license_header)"
# ─── DbC conditional content ─────────────────────────────────────────────────
DBC_SKILLS=""
DBC_STUB_DETAILS=""
DBC_TEST_NOTE=""
DBC_CONFORMANCE_STEP=""
DBC_COMPLETENESS_STEP=""
DBC_SKILLS_TABLE=""
JML_STANDARDS=""
if [[ "$DBC" == "true" ]]; then
# Context.md skills table entries
if [[ "$LANGUAGE" == "java" ]]; then
DBC_SKILLS="| Writing JML contracts | \`.dev/skills/jml-design/SKILL.md\` |
| Generating or reviewing tests from JML specs | \`.dev/skills/jml-test-generation/SKILL.md\` |
| After implementation, verify code matches contracts | \`.dev/skills/jml-conformance-check/SKILL.md\` |
| After implementation is stable, verify contract completeness | \`.dev/skills/jml-completeness-check/SKILL.md\` |"
DBC_STUB_DETAILS="For Java projects using JML, express contracts as JML annotations:
- Class invariants as \`@invariant\`
- Pre-conditions as \`@requires\`
- Post-conditions as \`@ensures\`
Run the \`jml-design\` skill during this step."
DBC_TEST_NOTE="Use the \`jml-test-generation\` skill to systematically derive test cases
from the JML contracts written in step 2."
DBC_CONFORMANCE_STEP="Run the \`jml-conformance-check\` skill."
DBC_COMPLETENESS_STEP="Run the \`jml-completeness-check\` skill."
DBC_SKILLS_TABLE="| \`jml-design\` | Write JML contracts during stub step |
| \`jml-test-generation\` | Derive tests from JML specs |
| \`jml-conformance-check\` | Verify code conforms to JML specs |
| \`jml-completeness-check\` | Verify JML specs cover all code |"
JML_STANDARDS="
---
## 9. JML annotations
### Placement relative to Java annotations
JML specification comments (\`//@ requires\`, \`//@ ensures\`, \`//@ also\`, etc.)
must be placed **after** the Javadoc block and **before** any Java annotations
(\`@Override\`, \`@SuppressWarnings\`, etc.).
Correct:
\`\`\`java
/**
* Adds the specified element to this collection.
*
* @param e element to add
* @return {@code true} if the collection changed
*/
//@ requires e != null;
//@ ensures contains(e);
@Override
public boolean add(E e) {
\`\`\`
Incorrect (OpenJML will reject this):
\`\`\`java
@Override
//@ requires e != null;
public boolean add(E e) {
\`\`\`
### \`also\` keyword on overriding methods
When a method overrides a parent method (from a superclass or interface), its
JML specification must begin with \`//@ also\` to indicate that the contract
extends the parent contract. This applies to all \`@Override\` methods.
\`\`\`java
//@ also
//@ requires e != null;
//@ ensures contains(e);
@Override
public boolean add(E e) {
\`\`\`"
else
# Non-Java DbC: generic placeholders, no JML-specific skills
DBC_STUB_DETAILS="Express contracts using the appropriate mechanism for your language:
- **Python**: use docstring contracts and runtime assertions
- **TypeScript**: use JSDoc contracts and runtime checks
Document invariants, pre-conditions, and post-conditions explicitly."
DBC_TEST_NOTE="Systematically derive test cases from the contracts written in step 2.
For each pre-condition, test both the nominal and violation cases.
For each post-condition, verify it holds after the operation."
DBC_CONFORMANCE_STEP="Manually review the implementation against the formal contracts."
DBC_COMPLETENESS_STEP="Manually review the contracts for completeness against the implementation."
DBC_SKILLS_TABLE=""
fi
fi
# ─── Template expansion ──────────────────────────────────────────────────────
expand_template() {
local input="$1" output="$2"
sed \
-e "s|{{PROJECT_NAME}}|$PROJECT_NAME|g" \
-e "s|{{PACKAGE}}|$PACKAGE|g" \
-e "s|{{PACKAGE_PATH}}|$PACKAGE_PATH|g" \
-e "s|{{AUTHOR}}|$AUTHOR|g" \
-e "s|{{YEAR}}|$YEAR|g" \
-e "s|{{LICENSE}}|$LICENSE|g" \
-e "s|{{LANGUAGE}}|$LANGUAGE|g" \
-e "s|{{SRC_MAIN}}|$SRC_MAIN|g" \
-e "s|{{SRC_TEST}}|$SRC_TEST|g" \
"$input" > "$output"
# Multi-line replacements (sed can't handle these well)
local tmp
tmp="$(mktemp)"
# Replace all multi-line placeholders
for var in LICENSE_HEADER DBC_SKILLS DBC_STUB_DETAILS DBC_TEST_NOTE \
DBC_CONFORMANCE_STEP DBC_COMPLETENESS_STEP DBC_SKILLS_TABLE \
JML_STANDARDS; do
local pattern="{{${var}}}"
local value="${!var}"
awk -v pat="$pattern" -v rep="$value" '{
idx = index($0, pat)
if (idx > 0) {
printf "%s", substr($0, 1, idx - 1)
printf "%s", rep
print substr($0, idx + length(pat))
} else {
print
}
}' "$output" > "$tmp" && mv "$tmp" "$output"
done
rm -f "$tmp"
}
copy_file() {
local input="$1" output="$2"
cp "$input" "$output"
}
# ─── Create directory structure ───────────────────────────────────────────────
DEV_DIR="$TARGET_DIR/.dev"
echo ""
echo "Creating .dev/ structure in $TARGET_DIR ..."
mkdir -p "$DEV_DIR/standards"
mkdir -p "$DEV_DIR/skills"
mkdir -p "$DEV_DIR/design"
mkdir -p "$DEV_DIR/backlog"
# ─── Expand templates ─────────────────────────────────────────────────────────
expand_template "$CDF_ROOT/templates/CONTEXT.md.tmpl" "$DEV_DIR/CONTEXT.md"
if [[ "$DBC" == "true" ]]; then
expand_template "$CDF_ROOT/templates/WORKFLOW-dbc.md.tmpl" "$DEV_DIR/WORKFLOW.md"
else
expand_template "$CDF_ROOT/templates/WORKFLOW.md.tmpl" "$DEV_DIR/WORKFLOW.md"
fi
expand_template "$CDF_ROOT/scaffolds/ARCHITECTURE.md.tmpl" "$DEV_DIR/design/ARCHITECTURE.md"
expand_template "$CDF_ROOT/scaffolds/BACKLOG.md.tmpl" "$DEV_DIR/backlog/BACKLOG.md"
expand_template "$CDF_ROOT/scaffolds/INVARIANTS.md.tmpl" "$DEV_DIR/design/INVARIANTS.md"
# ─── Copy standards for selected language ─────────────────────────────────────
case "$LANGUAGE" in
java)
expand_template "$CDF_ROOT/templates/standards/java/STANDARDS.md.tmpl" \
"$DEV_DIR/standards/JAVA_STANDARDS.md"
;;
python)
copy_file "$CDF_ROOT/templates/standards/python/STANDARDS.md" \
"$DEV_DIR/standards/PYTHON_STANDARDS.md"
;;
typescript)
copy_file "$CDF_ROOT/templates/standards/typescript/STANDARDS.md" \
"$DEV_DIR/standards/TYPESCRIPT_STANDARDS.md"
;;
*)
echo "Warning: no built-in standards for language '$LANGUAGE'."
echo "You can add your own in $DEV_DIR/standards/"
;;
esac
# ─── Copy skills ──────────────────────────────────────────────────────────────
CORE_SKILLS=(
static-analysis
design-review
test-coverage-review
api-consistency-check
architecture-drift-check
update-backlog
update-readme
new-class
refactor-class
)
DBC_SKILL_LIST=(
jml-design
jml-test-generation
jml-conformance-check
jml-completeness-check
)
for skill in "${CORE_SKILLS[@]}"; do
mkdir -p "$DEV_DIR/skills/$skill"
copy_file "$CDF_ROOT/skills/$skill/SKILL.md" "$DEV_DIR/skills/$skill/SKILL.md"
done
if [[ "$DBC" == "true" && "$LANGUAGE" == "java" ]]; then
for skill in "${DBC_SKILL_LIST[@]}"; do
mkdir -p "$DEV_DIR/skills/$skill"
copy_file "$CDF_ROOT/skills/$skill/SKILL.md" "$DEV_DIR/skills/$skill/SKILL.md"
done
fi
# ─── Generate config.yml record ───────────────────────────────────────────────
cat > "$DEV_DIR/config.yml" <<CONF
# claude-dev-framework configuration — generated on $(date +%Y-%m-%d)
project_name: $PROJECT_NAME
language: $LANGUAGE
author: $AUTHOR
year: $YEAR
license: $LICENSE
package: $PACKAGE
dbc: $DBC
CONF
# ─── Summary ──────────────────────────────────────────────────────────────────
echo ""
echo "Done! Generated .dev/ structure:"
echo ""
echo " $DEV_DIR/"
echo " ├── CONTEXT.md"
echo " ├── WORKFLOW.md"
echo " ├── config.yml"
echo " ├── standards/"
echo " │ └── $(echo "$LANGUAGE" | tr '[:lower:]' '[:upper:]')_STANDARDS.md"
echo " ├── design/"
echo " │ ├── ARCHITECTURE.md"
echo " │ └── INVARIANTS.md"
echo " ├── backlog/"
echo " │ └── BACKLOG.md"
echo " └── skills/"
for skill in "${CORE_SKILLS[@]}"; do
echo " ├── $skill/"
done
if [[ "$DBC" == "true" && "$LANGUAGE" == "java" ]]; then
for skill in "${DBC_SKILL_LIST[@]}"; do
echo " ├── $skill/"
done
fi
echo ""
if [[ "$DBC" == "true" ]]; then
echo " Workflow: Design by Contract (14 steps)"
else
echo " Workflow: Standard (10 steps)"
fi
echo ""
echo "Next steps:"
echo " 1. Edit $DEV_DIR/CONTEXT.md to describe your project"
echo " 2. Start coding with Claude using the workflow in $DEV_DIR/WORKFLOW.md"
echo ""
main() { :; }
main