Subversion Repositories oidplus

Rev

Rev 1430 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1426 daniel-mar 1
#!/usr/bin/env php
2
<?php
3
 
4
/*
5
 * OIDplus 2.0
6
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
 
21
// This script will be called at the ViaThinkSoft server side
22
 
23
use ViaThinkSoft\OIDplus\OIDplus;
24
 
25
require_once __DIR__.'/funcs.inc.php';
26
 
27
include __DIR__.'/../../../../../includes/oidplus.inc.php';
28
 
29
 
30
// Generate keypair with:
31
//	openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:8192
32
//	openssl rsa -pubout -in private.pem -out public.pem
33
 
34
$argc = $_SERVER['argc']; // to please Eclipse for PHP
35
$argv = $_SERVER['argv']; // to please Eclipse for PHP
36
 
37
if (PHP_SAPI != 'cli') {
38
	fwrite(STDERR, "This file can only be invoked in CLI mode.\n");
39
	die();
40
}
41
 
42
if (DIRECTORY_SEPARATOR != '/') {
43
	fwrite(STDERR,  "This script can only run on Unix like systems\n");
44
	exit(2);
45
}
46
 
47
if ($argc != 4) {
48
	fwrite(STDERR, "Usage: ".$argv[0]." <targetpath> <privkey> <force(1|0)>\n");
49
	exit(2);
50
}
51
 
52
$output_dir = $argv[1];
53
$priv_key = $argv[2];
54
$force = $argv[3];
55
 
56
if (!is_dir($output_dir)) {
57
	fwrite(STDERR, "Path $output_dir does not exist!\n");
58
	exit(1);
59
}
60
 
61
if (!is_file($priv_key)) {
62
	fwrite(STDERR, "Private key file $priv_key does not exist!\n");
63
	exit(1);
64
}
65
 
66
if (($force != '1') && ($force != '0')) {
67
	fwrite(STDERR, "Argument 'force' must be 0 or 1\n");
68
	exit(1);
69
}
70
 
71
 
72
// Step 0: We need two copies of a up-to-date GIT working copy (to compare old-new for changescripts)
73
// We do not delete the file in /tmp, because we can re-use it
74
// It's no secret, so other users can read it. They must not write to it, though.
75
 
76
foreach (['a','b'] as $ab) {
77
	$tmpdir = '/tmp/oidplus_git_'.$ab;
78
	if (!is_dir($tmpdir)) {
1427 daniel-mar 79
		exec('git clone '.escapeshellarg(OIDplus::getEditionInfo()['gitrepo']).' '.escapeshellarg($tmpdir).' --quiet', $out, $ec);
1426 daniel-mar 80
		if ($ec != 0) {
81
			fwrite(STDERR, "GIT Clone failed\n");
82
			exit(1);
83
		}
84
	}
1427 daniel-mar 85
	exec('cd '.escapeshellarg($tmpdir).' && git reset --hard --quiet && git checkout master --quiet && git pull --quiet', $out, $ec);
1426 daniel-mar 86
	if ($ec != 0) {
87
		fwrite(STDERR, "GIT Pull failed\n");
88
		exit(1);
89
	}
90
}
91
 
92
// Step 1: List all GIT commits and determine its OIDplus versions
93
 
1427 daniel-mar 94
$git_version_cache_file = OIDplus::localpath() . 'userdata/cache/git_version_cache.json';
1426 daniel-mar 95
$git_version_cache = file_exists($git_version_cache_file) ? json_decode(file_get_contents($git_version_cache_file),true) : ["git-to-ver"=>[],"version-first-commit"=>[]];
96
 
97
$ec = -1;
98
$out = array();
1427 daniel-mar 99
exec('cd /tmp/oidplus_git_a && git reset --hard --quiet && git checkout master --quiet && git log --reverse --quiet', $out, $ec);
1426 daniel-mar 100
if ($ec != 0) {
101
	fwrite(STDERR, "GIT Log failed\n");
102
	exit(1);
1430 daniel-mar 103
}
1426 daniel-mar 104
 
1430 daniel-mar 105
$cont = implode("\n", $out);
106
preg_match_all('%^commit (.+)\n.+trunk@(\d+) 02e%smU', $cont, $git_commits, PREG_SET_ORDER);
107
$svn_git = [];
108
foreach ($git_commits as $git_commit) {
109
	// ViaThinkSoft uses "Git-to-Svn" to synchronize SVN revisions to GitHub
110
	// We need to find the revision numbers for each commit, so we can identify
111
	// which commits were revision 1..1425, so we can see where version 2.0.0.<svnrev> applies
112
	// and when changelog.json.php applies.
113
	$svn_git[$git_commit[1]] = $git_commit[2];
114
}
115
preg_match_all('%^commit (.+)\n%smU', $cont, $git_commits, PREG_SET_ORDER);
116
foreach ($git_commits as $git_commit) {
117
	if (isset($git_version_cache["git-to-ver"][$git_commit[1]])) continue;
1426 daniel-mar 118
 
1430 daniel-mar 119
	$ary = explode('/',OIDplus::getEditionInfo()['gitrepo']);
120
	$github_user = $ary[3];
121
	$github_project = $ary[4];
122
	$v3_versionfile = 'https://raw.githubusercontent.com/'.$github_user.'/'.$github_project.'/'.$git_commit[1].'/changelog.json.php';
123
 
124
	$svn = $svn_git[$git_commit[1]] ?? 0;
125
	if (($svn >= 1) && ($svn <= 1425)) {
126
		// SVN Revision 0..1425 were named 2.0.0.<svnrev>. They did not have a changelog.json.php. Every revision was a new version.
127
		$ver = "2.0.0.$svn";
128
		$git_version_cache["git-to-ver"][$git_commit[1]] = $ver;
129
		if (!isset($git_version_cache["version-first-commit"][$ver])) {
130
			$git_version_cache["version-first-commit"][$ver] = $git_commit[1];
1426 daniel-mar 131
		}
1430 daniel-mar 132
	} else if ($ver = OIDplus::getVersion($v3_versionfile)) {
133
		// Beginning with SVN revision 1426, versions are defined by the first revision/commit that adds a new version to changelog.json.php
134
		$git_version_cache["git-to-ver"][$git_commit[1]] = $ver;
135
		if (!isset($git_version_cache["version-first-commit"][$ver])) {
136
			$git_version_cache["version-first-commit"][$ver] = $git_commit[1];
137
		}
138
	} else {
139
		fwrite(STDERR, "PROBLEM: " . $git_commit[1] . "\n");
1426 daniel-mar 140
	}
141
}
142
 
143
file_put_contents($git_version_cache_file, json_encode($git_version_cache, JSON_PRETTY_PRINT));
144
 
145
// Step 2: Write change-scripts (for ZIP/TAR-GZ distribution channel)
146
// The order of $git_version_cache is very critical here!
147
 
148
$prev_commit = '-';
149
$prev_version = '2.0.0.0';
1444 daniel-mar 150
$latest_version = null;
1426 daniel-mar 151
foreach ($git_version_cache["version-first-commit"] as $version => $version_first_commit) {
1444 daniel-mar 152
	$latest_version = $version;
1427 daniel-mar 153
	$changescript_file = $output_dir.'/changescript_'.$version.'.txt';
1426 daniel-mar 154
	if ($force || !file_exists($changescript_file) || !file_exists($changescript_file.'.gz')) {
1427 daniel-mar 155
		echo "Generate changescript for version $version ($version_first_commit)\n";
1426 daniel-mar 156
 
157
		if ($prev_commit == '-') {
158
			$old_dir = '/tmp/oidplus_git_empty';
159
			if (!is_dir($old_dir)) mkdir($old_dir);
160
		} else {
161
			$old_dir = '/tmp/oidplus_git_a';
1427 daniel-mar 162
			exec('cd '.escapeshellarg($old_dir).' && git reset --hard --quiet && git checkout '.escapeshellarg($prev_commit).' --quiet >/dev/null 2>&1', $out, $ec);
1426 daniel-mar 163
			if ($ec != 0) {
164
				fwrite(STDERR, "GIT Checkout $prev_commit failed\n");
165
				exit(1);
166
			}
167
			hotfix_dir($prev_version, $old_dir);
168
		}
169
 
170
		$new_dir = '/tmp/oidplus_git_b';
1427 daniel-mar 171
		exec('cd '.escapeshellarg($new_dir).' && git reset --hard --quiet && git checkout '.escapeshellarg($version_first_commit).' --quiet >/dev/null 2>&1', $out, $ec);
1426 daniel-mar 172
		if ($ec != 0) {
173
			fwrite(STDERR, "GIT Checkout $version_first_commit failed\n");
174
			exit(1);
175
		}
176
		hotfix_dir($version, $new_dir);
177
 
178
		oidplus_create_changescript($old_dir, $new_dir, $changescript_file, $prev_version, $version, $priv_key);
179
 
180
		if ($prev_commit == '-') {
181
			rmdir($old_dir);
182
		}
183
	}
184
	$prev_commit = $version_first_commit;
185
	$prev_version = $version;
186
}
1444 daniel-mar 187
 
188
// Step 3 (optional): Upload to GitHub (a different repository, just for deploying updates)
189
// How to setup a GitHub deployment key? See https://dylancastillo.co/how-to-use-github-deploy-keys/
190
 
191
shell_exec("cd ".escapeshellarg($output_dir)." && git add * 2>/dev/null");
192
shell_exec("cd ".escapeshellarg($output_dir)." && git commit -m ".escapeshellarg("Update $latest_version")." 2>/dev/null");
193
shell_exec("cd ".escapeshellarg($output_dir)." && git push 2>/dev/null");