Skip to content

Commit a82e49f

Browse files
committed
add script for create mathlib projects
1 parent a9fefeb commit a82e49f

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

Projects/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v4.*

Projects/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,32 @@ This will add an entry `My Cool Example` to the Example menu which loads
3838
the file from `Projects/my-cool-project/MyCustomProject/Demo1.lean`.
3939

4040
You might want to look at the provided `mathlib-demo` project for comparison.
41+
42+
## Creating Mathlib Projects with Different Versions
43+
44+
To create a new mathlib project with a specific Lean version, you can use the script `create_project.sh`:
45+
46+
```bash
47+
./create_project.sh <version>
48+
```
49+
50+
The version should be in the format of `v4.x.x` or `4.x.x`. For example:
51+
```bash
52+
./create_project.sh v4.13.0
53+
# or
54+
./create_project.sh 4.13.0
55+
```
56+
57+
This will:
58+
1. Create a new project in `Projects/<version>/`
59+
2. Set up the correct Lean toolchain version
60+
3. Configure mathlib dependencies
61+
4. Build the project with cache
62+
63+
Finally, you can add the project to the `config.json` file as described above.
64+
65+
```
66+
{ "folder": "v4.13.0",
67+
"name": "Lean 4.13.0",
68+
}
69+
```

Projects/create_project.sh

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/bin/bash
2+
# Exit on error
3+
set -e
4+
5+
cd "$(dirname "$0")"
6+
7+
# Check if version argument is provided
8+
if [ -z "$1" ]; then
9+
echo "Error: Please provide a Lean version number"
10+
echo "Usage: $0 <version>"
11+
exit 1
12+
fi
13+
14+
lean_version=$1
15+
16+
# Validate version format and add 'v' prefix if missing
17+
if [[ ! $lean_version =~ ^v?4\.[0-9]+\.[0-9]+$ ]]; then
18+
echo "Error: Invalid version format. Must be v4.x.x or 4.x.x"
19+
exit 1
20+
fi
21+
22+
# Add 'v' prefix if missing
23+
[[ $lean_version != v* ]] && lean_version="v$lean_version"
24+
25+
# Check if version directory already exists
26+
if [ -d "$lean_version" ]; then
27+
read -p "Directory $lean_version already exists. Overwrite? (y/N) " -n 1 -r
28+
echo
29+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
30+
echo "Operation cancelled."
31+
exit 1
32+
fi
33+
fi
34+
35+
# Create temporary directory
36+
temp_dir=$(mktemp -d)
37+
trap 'rm -rf "$temp_dir"' EXIT
38+
39+
echo "Creating new Lean 4 project of version $lean_version..."
40+
pushd "$temp_dir"
41+
42+
# Create new project
43+
lake new lean4web math.lean
44+
45+
pushd lean4web
46+
47+
# Set lean-toolchain version
48+
echo leanprover/lean4:$lean_version > lean-toolchain
49+
50+
# Update lakefile.lean
51+
echo "Updating lakefile.lean..."
52+
cat > lakefile.lean << EOF
53+
import Lake
54+
open Lake DSL
55+
56+
package «lean4web» where
57+
-- add any additional package configuration options here
58+
59+
require mathlib from git
60+
"https://github.com/leanprover-community/mathlib4.git" @ "$lean_version"
61+
62+
@[default_target]
63+
lean_lib «Lean4web» where
64+
-- add any library configuration options here
65+
EOF
66+
67+
# Update dependencies and build
68+
echo "Updating dependencies..."
69+
lake update -R || { echo "Error: lake update failed"; exit 1; }
70+
71+
echo "Getting cache..."
72+
lake exe cache get || { echo "Error: cache download failed"; exit 1; }
73+
74+
echo "Building project..."
75+
lake build || { echo "Error: build failed"; exit 1; }
76+
77+
# If everything succeeded, move to final location
78+
popd
79+
popd
80+
81+
rm -rf "$lean_version" 2>/dev/null || true
82+
mv "$temp_dir/lean4web" "$lean_version"
83+
84+
echo "Setup completed successfully!"

0 commit comments

Comments
 (0)