-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathrails.ts
697 lines (680 loc) · 17.3 KB
/
rails.ts
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import { filepaths } from "@fig/autocomplete-generators";
const RB_ICON =
"https://raw.githubusercontent.com/vscode-icons/vscode-icons/master/icons/file_type_ruby.svg";
const newCommand = {
name: "new",
description: "Create a new rails application",
args: {
name: "path",
description:
"Path to directory to be created containing the new application",
},
options: [
{
name: "-skip-namespace",
description: "Skip namespace (affects only isolated applications)",
args: {},
},
{
name: ["-r", "--ruby"],
description: "Path to the Ruby binary of your choice",
args: {},
},
{
name: "-m",
description:
"Path to some application template (can be a filesystem path or URL)",
},
{
name: "-d",
description: "Preconfigure for selected database - defaults to sqlite3",
args: {
name: "database",
suggestions: [
"mysql",
"postgresql",
"sqlite3",
"oracle",
"frontbase",
"ibm_db",
"sqlserver",
"jdbcmysql",
"jdbcsqlite3",
"jdbcpostgresql",
"jdbc",
],
},
},
{
name: "--skip-gemfile",
description: "Don't create a Gemfile",
},
{
name: "-G",
description: "Skip .gitignore file",
},
{
name: "--skip-keeps",
description: "Skip source control .keep files",
},
{
name: "-M",
description: "Skip Action Mailer files",
},
{
name: "--skip-action-mailbox",
description: "Skip Action Mailbox gem",
},
{
name: "--skip-action-text",
description: "Skip Action Text gem",
},
{
name: "-O",
description: "Skip Active Record files",
},
{
name: "--skip-active-storage",
description: "Skip Active Storage files",
},
{
name: "-P",
description: "Skip Puma related files",
},
{
name: "-C",
description: "Skip Action Cable files",
},
{
name: "-S",
description: "Skip Sprockets files",
},
{
name: "--skip-spring",
description: "Don't install Spring application preloader",
},
{
name: "--skip-listen",
description:
"Don't generate configuration that depends on the listen gem",
},
{
name: "-J",
description: "Skip JavaScript files",
},
{
name: "--skip-turbolinks",
description: "Skip turbolinks gem",
},
{
name: "-T",
description: "Skip test files",
},
{
name: "--skip-system-test",
description: "Skip system test files",
},
{
name: "--skip-bootsnap",
description: "Skip bootsnap gem",
},
{
name: "--dev",
description:
"Setup the application with Gemfile pointing to your Rails checkout",
},
{
name: "--edge",
description:
"Setup the application with Gemfile pointing to Rails repository",
},
{
name: "--rc",
description:
"Path to file containing extra configuration options for rails command",
args: { name: "path" },
},
{
name: "--no-rc",
description:
"Skip loading of extra configuration options from .railsrc file",
},
{
name: "--api",
description: "Preconfigure smaller stack for API only apps",
},
{
name: "-B",
description: "Don't run bundle install",
},
{
name: "--webpacker",
description: "Preconfigure Webpack with a particular framework",
args: {
name: "framework",
suggestions: ["react", "vue", "angular", "elm", "stimulus"],
},
},
{
name: "--skip-webpack-install]",
description: "Don't run Webpack install",
},
{
name: ["--f", "--force"],
description: "Overwrite files that already exist",
},
{
name: ["--p", "--pretend"],
description: "Run but do not make any changes",
},
{
name: ["--q", "--quiet"],
description: "Suppress status output",
},
{
name: ["--s", "--skip"],
description: "Skip files that already exist",
},
{
name: ["--h", "--help"],
description: "Show this help message and quit",
},
{
name: ["--v", "--version"],
description: "Show Rails version number and quit",
},
],
};
const dbOptions = [
{
name: "--backtrace",
description:
"Enable full backtrace. OUT can be stderr (default) or stdout",
},
{
name: "--comments",
description: "Show commented tasks only",
},
{
name: "--job-stats",
description:
"Display job statistics. LEVEL=history displays a complete job list",
},
{
name: "--rules",
description: "Trace the rules resolution",
},
{
name: "--suppress-backtrace",
description:
"Suppress backtrace lines matching regexp PATTERN. Ignored if --trace is on",
args: { name: "pattern" },
},
{
name: ["-A", "--all"],
description:
"Show all tasks, even uncommented ones (in combination with -T or -D)",
},
{
name: ["-B", "--build-all"],
description:
"Build all prerequisites, including those which are up-to-date",
},
{
name: ["-D", "--describe"],
description: "Describe the tasks (matching optional PATTERN), then exit",
args: {
name: "pattern",
isOptional: true,
},
},
{
name: ["-e", "--execute"],
description: "Execute some Ruby code and exit",
args: { name: "code" },
},
{
name: ["-E", "--execute-continue"],
description:
"Execute some Ruby code, then continue with normal task processing",
args: { name: "code" },
},
{
name: ["-f", "--rakefile"],
description: "Use FILENAME as the rakefile to search for",
args: { name: "filename" },
},
{
name: "-G",
description:
"Use standard project Rakefile search paths, ignore system wide rakefiles",
},
{
name: ["-g", "--system"],
description:
"Using system wide (global) rakefiles (usually '~/.rake/*.rake')",
},
{
name: ["-I", "--libdir"],
description: "Include LIBDIR in the search path for required modules",
args: { name: "libdir" },
},
{
name: ["-j", "--jobs"],
description:
"Specifies the maximum number of tasks to execute in parallel. (default is number of CPU cores + 4)",
args: { name: "number" },
},
{
name: ["-m", "--multitask"],
description: "Treat all tasks as multitasks",
args: {},
},
{
name: ["-n", "--dry-run"],
description: "Do a dry run without executing actions",
args: {},
},
{
name: "-N",
description: "Do not search parent directories for the Rakefile",
args: {},
},
{
name: ["-P", "--prereqs"],
description: "Display the tasks and dependencies, then exit",
args: {},
},
{
name: ["-p", "--execute-print"],
description: "Execute some Ruby code, print the result, then exit",
args: {},
},
{
name: ["-q", "--quiet"],
description: "Do not log messages to standard output",
args: {},
},
{
name: ["-r", "--require"],
description: "Require MODULE before executing rakefile",
args: { name: "module" },
},
{
name: ["-R", "--rakelibdir"],
description:
"Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib')",
args: { name: "rakelibdir" },
},
{
name: "--rakelib",
description:
"-s, --silent Like --quiet, but also suppresses the 'in directory' announcement",
args: {},
},
{
name: ["-t", "--trace"],
description:
"Turn on invoke/execute tracing, enable full backtrace. OUT can be stderr (default) or stdout",
args: {},
},
{
name: ["-T", "--tasks"],
description:
"Display the tasks (matching optional PATTERN) with descriptions, then exit. -AT combination displays all of tasks contained no description",
args: {},
},
{
name: ["-v", "--verbose"],
description: "Log message to standard output",
args: {},
},
{
name: ["-V", "--version"],
description: "Display the program version",
args: {},
},
{
name: ["-W", "--where"],
description: "Describe the tasks (matching optional PATTERN), then exit",
args: { name: "pattern" },
},
{
name: ["-X", "--no-deprecation-warnings"],
description: "Disable the deprecation warnings",
args: {},
},
{
name: ["-h", "--help"],
description: "Display this help message",
args: {},
},
];
const defaultCommands: Fig.Subcommand[] = [
{
name: ["c", "console"],
description: "Interact with your Rails application from the command line",
options: [
{
name: ["-s", "--sandbox"],
description: "Rollback database modifications on exit",
},
{
name: ["-e", "--environment"],
description: "Specifies the environment to run this console under",
args: {
name: "environment",
},
},
],
},
{
name: "server",
description:
"Launch a web server to access your application through a browser",
options: [
{
name: ["-e", "--environment"],
description:
"Specifies the environment to run this server under (e.g. test/development/production)",
args: { name: "environment" },
},
{
name: ["-p", "--port"],
description: "Runs Rails on the specified port - defaults to 3000",
args: { name: "port" },
},
{
name: ["-b", "--binding"],
description:
"Binds Rails to the specified IP - defaults to 'localhost' in development and '0.0.0.0' in other environments'",
args: { name: "IP" },
},
{
name: ["-c", "--config"],
description:
"Uses a custom rackup configuration - defaults to config.ru",
args: { name: "file" },
},
{
name: ["-d", "--daemon"],
description: "Runs server as a Daemon",
},
{
name: ["-u", "--using"],
description:
"Specifies the Rack server used to run the application (thin/puma/webrick)",
args: { name: "server" },
},
{
name: ["-P", "--pid"],
description: "Specifies the PID file - defaults to tmp/pids/server.pid",
args: { name: "PID" },
},
{
name: ["-C", "--dev-caching"],
description: "Specifies whether to perform caching in development",
},
{
name: "--early-hints",
description: "Enables HTTP/2 early hints",
},
{
name: "--log-to-stdout",
description:
"Whether to log to stdout. Enabled by default in development when not daemonized",
},
],
},
{
name: "db:create",
description: "Create your database",
options: dbOptions,
},
{
name: "db:migrate",
description: "Run any pending database migrations",
options: dbOptions,
},
{
name: "dbconsole",
description:
"Opens a console to your database (supports MySQL, PostgreSQL, and SQLite3)",
options: [
{
name: "-e",
description:
"Specifies the environment to run this dbconsole under (e.g. test/development/production)",
args: {},
},
{
name: "--mode",
description:
"Automatically put the sqlite3 database in the specified mode",
args: {
name: "mode",
suggestions: ["html", "list", "line", "column"],
},
},
{
name: "-p",
description: "Automatically provide the password from database.yml",
args: {},
},
{
name: "-c",
description: "Specifies the connection to use",
args: { name: "connection" },
},
{
name: ["-db", "--database"],
description: "Specifies the database to use",
},
],
},
{
name: "notes",
description: "Enumerate all annotations",
options: [
{
name: ["-a", "--annotations"],
requiresSeparator: true,
args: {
name: "annotations",
isVariadic: true,
description: "Filter by specific annotations, e.g. Foobar TODO",
},
},
],
},
{
name: "generate",
description: "Use templates to generate Rails resources",
args: [
{
name: "generator",
filterStrategy: "fuzzy",
suggestions: [
"model",
"resource",
"scaffold",
"controller",
"migration",
],
generators: {
script: ["rails", "g", "--help"],
cache: {
cacheByDirectory: true,
strategy: "stale-while-revalidate",
},
postProcess(out) {
const lines = out.split("Rails:")[1].trim().split("\n");
type GeneratorCommand = [string, string[]];
const dict: GeneratorCommand[] = ["Rails:", ...lines].reduce(
(arr, line) => {
if (line.endsWith(":"))
return [...arr, [line.replace(":", ""), []]];
const trimmed = line.trim();
if (!trimmed.length) return arr;
arr[arr.length - 1][1].push(line.trim());
return arr;
},
[]
);
return dict.reduce<Fig.Suggestion[]>((arr, [plugin, commands]) => {
return [
...arr,
...commands.map((name) => ({
insertValue: name,
name: `${name} (${plugin})`,
})),
];
}, []);
},
},
},
{
name: "args",
isVariadic: true,
},
],
options: [
{
name: "-h",
description: "Print generator's options and usage",
args: {},
},
{
name: "-p",
description: "Run but do not make any changes",
args: {},
},
{
name: "-f",
description: "Overwrite files that already exist",
args: {},
},
{
name: "-s",
description: "Skip files that already exist",
args: {},
},
{
name: "-q",
description: "Suppress status output",
args: {},
},
],
},
{
name: "routes",
description: "List all of the routes defined in your application",
options: [
{
name: "-c",
description:
"Filter by a specific controller, e.g. PostsController or Admin::PostsController",
args: {},
},
{
name: "-g",
description: "Grep routes by a specific pattern",
args: {},
},
{
name: "-E",
description: "Print routes expanded vertically with parts explained",
args: {},
},
],
},
{
name: "test",
description: "Run your test suite",
args: {
name: "files or directories",
generators: filepaths({
extensions: ["rb"],
editFileSuggestions: { icon: RB_ICON },
}),
isOptional: true,
},
async generateSpec(_, executeShellCommand) {
const { stdout: helpText } = await executeShellCommand({
command: "rails",
args: ["test", "--help"],
});
const argRegex =
/(?:(-[a-zA-Z]), )?(--[^ ]+?)[ =]([A-Z_]+)?[ \r\n]+([^\n]+)/g;
const options = Array.from(helpText.matchAll(argRegex)).map((match) => {
const [_match, _short, long, arg, description] = match;
return {
name: long.replace(/\[.+\]/g, ""),
description,
args: { name: arg },
};
});
return { name: "test", options };
},
},
newCommand,
];
// Generator that searches asynchronously for more Rails commands through the help command
export const railsCommandsGenerator: Fig.Generator = {
custom: async (_, executeShellCommand) => {
// parse help text to find more commands
let commands: Fig.Subcommand[] = [];
try {
const { stdout: helpText } = await executeShellCommand({
command: "rails",
// eslint-disable-next-line @withfig/fig-linter/no-useless-arrays
args: ["--tasks"],
});
const defaultCommandNames = defaultCommands.map((c) => c.name);
const matches = Array.from(helpText.matchAll(/rails ([^ ]+)/g));
commands = matches
.map((match) => ({ name: match[1] }))
.filter((cmd) => !defaultCommandNames.includes(cmd.name));
} catch (e) {
console.log("ERROR", e);
}
return commands;
},
cache: {
strategy: "stale-while-revalidate",
ttl: 1000 * 60 * 60 * 24 * 3, // 3 days
cacheByDirectory: true,
},
};
const completionSpec: Fig.Spec = {
name: "rails",
description: "Ruby on Rails CLI",
icon: "https://avatars.githubusercontent.com/u/4223?s=48&v=4",
generateSpec: async (_, executeShellCommand) => {
const isRailsDirectory =
(
await executeShellCommand({
command: "bash",
args: [
"-c",
`until [[ -f Gemfile ]] || [[ $PWD = '/' ]]; do cd ..; done; if [ -f Gemfile ]; then cat Gemfile | \\grep "gem ['\\"]rails['\\"]"; fi`,
],
})
).status === 0;
if (!isRailsDirectory) {
return {
name: "rails",
subcommands: [newCommand],
};
}
return {
name: "rails",
subcommands: defaultCommands,
args: {
generators: railsCommandsGenerator,
isOptional: true,
},
};
},
};
export default completionSpec;