-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetMyCodeCursorSettings
executable file
·301 lines (252 loc) · 10.1 KB
/
setMyCodeCursorSettings
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
#!/usr/bin/env perl
use strict;
use warnings;
use File::Find;
use JSON::PP;
use Cwd qw(abs_path getcwd);
use File::Spec;
use File::Spec::Functions qw(abs2rel); # Add this line with the other imports
use File::Path qw(make_path);
use File::Basename;
# List of additional ignore files to process for user-level settings
my @user_ignore_files = (
"$ENV{HOME}/gitignore_global",
);
# Initialize variables
my %user_exclude_patterns; # From user's global ignore files
my %workspace_exclude_patterns; # From workspace .gitignore files
my %processed_files; # Track processed files to avoid duplicates
my %dangerous_pattern_sources; # Track where dangerous patterns came from
my @dangerous_patterns = ('*', '.*');
# Get current working directory and git root
my $cwd = getcwd();
chomp(my $git_root = `git rev-parse --show-toplevel`);
die "Not in a git repository" if $? != 0;
$git_root = abs_path($git_root);
# Ensure cwd is within git root
my $rel_cwd = $cwd;
$rel_cwd =~ s|^$git_root/||;
print STDERR "Working in subdirectory: $rel_cwd\n";
sub make_relative_to_git_root {
my $path = shift;
# First make it absolute if it isn't already
$path = File::Spec->rel2abs($path, $git_root) unless File::Spec->file_name_is_absolute($path);
# Then make it relative to git root
return abs2rel($path, $git_root);
}
sub convert_pattern {
my ($pattern, $rel_path, $file, $is_user_setting) = @_; # Add is_user_setting parameter
# Remove comments and empty lines
return '' if $pattern =~ /^\s*#/ || $pattern =~ /^\s*$/;
$pattern =~ s/^\s+|\s+$//g; # trim whitespace
# Handle negation patterns (we'll skip them as VS Code doesn't support them well)
return '' if $pattern =~ /^!/;
# Check for dangerous patterns
if (grep { $pattern eq $_ || $pattern =~ /^\/?\Q$_\E$/ } @dangerous_patterns) {
my $scope = $rel_path ? $rel_path : "global ignore file";
my $location = $file || "unknown file";
$dangerous_pattern_sources{$pattern} ||= [];
push @{$dangerous_pattern_sources{$pattern}}, $location;
print STDERR "\nWARNING: Potentially dangerous pattern '$pattern' in $scope\n";
print STDERR " Found in: $location\n";
if ($rel_path && $rel_path ne '.') {
print STDERR " Suggestion: Replace with more specific pattern like '$rel_path/*'\n";
} else {
print STDERR " Suggestion: Replace with a more specific pattern\n";
}
return '';
}
# Convert .gitignore pattern to VS Code pattern
$pattern =~ s|^\./||; # Remove leading ./
$pattern =~ s|^/||; # Remove leading /
# For user settings, we want all patterns to be global
if ($is_user_setting) {
# Don't add **/ if it's already there
return $pattern =~ m|^\*\*/| ? $pattern : "**/$pattern";
}
# For workspace settings, keep the original logic
if ($pattern =~ m|^[^/]+$|) {
# Simple pattern with no path separators (e.g., *.txt)
return "**/$pattern";
}
elsif ($pattern =~ m|^\*\*/|) {
# Pattern starts with **/ - keep as is
return $pattern;
}
else {
# Directory-specific pattern
if ($rel_path && $rel_path ne '.') {
# Make sure rel_path is relative to git root
$rel_path = make_relative_to_git_root($rel_path);
# Remove any leading path components that match rel_path
$pattern =~ s|^$rel_path/||;
$pattern = "$rel_path/$pattern";
}
# Handle directory patterns consistently
if ($pattern =~ /\/$/ ||
$pattern =~ /\/(node_modules|dist|target|logs|store)\/?$/) {
return $pattern;
} else {
return "**/$pattern";
}
}
}
sub process_ignore_file {
my ($file, $desc, $rel_path, $patterns_ref, $is_user_setting) = @_;
return unless -f $file;
# Skip if already processed
my $abs_path = abs_path($file);
return if $processed_files{$abs_path}++;
# Ensure rel_path is relative to git root
$rel_path = make_relative_to_git_root($rel_path) if $rel_path;
print STDERR "Processing $desc: $file\n";
open(my $fh, '<', $file) or die "Cannot open $file: $!";
while (my $line = <$fh>) {
chomp $line;
my $pattern = convert_pattern($line, $rel_path, $file, $is_user_setting);
next unless $pattern;
print STDERR "Adding pattern: $pattern\n" if $ENV{DEBUG};
$patterns_ref->{$pattern} = JSON::PP::true;
}
close($fh);
}
sub find_nearest_settings_json {
my $dir = $cwd;
while (1) {
my $settings = "$dir/.vscode/settings.json";
return $settings if -f $settings;
my $vscode_dir = "$dir/.vscode";
return "$vscode_dir/settings.json" if -d $vscode_dir;
# Stop if we've reached git root or filesystem root
last if $dir eq $git_root || $dir eq '/';
# Go up one directory
$dir = dirname($dir);
}
# If no existing settings.json found, create one in current directory
return "$cwd/.vscode/settings.json";
}
sub update_settings_json {
my ($settings_file, $is_user_settings) = @_;
# Create directory if needed
my $dir = dirname($settings_file);
make_path($dir) unless -d $dir;
# Read existing settings
my $existing_settings = {};
if (-f $settings_file) {
local $/;
open(my $fh, '<', $settings_file) or die "Cannot open $settings_file: $!";
my $json_text = <$fh>;
close($fh);
if ($json_text =~ /\S/) {
eval {
$existing_settings = JSON::PP->new->decode($json_text);
};
if ($@) {
my $backup = "$settings_file.bak." . time();
rename $settings_file, $backup;
print STDERR "Backed up problematic settings to $backup\n";
$existing_settings = {};
}
}
}
# Helper to sort patterns
sub sort_patterns {
my @patterns = @_;
return sort {
# Split patterns into components
my ($a_dirs) = $a =~ m{^(?:\*\*/)?(.+?)/?$};
my ($b_dirs) = $b =~ m{^(?:\*\*/)?(.+?)/?$};
# Count path segments
my $a_depth = ($a_dirs =~ tr{/}{});
my $b_depth = ($b_dirs =~ tr{/}{});
# Sort by:
# 1. Root patterns first (starting with **/)
# 2. Then by directory depth (shallower first)
# 3. Then alphabetically
($a !~ m{^\*\*/} cmp $b !~ m{^\*\*/}) || # **/ patterns first
($a_depth <=> $b_depth) || # Then by depth
($a_dirs cmp $b_dirs) # Then alphabetically
} @patterns;
}
# Sort the patterns and rebuild the exclude hash
my $patterns_ref = $is_user_settings ? \%user_exclude_patterns : \%workspace_exclude_patterns;
my %sorted_excludes = map { $_ => JSON::PP::true }
sort_patterns(keys %$patterns_ref);
# Replace search.exclude while preserving other settings
$existing_settings->{'search.useIgnoreFiles'} = JSON::PP::true;
$existing_settings->{'search.useGlobalIgnoreFiles'} = JSON::PP::true;
$existing_settings->{'search.exclude'} = \%sorted_excludes;
# Write updated settings with sorted patterns
open(my $fh, '>', $settings_file) or die "Cannot write to $settings_file: $!";
print $fh JSON::PP->new->pretty->canonical->encode($existing_settings);
close($fh);
print STDERR "Updated $settings_file\n";
}
# Process patterns for workspace settings
sub process_workspace_ignores {
%workspace_exclude_patterns = ();
# First, collect all .gitignore files between cwd and git root
my @parent_ignores;
my $dir = $cwd;
while (1) {
if (-f "$dir/.gitignore") {
unshift @parent_ignores, {
file => "$dir/.gitignore",
rel_path => make_relative_to_git_root($dir)
};
}
last if $dir eq $git_root;
$dir = dirname($dir);
}
# Process parent .gitignore files first (root to cwd)
for my $ignore (@parent_ignores) {
process_ignore_file($ignore->{file},
"Git worktree ignore file",
$ignore->{rel_path},
\%workspace_exclude_patterns);
}
# Now find .gitignore files in subdirectories of cwd, but with restrictions
find({
wanted => sub {
return unless $_ eq '.gitignore';
# Skip if not in cwd's subtree
return if $File::Find::dir lt $cwd;
# Skip node_modules directories
return if $File::Find::dir =~ m|/node_modules/|;
# Skip other common dependency directories if needed
return if $File::Find::dir =~ m{/(?:vendor|bower_components)/};
my $rel_path = $File::Find::name;
$rel_path =~ s|^$git_root/||;
my $dir_path = $rel_path;
$dir_path =~ s|/\.gitignore$||;
process_ignore_file($_,
"Git worktree ignore file at $rel_path",
$dir_path,
\%workspace_exclude_patterns);
},
no_chdir => 1
}, $cwd);
}
# First handle user settings
for my $file (@user_ignore_files) {
process_ignore_file($file, "User ignore file", '', \%user_exclude_patterns, 1); # Note the 1 at the end
}
my $home = $ENV{HOME};
update_settings_json("$home/.config/Code/User/settings.json", 1);
update_settings_json("$home/.config/Cursor/User/settings.json", 1);
# Then handle nearest workspace settings
process_workspace_ignores();
my $workspace_settings = find_nearest_settings_json();
print STDERR "Updating workspace settings: $workspace_settings\n";
update_settings_json($workspace_settings, 0);
# Print summary of dangerous patterns
if (%dangerous_pattern_sources) {
print STDERR "\nWARNING: Dangerous patterns were found:\n";
for my $pattern (sort keys %dangerous_pattern_sources) {
print STDERR " $pattern found in:\n";
for my $file (@{$dangerous_pattern_sources{$pattern}}) {
print STDERR " - $file\n";
}
}
print STDERR "\nConsider replacing these patterns with more specific ones.\n";
}