From 5f450bc68a6ce3b87f00faec6391e00f1deef0b5 Mon Sep 17 00:00:00 2001 From: Francois Marier Date: Mon, 3 Feb 2020 07:24:48 -0800 Subject: [PATCH] Auto-generate NTP backgrounds LICENSE file (fixes brave/brave-browser#7460) --- lib/licensing.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/lib/licensing.js b/lib/licensing.js index e0a3f313d3166..d7b206981e918 100644 --- a/lib/licensing.js +++ b/lib/licensing.js @@ -130,10 +130,67 @@ const listSubComponents = (baseDir) => { return subComponents } +const listNtpBackgrounds = (metadataFile) => { + const metadata = fs.readFileSync(metadataFile).toString() + let images = [] + // Hack to import this TypeScript file in Node: + eval(metadata.replace('export const images: NewTab.Image[]', 'images')) + return images +} + +const generateNtpBackgroundsLicense = (preamble, components) => { + let componentNotices = '' + + for (let component of components) { + if (componentNotices.length != 0) { + componentNotices += '\n' + } + + const fileName = component['source'] + if (!fileName) { + console.error('Missing source for background image ' + component['name']) + process.exit(1) + } + const authorName = component['author'] + if (!authorName) { + console.error('Missing author for background image ' + component['name']) + process.exit(1) + } + const authorLink = component['link'] + if (!authorLink) { + console.error('Missing link for background image ' + component['name']) + process.exit(1) + } + const originalUrl = component['originalUrl'] + if (!originalUrl) { + console.error('Missing originalUrl for background image ' + component['name']) + process.exit(1) + } + const license = component['license'] + if (!license) { + console.error('Missing license for background image ' + component['name']) + process.exit(1) + } + if ((license != 'used with permission') && + (license.substr(0, 8) != 'https://') && (license.substr(0, 7) != 'http://')) { + console.error('Invalid license for background image ' + component['name'] + '. It needs to be a URL or the string "used with permission".') + process.exit(1) + } + + componentNotices += 'File: ' + fileName + '\n' + + 'Author: ' + authorName + ' (' + authorLink + ')\n' + + 'URL: ' + originalUrl + '\n' + + 'License: ' + license + '\n' + } + + return preamble + '\n\n' + componentNotices +} + const licensing = { updateLicenses: () => { console.log('regenerating LICENSE files...') - const braveComponentsThirdPartyDir = path.join(config.projects['brave-core'].dir, 'components', 'third_party') + const braveComponentsDir = path.join(config.projects['brave-core'].dir, 'components') + const braveComponentsThirdPartyDir = path.join(braveComponentsDir, 'third_party') // Brave Ad Block component const braveAdBlockDir = path.join(braveComponentsThirdPartyDir, 'adblock') @@ -154,6 +211,13 @@ const licensing = { let localDataLicense = generateExternalComponentLicenseFile(localDataPreamble, localDataComponents) fs.writeFileSync(path.join(braveComponentsThirdPartyDir, 'local_data', 'LICENSE'), localDataLicense) console.log(' - ' + localDataComponents.length + ' sub-components added in local_data/LICENSE') + + const braveNtpDataDir = path.join(braveComponentsDir, 'brave_new_tab_ui', 'data') + let ntpBackgroundsPreamble = 'These licenses do not apply to any of the code shipped with the Brave Browser and instead apply to background images used on the new tab page. The Brave Browser and such data files are separate and independent works.' + const ntpBackgrounds = listNtpBackgrounds(path.join(braveNtpDataDir, 'backgrounds.ts')) + let ntpBackgroundsLicense = generateNtpBackgroundsLicense(ntpBackgroundsPreamble, ntpBackgrounds) + fs.writeFileSync(path.join(braveNtpDataDir, 'LICENSE'), ntpBackgroundsLicense) + console.log(' - ' + ntpBackgrounds.length + ' sub-components added in brave_new_tab_ui/data/LICENSE') } }