-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymdotfiles
More file actions
executable file
·233 lines (204 loc) · 6.17 KB
/
Copy pathsymdotfiles
File metadata and controls
executable file
·233 lines (204 loc) · 6.17 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
#!/usr/bin/env php
# =============================================================================
# Symlink files
# By Justin Sternberg <me@jtsternberg.com>
# https://github.com/jtsternberg/Dot-Files/blob/master/symdotfiles
#
# Version 2.0.0
#
# Creates symlinks to all files in the current directory
# to the directory above it.
#
# Usage:
# cd to this script's directory and..
# php symdotfiles [testrun] [hard]
# =============================================================================
<?php
$hard = false;
$testrun = false;
$dirs_only = false;
$working_dir = dirname( __FILE__ );
$destination_dir = $_SERVER['HOME'];
foreach ( (array) $argv as $arg ) {
if ( ! $testrun ) {
$testrun = 'testrun' == $arg;
}
if ( ! $hard ) {
$hard = 'hard' == $arg;
}
if ( ! $dirs_only ) {
$dirs_only = '--dirsonly' == $arg;
}
if ( 0 === strpos( $arg, '--dir=' ) ) {
$working_dir = str_replace( '--dir=', '', $arg );
}
if ( 0 === strpos( $arg, '--destination=' ) ) {
$destination_dir = realpath( str_replace( '--destination=', '', $arg ) );
}
}
$destination_dir = rtrim( $destination_dir, '/\\' ) . '/';
$files = array_merge(
glob( $working_dir . '/.*' ),
glob( $working_dir . '/*' )
);
$is_test = $testrun ? ' (test run)' : '';
$is_hard = $hard ? ' (hard -- overwrite existing)' : '';
do_notice( "Making those symlinks$is_test$is_hard", true, "\n" );
$moved = '';
$ignore = array(
'.DS_Store',
'.',
'..',
'.git',
'.gitmodules',
'.gitignore',
'zsh-custom',
'private',
'symdotfiles',
'zsh-custom',
'online-check.sh',
'misc',
'.zshrc.pre-oh-my-zsh',
'.gitconfig-macos', // platform-specific; symlinked as ~/.gitconfig-local
'.gitconfig-linux', // platform-specific; symlinked as ~/.gitconfig-local
'pi', // nested; pi/agent/models.json is symlinked as ~/.pi/agent/models.json
'aider', // config only; passed to aider via --model-settings-file/--model-metadata-file
'src', // PSR-4 class tree; loaded via src/bootstrap.php, not $HOME symlinks
);
$ignore_begins = array(
'.',
);
$noignore = array(
'.gemrc',
'.gitconfig',
'.gitignore_global',
'.zlogin',
'.zprofile',
'.zshenv',
'.zshrc',
);
$count = 0;
foreach( $files as $index => $file ) {
$filepath = realpath( $file );
$filename = basename( $file );
if ( in_array( $filename, $ignore ) ) {
continue;
}
foreach ( $ignore_begins as $needle ) {
if ( 0 === strpos( $filename, $needle ) && ! in_array( $filename, $noignore, true ) ) {
continue 2;
}
}
if ( $dirs_only && ! is_dir( $filename ) ) {
continue;
}
$from = str_replace( $filename, '', $file );
do_notice( ( ++$count ) .' File: ' . $filename, false, false );
$newfile = $destination_dir . $filename;
$exists = file_exists( $newfile );
if ( ! $hard ) {
if ( $exists ) {
do_notice( ' : Exists and will not be created.', false, false );
continue;
}
}
if ( ! $testrun ) {
if ( $exists && $hard ) {
$removed = is_dir( $newfile ) && ! is_link( $newfile )
? rmdir( $newfile )
: unlink( $newfile );
if ( ! $removed ) {
do_notice( ' : Could not be replaced.', false, false );
continue;
}
}
// do_notice( print_r( array( $filepath, $newfile ), true ) );
$linked = symlink( $filepath, $newfile );
if ( ! $linked ) {
$moved = 'Was NOT moved to:';
} else {
$success = true;
}
}
do_notice( ' '. $moved .' ' . $newfile, false, false );
}
// Symlink platform-specific gitconfig to ~/.gitconfig-local
if ( ! $dirs_only ) {
$os = PHP_OS_FAMILY; // 'Darwin' on macOS, 'Linux' on Linux
$platform_gitconfig = $working_dir . '/.gitconfig-' . ( $os === 'Darwin' ? 'macos' : 'linux' );
$gitconfig_local = $destination_dir . '.gitconfig-local';
do_notice( "Symlinking platform gitconfig ($os)", true, "\n" );
if ( file_exists( $platform_gitconfig ) ) {
if ( file_exists( $gitconfig_local ) || is_link( $gitconfig_local ) ) {
if ( ! $hard ) {
do_notice( " : $gitconfig_local exists, skipping (use 'hard' to overwrite)", false, false );
} else {
if ( ! $testrun ) {
unlink( $gitconfig_local );
symlink( $platform_gitconfig, $gitconfig_local );
}
do_notice( " : $gitconfig_local -> $platform_gitconfig", false, false );
}
} else {
if ( ! $testrun ) {
symlink( $platform_gitconfig, $gitconfig_local );
}
do_notice( " : $gitconfig_local -> $platform_gitconfig", false, false );
}
} else {
do_notice( " : No platform gitconfig found at $platform_gitconfig", false, false );
}
}
// Symlink nested files that live inside app dirs in $HOME (too stateful to
// symlink the whole dir). Add 'repo/path' => 'home/path' entries as needed.
if ( ! $dirs_only ) {
$nested_links = array(
'pi/agent/models.json' => '.pi/agent/models.json',
);
foreach ( $nested_links as $repo_rel => $home_rel ) {
$repo_file = $working_dir . '/' . $repo_rel;
$home_file = $destination_dir . $home_rel;
do_notice( "Symlinking nested file ($repo_rel)", true, "\n" );
if ( ! file_exists( $repo_file ) ) {
do_notice( " : No repo file found at $repo_file", false, false );
continue;
}
if ( file_exists( $home_file ) || is_link( $home_file ) ) {
if ( ! $hard ) {
do_notice( " : $home_file exists, skipping (use 'hard' to overwrite)", false, false );
continue;
}
if ( ! $testrun ) {
// Back up a real file before replacing it; symlinks can just go.
if ( is_file( $home_file ) && ! is_link( $home_file ) ) {
rename( $home_file, $home_file . '.bak' );
} else {
unlink( $home_file );
}
symlink( $repo_file, $home_file );
}
do_notice( " : $home_file -> $repo_file (backup at $home_file.bak if it was a real file)", false, false );
} else {
if ( ! $testrun ) {
if ( ! is_dir( dirname( $home_file ) ) ) {
mkdir( dirname( $home_file ), 0777, true );
}
symlink( $repo_file, $home_file );
}
do_notice( " : $home_file -> $repo_file", false, false );
}
}
}
function do_notice( $text, $before = true, $after = true ) {
$text = html_entity_decode( $text );
$stuff = `echo '>>>'`;
if ( is_string( $before ) ) {
echo $before;
}
echo $before ? "\n" . $stuff : '';
echo `echo '\033[0;34m>> $text\033[0m'`;
echo $after ? $stuff : '';
if ( is_string( $after ) ) {
echo $after . "\n\n";
}
}