Failed Conditions
Push — develop ( 1c5a15...6267a3 )
by Reüel
04:16
created

version_update_awk_actions()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 15
ccs 0
cts 10
cp 0
crap 30
rs 9.6111
1
<?php
2
/**
3
 * Repositories.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
$working_dir      = getcwd();
12
$project_dir      = dirname( __DIR__ );
13
$repositories_dir = $project_dir . '/repositories';
14
15
$organisations = array(
16
	'pronamic'          => array(
17
		'wp-datetime',
18
		'wp-money',
19
	),
20
	'wp-pay'            => array(
21
		'core',
22
	),
23
	'wp-pay-gateways'   => array(
24
		'adyen',
25
		'buckaroo',
26
		'ems-e-commerce',
27
		'icepay',
28
		'ideal',
29
		'ideal-advanced-v3',
30
		'ideal-basic',
31
		'ing-kassa-compleet',
32
		'mollie',
33
		'mollie-ideal',
34
		'multisafepay',
35
		'nocks',
36
		'ogone',
37
		'omnikassa',
38
		'omnikassa-2',
39
		'pay-nl',
40
		'sisow',
41
		'targetpay',
42
	),
43
	'wp-pay-extensions' => array(
44
		'charitable',
45
		'easy-digital-downloads',
46
		'event-espresso',
47
		'event-espresso-legacy',
48
		'formidable-forms',
49
		'give',
50
		'gravityforms',
51
		'memberpress',
52
		'ninjaforms',
53
		'restrict-content-pro',
54
		's2member',
55
		'woocommerce',
56
		'wp-e-commerce',
57
	),
58
);
59
60
function version_update_awk_actions() {
61
	global $argv;
62
63
	$version_update = isset( $argv[2] ) ? $argv[2] : 'patch';
64
65
	switch ( $version_update ) {
66
		case 'major':
67
			return 'NR==1{printf "%s.0.0", ++$NR};';
68
69
		case 'minor':
70
			return 'NF==2{print ++$NF}; NF>0{$(NF-1)++; $NF=0; print};';
71
72
		case 'patch':
73
		default:
74
			return 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print};';
75
	}
76
}
77
78
foreach ( $organisations as $organisation => $repositories ) {
79
	echo '# ', $organisation, PHP_EOL;
80
81
	foreach ( $repositories as $repository ) {
82
		echo '- ', $repository, PHP_EOL;
83
84
		$git_url = sprintf(
85
			'https://github.com/%s/%s.git',
86
			$organisation,
87
			$repository
88
		);
89
90
		$git_dir = $repositories_dir . '/' . $organisation . '/' . $repository;
91
92
		if ( ! is_dir( $git_dir ) ) {
93
			echo shell_exec( 'git clone ' . $git_url . ' ' . $git_dir );
94
		}
95
96
		// Git flow.
97
		chdir( $git_dir );
98
99
		$command = null;
100
101
		if ( isset( $argv[1] ) && 'develop' === $argv[1] ) {
102
			$command = 'git checkout develop';
103
		}
104
105
		if ( isset( $argv[1] ) && 'pull' === $argv[1] ) {
106
			$command = 'git pull';
107
		}
108
109
		if ( isset( $argv[1] ) && 'log' === $argv[1] ) {
110
			$command = 'git --no-pager log $(git describe --tags --abbrev=0)..HEAD --oneline';
111
		}
112
113
		if ( isset( $argv[1] ) && 'changelog' === $argv[1] ) {
114
			$command = '
115
				CURRENT_TAG=$(git describe --tags --abbrev=0);
116
				LOG=$(git --no-pager log ${CURRENT_TAG}..HEAD --oneline);
117
118
				# Exit if there are no changes in Git repository.
119
				if [ $(echo "$LOG" | wc -l) -eq 1 ]; then
120
					echo "Version: ${CURRENT_TAG}";
121
					exit;
122
				fi;
123
124
				# Set version numbers.
125
				NEW_VERSION=$(echo "$CURRENT_TAG" | awk -F. -v OFS=. \'' . version_update_awk_actions() . '\');
126
				echo "Version: ${CURRENT_TAG} --> ${NEW_VERSION}";
127
128
				# Add title and log.
129
				TITLE_LINENR=$(grep -n "## \[" CHANGELOG.md | head -2 | tail -1 | cut -d: -f1);
130
				LOG=$(echo "$LOG" | sed \'s/^[a-z0-9]\{7\}/-/\' | sed \'s/^- Merge tag.*//\'; echo "";);
131
				TITLE="## [${NEW_VERSION}] - $(date \'+%Y-%m-%d\')' . \PHP_EOL . '";
132
				ex -s -c "${TITLE_LINENR}i|${TITLE}${LOG}' . str_repeat( \PHP_EOL, 2 ) . '" -c x CHANGELOG.md;
133
134
				# Update footer links.
135
				LINK_LINENR=$(grep -n "\[unreleased\]" CHANGELOG.md | tail -1 | cut -d: -f1);
136
				LINK="[${NEW_VERSION}]: https://github.com/' . $organisation . '/' . $repository . '/compare/${CURRENT_TAG}...${NEW_VERSION}";
137
				CHANGELOG=$(cat CHANGELOG.MD | sed "${LINK_LINENR}s/${CURRENT_TAG}...HEAD/${NEW_VERSION}...HEAD/" | tee CHANGELOG.md);
138
				ex -s -c "$(( ${LINK_LINENR} + 1 ))i|${LINK}" -c x CHANGELOG.md';
139
		}
140
141
		if ( isset( $argv[1] ) && 'update-package-version' === $argv[1] ) {
142
			$command = '
143
				CURRENT_TAG=$(git describe --tags --abbrev=0);
144
				LOG=$(git --no-pager log ${CURRENT_TAG}..HEAD --oneline);
145
146
				# Exit if there are no changes in Git repository.
147
				if [ $(echo "$LOG" | wc -l) -eq 1 ]; then
148
					echo "Version: ${CURRENT_TAG}";
149
					exit;
150
				fi;
151
152
				# Set version numbers.
153
				NEW_VERSION=$(echo "$CURRENT_TAG" | awk -F. -v OFS=. \'' . version_update_awk_actions() . '\');
154
				echo "Version: ${CURRENT_TAG} --> ${NEW_VERSION}";
155
156
				# Update version number.
157
				VERSION_LINENR=$(grep -n "\"version\":" package.json | tail -1 | cut -d: -f1);
158
				PACKAGE_JSON=$(cat package.json | sed "${VERSION_LINENR}s/\"${CURRENT_TAG}\"/\"${NEW_VERSION}\"/" | tee package.json);';
159
		}
160
161
		if ( isset( $argv[1] ) && in_array( $argv[1], array( 'git', 'grunt', 'composer', 'npm', 'ncu' ), true ) ) {
162
			if ( isset( $argv[2] ) ) {
163
				$command = sprintf( '%s %s', $argv[1], $argv[2] );
164
			} else {
165
				$command = sprintf( '%s', $argv[1] );
166
			}
167
		}
168
169
		if ( null !== $command ) {
170
			if ( ! isset( $argv[1] ) || ( isset( $argv[1] ) && ! in_array( $argv[1], array( 'changelog', 'update-package-version' ), true ) ) ) {
171
				echo $command, PHP_EOL;
172
			}
173
174
			echo shell_exec( $command ), PHP_EOL;
175
		}
176
	}
177
}
178