Class: LicenseAuto::Bundler

Inherits:
VirtualPackageManager show all
Defined in:
lib/license_auto/package_manager/bundler.rb

Constant Summary collapse

LANGUAGE =
'Ruby'

Instance Method Summary collapse

Methods inherited from VirtualPackageManager

#dependency_file_path_names

Constructor Details

#initialize(path) ⇒ Bundler

Returns a new instance of Bundler.



11
12
13
# File 'lib/license_auto/package_manager/bundler.rb', line 11

def initialize(path)
  super(path)
end

Instance Method Details

#dependency_file_patternObject



15
16
17
# File 'lib/license_auto/package_manager/bundler.rb', line 15

def dependency_file_pattern
  /#{@path}\/gem.*\.lock$/i
end

#gemfile_patternObject



19
20
21
# File 'lib/license_auto/package_manager/bundler.rb', line 19

def gemfile_pattern
  /gemfile$/i
end

#parse_dependenciesObject



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/license_auto/package_manager/bundler.rb', line 23

def parse_dependencies
  # gemfiles = dependency_file_path_names(pattern=gemfile_pattern)
  # definition = ::Bundler::Definition.build(gemfiles.first, nil, nil)

  # definition.dependencies.each {|dep|
  #   LicenseAuto.logger.debug(dep.name + ' ' + dep.source.remotes.to_s)
  # }

  lock_files = dependency_file_path_names
  if lock_files.empty?
    LicenseAuto.logger.info("Gemfile.lock file not exisit")
    gem_files = dependency_file_path_names(pattern=gemfile_pattern)
    # TODO: parse gem_files
    unless gem_files.empty?
      LicenseAuto.logger.info("Gemfile exisit: #{gem_files}")
    end

    return []
  else
    lock_files.map {|dep_file|
      LicenseAuto.logger.debug(dep_file)
      lockfile_parser = ::Bundler::LockfileParser.new(::Bundler.read_file(dep_file))
      {
          dep_file: dep_file,
          deps: lockfile_parser.specs.map {|spec|
                  remote =
                      case
                        when spec.source.class == ::Bundler::Source::Git
                          spec.source.uri
                        when spec.source.class == ::Bundler::Source::Rubygems
                          if spec.source.remotes.size == 1
                            spec.source.remotes.first.to_s
                          elsif spec.source.remotes.size >= 1
                            # remotes =
                            #     if Gems.info(spec.name) == RubyGemsOrg::GEM_NOT_FOUND
                            #       spec.source.remotes.reject {|uri|
                            #         uri.to_s == RubyGemsOrg::URI
                            #       }
                            #     else
                            #       spec.source.remotes
                            #     end
                            # TODO: support http://www.gemfury.com, aka multi `source` DSL; requre 'rubygems'?
                            spec.source.remotes.map {|r|
                              r.to_s
                            }.join(',')
                          end
                        when spec.source.class == ::Bundler::Source::Path::Installer
                          # Untested
                          spec.full_gem_path
                        else
                          raise('Yo, this error should ever not occur!')
                      end
                  {
                      name: spec.name,
                      version: spec.version.to_s,
                      remote: remote
                  }
            }
      }
    }
  end
  # LicenseAuto.logger.debug(JSON.pretty_generate(dep_files))
end