-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from hmiyado/about-libraries-json
- Loading branch information
Showing
5 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import he from 'he' | ||
import crypto from 'crypto' | ||
|
||
import Formatter from '../Formatter' | ||
import License from '../../models/License' | ||
|
||
export default class AboutLibrariesJson implements Formatter { | ||
constructor(private opt: AboutLibrariesJsonOption, private writer: Writer) {} | ||
|
||
output(licenses: License[]): void { | ||
const configPath = this.opt.outputPath || 'android/config' | ||
const licensesPath = `${configPath}/licenses` | ||
const librariesPath = `${configPath}/libraries` | ||
|
||
const licenseIdentifiers: string[] = [] | ||
licenses.forEach(license => { | ||
const authorName = license.authorName | ||
const name = license.libraryName | ||
const version = license.version | ||
const description = he.encode(license.description) | ||
const licenseName = license.license ? license.license : '' | ||
const libraryUniqueId = this.getLibraryUniqueId(license) | ||
const licenseUniqueId = this.getLicenseUniqueId(license) | ||
|
||
const libraryJson = { | ||
uniqueId: libraryUniqueId, | ||
developers: [ | ||
{ | ||
name: authorName, | ||
organisationUrl: '' | ||
} | ||
], | ||
artifactVersion: version, | ||
description, | ||
name, | ||
tag: '', | ||
licenses: [licenseUniqueId] | ||
} | ||
|
||
const libraryJsonPath = `${librariesPath}/${libraryUniqueId}.json` | ||
this.writer | ||
.write(libraryJsonPath, JSON.stringify(libraryJson)) | ||
.then(() => | ||
console.log(`output about-libraries format to '${libraryJsonPath}'.`) | ||
) | ||
.catch(e => console.error(e)) | ||
|
||
if ( | ||
licenseUniqueId != '' && | ||
!licenseIdentifiers.includes(licenseUniqueId) | ||
) { | ||
licenseIdentifiers.push(licenseUniqueId) | ||
const content = license.licenseContent | ||
const licenseJson = { | ||
content, | ||
hash: licenseUniqueId, | ||
url: '', | ||
name: licenseName | ||
} | ||
|
||
const licenseJsonPath = `${licensesPath}/${licenseUniqueId}.json` | ||
this.writer | ||
.write(licenseJsonPath, JSON.stringify(licenseJson)) | ||
.then(() => | ||
console.log( | ||
`output about-libraries format to '${licenseJsonPath}'.` | ||
) | ||
) | ||
.catch(e => console.error(e)) | ||
} | ||
}) | ||
} | ||
|
||
private getLibraryUniqueId(license: License): string { | ||
return license.libraryName.replace(' ', '_').replace('/', '_') | ||
} | ||
|
||
private getLicenseUniqueId(license: License): string { | ||
if (license.license == null) { | ||
return '' | ||
} | ||
const prefix = license.license.replace(' ', '_') | ||
if (license.licenseContent == '') { | ||
return prefix | ||
} else { | ||
const licenseContentHash = crypto | ||
.createHash('sha256') | ||
.update(Buffer.from(license.licenseContent)) | ||
.digest('hex') | ||
// same license and licenseContent is considered identical | ||
return `${prefix}_${licenseContentHash}` | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters