1
0
mirror of https://github.com/github/choosealicense.com synced 2024-11-05 19:28:50 +01:00
choosealicense.com/spec/spec_helper.rb

144 lines
3.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-03-27 02:19:44 +02:00
2015-03-07 18:47:24 +01:00
require 'jekyll'
2015-03-07 19:38:52 +01:00
require 'json'
require 'licensee'
2016-01-15 23:40:32 +01:00
require 'open-uri'
2015-03-07 18:47:24 +01:00
2016-02-08 21:37:19 +01:00
module SpecHelper
class << self
attr_accessor :config, :licenses, :site, :spdx
attr_accessor :osi_approved_licenses, :fsf_approved_licenses, :od_approved_licenses
end
end
2015-03-07 18:47:24 +01:00
def config_file
2016-02-08 21:37:19 +01:00
File.expand_path './_config.yml', source
2015-03-07 18:47:24 +01:00
end
def source
2016-02-08 21:37:19 +01:00
File.expand_path('../', File.dirname(__FILE__))
2015-03-07 18:47:24 +01:00
end
2016-01-29 18:44:28 +01:00
def licenses_path
2016-02-08 21:37:19 +01:00
File.expand_path '_licenses', source
2016-01-29 18:44:28 +01:00
end
2015-03-07 18:47:24 +01:00
def config
2016-02-08 21:37:19 +01:00
SpecHelper.config ||= begin
config = Jekyll::Configuration.new.read_config_file config_file
config = Jekyll::Utils.deep_merge_hashes(config, source: source)
Jekyll::Utils.deep_merge_hashes(Jekyll::Configuration::DEFAULTS, config)
end
2015-03-07 18:47:24 +01:00
end
def licenses
2016-02-08 21:37:19 +01:00
SpecHelper.licenses ||= begin
site.collections['licenses'].docs.map do |license|
spdx_lcase = File.basename(license.basename, '.txt')
license.to_liquid.merge('spdx-lcase' => spdx_lcase)
2016-01-20 16:37:44 +01:00
end
2016-01-18 23:05:02 +01:00
end
end
def shown_licenses
2017-03-27 02:19:44 +02:00
licenses.reject { |l| l['hidden'] }
end
2015-03-07 18:47:24 +01:00
def site
2016-02-08 21:37:19 +01:00
SpecHelper.site ||= begin
2015-03-07 18:47:24 +01:00
site = Jekyll::Site.new(config)
site.reset
site.read
site
end
end
def rules
2016-02-08 21:37:19 +01:00
site.data['rules']
2015-03-07 18:47:24 +01:00
end
2015-08-22 21:25:16 +02:00
def fields
2016-02-08 21:37:19 +01:00
site.data['fields']
2015-08-22 21:25:16 +02:00
end
def meta
2016-02-08 21:37:19 +01:00
site.data['meta']
2015-08-22 21:25:16 +02:00
end
2015-03-07 18:47:24 +01:00
def rule?(tag, group)
2016-02-08 21:37:19 +01:00
rules[group].any? { |r| r['tag'] == tag }
2015-03-07 18:47:24 +01:00
end
2015-03-07 19:38:52 +01:00
def spdx_list
2017-12-29 23:30:08 +01:00
SpecHelper.spdx ||= begin
url = 'https://spdx.org/licenses/licenses.json'
list = JSON.parse(OpenURI.open_uri(url).read)['licenses']
2017-12-29 23:30:08 +01:00
list.each_with_object({}) do |values, memo|
memo[values['licenseId']] = values
end
end
2015-03-07 19:38:52 +01:00
end
2016-01-15 23:40:32 +01:00
def spdx_ids
spdx_list.map { |name, _properties| name }
2016-01-15 23:40:32 +01:00
end
2015-03-07 19:38:52 +01:00
def find_spdx(license)
spdx_list.find { |name, _properties| name.casecmp(license).zero? }
2015-03-07 19:38:52 +01:00
end
2016-01-15 23:40:32 +01:00
2016-01-16 00:01:07 +01:00
def osi_approved_licenses
2016-02-08 21:37:19 +01:00
SpecHelper.osi_approved_licenses ||= begin
2016-01-16 00:01:07 +01:00
licenses = {}
2017-12-29 23:30:08 +01:00
list = spdx_list.select { |_id, meta| meta['isOsiApproved'] }
2016-01-16 00:01:07 +01:00
list.each do |id, meta|
2016-02-08 21:37:19 +01:00
licenses[id.downcase] = meta['name']
2016-01-16 00:01:07 +01:00
end
licenses
end
2016-01-15 23:40:32 +01:00
end
def fsf_approved_licenses
2016-02-08 21:37:19 +01:00
SpecHelper.fsf_approved_licenses ||= begin
url = 'https://wking.github.io/fsf-api/licenses-full.json'
object = JSON.parse(OpenURI.open_uri(url).read)
2016-01-15 23:40:32 +01:00
licenses = {}
object['licenses'].each_value do |meta|
next unless (meta.include? 'identifiers') && (meta['identifiers'].include? 'spdx') && (meta.include? 'tags') && (meta['tags'].include? 'libre')
meta['identifiers']['spdx'].each do |identifier|
licenses[identifier.downcase] = meta['name']
end
end
2016-01-15 23:40:32 +01:00
licenses
end
end
def od_approved_licenses
2016-02-08 21:37:19 +01:00
SpecHelper.od_approved_licenses ||= begin
url = 'https://licenses.opendefinition.org/licenses/groups/od.json'
data = OpenURI.open_uri(url).read
2016-01-15 23:40:32 +01:00
data = JSON.parse(data)
licenses = {}
data.each do |id, meta|
2016-02-08 21:37:19 +01:00
licenses[id.downcase] = meta['title']
2016-01-15 23:40:32 +01:00
end
licenses
end
end
2016-01-16 00:01:07 +01:00
def approved_licenses
(osi_approved_licenses.keys + fsf_approved_licenses.keys + od_approved_licenses.keys).flatten.uniq.sort
2016-01-15 23:40:32 +01:00
end
module Licensee
class License
class << self
def license_dir
dir = ::File.dirname(__FILE__)
::File.expand_path '../_licenses', dir
end
end
end
end