Class: LicenseFinder::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/license_finder/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Configuration

Returns a new instance of Configuration.



42
43
44
45
46
47
48
49
50
# File 'lib/license_finder/configuration.rb', line 42

def initialize(config={})
  config = self.class.config_hash(config)

  @whitelist = config['whitelist'] || []
  @ignore_groups = (config["ignore_groups"] || [])
  @dependencies_dir = config['dependencies_file_dir'] || './doc/'
  @project_name = config['project_name'] || determine_project_name
  FileUtils.mkdir_p(@dependencies_dir)
end

Instance Attribute Details

#dependencies_dirObject

Returns the value of attribute dependencies_dir.



5
6
7
# File 'lib/license_finder/configuration.rb', line 5

def dependencies_dir
  @dependencies_dir
end

#ignore_groupsObject

Returns the value of attribute ignore_groups.



5
6
7
# File 'lib/license_finder/configuration.rb', line 5

def ignore_groups
  @ignore_groups
end

#project_nameObject

Returns the value of attribute project_name.



5
6
7
# File 'lib/license_finder/configuration.rb', line 5

def project_name
  @project_name
end

#whitelistObject

Returns the value of attribute whitelist.



5
6
7
# File 'lib/license_finder/configuration.rb', line 5

def whitelist
  @whitelist
end

Class Method Details

.config_file_pathObject



7
8
9
# File 'lib/license_finder/configuration.rb', line 7

def self.config_file_path
  File.join('.', 'config', 'license_finder.yml')
end

.config_hash(config) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/license_finder/configuration.rb', line 34

def self.config_hash(config)
  if File.exists?(config_file_path)
    yaml = File.read(config_file_path)
    config = YAML.load(yaml).merge config
  end
  config
end

.ensure_defaultObject



11
12
13
14
# File 'lib/license_finder/configuration.rb', line 11

def self.ensure_default
  make_config_file unless File.exists?(config_file_path)
  new
end

.make_config_fileObject



16
17
18
19
20
21
22
# File 'lib/license_finder/configuration.rb', line 16

def self.make_config_file
  FileUtils.mkdir_p(File.join('.', 'config'))
  FileUtils.cp(
    ROOT_PATH.join('..', 'files', 'license_finder.yml'),
    config_file_path
  )
end

.move!Object



24
25
26
27
28
29
30
31
32
# File 'lib/license_finder/configuration.rb', line 24

def self.move!
  config = config_hash('dependencies_file_dir' => './doc/')
  File.open(config_file_path, 'w') do |f|
    f.write YAML.dump(config)
  end

  FileUtils.mkdir_p("doc")
  FileUtils.mv(Dir["dependencies.*"], "doc")
end

Instance Method Details

#database_uriObject



52
53
54
# File 'lib/license_finder/configuration.rb', line 52

def database_uri
  URI.escape(File.expand_path(File.join(dependencies_dir, "dependencies.db")))
end

#dependencies_htmlObject



68
69
70
# File 'lib/license_finder/configuration.rb', line 68

def dependencies_html
  File.join(dependencies_dir, "dependencies.html")
end

#dependencies_legacy_textObject



64
65
66
# File 'lib/license_finder/configuration.rb', line 64

def dependencies_legacy_text
  File.join(dependencies_dir, "dependencies.txt")
end

#dependencies_textObject



60
61
62
# File 'lib/license_finder/configuration.rb', line 60

def dependencies_text
  File.join(dependencies_dir, "dependencies.csv")
end

#dependencies_yamlObject



56
57
58
# File 'lib/license_finder/configuration.rb', line 56

def dependencies_yaml
  File.join(dependencies_dir, "dependencies.yml")
end

#saveObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/license_finder/configuration.rb', line 77

def save
  File.open(Configuration.config_file_path, 'w') do |file|
    file.write({
      'whitelist' => @whitelist.uniq,
      'ignore_groups' => @ignore_groups.uniq,
      'dependencies_file_dir' => @dependencies_dir,
      'project_name' => @project_name
    }.to_yaml)
  end
end

#whitelisted?(license_name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/license_finder/configuration.rb', line 72

def whitelisted?(license_name)
  license = License.find_by_name(license_name) || license_name
  whitelisted_licenses.include? license
end