Skip to content

Commit

Permalink
Update build.php
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Dec 19, 2023
1 parent 975ee8f commit 9634143
Showing 1 changed file with 2 additions and 102 deletions.
104 changes: 2 additions & 102 deletions _other/builder/build.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,107 +113,7 @@ function replaceForWindows(): array

function replaceForAllOtherOSes(): array
{
return explode(PHP_EOL, run('grep -E -r -l -i "Builder|builder|create_builder_table" --exclude-dir=vendor ./* ./.github/* | grep -v '.basename(__FILE__)));
}

function getGitHubApiEndpoint(string $endpoint): ?stdClass
{
try {
$curl = curl_init("https://api.github.com/{$endpoint}");
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPGET => true,
CURLOPT_HTTPHEADER => [
'User-Agent: spatie-configure-script/1.0',
],
]);

$response = curl_exec($curl);
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

curl_close($curl);

if ($statusCode === 200) {
return json_decode($response);
}
} catch (Exception $e) {
// ignore
}

return null;
}

function searchCommitsForGitHubUsername(): string
{
$authorName = strtolower(trim(shell_exec('git config user.name')));

$committersRaw = shell_exec("git log --author='@users.noreply.github.com' --pretty='%an:%ae' --reverse");
$committersLines = explode("\n", $committersRaw);
$committers = array_filter(array_map(function ($line) use ($authorName) {
$line = trim($line);
[$name, $email] = explode(':', $line) + [null, null];

return [
'name' => $name,
'email' => $email,
'isMatch' => strtolower($name) === $authorName && ! str_contains($name, '[bot]'),
];
}, $committersLines), fn ($item) => $item['isMatch']);

if (empty($committers)) {
return '';
}

$firstCommitter = reset($committers);

return explode('@', $firstCommitter['email'])[0] ?? '';
}

function guessGitHubUsernameUsingCli()
{
try {
if (preg_match('/ogged in to github\.com as ([a-zA-Z-_]+).+/', shell_exec('gh auth status -h github.com 2>&1'), $matches)) {
return $matches[1];
}
} catch (Exception $e) {
// ignore
}

return '';
}

function guessGitHubUsername(): string
{
$username = searchCommitsForGitHubUsername();
if (! empty($username)) {
return $username;
}

$username = guessGitHubUsernameUsingCli();
if (! empty($username)) {
return $username;
}

// fall back to using the username from the git remote
$remoteUrl = shell_exec('git config remote.origin.url');
$remoteUrlParts = explode('/', str_replace(':', '/', trim($remoteUrl)));

return $remoteUrlParts[1] ?? '';
}

function guessGitHubVendorInfo($authorName, $username): array
{
$remoteUrl = shell_exec('git config remote.origin.url');
$remoteUrlParts = explode('/', str_replace(':', '/', trim($remoteUrl)));

$response = getGitHubApiEndpoint("orgs/{$remoteUrlParts[1]}");

if ($response === null) {
return [$authorName, $username];
}

return [$response->name ?? $authorName, $response->login ?? $username];
return explode(PHP_EOL, run('grep -E -r -l -i "Builder|builder|create_builder_table" --exclude-dir=vendor ./* | grep -v '.basename(__FILE__)));
}

writeln(' ');
Expand All @@ -235,7 +135,7 @@ function guessGitHubVendorInfo($authorName, $username): array
writeln(' ');
writeln('Welcome to Moox Builder');
writeln(' ');
writeln('This script will guide you through the process of configuring your own Moox package.');
writeln('This script will guide you through the process of building your own Moox package.');
writeln(' ');

$authorName = ask('Author name', 'Moox Developer');
Expand Down

0 comments on commit 9634143

Please sign in to comment.