IncrementMinorVersion.php
119 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright (c) Microsoft Corporation. All Rights Reserved. |
| 4 | * Licensed under the MIT License. See License in the project root |
| 5 | * for license information. |
| 6 | * |
| 7 | * Bumps up the minor version in src/Core/GraphConstants.php & README based on the latest published package version on Packagist |
| 8 | * |
| 9 | * Assumptions: |
| 10 | * - Script is run from the repo root |
| 11 | * - Script is run on a Unix environment (affects file path separator to files) |
| 12 | * - Packagist returns tagged versions in descending order (latest release first) |
| 13 | */ |
| 14 | |
| 15 | const CONSTANTS_FILEPATH = "./src/Core/GraphConstants.php"; |
| 16 | const SDK_VERSION_VAR_NAME = "SDK_VERSION"; # Name of version variable in GraphConstants.php |
| 17 | const PACKAGIST_ENDPOINT = "https://packagist.org/packages/microsoft/microsoft-graph.json"; |
| 18 | const CONSTANTS_README_FILEPATH = "./README.md"; |
| 19 | |
| 20 | function getLatestMinorPackagistVersion(string $majorVersion): string |
| 21 | { |
| 22 | $handle = curl_init(); |
| 23 | curl_setopt($handle, CURLOPT_URL, PACKAGIST_ENDPOINT); |
| 24 | curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
| 25 | curl_setopt($handle, CURLOPT_TIMEOUT, 100); |
| 26 | curl_setopt($handle, CURLOPT_FAILONERROR, true); |
| 27 | |
| 28 | echo "Fetching latest SDK version from " . PACKAGIST_ENDPOINT . "\n"; |
| 29 | $response = curl_exec($handle); |
| 30 | |
| 31 | if (curl_error($handle)) { |
| 32 | throw new Exception("Failed to get latest packagist version: ". curl_error($handle)); |
| 33 | } |
| 34 | |
| 35 | curl_close($handle); |
| 36 | |
| 37 | $responseJson = json_decode($response, true); |
| 38 | if (!array_key_exists("package", $responseJson) |
| 39 | || !array_key_exists("versions", $responseJson["package"]) |
| 40 | || empty($responseJson["package"]["versions"])) { |
| 41 | |
| 42 | throw new Exception("Unable to find versions in the packagist response JSON: ". $responseJson); |
| 43 | } |
| 44 | |
| 45 | $versions = $responseJson["package"]["versions"]; |
| 46 | foreach ($versions as $version => $versionMetadata) { |
| 47 | # Ignore branch versions |
| 48 | if (!preg_match('/^dev-.*|.*-dev$/', $version)) { |
| 49 | $split = explode('.', $version); |
| 50 | if (!empty($split) && $split[0] === $majorVersion) { |
| 51 | # Non-branch versions are returned in descending order. |
| 52 | echo "Latest packagist version: {$version}\n"; |
| 53 | return $version; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | function getCurrentSdkVersion() |
| 60 | { |
| 61 | $fileContents = file_get_contents(CONSTANTS_FILEPATH); |
| 62 | if ($fileContents) { |
| 63 | $pattern = '/'. SDK_VERSION_VAR_NAME . '\s+=\s+".+"/'; |
| 64 | $regexMatches = []; |
| 65 | preg_match($pattern, $fileContents, $regexMatches); |
| 66 | if ($regexMatches && $regexMatches[0]) { |
| 67 | $split = explode('"', $regexMatches[0]); |
| 68 | return $split[1]; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | function incrementMinorVersion(string $version): string |
| 74 | { |
| 75 | $splitVersion = explode(".", $version); |
| 76 | # Increment minor version |
| 77 | $splitVersion[1] = strval(intval($splitVersion[1]) + 1); |
| 78 | # Set patch to 0 |
| 79 | $splitVersion[2] = preg_replace('/[0-9]+/', "0", $splitVersion[2]); |
| 80 | return implode(".", $splitVersion); |
| 81 | } |
| 82 | |
| 83 | function updateGraphConstants(string $version) |
| 84 | { |
| 85 | $fileContents = file_get_contents(CONSTANTS_FILEPATH); |
| 86 | if ($fileContents) { |
| 87 | $pattern = '/'. SDK_VERSION_VAR_NAME . '\s+=\s+".+"/'; |
| 88 | $replacement = SDK_VERSION_VAR_NAME . ' = "' . $version . '"'; |
| 89 | if (!file_put_contents(CONSTANTS_FILEPATH, preg_replace($pattern, $replacement, $fileContents))) { |
| 90 | throw new Exception("Unable to find and replace SDK version variable ". SDK_VERSION_VAR_NAME); |
| 91 | } |
| 92 | echo "Successfully updated " . CONSTANTS_FILEPATH . "\n"; |
| 93 | return; |
| 94 | } |
| 95 | throw new Exception("Could not read GraphConstants.php at ". CONSTANTS_FILEPATH); |
| 96 | } |
| 97 | |
| 98 | function updateReadMe(string $version) |
| 99 | { |
| 100 | $fileContents = file_get_contents(CONSTANTS_README_FILEPATH); |
| 101 | if ($fileContents) { |
| 102 | $pattern = '/"microsoft\/microsoft-graph":\s+".+"/'; |
| 103 | $replacement = "\"microsoft/microsoft-graph\": \"^{$version}\""; |
| 104 | if (!file_put_contents(CONSTANTS_README_FILEPATH, preg_replace($pattern, $replacement, $fileContents))) { |
| 105 | throw new Exception("Unable to find and replace SDK version"); |
| 106 | } |
| 107 | echo "Successfully updated README\n"; |
| 108 | return; |
| 109 | } |
| 110 | throw new Exception("Could not read README.md at " . CONSTANTS_README_FILEPATH); |
| 111 | } |
| 112 | |
| 113 | $currentSdkVersion = getCurrentSdkVersion(); |
| 114 | $currentMajorVersion = explode('.', $currentSdkVersion)[0]; |
| 115 | $version = incrementMinorVersion(getLatestMinorPackagistVersion($currentMajorVersion)); |
| 116 | echo "Version after minor increment: {$version}\n"; |
| 117 | updateGraphConstants($version); |
| 118 | updateReadMe($version); |
| 119 |