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
99 lines (87 loc) · 3.82 KB
/
Copy pathIncrementMinorVersion.php
File metadata and controls
99 lines (87 loc) · 3.82 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
<?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 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 CONSTANTS_README_FILEPATH = "./README.md";
function getLatestPackagistVersion(): 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)) {
# First non-branch version is the latest based on payload structure
echo "Latest packagist version: {$version}\n";
return $version;
}
}
}
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 updateGraphConstants(string $version)
{
$fileContents = file_get_contents(CONSTANTS_FILEPATH);
if ($fileContents) {
$pattern = '/'. SDK_VERSION_VAR_NAME . '\s+=\s+".+"/';
$replacement = SDK_VERSION_VAR_NAME . ' = "' . $version . '"';
if (!file_put_contents(CONSTANTS_FILEPATH, preg_replace($pattern, $replacement, $fileContents))) {
throw new Exception("Unable to find and replace SDK version variable ". SDK_VERSION_VAR_NAME);
}
echo "Successfully updated " . CONSTANTS_FILEPATH . "\n";
return;
}
throw new Exception("Could not read GraphConstants.php at ". CONSTANTS_FILEPATH);
}
function updateReadMe(string $version)
{
$fileContents = file_get_contents(CONSTANTS_README_FILEPATH);
if ($fileContents) {
$pattern = '/"microsoft\/microsoft-graph":\s+".+"/';
$replacement = "\"microsoft/microsoft-graph\": \"^{$version}\"";
if (!file_put_contents(CONSTANTS_README_FILEPATH, preg_replace($pattern, $replacement, $fileContents))) {
throw new Exception("Unable to find and replace SDK version");
}
echo "Successfully updated README\n";
return;
}
throw new Exception("Could not read README.md at " . CONSTANTS_README_FILEPATH);
}
$version = incrementMinorVersion(getLatestPackagistVersion());
echo "Version after minor increment: {$version}\n";
updateGraphConstants($version);
updateReadMe($version);