Class: Licensed::Command::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/licensed/command/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Cache

Returns a new instance of Cache.



6
7
8
# File 'lib/licensed/command/cache.rb', line 6

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/licensed/command/cache.rb', line 4

def config
  @config
end

Instance Method Details

#dependencies(source) ⇒ Object



50
51
52
# File 'lib/licensed/command/cache.rb', line 50

def dependencies(source)
  source.dependencies.select { |d| !@config.ignored?(d) }
end

#run(force: false) ⇒ Object



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
# File 'lib/licensed/command/cache.rb', line 10

def run(force: false)
  @config.sources.each do |source|
    @config.ui.info "Caching licenses for #{source.type} dependencies:"

    dependencies(source).each do |dependency|
      filename = @config.path.join("#{source.type}/#{dependency["name"]}.txt")

      if File.exist?(filename)
        license = Licensed::License.read(filename)

        # Version did not change, no need to re-cache
        if !force && dependency["version"] == license["version"]
          @config.ui.info "  Using #{dependency["name"]} (#{dependency["version"]})"
          next
        end
      end

      @config.ui.info "  Caching #{dependency["name"]} (#{dependency["version"]})"

      dependency.detect_license!
      dependency.save(filename)
    end

    # Clean up dependencies that have been removed
    names = dependencies(source).map { |d| d["name"] }

    license_source_path = @config.path.join(source.type)
    Dir.glob(license_source_path.join("**/*.txt")).each do |file|
      file_path = Pathname.new(file)
      relative_path = file_path.relative_path_from(license_source_path).to_s
      FileUtils.rm(file) unless names.include?(relative_path.chomp(".txt"))
    end
  end

  @config.ui.confirm "License caching complete!"
  @config.sources.each do |source|
    @config.ui.confirm "* #{source.type} dependencies: #{dependencies(source).size}"
  end
end

#success?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/licensed/command/cache.rb', line 54

def success?
  true
end