Verified Commit 61d9e3b7 authored by Balasankar C's avatar Balasankar C
Browse files

Add dependency relationship also to the output

parent a902aa9a
Loading
Loading
Loading
Loading
+48 −1
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ module Omnibus
  class DependencyInformation

    JSON_FILE = "dependency_licenses.json".freeze
    DEP_FILE = "dependency_relations.json".freeze

    class << self
      def process!(project)
@@ -21,6 +22,10 @@ module Omnibus
      File.expand_path(JSON_FILE, project.install_dir)
    end

    def dep_location
      File.expand_path(DEP_FILE, project.install_dir)
    end

    def process!
      csv_output = ""
      output = {}
@@ -35,7 +40,6 @@ module Omnibus
      Dir.glob(File.expand_path("*.csv", csv_dir)).each do |csv_file|
        csv_output += File.open(csv_file).read + "\n"
      end
      FileUtils.rm_rf(csv_dir)

      # Creating JSON output
      csv_output.each_line do |line|
@@ -47,6 +51,49 @@ module Omnibus
      File.open(json_location, "w") do |f|
        f.write(JSON.pretty_generate(output))
      end

      generate_relation(project, output)
    end

    def generate_relation(project, output)
      relations = {}
      keys = []
      project.library.each do |component|
          unless relations.has_key?(component)
            keys << component.name
            relations[component.name] = []
          end
        component.dependencies.each do |dependency|
          relations[component.name] << {'name': dependency, 'version': output[dependency][:version], 'license': output[dependency][:license] }
        end
      end

      csv_dir = File.expand_path("licenses", project.install_dir)
      Dir.glob(File.expand_path("*.csv", csv_dir)).each do |csv_file|
        component = File.basename(csv_file, File.extname(csv_file))

        unless relations.has_key?(component)
          keys << component
          relations[component] = []
        end
        csv_output = File.open(csv_file).read
        csv_output.each_line do |line|
          line = line.strip
          next if line.empty?
          dependencies = line.split(",")
          dependency = dependencies[0]
          relations[component] << {'name': dependency, 'version': output[dependency][:version], 'license': output[dependency][:license] }
        end

      end

      f = File.open(dep_location, "w")
      final_output = []
      keys.each do |key|
        final_output << {name: key, version: output[key][:version], license: output[key][:license], dependencies: relations[key]}
      end
      f.write(JSON.pretty_generate(final_output))
      f.close()
    end
  end
end