Completed
Push — master ( 9ed743...6b5e3b )
by Angus
02:43
created

deploy.php ➔ runS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/** https://deployer.org/
3
 *
4
 * Call via: dep -f=_scripts/deploy.php deploy
5
 *
6
 */
7
namespace Deployer;
8
9
if(basename(getcwd()) !== 'manga-tracker') die('Bad CWD: Call from manga-tracker with dep -f=_scripts/deploy.php deploy');
10
11
require 'recipe/cachetool.php'; //requires deployer/recipes
12
require 'recipe/codeigniter.php';
13
14
// Project name
15
set('application', 'trackr.moe');
16
17
// Project repository
18
set('repository', '[email protected]:DakuTree/manga-tracker.git');
19
20
// [Optional] Allocate tty for git clone. Default value is false.
21
set('git_tty', FALSE);
22
23
// Fix bug on windows
24
set('ssh_multiplexing', false);
25
26
set('clear_paths', [
27
	'.docker',
28
	'.git',
29
	'.github',
30
	'.idea'
31
]);
32
33
add('copy_files', [
34
	'_scripts/config.ini',
35
	'application/config/_secure/email.php',
36
	'application/config/_secure/mailgun.php',
37
	'application/config/_secure/monolog.php',
38
	'application/config/_secure/recaptcha.php',
39
	'application/config/_secure/sites.php',
40
	'application/config/production/database_password.php'
41
]);
42
add('shared_dirs', []);
43
44
// Writable dirs by web server 
45
add('writable_dirs', []);
46
47
set('allow_anonymous_stats', false);
48
49
// Hosts
50
host('trackr.moe')
51
	->user('deployer')
52
	->identityFile('~/.ssh/deploy.ppk')
53
	->set('deploy_path', '/var/www/trackr.moe');
54
55
// Tasks
56
//task('deploy:symlink', function() {}); //Uncomment when testing.
57
//task('cleanup', function() {});        //Uncomment when testing.
58
59
task('deploy:compile_assets', function() {
60
	// less
61
	//// themes
62
	writeln('➤➤ <comment>Compiling Themes</comment>');
63
	runS('( \
64
		cd {{release_path}}/public/assets/less && \
65
		exec lessc \
66
		--csscomb \
67
		--modify-var=themeLocation=common\\\\themes\\\\light \
68
		main.less \
69
		../css/main.light.css \
70
	)');
71
	runS('( \
72
		cd {{release_path}}/public/assets/less && \
73
		exec lessc \
74
		--csscomb \
75
		--modify-var=themeLocation=common\\\\themes\\\\dark \
76
		main.less \
77
		../css/main.dark.css \
78
	)');
79
	//// userscript css
80
	writeln('➤➤ <comment>Compiling Userscript CSS</comment>');
81
	runS('( \
82
		cd {{release_path}}/public/userscripts/assets/ && \
83
		exec lessc \
84
		--csscomb \
85
		main.less \
86
		main.css \
87
	)');
88
89
	//js
90
	writeln('➤➤ <comment>Compiling JavaScript</comment>');
91
	runS('( \
92
		cd {{release_path}}/public/assets/js && \
93
		exec google-closure-compiler-js \
94
		--compilationLevel SIMPLE_OPTIMIZATIONS \
95
		{{release_path}}/public/assets/js/main.js \
96
		{{release_path}}/public/assets/js/pages/*.js \
97
		> {{release_path}}/public/assets/js/compiled.min.js \
98
	)');
99
100
	//icons
101
	writeln('➤➤ <comment>Compiling Icons</comment>');
102
	runS('( \
103
		cd {{release_path}} && \
104
		php -r "include \'_scripts/SpritesheetGenerator.php\'; (new SpriteSheet(\'site\', FALSE))->generate();" && \
105
		php -r "include \'_scripts/SpritesheetGenerator.php\'; (new SpriteSheet(\'time\', FALSE))->generate();"
106
	)');
107
108
	// userscript .meta.js
109
	writeln('➤➤ <comment>Compiling Userscript .meta.js</comment>');
110
	runS('( \
111
		cd {{release_path}} && \
112
		userscript-utils get-updateblock -du -i public/userscripts/manga-tracker.user.js -o public/userscripts/manga-tracker.meta.js \
113
	)');
114
	// userscript version constant
115
	writeln('➤➤ <comment>Tweaking Userscript Version Constant</comment>');
116
	runS('( \
117
		cd {{release_path}} && \
118
		USERSCRIPT_VERSION=$(grep -oP \'@version[\s]*\K([0-9\.]+)\' public/userscripts/manga-tracker.meta.js); \
119
		sed -i -r "s/define\(\'USERSCRIPT_VERSION\', \'[0-9\.]+\'\);/define\(\'USERSCRIPT_VERSION\', \'$USERSCRIPT_VERSION\'\);/g" application/config/constants.php \
120
	)');
121
});
122
123
task('deploy:copy_files', function () {
124
	$sharedPath = '{{deploy_path}}/shared';
125
	foreach (get('copy_files') as $file) {
126
		$dirname = dirname(parse($file));
127
		// Create dir of shared file
128
		runS("mkdir -p $sharedPath/" . $dirname);
129
		// Check if shared file does not exist in shared.
130
		// and file exist in release
131
		if (!test("[ -f $sharedPath/$file ]") && test("[ -f {{release_path}}/$file ]")) {
132
			// Copy file in shared dir if not present
133
			runS("cp -rv {{release_path}}/$file $sharedPath/$file");
134
		}
135
		// Remove from source.
136
		runS("if [ -f $(echo {{release_path}}/$file) ]; then rm -rf {{release_path}}/$file; fi");
137
		// Ensure dir is available in release
138
		runS("if [ ! -d $(echo {{release_path}}/$dirname) ]; then mkdir -p {{release_path}}/$dirname;fi");
139
		// Touch shared
140
		runS("touch $sharedPath/$file");
141
142
		runS("cp $sharedPath/$file {{release_path}}/$file");
143
	}
144
});
145
146
task('deploy:migrate_db', function () {
147
	// Migration is disabled by default on production, so we need to toggle it temporally.
148
	runS('( \
149
		cd {{release_path}} && \
150
		sed -i -r "s/(migration_enabled.*?)FALSE/\1TRUE/g" application/config/production/migration.php && \
151
		CI_ENV="production" php public/index.php admin/migrate && \
152
		sed -i -r "s/(migration_enabled.*?)TRUE/\1FALSE/g" application/config/production/migration.php \
153
	)');
154
});
155
156
task('deploy:maintenance_enable', function () {
157
	//define('MAINTENANCE', FALSE);
158
	runS('( \
159
		cd {{previous_release}} && \
160
		sed -i -r "s/(\'MAINTENANCE\',) FALSE/\1 TRUE/" public/index.php \
161
	)');
162
	runS('( \
163
		cd {{release_path}} && \
164
		sed -i -r "s/(\'MAINTENANCE\',) FALSE/\1 TRUE/" public/index.php \
165
	)');
166
});
167
task('deploy:maintenance_disable', function () {
168
	//define('MAINTENANCE', FALSE);
169
	runS('( \
170
		cd {{release_path}} && \
171
		sed -i -r "s/(\'MAINTENANCE\',) TRUE/\1 FALSE/" public/index.php \
172
	)');
173
});
174
175
// Events
176
after('deploy:vendors', 'deploy:compile_assets');
177
after('deploy:vendors', 'deploy:clear_paths');
178
179
after('deploy:shared', 'deploy:copy_files');
180
181
before('deploy:symlink', 'deploy:maintenance_enable');
182
after('deploy:maintenance_enable', 'cachetool:clear:opcache');
183
after('deploy:symlink', 'deploy:migrate_db');
184
185
after('deploy', 'deploy:maintenance_disable');
186
after('deploy:maintenance_disable', 'cachetool:clear:opcache');
187
after('deploy:failed', 'deploy:unlock'); // [Optional] if deploy fails automatically unlock.
188
189
//TODO: After deploy:success, ask if we want to send a tweet and/or update notices?
190
//      https://deployer.org/docs/api
191
192
function runS(string $runCommand) : void {
193
	$runCommand = preg_replace("/\r\n/", "\n", $runCommand);
194
	run($runCommand);
195
}
196