forked from microsoftgraph/msgraph-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSdkVersionUtils.php
More file actions
139 lines (127 loc) · 5 KB
/
Copy pathSdkVersionUtils.php
File metadata and controls
139 lines (127 loc) · 5 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?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 SDK version in $filePathsToUpdate 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 SDK_VERSION_VAR_NAME = "SDK_VERSION"; # Name of version variable in GraphConstants.php
const PACKAGE_NAME = "microsoft/microsoft-graph";
const PACKAGIST_ENDPOINT = "https://packagist.org/packages/".PACKAGE_NAME.".json";
const README_FILEPATH = "./README.md";
/**
* Gets latest stable packagist version if $stable == true, else gets the latest Release Candidate
*
* @param boolean $stable
* @return string
*/
function getLatestPackagistVersion(bool $stable = true): 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 (array_keys($versions) as $version) {
# Ignore branch versions
if (stripos($version, "dev") === false) {
if ($stable) {
if (stripos($version, "RC") === false) {
echo "Latest packagist version: {$version}\n";
return $version;
}
continue;
}
if (stripos($version, "RC")) {
echo "Latest packagist version: {$version}\n";
return $version;
}
}
}
return '';
}
function getGraphConstantsSdkVersion()
{
$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 incrementVersion(string $version): string
{
$splitVersion = explode('.', $version);
$rcMatches = explode('RC', $splitVersion[2]);
if ($rcMatches && sizeof($rcMatches) > 1) {
# Increment release candidate
$rcVersion = ($rcMatches[1]) ? intval($rcMatches[1]) + 1 : 2;
$splitVersion[2] = "0-RC{$rcVersion}";
} else {
# Increment minor version
$splitVersion[1] = strval(intval($splitVersion[1]) + 1);
# Set patch
$splitVersion[2] = '0';
}
return implode(".", $splitVersion);
}
function updateReadme(string $bumpedVersion)
{
echo "Reading contents at ".README_FILEPATH."...\n";
$fileContents = file_get_contents(README_FILEPATH);
if ($fileContents) {
$package = str_replace('/', '\/', PACKAGE_NAME);
$pattern = '/"'.$package.'"\s*:\s*".+"/';
$replacement = '"'.PACKAGE_NAME.'": "^'.$bumpedVersion.'"';
$numReplacements = 0;
$updatedContents = preg_replace($pattern, $replacement, $fileContents, -1, $numReplacements);
if (!$numReplacements) {
echo "Unable to find and replace SDK version\n";
return;
}
echo file_put_contents(README_FILEPATH, $updatedContents) ? "Successfully updated ".README_FILEPATH. "\n" : "Failed to update ".README_FILEPATH."\n";
return;
}
echo "Could not read contents at ".README_FILEPATH."\n";
}
function updateGraphConstants(string $filePath, string $bumpedVersion)
{
echo "Reading contents at {$filePath}...\n";
$fileContents = file_get_contents($filePath);
if ($fileContents) {
$pattern = '/'. SDK_VERSION_VAR_NAME . '\s+=\s+".+"/';
$replacement = SDK_VERSION_VAR_NAME . " = \"{$bumpedVersion}\"";
$numReplacements = 0;
$updatedContents = preg_replace($pattern, $replacement, $fileContents, -1, $numReplacements);
if (!$numReplacements) {
echo "Unable to find and replace SDK version\n";
return;
}
echo file_put_contents($filePath, $updatedContents) ? "Successfully updated {$filePath}\n" : "Failed to update {$filePath}\n";
return;
}
echo "Could not read contents at {$filePath}\n";
}