|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Packagist Publishing Script |
| 4 | + * |
| 5 | + * This script handles publishing a package to Packagist using the Packagist API. |
| 6 | + * It requires the following environment variables: |
| 7 | + * - PACKAGIST_USERNAME: The Packagist username |
| 8 | + * - PACKAGIST_API_TOKEN: The Packagist API token |
| 9 | + * - PACKAGE_NAME: The name of the package to publish (e.g., vendor/package) |
| 10 | + * |
| 11 | + * @php 7.4 |
| 12 | + */ |
| 13 | + |
| 14 | +$username = getenv('PACKAGIST_USERNAME'); |
| 15 | +$apiToken = getenv('PACKAGIST_API_TOKEN'); |
| 16 | +$packageName = getenv('PACKAGE_NAME'); |
| 17 | + |
| 18 | +if (!$username || !$apiToken || !$packageName) { |
| 19 | + echo "Error: Missing required environment variables.\n"; |
| 20 | + echo "Please ensure PACKAGIST_USERNAME, PACKAGIST_API_TOKEN, and PACKAGE_NAME are set.\n"; |
| 21 | + exit(1); |
| 22 | +} |
| 23 | + |
| 24 | +echo "Starting Packagist publishing process for package: $packageName\n"; |
| 25 | + |
| 26 | +$apiUrl = "https://packagist.org/api/update-package?username=$username&apiToken=$apiToken"; |
| 27 | + |
| 28 | +$data = [ |
| 29 | + 'repository' => [ |
| 30 | + 'url' => "https://github.com/lingodotdev/lingo.dev" |
| 31 | + ] |
| 32 | +]; |
| 33 | + |
| 34 | +$ch = curl_init($apiUrl); |
| 35 | + |
| 36 | +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 37 | +curl_setopt($ch, CURLOPT_POST, true); |
| 38 | +curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); |
| 39 | +curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
| 40 | + 'Content-Type: application/json', |
| 41 | + 'Accept: application/json' |
| 42 | +]); |
| 43 | + |
| 44 | +echo "Sending request to Packagist API...\n"; |
| 45 | +$response = curl_exec($ch); |
| 46 | +$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 47 | + |
| 48 | +if (curl_errno($ch)) { |
| 49 | + echo "Error: " . curl_error($ch) . "\n"; |
| 50 | + curl_close($ch); |
| 51 | + exit(1); |
| 52 | +} |
| 53 | + |
| 54 | +curl_close($ch); |
| 55 | + |
| 56 | +$responseData = json_decode($response, true); |
| 57 | + |
| 58 | +echo "HTTP Response Code: $httpCode\n"; |
| 59 | +echo "Response: " . print_r($responseData, true) . "\n"; |
| 60 | + |
| 61 | +if ($httpCode >= 200 && $httpCode < 300) { |
| 62 | + echo "Package $packageName successfully published to Packagist!\n"; |
| 63 | + exit(0); |
| 64 | +} else { |
| 65 | + echo "Failed to publish package $packageName to Packagist.\n"; |
| 66 | + exit(1); |
| 67 | +} |
0 commit comments