Skip to content

Commit bb7ce19

Browse files
committed
Enable concurrent minifying by passing t parameter
1 parent 99b48e9 commit bb7ce19

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

minify.rb

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,25 @@
44
# Run example: ruby minify.rb target_directory
55

66
dir = ARGV[0]
7+
8+
pids = []
9+
10+
# set multi threaded flag
11+
if ARGV.length == 2 and ARGV[1] == 't'
12+
multi_threaded = true
13+
else
14+
multi_threaded = false
15+
end
16+
17+
# directory pattern to scan
718
pattern = "#{dir}#{File::SEPARATOR}*.js"
819

920
unless dir.is_a? String
1021
puts 'Expect first argument to be a directory path, but was ' + dir.to_s
1122
exit -1
1223
end
1324

25+
# log output
1426
puts 'dir: ' + dir
1527
puts 'pattern: ' + pattern
1628
puts 'JavaScript (including min.js) files found: ' + Dir[pattern].each.size.to_s
@@ -44,23 +56,33 @@
4456
command = "uglifyjs #{file} -o #{without_extension}.min.js --source-map #{without_extension}.min.map"
4557

4658
# execute command
47-
system command
59+
if multi_threaded
60+
pids.push(spawn command) # spawn doesn't wait for execution
61+
else
62+
system command # system does wait
4863

49-
# check if command succeeded
50-
if $?.success?
51-
success_count += 1
64+
# check if command succeeded
65+
if $?.success?
66+
success_count += 1
5267

53-
puts 'Successfully minified ' + file
54-
else
55-
puts $?
56-
puts 'WARNING, minify command failed ' + command
68+
puts 'Successfully minified ' + file
69+
else
70+
puts $?
71+
puts 'WARNING, minify command failed ' + command
5772

58-
fail_count += 1
73+
fail_count += 1
74+
end
5975
end
6076
end
6177
end
6278
end
6379

80+
# wait for all processes to finish
81+
pids.each { |pid|
82+
puts "Wait for PID #{pid}"
83+
Process.wait pid
84+
}
85+
6486
puts "Processed #{js_count} files, #{success_count} successes and #{fail_count} failures"
6587
# exit success if fail_count == 0
6688
exit(fail_count)

minify_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def test_minify
3131
# command should exit successfully
3232
assert $?.success?
3333

34+
assert_minified
35+
end
36+
37+
def assert_minified
3438
files = Dir.entries(@dir)
3539
assert files.include? 'another.min.js'
3640
assert files.include? 'another.min.map'
@@ -47,4 +51,11 @@ def test_invalid_arguments
4751

4852
assert !$?.success?
4953
end
54+
55+
def test_concurrent
56+
system @command + ' t'
57+
58+
assert($?.success?, 'command should complete')
59+
assert_minified
60+
end
5061
end

0 commit comments

Comments
 (0)