Skip to content

Commit

Permalink
Merge pull request #65 from hmiyado/about-libraries-json
Browse files Browse the repository at this point in the history
  • Loading branch information
k-tomoyasu authored Oct 1, 2022
2 parents a78f1f5 + ff0f0bb commit 143be27
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 5 deletions.
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,64 @@
# react-native-oss-license

[![npm badge](https://badge.fury.io/js/react-native-oss-license.svg)](https://www.npmjs.com/package/react-native-oss-license)
`react-native-oss-license` is **license list generator for React Native App(iOS & Android)**.
It generates license lists of npm libraries for iOS, Android.
This CLI tool allow you to easily generate content of oss-license.

## Installation

`npm i -g react-native-oss-license`

## [Sample App](https://github.com/k-tomoyasu/react-native-oss-license/tree/master/sample/)
## [Sample App](https://github.com/k-tomoyasu/react-native-oss-license/tree/master/sample/)

## Usage

### iOS
Recommended to use with [LicensePlist](https://github.com/mono0926/LicensePlist) that scan cocoaopds, carthage.

Recommended to use with [LicensePlist](https://github.com/mono0926/LicensePlist) that scan cocoaopds, carthage.

#### [LicensePlist](https://github.com/mono0926/LicensePlist)

`react-native-oss-license` generate `plist` that you can locate to `Settings.bundle`.
Run `react-native-oss-license --format settings-bundle` when your are in the directory that contains `package.json`
You can merge output `react-native-oss-license` and `LicensePlist`.

### Android

It is assumed to be used with other tools.

#### LicenseToolsPlugin

[License Tools Plugin for Android](https://github.com/cookpad/LicenseToolsPlugin) is Gradle plugin to check library licenses and generate license pages.
Run `react-native-oss-license --format license-tools-plugin`.
It generate license list in YAML format.
`react-native-oss-license` generate same format content. You can merge results.

#### AboutLibraries
#### AboutLibraries(under v8.9.4)

[AboutLibraries](https://github.com/mikepenz/AboutLibraries) provides fragment/activity that show license list.
`react-native-oss-license` generate string resource xml `AboutLibraries` use.
Run `react-native-oss-license --format about-libraries`, output strings.xml that you can put into `res/values/`.
and output stdout `withLibraries("package_name_A", "package_name_B" ...)` that pass to method `withLibraries`.

#### AboutLibraries(over v10.0.0)

[AboutLibraries](https://github.com/mikepenz/AboutLibraries) provides Jetpack Compose that show license list.
`react-native-oss-license` generate JSON files `AboutLibraries` use.
Run `react-native-oss-license --format about-libraries-json`, output .json that you can put into `config`.
`config/libraries` contains libraries json files.
`config/licenses` contains licenses json files.
You can specify any other directory instead of `config` with `--output-path` option.

### CLI

```sh
> cd {project-root}
> react-native-oss-license --help
Usage: react-native-oss-license [options]

Options:
-f, --format <format> output format. options:[settings-bundle,license-tools-plugin,about-libraries]
-f, --format <format> output format. options:[settings-bundle,license-tools-plugin,about-libraries, about-libraries-json]
--dev include devDependencies (default: false)
--depth <depth> dependencies depth (default: null)
--output-path <outputPath> specify path where output file
Expand All @@ -58,17 +76,23 @@ output settings-bundle format to 'ios/com.k-tomoyasu.react-native-oss-license.Ou
```

## screen-shots

### iOS

![settings-bundle-list](screenshots/settings-bundle-list.png)
![settings-bundle-detail](screenshots/settings-bundle-detail.png)

### Android

#### license-tools-plugin

![license-tools-plugin](screenshots/license-tools-plugin.png)

#### AboutLibraries

![about-libraries](screenshots/about-libraries.png)

## Acknowledgment

This is based on [dart-oss-licenses](https://github.com/ko2ic/dart_oss_licenses) consepts.
And referred [license-list](https://github.com/yami-beta/license-list).
3 changes: 3 additions & 0 deletions src/formatter/FormatterFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import LicenseToolsPlugin from './android/LicenseToolsPlugin'
import Format from '../models/Format'
import AboutLibraries from './android/AboutLibraries'
import FileWriter from '../writer/FileWriter'
import AboutLibrariesJson from './android/AboutLibrariesJson'

export default class FormatterFactory {
static create(opt: CmdOption): Formatter {
Expand All @@ -21,6 +22,8 @@ export default class FormatterFactory {
return new LicenseToolsPlugin(opt, writer)
case Format.AboutLibraries:
return new AboutLibraries(opt, writer)
case Format.AboutLibrariesJson:
return new AboutLibrariesJson(opt, writer)
default: {
throw new Error(
`invalid format [${
Expand Down
94 changes: 94 additions & 0 deletions src/formatter/android/AboutLibrariesJson.ts
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}`
}
}
}
3 changes: 2 additions & 1 deletion src/models/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
enum Format {
SettingsBunddle = 'settings-bundle',
LicenseToolsPlugin = 'license-tools-plugin',
AboutLibraries = 'about-libraries'
AboutLibraries = 'about-libraries',
AboutLibrariesJson = 'about-libraries-json'
}

namespace Format {
Expand Down
1 change: 1 addition & 0 deletions src/models/FormatterOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type AboutLibrariesOption = {
*/
usesPlugin: boolean
} & FormatterOption
type AboutLibrariesJsonOption = {} & FormatterOption
type LicenseToolsPluginOption = {} & FormatterOption

0 comments on commit 143be27

Please sign in to comment.