-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJustfile
More file actions
254 lines (216 loc) · 7.77 KB
/
Justfile
File metadata and controls
254 lines (216 loc) · 7.77 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
# Justfile for leetcode project
# Path to lc command
lc := "~/.pyenv/versions/leetcode/bin/lc"
# Detect language from invocation directory (before just changes to Justfile dir)
# This is set by the shell before just runs
invocation_dir := env('PWD', '')
start *args:
{{lc}} start {{args}}
submit problem_number lang="":
#!/usr/bin/env bash
lang_arg=""
if [[ -n "{{lang}}" ]]; then
lang_arg="--lang {{lang}}"
else
dir_name="$(basename "{{invocation_dir}}")"
if [[ "$dir_name" =~ ^(cpp|py|rs)$ ]]; then
lang_arg="--lang $dir_name"
fi
fi
{{lc}} submit {{problem_number}} $lang_arg
try problem_number lang="":
#!/usr/bin/env bash
lang_arg=""
if [[ -n "{{lang}}" ]]; then
lang_arg="--lang {{lang}}"
else
dir_name="$(basename "{{invocation_dir}}")"
if [[ "$dir_name" =~ ^(cpp|py|rs)$ ]]; then
lang_arg="--lang $dir_name"
fi
fi
{{lc}} try {{problem_number}} $lang_arg
setup +langs:
#!/usr/bin/env bash
set -euo pipefail
langs="{{langs}}"
# Validate languages
valid_langs="cpp py rs"
for lang in $langs; do
if ! echo "$valid_langs" | grep -qw "$lang"; then
echo "Error: Invalid language '$lang'. Valid options: $valid_langs" >&2
exit 1
fi
done
has_lang() {
echo "$langs" | grep -qw "$1"
}
# Helper to create language .envrc (non-destructive)
create_lang_envrc() {
local lang_dir="$1"
local lang_slug="$2"
local source_up_line="source_up"
local envrc_line="leetcode workspace use $lang_slug > /dev/null 2>&1"
local envrc_path="$lang_dir/.envrc"
if [ ! -f "$envrc_path" ]; then
printf '%s\n%s\n' "$source_up_line" "$envrc_line" > "$envrc_path"
echo "Created $envrc_path"
else
if ! grep -qF "$source_up_line" "$envrc_path"; then
# Prepend source_up before existing content
local tmp
tmp="$(cat "$envrc_path")"
printf '%s\n%s\n' "$source_up_line" "$tmp" > "$envrc_path"
echo "Added source_up to $envrc_path"
fi
if ! grep -qF "$envrc_line" "$envrc_path"; then
echo "$envrc_line" >> "$envrc_path"
echo "Added workspace activation to $envrc_path"
else
echo "$envrc_path already configured"
fi
fi
if command -v direnv &> /dev/null; then
direnv allow "$envrc_path"
fi
}
# Map our language slugs to leetcode-cli language names
leetcode_lang() {
case "$1" in
cpp) echo "cpp" ;;
py) echo "python3" ;;
rs) echo "rust" ;;
esac
}
# Helper to create workspace if it doesn't exist, and configure its language
create_workspace() {
local lang_slug="$1"
local lang_dir="$2"
if ! leetcode workspace list 2>/dev/null | grep -q "^ $lang_slug$"; then
echo "Creating workspace '$lang_slug'..."
leetcode workspace create -w "$lang_dir" "$lang_slug"
else
echo "Workspace '$lang_slug' already exists"
fi
echo "Configuring workspace '$lang_slug' language..."
leetcode workspace use "$lang_slug" > /dev/null 2>&1
leetcode config -l "$(leetcode_lang "$lang_slug")"
}
# --- Shared setup ---
# Ensure nodeenv is installed
if ! command -v nodeenv &> /dev/null; then
echo "nodeenv not found, installing via homebrew..."
brew install nodeenv
else
echo "nodeenv is already installed"
fi
# Create node environment if it doesn't exist
if [ ! -d .node ]; then
echo "Creating node environment..."
nodeenv .node
else
echo "Node environment already exists"
fi
# Install leetcode-cli in node environment
echo "Installing leetcode-cli..."
source .node/bin/activate
npm install -g @night-slayer18/leetcode-cli
# Ensure pyenv virtualenv 'leetcode' exists (needed for lc command)
_pyenv_versions="$(pyenv versions --bare 2>/dev/null || true)"
if ! echo "$_pyenv_versions" | grep -q '^leetcode$'; then
echo "Creating pyenv virtualenv 'leetcode'..."
pyenv virtualenv 3 leetcode
else
echo "pyenv virtualenv 'leetcode' already exists"
fi
# Create .python-version for auto-activation (non-destructive)
if [ ! -f .python-version ]; then
echo "leetcode" > .python-version
echo "Created .python-version for auto-activation"
elif [ "$(cat .python-version)" != "leetcode" ]; then
echo 'Warning: .python-version exists with different content, skipping' >&2
else
echo '.python-version already configured'
fi
# Install base Python package (needed for lc command)
echo "Installing lc command..."
~/.pyenv/versions/leetcode/bin/pip install -e 'py/'
# --- C++ setup ---
if has_lang cpp; then
echo "=== Setting up C++ ==="
# Ensure cmake is installed
if ! command -v cmake &> /dev/null; then
echo "cmake not found, installing via homebrew..."
brew install cmake
else
echo "cmake is already installed"
fi
# Configure CMake for C++
echo "Configuring CMake..."
cmake -B cpp/build -S cpp
# Create workspace and .envrc
create_workspace cpp cpp
create_lang_envrc cpp cpp
fi
# --- Python setup ---
if has_lang py; then
echo "=== Setting up Python ==="
# Install dev dependencies (pytest, pytest-repeat, py-spy, etc.)
echo "Installing dev dependencies..."
~/.pyenv/versions/leetcode/bin/pip install -e 'py/[dev]'
# Create workspace and .envrc
create_workspace py py
create_lang_envrc py py
fi
# --- Rust setup ---
if has_lang rs; then
echo "=== Setting up Rust ==="
# Ensure rust/cargo is installed
if ! command -v cargo &> /dev/null; then
echo "cargo not found, installing via rustup..."
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source "$HOME/.cargo/env"
else
echo "cargo is already installed"
fi
# Create workspace and .envrc
create_workspace rs rs
create_lang_envrc rs rs
fi
# --- Profiling tools setup ---
# Install samply for profiling (C++/Rust) if any of those languages are selected
if has_lang cpp || has_lang rs; then
if ! command -v samply &>/dev/null; then
echo "Installing samply for profiling..."
cargo install samply
else
echo "samply is already installed"
fi
fi
# --- Default language selection ---
echo ""
echo "Select default language for 'just start':"
select default_lang in $langs; do
if [ -n "$default_lang" ]; then
break
fi
done
# --- Create root .envrc ---
echo "Creating .envrc..."
envrc_content="source_env_if_exists .node/bin/activate"$'\n'"export LEETCODE_DEFAULT_LANG=$default_lang"
# Check if .envrc exists and preserve any user additions
if [ -f .envrc ]; then
# Read existing content, filter out lines we manage
existing=$(grep -v '^source_env_if_exists .node/bin/activate$' .envrc | \
grep -v '^export LEETCODE_DEFAULT_LANG=' || true)
if [ -n "$existing" ]; then
envrc_content="$envrc_content"$'\n'"$existing"
fi
fi
envrc_file=.envrc
echo "$envrc_content" > "$envrc_file"
printf 'Created %s with LEETCODE_DEFAULT_LANG=%s\n' "$envrc_file" "$default_lang"
if command -v direnv &> /dev/null; then
direnv allow
fi
echo 'Setup complete!'