Files
learn-rust/create_project.sh
2025-07-01 15:15:13 +03:00

37 lines
974 B
Bash
Executable File

#!/bin/bash
set -e
name="$1"
if [ -z "$name" ]; then
echo "❌ Usage: ./create_project.sh <project_name>"
exit 1
fi
# Step 1: Create new cargo project without Git
cargo new "$name" --vcs none
# Step 2: Insert project into workspace Cargo.toml
if grep -q "^\[workspace\]" Cargo.toml; then
if grep -q "\"$name\"" Cargo.toml; then
echo "⚠️ Project '$name' already exists in workspace members. Skipping insertion."
else
echo "📦 Adding '$name' to workspace members..."
# Find the line number of the closing bracket ]
end_line=$(awk '/members\s*=\s*\[/,/\]/{if ($0 ~ /\]/) print NR}' Cargo.toml)
# Insert before the closing bracket
awk -v name="$name" -v end="$end_line" '
NR == end { print " \"" name "\"," }
{ print }
' Cargo.toml > Cargo.toml.tmp && mv Cargo.toml.tmp Cargo.toml
fi
else
echo "❌ No [workspace] section found in Cargo.toml."
exit 1
fi
echo "✅ Project '$name' created and added to workspace!"