Class: Licensed::Migrations::V2

Inherits:
Object
  • Object
show all
Defined in:
lib/licensed/migrations/v2.rb

Constant Summary collapse

YAML_FRONTMATTER_PATTERN =
/\A---\s*\n(.*?\n?)^---\s*$\n?(.*)\z/m
TEXT_SEPARATOR =
("-" * 80).freeze
LICENSE_SEPARATOR =
("*" * 80).freeze

Class Method Summary collapse

Class Method Details

.migrate(config_path, shell = Licensed::UI::Shell.new) ⇒ 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
# File 'lib/licensed/migrations/v2.rb', line 11

def self.migrate(config_path, shell = Licensed::UI::Shell.new)
  shell.info "updating to v2"

  shell.info "updating bundler configuration keys"
  # replace all "rubygem" and "rubygems" configuration keys with "bundler"
  # to account for the bundler source's `type` change from `rubygem` to `bundler`
  File.write(config_path, File.read(config_path).gsub(/("?)rubygems?("?):/, "\\1bundler\\2:"))

  shell.info "updating cached records"
  # load the configuration to find and update cached contents
  configuration = Licensed::Configuration.load_from(config_path)
  configuration.apps.each do |app|

    # move any bundler records from the `rubygem` folder to the `bundler` folder
    rubygem_cache = app.cache_path.join("rubygem")
    if rubygem_cache.exist?
      File.rename rubygem_cache, app.cache_path.join("bundler")
    end

    app.sources.each do |source|
      cache_path = app.cache_path.join(source.class.type)
      next unless File.exist?(cache_path)
      Dir.chdir cache_path do
        # licensed v1 cached records were stored as .txt files with YAML frontmatter
        Dir["**/*.txt"].each do |file|
          yaml, licenses, notices = parse_file(file)

          # rename the rubygem type to bundler
          yaml["type"] = "bundler" if yaml["type"] == "rubygem"

          # set licenses and notices as yaml properties
          yaml["licenses"] = licenses.map { |text| { "text" => text } }
          yaml["notices"] = notices.map { |text| { "text" => text } }

          # v2 records are stored in `.dep.yml` files
          # write the new yaml contents to the new file and delete old file
          new_file = file.gsub(".txt", ".dep.yml")
          File.write(new_file, yaml.to_yaml)
          File.delete(file)
        end
      end
    end
  end
end

.parse_file(filename) ⇒ Object

find the yaml and non-yaml data according to parsing logic from v1



57
58
59
60
61
62
63
64
# File 'lib/licensed/migrations/v2.rb', line 57

def self.parse_file(filename)
  match = File.read(filename).scrub.match(YAML_FRONTMATTER_PATTERN)
  yaml = YAML.load(match[1])
  # in v1, licenses and notices are separated by special text dividers
  licenses, *notices = match[2].split(TEXT_SEPARATOR).map(&:strip)
  licenses = licenses.split(LICENSE_SEPARATOR).map(&:strip)
  [yaml, licenses, notices]
end