File tree Expand file tree Collapse file tree 2 files changed +42
-9
lines changed Expand file tree Collapse file tree 2 files changed +42
-9
lines changed Original file line number Diff line number Diff line change 4
4
# Run example: ruby minify.rb target_directory
5
5
6
6
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
7
18
pattern = "#{ dir } #{ File ::SEPARATOR } *.js"
8
19
9
20
unless dir . is_a? String
10
21
puts 'Expect first argument to be a directory path, but was ' + dir . to_s
11
22
exit -1
12
23
end
13
24
25
+ # log output
14
26
puts 'dir: ' + dir
15
27
puts 'pattern: ' + pattern
16
28
puts 'JavaScript (including min.js) files found: ' + Dir [ pattern ] . each . size . to_s
44
56
command = "uglifyjs #{ file } -o #{ without_extension } .min.js --source-map #{ without_extension } .min.map"
45
57
46
58
# 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
48
63
49
- # check if command succeeded
50
- if $?. success?
51
- success_count += 1
64
+ # check if command succeeded
65
+ if $?. success?
66
+ success_count += 1
52
67
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
57
72
58
- fail_count += 1
73
+ fail_count += 1
74
+ end
59
75
end
60
76
end
61
77
end
62
78
end
63
79
80
+ # wait for all processes to finish
81
+ pids . each { |pid |
82
+ puts "Wait for PID #{ pid } "
83
+ Process . wait pid
84
+ }
85
+
64
86
puts "Processed #{ js_count } files, #{ success_count } successes and #{ fail_count } failures"
65
87
# exit success if fail_count == 0
66
88
exit ( fail_count )
Original file line number Diff line number Diff line change @@ -31,6 +31,10 @@ def test_minify
31
31
# command should exit successfully
32
32
assert $?. success?
33
33
34
+ assert_minified
35
+ end
36
+
37
+ def assert_minified
34
38
files = Dir . entries ( @dir )
35
39
assert files . include? 'another.min.js'
36
40
assert files . include? 'another.min.map'
@@ -47,4 +51,11 @@ def test_invalid_arguments
47
51
48
52
assert !$?. success?
49
53
end
54
+
55
+ def test_concurrent
56
+ system @command + ' t'
57
+
58
+ assert ( $?. success? , 'command should complete' )
59
+ assert_minified
60
+ end
50
61
end
You can’t perform that action at this time.
0 commit comments