forked from twitter/twitter-text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
140 lines (116 loc) · 3.34 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
require 'rubygems'
require 'yaml'
require 'json'
require 'date'
require 'digest'
namespace :test do
namespace :conformance do
desc "Prepare JS conformance test suite"
task :prepare do
test_files = ['autolink', 'extract', 'hit_highlighting', 'validate', 'tlds']
r = {}
f = File.open(repo_path("test", "conformance.js"), "w")
f.write("var cases = {};")
test_files.each do |test_file|
path = repo_path("..", "conformance", test_file + ".yml")
yml = YAML.load_file(path)
f.write("cases.#{test_file} = #{yml['tests'].to_json};")
end
f.close
end
desc "Run conformance test suite"
task :run do
exec('open test/conformance.html')
end
end
desc "Run conformance test suite"
task :conformance => ['conformance:prepare', 'conformance:run'] do
end
desc "Run JavaScript test suite"
task :run do
exec('open test/test.html')
end
desc "Run NodeJS test suite"
task :node do
exec('node test/node_tests.js');
end
end
desc "Run JavaScript test suite"
task :test => ['test:run']
directory "pkg"
task :package, [:version] => [:pkg] do |t, args|
pkg_name = "twitter-text-#{args.version}.js"
puts "Building #{pkg_name}..."
pkg_file = File.open(repo_path("pkg", pkg_name), "w")
puts "Writing header..."
header_comment = <<-COMMENT
/*!
* twitter-text-js #{args.version}
*
* Copyright 2012 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this work except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
COMMENT
pkg_file.write(header_comment)
puts "Writing library..."
js_file = File.open(repo_path("twitter-text.js"), "r")
pkg_file.write(js_file.read)
js_file.close
pkg_file.close
puts "Minify pkg..."
src_file = repo_path("pkg", pkg_name)
dst_file = repo_path("pkg", "twitter-text-#{args.version}.min.js")
exec('node_modules/uglify-js/bin/uglifyjs ' + src_file + ' -o ' + dst_file);
puts "Done with #{pkg_name}"
end
namespace :tlds do
desc "Update tlds in twitter-text.js based on conformance tld_lib.yml"
task :update do
tld_yml = repo_path('..', 'conformance', 'tld_lib.yml')
tlds = YAML.load_file(tld_yml)
cctlds = format_tlds(tlds['country'], 100)
gtlds = format_tlds(tlds['generic'], 100)
twitter_text_js = File.read(repo_path('twitter-text.js'))
replace_tlds!(twitter_text_js, 'validGTLD', gtlds)
replace_tlds!(twitter_text_js, 'validCCTLD', cctlds)
File.open(repo_path('twitter-text.js'), 'w') do |file|
file.write(twitter_text_js)
end
end
end
def format_tlds(tlds, line_length)
tld_line = []
lines = []
tlds.each do |tld|
if line_js(tld_line + [tld]).length > line_length
lines << line_js(tld_line)
tld_line = [tld]
else
tld_line << tld
end
end
lines << line_js(tld_line) if tld_line.length > 0
lines.join("|' +\n") + "' +"
end
def line_js(tlds)
quote = "'"
indent = 4
' ' * indent + quote + tlds.join('|')
end
def replace_tlds!(source_code, name, tlds)
start = Regexp.quote("twttr.txt.regexen.#{name} =")
source_code.sub!(/#{start}.*?;\n/m, <<-D)
twttr.txt.regexen.#{name} = regexSupplant(RegExp(
'(?:(?:' +
#{tlds}
')(?=[^0-9a-zA-Z@]|$))'));
D
end
def repo_path(*path)
File.join(File.dirname(__FILE__), *path)
end