-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathjustfile
475 lines (371 loc) · 14.4 KB
/
justfile
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
# Format entire project
format:
cargo fmt
pnpm format
# Build the WebAssembly for a specific target (usually either `web` or `bundler`)
build-wasm:
cd "{{justfile_directory()}}/harper-wasm" && wasm-pack build --target web
# Build `harper.js` with all size optimizations available.
build-harperjs: build-wasm
#! /bin/bash
set -eo pipefail
pnpm install
# Removes a duplicate copy of the WASM binary if Vite is left to its devices.
perl -pi -e 's/new URL\(.*\)/new URL()/g' "{{justfile_directory()}}/harper-wasm/pkg/harper_wasm.js"
cd "{{justfile_directory()}}/packages/harper.js"
pnpm build
# Generate API reference
./docs.sh
test-harperjs: build-harperjs
#!/bin/bash
set -eo pipefail
pnpm install
cd "{{justfile_directory()}}/packages/harper.js"
pnpm playwright install
pnpm test
# Test runnable examples
cd "{{justfile_directory()}}/packages/harper.js/examples/commonjs-simple"
pnpm start
dev-wp: build-harperjs
#! /bin/bash
set -eo pipefail
cd "{{justfile_directory()}}/packages/wordpress-plugin"
pnpm install
pnpm wp-now start &
pnpm start
# Build the WordPress plugin
build-wp: build-harperjs
#! /bin/bash
set -eo pipefail
cd "{{justfile_directory()}}/packages/wordpress-plugin"
pnpm install
pnpm build
pnpm plugin-zip
# Compile the website's dependencies and start a development server. Note that if you make changes to `harper-wasm`, you will have to re-run this command.
dev-web: build-harperjs
#! /bin/bash
set -eo pipefail
cd "{{justfile_directory()}}/packages/web"
pnpm install
pnpm dev
# Build the Harper website.
build-web: build-harperjs
#! /bin/bash
set -eo pipefail
cd "{{justfile_directory()}}/packages/web"
pnpm install
pnpm build
# Build the Harper Obsidian plugin.
build-obsidian: build-harperjs
#! /bin/bash
set -eo pipefail
cd "{{justfile_directory()}}/packages/obsidian-plugin"
pnpm install
pnpm build
zip harper-obsidian-plugin.zip manifest.json main.js
# Run VSCode plugin unit and integration tests.
test-vscode:
#! /bin/bash
set -eo pipefail
ext_dir="{{justfile_directory()}}/packages/vscode-plugin"
bin_dir="${ext_dir}/bin"
if ! [[ -d "$bin_dir" ]]; then
mkdir "$bin_dir"
fi
cargo build --release
cp "{{justfile_directory()}}/target/release/harper-ls"* "$bin_dir"
cd "$ext_dir"
pnpm install
# For environments without displays like CI servers or containers
if [[ "$(uname)" == "Linux" ]] && [[ -z "$DISPLAY" ]]; then
xvfb-run --auto-servernum pnpm test
else
pnpm test
fi
# Build and package the Visual Studio Code extension.
# If `target` is passed, it is assumed that `harper-ls` has been compiled beforehand and is in `packages/vscode-plugin/bin`. This is used in CI.
package-vscode target="":
#! /bin/bash
set -eo pipefail
ext_dir="{{justfile_directory()}}/packages/vscode-plugin"
bin_dir="${ext_dir}/bin"
cp LICENSE "$ext_dir"
if [[ -z "{{target}}" ]]; then
cargo build --release
if ! [[ -d "$bin_dir" ]]; then
mkdir "$bin_dir"
fi
cp "{{justfile_directory()}}/target/release/harper-ls"* "$bin_dir"
fi
cd "$ext_dir"
pnpm install
if [[ -n "{{target}}" ]]; then
pnpm package --target {{target}}
else
pnpm package
fi
update-vscode-linters:
#! /bin/bash
set -eo pipefail
linters=$(
cargo run --bin harper-cli -- config |
jq 'with_entries(.key |= "harper.linters." + . |
.value |= {
"scope": "resource",
"type": "boolean",
"default": .default_value,
"description": .description
}
)'
)
cd "{{justfile_directory()}}/packages/vscode-plugin"
manifest_without_linters=$(
jq 'walk(
if type == "object" then
with_entries(select(.key | startswith("harper.linters") | not))
end
)' package.json
)
jq --argjson linters "$linters" \
'.contributes.configuration.properties += $linters' <<< \
"$manifest_without_linters" > \
package.json
just format
# Run Rust formatting and linting.
check-rust:
#! /bin/bash
set -eo pipefail
cargo fmt -- --check
cargo clippy -- -Dwarnings -D clippy::dbg_macro -D clippy::needless_raw_string_hashes
# Perform format and type checking.
check: check-rust build-web
#! /bin/bash
set -eo pipefail
pnpm install
pnpm check
# Needed because Svelte has special linters
cd "{{justfile_directory()}}/packages/web"
pnpm check
# Populate build caches and install necessary local tooling (tools callable via `pnpm run <tool>`).
setup: build-harperjs build-obsidian test-vscode test-harperjs build-web build-wp
# Perform full format and type checking, build all projects and run all tests. Run this before pushing your code.
precommit: check test build-harperjs build-obsidian build-web build-wp
#! /bin/bash
set -eo pipefail
cargo doc
cargo build
# Install `harper-cli` and `harper-ls` to your machine via `cargo`
install:
cargo install --path harper-ls --locked
cargo install --path harper-cli --locked
# Run `harper-cli` on the Harper repository
dogfood:
#! /bin/bash
cargo build --release
for file in `fd -e rs`
do
echo Linting $file
./target/release/harper-cli lint $file
done
# Test everything.
test: test-vscode test-harperjs
cargo test
# Use `harper-cli` to parse a provided file and print out the resulting tokens.
parse file:
cargo run --bin harper-cli -- parse {{file}}
# Lint a provided file using Harper and print the results.
lint file:
cargo run --bin harper-cli -- lint {{file}}
# Show the spans of the parsed tokens overlapped in the provided file.
spans file:
cargo run --bin harper-cli -- spans {{file}}
# Add a noun to Harper's curated dictionary.
addnoun noun:
#! /bin/bash
DICT_FILE=./harper-core/dictionary.dict
cat $DICT_FILE | grep "^{{noun}}/"
if [ $? -eq 0 ]
then
echo "That noun may already be in the dictionary."
exit 0
fi
# 'M': possessive -'s suffix for both common and proper nouns
flags='M'
# If the first letter is uppercase, treat it as a proper noun
if [[ "{{noun}}" =~ ^[A-Z] ]]; then
# '2': proper noun, usually no plural
flags+='2'
else
# '1': (common) singular noun, 'S': plural -(e)s
flags+='1S'
fi
# Echo the noun with its flags to the dictionary file
[[ -s $DICT_FILE && -n $(tail -c1 "$DICT_FILE") ]] && echo >> "$DICT_FILE"
echo "{{noun}}/$flags" >> "$DICT_FILE"
# Search Harper's curated dictionary for a specific word
searchdictfor word:
#! /bin/bash
if command -v rg > /dev/null; then
cargo run --bin harper-cli -- words | rg {{word}}
else
cargo run --bin harper-cli -- words | grep {{word}}
fi
# Find words in the user's `harper-ls/dictionary.txt` for words already in the curated dictionary.
userdictoverlap:
#! /bin/bash
USER_DICT_FILE="$HOME/.config/harper-ls/dictionary.txt"
while read -r line; do
just searchdictfor $line 2> /dev/null
done < $USER_DICT_FILE
# Get the metadata associated with a particular word in Harper's dictionary as JSON.
getmetadata word:
cargo run --bin harper-cli -- metadata {{word}}
# Get all the forms of a word using the affixes.
getforms word:
cargo run --bin harper-cli -- forms {{word}}
# Get a random sample of words from Harper's dictionary and list all forms of each.
sampleforms count:
#!/bin/bash
set -eo pipefail
DICT_FILE=./harper-core/dictionary.dict
# USER_DICT_FILE="$HOME/.config/harper-ls/dictionary.txt"
if [ "{{count}}" -eq 0 ]; then
exit 0
fi
total_lines=$(wc -l < $DICT_FILE)
# Cross-platform random line selection
if command -v shuf >/dev/null 2>&1; then
words=$(shuf -n "{{count}}" "$DICT_FILE")
elif command -v jot >/dev/null 2>&1; then
words=$(jot -r "{{count}}" 1 "$total_lines" | while read -r line_num; do \
sed -n "$line_num"p "$DICT_FILE"; \
done)
else
echo "Error: Neither 'shuf' nor 'jot' found. Cannot generate random words." >&2
exit 1
fi
cargo run --bin harper-cli -- forms $words
bump-versions: update-vscode-linters
#! /bin/bash
set -eo pipefail
cargo ws version --no-git-push --no-git-tag --force '*'
HARPER_VERSION=$(tq --file harper-core/Cargo.toml .package.version)
cd "{{justfile_directory()}}/packages/harper.js"
cat package.json | jq ".version = \"$HARPER_VERSION\"" > package.json.edited
mv package.json.edited package.json
cd "{{justfile_directory()}}/packages/vscode-plugin"
cat package.json | jq ".version = \"$HARPER_VERSION\"" > package.json.edited
mv package.json.edited package.json
just format
lazygit
# Enter an infinite loop of property testing until a bug is found.
fuzz:
#!/usr/bin/bash
while true
do
QUICKCHECK_TESTS=100000 cargo test
if [[ x$? != x0 ]] ; then
exit $?
fi
done
registerlinter module name:
#! /bin/bash
D="{{justfile_directory()}}/harper-core/src/linting"
sed -i "/pub use an_a::AnA;/a pub use {{module}}::{{name}};" "$D/mod.rs"
sed -i "/use super::an_a::AnA;/a use super::{{module}}::{{name}};" "$D/lint_group.rs"
sed -i "/insert_struct_rule!(ChockFull, true);/a \ \ \ \ insert_struct_rule!({{name}}, true);" "$D/lint_group.rs"
just format
# Print affixes and their descriptions from affixes.json
printaffixes:
#! /usr/bin/env node
Object.entries(
require('{{justfile_directory()}}/harper-core/affixes.json').affixes
).forEach(([affix, fields]) => {
const description = fields['#'] || '';
description && console.log(affix + ': ' + description);
});
# Get the most recent changes to the curated dictionary. Includes an optional argument to specify the number of commits to look back. Defaults to 1.
newest-dict-changes *numCommits:
#! /usr/bin/env node
const { exec } = require('child_process');
const DICT_FILE = 'harper-core/dictionary.dict';
const [RST, BOLD, DIM, ITAL, NORM] = [0, 1, 2, 3, 22].map(c => `\x1b[${c}m`);
const [RED, GRN, YLW, BLU, MGN, CYN, WHT] = [1, 2, 3, 4, 5, 6, 7].map(c => `\x1b[${30+c}m`);
const argv = [...process.argv];
const [showHashes, showDiff] = ["--show-hashes", "--show-diff"].map(flag => argv.includes(flag) && argv.splice(argv.indexOf(flag), 1));
// uncomment first line to use in justfile, comment out second line to use standalone
const numCommits = "{{numCommits}}" || 1;
// const numCommits = argv[2] || 1;
// Command to get the last commit hash that modified the specified file
const hashCommand = `git log --no-merges -n ${numCommits} --format="%H" -- ${DICT_FILE}`;
console.log(`${MGN}${BOLD}GET HASHES${NORM}: ${hashCommand}${RST}`);
// Execute the command to get the hash
exec(hashCommand, (error, hashString, stderr) => {
if (error) return console.error(`Error executing command: ${error.message}`);
if (stderr) return console.error(`stderr: ${stderr}`);
// avoid empty last line
const longHashes = hashString.trim().split('\n');
if (showHashes) console.log(longHashes.length, longHashes);
if (longHashes.length < 1) {
console.error('No hash(es) returned. Exiting.');
process.exit(1);
}
// keep the last line and second last if there's more than one hash
const [hash2, hash1] = longHashes.slice(-2).map((h) => h.substring(0, 7));
// Command to get the word-level diff using the retrieved hash, using either one or two hashes
const hashes = longHashes.length == 1 ? `${hash2}` : `${hash1} ${hash2}`;
const diffCommand = `git diff --word-diff --no-color --unified=0 ${hashes} -- ${DICT_FILE}`;
console.log(`${MGN}${BOLD}GET DIFF${NORM}: ${diffCommand}${RST}`);
// Execute the diff command with a large buffer to avoid failing to handle large diffs such as:
// git diff --word-diff --no-color --unified=0 0761702 baeb08e -- harper-core/dictionary.dict
exec(diffCommand, { maxBuffer: 2048 * 1024 }, (diffError, diffString, diffStderr) => {
if (diffError) {
console.error(`Error executing diff command: ${diffError.message}`);
return;
}
if (diffStderr) return console.error(`stderr: ${diffStderr}`);
if (showDiff) console.log(`DIFFSTART\n${diffString}\nDIFFEND`);
// uncomment first line to use in justfile, comment out second line to use standalone
const affixes = require('{{justfile_directory()}}/harper-core/affixes.json').affixes;
// const affixes = require('./harper-core/affixes.json').affixes;
diffString.split("\n").forEach(line => {
const match = line.match(/^(?:\[-(.*?)-\])?(?:\{\+(.*?)\+\})?$/);
if (match) {
let [, before, after] = match;
if (before && after) {
// An entry changed
const [[oldword, oldaff], [newword, newaff]] = [before, after].map(e => e.split('/'));
if (oldword === newword) {
if (oldaff !== newaff) {
const [oldRest, newRest] = [oldaff, newaff].map(aff => aff ? `${DIM}/${aff}${RST}`: '');
console.log(`${BOLD}${CYN}CHG${RST} # ${oldword}${oldRest} -> ${newRest}`);
const [oldNorm, newNorm] = [oldaff, newaff].map(a => a ? a.split(''): [])
.map(a => new Set(a))
.map(a => Array.from(a))
.map(a => a.sort());
const removed = oldNorm.filter(o => !newNorm.includes(o));
const added = newNorm.filter(n => !oldNorm.includes(n));
const [addStr, remStr] = [added, removed]
.map(a => a.map(a => ` ${BOLD}${ITAL}${a}${RST} -> ${ (affixes[a] && affixes[a]['#']) || '???' }`)
.join('\n')
);
if (removed.length > 0) console.log(`${RED} ${BOLD}REMOVED${RST}:\n${remStr}`);
if (added.length > 0) console.log(`${GRN} ${BOLD}ADDED${RST}:\n${addStr}`);
} else {
// should never happen
console.log(`${YLW} ?NO AFFIX CHG? '${oldaff}' -> '${newaff}'${RST}`);
}
} else {
// The word changed rather than its affixes
console.log(`${YLW} ${BOLD}CHANGED${RST} ${RED}${oldword}${RST} -> ${GRN}${newword}${RST}`);
}
} else if (before || after) {
// An entry was added or removed
const [entry, symbol, action, colour] = before ? [before, "-", 'DEL', RED] : [after, "+", 'ADD', GRN];
const [word, affix] = entry.split('/');
console.log(`${colour}${BOLD}${action}${RST} ${symbol} ${word}${ affix ? `${DIM}/${affix}` : '' }${RST}`);
}
}
});
});
});