Commit c2a6e4b

mo khan <mo.khan@gmail.com>
2020-05-15 19:40:06
Spawn separate thread for plugins and improve progress bar output
1 parent 244e9ff
Changed files (2)
lib
spandx
lib/spandx/cli/commands/scan.rb
@@ -31,14 +31,22 @@ module Spandx
         end
 
         def each_dependency_from(file)
-          res = ::Spandx::Core::Parser
-            .for(file)
-            .parse(file)
-          bar = TTY::ProgressBar.new('Add data to dependencies [:bar, :elapsed] :percent', total: res.size)
-          res.map do |dependency|
+          dependencies = ::Spandx::Core::Parser.for(file).parse(file)
+          bar = TTY::ProgressBar.new("#{file} [:bar, :elapsed] :percent", total: dependencies.size)
+          queue = Queue.new
+
+          Thread.new do
+            dependencies.each { |dependency| queue.enq(enhance(dependency)) }
+            queue.enq(:done)
+          end
+
+          loop do
+            item = queue.deq
+            break if item == :done
+
             bar.advance(1)
-            enhance(dependency)
-          end.each { |dependency| yield dependency }
+            yield item
+          end
         end
 
         def format(output)
lib/spandx/core/path_traversal.rb
@@ -14,6 +14,14 @@ module Spandx
         each_file_in(root, &block)
       end
 
+      def to_enum
+        Enumerator.new do |yielder|
+          each do |item|
+            yielder.yield item
+          end
+        end
+      end
+
       private
 
       def recursive?