forked from microsoftgraph/msgraph-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncrementMinorVersion.php
More file actions
107 lines (94 loc) · 4 KB
/
Copy pathIncrementMinorVersion.php
File metadata and controls
107 lines (94 loc) · 4 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
<?php
/**
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* Licensed under the MIT License. See License in the project root
* for license information.
*
* Bumps up the minor version in src/Core/GraphConstants.php & README based on the latest published package version on Packagist
*
* Assumptions:
* - Script is run from the repo root
* - Script is run on a Unix environment (affects file path separator to files)
* - Packagist returns tagged versions in descending order (latest release first)
*/
const GRAPH_CONSTANTS_FILEPATH = "./src/Core/GraphConstants.php";
const SDK_VERSION_VAR_NAME = "SDK_VERSION"; # Name of version variable in GraphConstants.php
const PACKAGIST_ENDPOINT = "https://packagist.org/packages/microsoft/microsoft-graph.json";
const README_FILEPATH = "./README.md";
const DOCS_FILEPATH = "./docs/classes/Microsoft-Graph-Core-GraphConstants.html";
$filePathsToUpdate = [GRAPH_CONSTANTS_FILEPATH, README_FILEPATH, DOCS_FILEPATH];
function getLatestMinorPackagistVersion(string $majorVersion): string
{
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, PACKAGIST_ENDPOINT);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_TIMEOUT, 100);
curl_setopt($handle, CURLOPT_FAILONERROR, true);
echo "Fetching latest SDK version from " . PACKAGIST_ENDPOINT . "\n";
$response = curl_exec($handle);
if (curl_error($handle)) {
throw new Exception("Failed to get latest packagist version: ". curl_error($handle));
}
curl_close($handle);
$responseJson = json_decode($response, true);
if (!array_key_exists("package", $responseJson)
|| !array_key_exists("versions", $responseJson["package"])
|| empty($responseJson["package"]["versions"])) {
throw new Exception("Unable to find versions in the packagist response JSON: ". $responseJson);
}
$versions = $responseJson["package"]["versions"];
foreach ($versions as $version => $versionMetadata) {
# Ignore branch versions
if (!preg_match('/^dev-.*|.*-dev$/', $version)) {
$split = explode('.', $version);
if (!empty($split) && $split[0] === $majorVersion) {
# Non-branch versions are returned in descending order.
echo "Latest packagist version: {$version}\n";
return $version;
}
}
}
return '';
}
function getCurrentSdkVersion()
{
$fileContents = file_get_contents(GRAPH_CONSTANTS_FILEPATH);
if ($fileContents) {
$pattern = '/'. SDK_VERSION_VAR_NAME . '\s+=\s+".+"/';
$regexMatches = [];
preg_match($pattern, $fileContents, $regexMatches);
if ($regexMatches && $regexMatches[0]) {
$split = explode('"', $regexMatches[0]);
return $split[1];
}
}
}
function incrementMinorVersion(string $version): string
{
$splitVersion = explode(".", $version);
# Increment minor version
$splitVersion[1] = strval(intval($splitVersion[1]) + 1);
# Set patch to 0
$splitVersion[2] = preg_replace('/[0-9]+/', "0", $splitVersion[2]);
return implode(".", $splitVersion);
}
function updateFiles(string $filePath, string $currentVersion, string $bumpedVersion)
{
$fileContents = file_get_contents($filePath);
if ($fileContents) {
$pattern = '/'.$currentVersion.'/';
if (!file_put_contents($filePath, preg_replace($pattern, $bumpedVersion, $fileContents))) {
throw new Exception("Unable to find and replace SDK version");
}
echo "Successfully updated {$filePath}\n";
return;
}
throw new Exception("Could not read contents at " . $filePath);
}
$currentSdkVersion = getCurrentSdkVersion();
$currentMajorVersion = explode('.', $currentSdkVersion)[0];
$bumpedSdkVersion = incrementMinorVersion(getLatestMinorPackagistVersion($currentMajorVersion));
echo "Version after minor increment: {$bumpedSdkVersion}\n";
foreach ($filePathsToUpdate as $path) {
updateFiles($path, $currentSdkVersion, $bumpedSdkVersion);
}