forked from christeredvartsen/php-bittorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
361 lines (306 loc) · 10.4 KB
/
Rakefile
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
require 'date'
require 'digest/md5'
require 'fileutils'
require 'nokogiri'
basedir = "."
build = "#{basedir}/build"
source = "#{basedir}/PHP"
desc "Task used by Jenkins-CI"
task :jenkins => [:lint, :prepare, :installdep, :phpunit, :phpdoc, :phploc, :phpcs_ci, :phpcb, :phpcpd, :phpmd, :phpmd_html]
desc "Task used by Travis-CI"
task :travis => [:installdep, :phpunit]
desc "Default task"
task :default => [:lint, :prepare, :installdep, :phpunit, :phpdoc, :phpcs]
desc "Clean up and create artifact directories"
task :prepare do
puts "Prepare build"
FileUtils.rm_rf build
FileUtils.mkdir build
["coverage", "logs", "docs", "code-browser"].each do |d|
FileUtils.mkdir "#{build}/#{d}"
end
end
desc "Check syntax on all php files in the project"
task :lint do
puts "lint PHP files"
`git ls-files "*.php"`.split("\n").each do |f|
begin
sh %{php -l #{f}}
rescue Exception
exit 1
end
end
end
desc "Install dependencies"
task :installdep do
if ENV["TRAVIS"] == "true"
system "composer --no-ansi install --dev"
else
Rake::Task["install_composer"].invoke
system "php -d \"apc.enable_cli=0\" composer.phar install --dev"
end
end
desc "Update dependencies"
task :updatedep do
Rake::Task["install_composer"].invoke
system "php -d \"apc.enable_cli=0\" composer.phar update --dev"
end
desc "Install/update composer itself"
task :install_composer do
if File.exists?("composer.phar")
system "php -d \"apc.enable_cli=0\" composer.phar self-update"
else
system "curl -s http://getcomposer.org/installer | php -d \"apc.enable_cli=0\""
end
end
desc "Run unit tests"
task :phpunit do
config = "phpunit.xml.dist"
if ENV["TRAVIS"] == "true"
config = "phpunit.xml.travis"
elsif File.exists?("phpunit.xml")
config = "phpunit.xml"
end
begin
sh %{vendor/bin/phpunit --verbose -c #{config}}
rescue Exception
exit 1
end
end
desc "Generate API documentation using phpdoc"
task :phpdoc do
puts "Generate API docs"
system "phpdoc -d #{source} -t #{build}/docs --title \"PHP BitTorrent API Documentation\""
end
desc "Generate phploc logs"
task :phploc do
puts "Generate LOC data"
system "phploc --log-csv #{build}/logs/phploc.csv --log-xml #{build}/logs/phploc.xml #{source}"
end
desc "Generate checkstyle.xml using PHP_CodeSniffer"
task :phpcs_ci do
puts "Generate CS violation reports"
system "phpcs --report=checkstyle --report-file=#{build}/logs/checkstyle.xml --standard=Imbo #{source}"
end
desc "Check CS"
task :phpcs do
puts "Check CS"
system "phpcs --standard=Imbo #{source}"
end
desc "Aggregate tool output with PHP_CodeBrowser"
task :phpcb do
puts "Generate codebrowser"
system "phpcb --source #{source} --output #{build}/code-browser"
end
desc "Generate pmd-cpd.xml using PHPCPD"
task :phpcpd do
puts "Generate CPD logs"
system "phpcpd --log-pmd #{build}/logs/pmd-cpd.xml #{source}"
end
desc "Generate pmd.xml using PHPMD (configuration in phpmd.xml)"
task :phpmd do
puts "Generate mess detection logs"
system "phpmd #{source} xml #{basedir}/phpmd.xml --reportfile #{build}/logs/pmd.xml"
end
desc "Generate pmd.html using PHPMD (configuration in phpmd.xml)"
task :phpmd_html do
puts "Generate mess detection HTML"
system "phpmd #{source} html #{basedir}/phpmd.xml --reportfile #{build}/logs/pmd.html"
end
desc "Create a PEAR package"
task :generate_pear_package, :version do |t, args|
puts "Create a PEAR package"
version = args[:version]
if /^[\d]+\.[\d]+\.[\d]+$/ =~ version
now = DateTime.now
hash = Digest::MD5.new
xml = Nokogiri::XML::Builder.new { |xml|
xml.package(:version => "2.0", :xmlns => "http://pear.php.net/dtd/package-2.0", "xmlns:tasks" => "http://pear.php.net/dtd/tasks-1.0", "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation" => ["http://pear.php.net/dtd/tasks-1.0", "http://pear.php.net/dtd/tasks-1.0.xsd", "http://pear.php.net/dtd/package-2.0", "http://pear.php.net/dtd/package-2.0.xsd"].join(" ")) {
xml.name "PHP_BitTorrent"
xml.channel "pear.starzinger.net"
xml.summary "Components for working with torrent files and the BitTorrent format in PHP"
xml.description "PHP_BitTorrent is a set of components that can be used to interact with torrent files (read+write) and encode/decode to/from the BitTorrent format"
xml.lead {
xml.name "Christer Edvartsen"
xml.user "christeredvartsen"
xml.email "[email protected]"
xml.active "yes"
}
xml.date now.strftime('%Y-%m-%d')
xml.time now.strftime('%H:%M:%S')
xml.version {
xml.release version
xml.api version
}
xml.stability {
xml.release "stable"
xml.api "stable"
}
xml.license "MIT", :uri => "http://www.opensource.org/licenses/mit-license.php"
xml.notes "http://github.com/christeredvartsen/php-bittorrent/blob/#{version}/README.markdown"
xml.contents {
xml.dir(:name => "/") {
# PHP files
`git ls-files PHP`.split("\n").each { |f|
xml.file(:md5sum => hash.hexdigest(File.read(f)), :role => "php", :name => f)
}
# Misc
["README.markdown", "LICENSE", "ChangeLog.markdown"].each { |f|
xml.file(:md5sum => hash.hexdigest(File.read(f)), :role => "doc", :name => f)
}
}
}
xml.dependencies {
xml.required {
xml.php {
xml.min "5.3.2"
}
xml.pearinstaller {
xml.min "1.9.0"
}
}
}
xml.phprelease
}
}
# Write XML to package.xml
File.open("package.xml", "w") { |f|
f.write(xml.to_xml)
}
# Generate pear package
system "pear package"
File.unlink("package.xml")
else
puts "'#{version}' is not a valid version"
exit 1
end
end
desc "Generate phar archive"
task :generate_phar_archive, :version do |t, args|
puts "Generate PHAR archive"
version = args[:version]
if /^[\d]+\.[\d]+\.[\d]+$/ =~ version
# Path to stub
stub = "#{basedir}/stub.php"
# Generate stub
File.open(stub, "w") do |f|
f.write(<<-STUB)
<?php
/**
* PHP BitTorrent
*
* Copyright (c) 2011-2013, Christer Edvartsen <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package PHP\BitTorrent
* @author Christer Edvartsen <[email protected]>
* @copyright Copyright (c) 2011-2013, Christer Edvartsen <[email protected]>
* @license http://www.opensource.org/licenses/mit-license MIT License
* @link https://github.com/christeredvartsen/php-bittorrent
* @version #{version}
*/
Phar::mapPhar();
$basePath = 'phar://' . __FILE__;
spl_autoload_register(function($class) use ($basePath) {
if (strpos($class, 'PHP\\\\BitTorrent\\\\') !== 0) {
return false;
}
$file = $basePath . DIRECTORY_SEPARATOR . substr(str_replace('\\\\', DIRECTORY_SEPARATOR, $class), 4) . '.php';
if (file_exists($file)) {
require $file;
return true;
}
return false;
});
__HALT_COMPILER();
STUB
end
# Generate the phar archive
system "phar-build -s #{source} -S #{stub} --phar #{basedir}/php-bittorrent.phar --ns --strip-files '.php$'"
# Remove the stub
File.unlink("stub.php")
else
puts "'#{version}' is not a valid version"
exit 1
end
end
desc "Publish a PEAR package to pear.starzinger.net"
task :publish_pear_package, :version do |t, args|
puts "Publish PEAR package"
version = args[:version]
if /^[\d]+\.[\d]+\.[\d]+$/ =~ version
# Generate PEAR package
Rake::Task["generate_pear_package"].invoke(version)
package = "PHP_BitTorrent-#{version}.tgz"
if File.exists?(package)
wd = Dir.getwd
Dir.chdir("/home/christer/dev/christeredvartsen.github.com")
system "git pull origin master"
system "pirum add . #{wd}/#{package}"
system "git add --all"
system "git commit -am 'Added #{package[0..-5]}'"
system "git push"
Dir.chdir(wd)
File.unlink(package)
else
puts "#{package} does not exist. Run the pear task first to create the package"
end
else
puts "'#{version}' is not a valid version"
exit 1
end
end
desc "Tag current state of the master branch and push it to GitHub"
task :tag_master_branch, :version do |t, args|
puts "Tag release"
version = args[:version]
if /^[\d]+\.[\d]+\.[\d]+$/ =~ version
# Checkout the master branch
system "git checkout master"
# Merge in the current state of the develop branch
system "git merge develop"
# Update phar arhive
Rake::Task["generate_phar_archive"].invoke(version)
system "git add php-bittorrent.phar"
system "git commit -m 'Updated phar archive' php-bittorrent.phar"
# Tag release and push
system "git tag #{version}"
system "git push"
system "git push --tags"
system "git checkout develop"
else
puts "'#{version}' is not a valid version"
exit 1
end
end
desc "Release a new version"
task :release, :version do |t, args|
puts "Release a new version of the library"
version = args[:version]
if /^[\d]+\.[\d]+\.[\d]+$/ =~ version
# Publish to the PEAR channel
Rake::Task["publish_pear_package"].invoke(version)
# Tag the current state of master and push to GitHub
Rake::Task["tag_master_branch"].invoke(version)
else
puts "'#{version}' is not a valid version"
exit 1
end
end