summaryrefslogtreecommitdiff
path: root/lib/license/finder/ext/dotnet.rb
blob: eebbbbd57de0840c6729ec9b7fbbf3e8c4c45bd7 (plain)
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
# frozen_string_literal: true

module LicenseFinder
  class Dotnet
    def possible_package_paths
      project_path.glob('*.sln') +
        project_path.glob('*.csproj') +
        project_path.glob('*.vbproj') +
        project_path.glob('*.fsproj')
    end

    def installed?(*)
      true
    end

    def prepare
      create_vendor_path
      tool_box.install(tool: :"dotnet-core", version: dotnet_version)

      shell.execute([
        "/opt/asdf/installs/dotnet-core/#{dotnet_version}/dotnet",
        :restore, detected_package_path,
        '--locked-mode',
        '--no-cache',
        '--packages', vendor_path,
        '--verbosity', :normal
      ])
    end

    def current_packages
      asset_files.flat_map do |file|
        json = JSON.parse(IO.read(file))
        json.fetch('libraries', []).map do |slug, data|
          name, version = slug.split('/')
          map_from(name, version, data)
        end
      end
    end

    def dotnet_version
      @dotnet_version ||= tool_box.version_of(:"dotnet-core")
    end

    private

    def map_from(name, version, data)
      Dependency.new(
        'NuGet',
        name,
        version,
        spec_licenses: licenses_from(data['path'], data.fetch('files', [])),
        detection_path: detected_package_path,
        install_path: vendor_path.join(data['path'])
      )
    end

    def nuspec_path_from(path, files)
      install_path = vendor_path.join(path)
      nuspec_filename = files.find { |x| x.end_with?('.nuspec') }
      return install_path.join(nuspec_filename) if nuspec_filename
    end

    def licenses_from(path, files)
      nuspec = nuspec_path_from(path, files)
      return [] if nuspec.nil? || !nuspec.exist?

      ::License::Management::Nuspec.new(nuspec.read).licenses
    end
  end
end