diff --git a/build/lib/licensing.js b/build/lib/licensing.js index e0a3f313d316..66a4d71e8ac1 100644 --- a/build/lib/licensing.js +++ b/build/lib/licensing.js @@ -130,10 +130,56 @@ 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 getValidatedComponentField = (component, fieldName) => { + const fieldValue = component[fieldName] + if (!fieldValue) { + console.error('Missing' + fieldName + ' for background image ' + component['name']) + process.exit(1) + } + return fieldValue +} + +const generateNtpBackgroundsLicense = (preamble, components) => { + let componentNotices = '' + + for (let component of components) { + if (componentNotices.length != 0) { + componentNotices += '\n' + } + + const fileName = getValidatedComponentField(component, 'source') + const authorName = getValidatedComponentField(component, 'author') + const authorLink = getValidatedComponentField(component, 'link') + const originalUrl = getValidatedComponentField(component, 'originalUrl') + const license = getValidatedComponentField(component, 'license') + 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 +200,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') } }