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.



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

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#run(force: false) ⇒ Object



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
71
72
73
74
75
# File 'lib/licensed/command/cache.rb', line 11

def run(force: false)
  summary = @config.apps.flat_map do |app|
    app_name = app["name"]
    @config.ui.info "Caching licenses for #{app_name}:"

    # load the app environment
    Dir.chdir app.source_path do

      # map each available app source to it's dependencies
      app.sources.map do |source|
        type = source.class.type

        @config.ui.info "  #{type} dependencies:"

        names = []
        cache_path = app.cache_path.join(type)

        # exclude ignored dependencies
        dependencies = source.dependencies.select { |d| !app.ignored?(d) }

        # ensure each dependency is cached
        dependencies.each do |dependency|
          name = dependency["name"]
          version = dependency["version"]

          names << name
          filename = cache_path.join("#{name}.txt")

          # try to load existing license from disk
          # or default to a blank license
          license = Licensed::License.read(filename) || Licensed::License.new

          # cached version string exists and did not change, no need to re-cache
          has_version = !license["version"].nil? && !license["version"].empty?
          if !force && has_version && version == license["version"]
            @config.ui.info "    Using #{name} (#{version})"
            next
          end

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

          dependency.detect_license!
          # use the cached license value if the license text wasn't updated
          dependency["license"] = license["license"] if dependency.license_text_match?(license)

          dependency.save(filename)
        end

        # Clean up cached files that dont match current dependencies
        Dir.glob(cache_path.join("**/*.txt")).each do |file|
          file_path = Pathname.new(file)
          relative_path = file_path.relative_path_from(cache_path).to_s
          FileUtils.rm(file) unless names.include?(relative_path.chomp(".txt"))
        end

        "* #{app_name} #{type} dependencies: #{dependencies.size}"
      end
    end
  end

  @config.ui.confirm "License caching complete!"
  summary.each do |message|
    @config.ui.confirm message
  end
end

#success?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/licensed/command/cache.rb', line 77

def success?
  true
end