summaryrefslogtreecommitdiff
path: root/lib/license/finder/ext/dotnet.rb
blob: c5ff485fe9787f463befb2ad0a44564706429758 (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
# 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 prepare
      shell.execute(['apt-get', :update, '-q'])
      shell.execute(['apt-get', :install, '-y', '--no-install-recommends', 'dotnet-sdk-3.1'])
      shell.execute([:mkdir, '-p', vendor_path.to_s]) unless vendor_path.exist?
      shell.execute([
        :dotnet,
        :restore, detected_package_path.to_s,
        '--no-cache',
        '--packages', vendor_path.to_s,
        '--locked-mode',
        '--verbosity', :detailed
      ])
    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

    private

    def vendor_path
      @vendor_path ||= Pathname.pwd.join('.gitlab', 'cache', 'vendor')
    end

    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