1
0
mirror of https://github.com/github/choosealicense.com synced 2024-12-22 12:50:24 +01:00
choosealicense.com/script/vendor-license-text
2017-11-20 10:21:45 -08:00

65 lines
2.4 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# frozen_string_literal: true
require 'jekyll'
require 'tmpdir'
tmp_dir = Dir.mktmpdir
git_cmd = "git clone --depth=1 'https://github.com/spdx/license-list-data.git' #{tmp_dir}"
system(git_cmd)
text_dir = File.join(tmp_dir, 'text')
site_path = File.expand_path('../', File.dirname(__FILE__))
config = Jekyll.configuration('source' => site_path)
site = Jekyll::Site.new(config)
site.read
site.collections['licenses'].docs.each do |license|
puts "Updating the #{license.data['title']}..."
text_file = File.join(text_dir, "#{license.data['spdx-id']}.txt")
spdx_text = File.read(text_file).gsub(/\s+\n/, "\n")
license_key = license.data['spdx-id'].downcase
if ['bsd-2-clause', 'bsd-3-clause'].include? license_key
spdx_text = spdx_text.gsub(/<year>/, '[year]')
spdx_text = spdx_text.gsub(/<owner>/, '[fullname]')
elsif license_key == 'bsd-3-clause-clear'
spdx_text = spdx_text.gsub(/\[xxxx\]-\[xxxx\]/, '[year]')
spdx_text = spdx_text.gsub(/\[Owner Organization\]/, '[fullname]')
elsif license_key == 'isc'
spdx_text = spdx_text.gsub(/Copyright.*Consortium/m, 'Copyright (c) [year], [fullname]')
elsif license_key == 'mit'
spdx_text = spdx_text.gsub(/<year>/, '[year]')
spdx_text = spdx_text.gsub(/<copyright holders>/, '[fullname]')
elsif license_key == 'ncsa'
spdx_text = spdx_text.gsub(/<Year>/, '[year]')
spdx_text = spdx_text.gsub(/<Owner Organization Name>/, '[fullname]')
spdx_text = spdx_text.gsub(/<Name of Development Group>/, '[fullname]')
spdx_text = spdx_text.gsub(/<Name of Institution>/, '[project]')
spdx_text = spdx_text.gsub(%r{<URL for Development Group/Institution>}, '[project_url]')
spdx_text = spdx_text.gsub(/<Name of Development Group, Name of Institution>/, '[fullname], [project]')
end
license_text = ''
spdx_text.lines.each do |line|
if line.length <= 78
license_text += line
else
indented_line = line.match(/\A(\s*)(.*)/)
line_indent = indented_line[1]
line_text = indented_line[2]
text_width = 78 - line_indent.length
license_text += line_text.gsub(/(.{1,#{text_width}})(\s+|\Z)/, "#{line_indent}\\1\n")
end
end
raw_content = File.read(license.path)
matches = raw_content.match Jekyll::Document::YAML_FRONT_MATTER_REGEXP
output = matches[1]
output << "---\n\n"
output << license_text.gsub(/\s+\n/, "\n")
File.write(license.path, output)
end