-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathself-update.php
More file actions
166 lines (139 loc) · 4.75 KB
/
self-update.php
File metadata and controls
166 lines (139 loc) · 4.75 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
<?php
/**
* This file is responsible for ensuring that the CLI tool is up-to-date, and running on the correct branch.
* Because of that, it should be as simple as possible, and not rely on any other files. For example, we can't
* use anything coming from Composer because we can't load it until after we've updated.
*/
// region HELPER FUNCTIONS
/**
* Outputs a message to the console, unless the `--quiet` flag is set.
*
* @param string $message The message to output.
* @param boolean|null $quiet Whether to suppress the output.
*
* @return void
*/
function team51_cli_print_message( string $message, ?bool $quiet = null ): void {
$quiet ??= (bool) $GLOBALS['team51_cli_is_quiet'];
if ( ! $quiet ) {
echo $message . PHP_EOL;
}
}
/**
* Executes a system command.
*
* @param string $command The command to run.
*
* @return array
*/
function team51_cli_run_system_command( string $command ): array {
$output = null;
$result_code = null;
// Execute the command and redirect STDERR to STDOUT.
exec( "$command 2>&1", $output, $result_code );
if ( 0 !== $result_code ) {
team51_cli_print_message( sprintf( 'Error running command: %s', $command ), false );
foreach ( $output as $line ) {
team51_cli_print_message( $line, false );
}
}
return array(
'output' => $output,
'result_code' => $result_code,
);
}
/**
* Ensures that the CLI tool is up-to-date and running on the correct branch.
*
* @return void
*/
function team51_cli_self_update(): void {
// Get the current branch.
$command = team51_cli_run_system_command( sprintf( 'git -C %s branch --show-current', TEAM51_CLI_ROOT_DIR ) );
// Maybe switch to trunk.
if ( 'trunk' !== $command['output'][0] ) {
team51_cli_print_message( 'Not on `trunk`. Switching...' );
team51_cli_run_system_command( sprintf( 'git -C %s stash', TEAM51_CLI_ROOT_DIR ) );
team51_cli_run_system_command( sprintf( 'git -C %s checkout -f trunk', TEAM51_CLI_ROOT_DIR ) );
}
// Reset branch.
team51_cli_run_system_command( sprintf( 'git -C %s fetch origin', TEAM51_CLI_ROOT_DIR ) );
team51_cli_run_system_command( sprintf( 'git -C %s reset --hard origin/trunk', TEAM51_CLI_ROOT_DIR ) );
}
// endregion
// region TIMESTAMP FUNCTIONS
/**
* Gets the timestamp from the .dev file.
*
* @return int|null The timestamp, or null if the file doesn't exist or doesn't contain a valid timestamp.
*/
function team51_cli_get_dev_timestamp(): ?int {
$dev_file = TEAM51_CLI_ROOT_DIR . '/.dev';
if ( ! file_exists( $dev_file ) ) {
return null;
}
$content = file_get_contents( $dev_file );
if ( empty( $content ) || ! is_numeric( $content ) ) {
// If the file is empty or doesn't contain a valid timestamp, return a far future date (2035) to force dev mode.
return 2062837972;
}
return (int) $content;
}
/**
* Writes the current timestamp to the .dev file.
*
* @return void
*/
function team51_cli_update_dev_timestamp(): void {
$dev_file = TEAM51_CLI_ROOT_DIR . '/.dev';
$timestamp = time();
file_put_contents( $dev_file, $timestamp );
}
// endregion
// region EXECUTION LOGIC
$team51_cli_is_quiet = file_exists( TEAM51_CLI_ROOT_DIR . '/.quiet' );
$team51_cli_is_dev = false; // Will be set based on .dev file timestamp or --dev flag
$team51_is_autocomplete = false;
$team51_cli_force_update = false;
foreach ( $argv as $arg ) {
switch ( $arg ) {
case 'completion':
case '_complete':
$team51_is_autocomplete = true;
return; // Don't run the rest of the script.
case '-q':
case '--quiet':
$team51_cli_is_quiet = true;
break;
case '--dev':
$team51_cli_is_dev = true;
break;
case '--force-update':
$team51_cli_force_update = true;
break;
}
}
// Print the ASCII art.
team51_cli_print_message( file_get_contents( TEAM51_CLI_ROOT_DIR . '/.ascii' ) );
// Check for updates.
if ( $team51_cli_is_dev ) {
team51_cli_print_message( "\033[44mRunning in developer mode. Skipping update check.\033[0m" );
} else {
$dev_timestamp = team51_cli_get_dev_timestamp();
$seven_days_in_seconds = 7 * 24 * 60 * 60; // 7 days in seconds
$should_update = $team51_cli_force_update ||
null === $dev_timestamp ||
( time() - $dev_timestamp ) >= $seven_days_in_seconds;
if ( $should_update ) {
team51_cli_print_message( "\033[33mChecking for updates..\033[0m" );
team51_cli_self_update();
// Update the timestamp after checking for updates
team51_cli_update_dev_timestamp();
} else {
team51_cli_print_message( "\033[33mSkipping update check (less than 7 days since last check).\033[0m" );
}
}
// Update Composer.
team51_cli_run_system_command( sprintf( 'composer install --working-dir %s --no-interaction', TEAM51_CLI_ROOT_DIR ) );
team51_cli_run_system_command( sprintf( 'composer dump-autoload -o --working-dir %s --no-interaction', TEAM51_CLI_ROOT_DIR ) );
// endregion