Skip to content

Commit

Permalink
Merge pull request #18 from icapps/feature/#13-custom-files
Browse files Browse the repository at this point in the history
#13 & #3: Added support for custom licenses import via file or http o…
  • Loading branch information
vanlooverenkoen authored Mar 26, 2021
2 parents e82a118 + d055d4c commit bd3b748
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 44 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Changelog

## [1.2.0] - 2021-03-26
## [2.0.0] - 2021-03-26
### Breaking
-Version & LicenseUrl can return null from now on
### Added
-Support for importing extra dependencies that are not included in the pubspec. (Android or iOS specific code for example)
-Nullsafe flag again
-Stric mode codebase
### Fixed
-Example android v2 embedding
-Example android x migration

## [1.1.1] - 2021-03-07
### Fixed
Expand Down
21 changes: 11 additions & 10 deletions bin/icapps_license.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';
import 'package:path/path.dart';

import 'src/model/dto/dependency.dart';
import 'src/extension/string_builder_extension.dart';
import 'src/params.dart';

const baseUrl = 'https://pub.dev/api/packages/';
Expand Down Expand Up @@ -39,19 +40,19 @@ Future<void> main(List<String> args) async {
..writeln()
..writeln('class License {')
..writeln(' final String name;')
..writeln(' final String version;')
..writeln(' final String$nullableFieldInfix url;')
..writeln(' final String licenseUrl;')
..writeln(' final String license;')
..writeln(' final String$nullableFieldInfix version;')
..writeln(' final String$nullableFieldInfix url;')
..writeln(' final String$nullableFieldInfix licenseUrl;')
..writeln()
..writeln(' License({');
if (params.nullSafe) {
sb
..writeln(' required this.name,')
..writeln(' required this.version,')
..writeln(' required this.licenseUrl,')
..writeln(' required this.license,')
..writeln(' required this.url,');
..writeln(' this.version,')
..writeln(' this.licenseUrl,')
..writeln(' this.url,');
} else {
sb
..writeln(' this.name,')
Expand Down Expand Up @@ -97,10 +98,10 @@ Future<void> main(List<String> args) async {
String _getDependencyText(Dependency dependency) {
final sb = StringBuffer()
..writeln(' ..add(License(')
..writeln(' name: \'${dependency.name}\',')
..writeln(' version: \'${dependency.version}\',')
..writeln(' url: \'${dependency.url}\',')
..writeln(' licenseUrl: \'${dependency.licenseUrl}\',')
..writelnWithQuotesOrNull('name', dependency.name)
..writelnWithQuotesOrNull('version', dependency.version)
..writelnWithQuotesOrNull('url', dependency.url)
..writelnWithQuotesOrNull('licenseUrl', dependency.licenseUrl)
..writeln(' license: \'\'\'${dependency.license}\'\'\',')
..writeln(' ))');
return sb.toString();
Expand Down
9 changes: 9 additions & 0 deletions bin/src/extension/string_builder_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extension StringBuilderExtension on StringBuffer {
void writelnWithQuotesOrNull(String key, String? value) {
if (value == null) {
writeln(' $key: null,');
} else {
writeln(' $key: \'$value\',');
}
}
}
10 changes: 5 additions & 5 deletions bin/src/model/dto/dependency.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class Dependency {
final String name;
final String version;
final String license;
final String licenseUrl;
final String? version;
final String? licenseUrl;
final String? url;

Dependency({
required this.name,
required this.version,
required this.license,
required this.licenseUrl,
required this.url,
this.version,
this.licenseUrl,
this.url,
});
}
100 changes: 92 additions & 8 deletions bin/src/params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const licensePathShort = '/LICENSE';
class Params {
static const yamlConfigLicense = 'icapps_license';
static const yamlConfigLicensesList = 'licenses';
static const yamlConfigExtraLicensesList = 'extra_licenses';
static const yamlConfigExtraLicenseName = 'name';
static const yamlConfigExtraLicenseVersion = 'version';
static const yamlConfigExtraLicenseUrl = 'url';
static const yamlConfigExtraLicenseLicenseUrl = 'license';
static const yamlConfigFailFast = 'failFast';
static const yamlConfigNullSafety = 'nullsafety';

Expand Down Expand Up @@ -71,7 +76,23 @@ class Params {
devDependenciesYamlList[key] as Object; // ignore: avoid_as
final dependency = await _getDependency(
stringKey, value, _getOverrideLicenseUrl(icappsLicenseConfig, key));
if (dependency != null) {
devDependencies.add(dependency);
}
}
}

if (icappsLicenseConfig == null) return;
final extraDependenciesYamlList =
icappsLicenseConfig[yamlConfigExtraLicensesList]
as YamlMap?; // ignore: avoid_as
if (extraDependenciesYamlList != null &&
extraDependenciesYamlList.isNotEmpty) {
for (final key in extraDependenciesYamlList.keys) {
final stringKey = key as String; // ignore: avoid_as
final value =
extraDependenciesYamlList[key] as Object; // ignore: avoid_as
final dependency = await _getExtraDependency(stringKey, value);
if (dependency != null) {
devDependencies.add(dependency);
}
Expand All @@ -96,7 +117,7 @@ class Params {
return null;
} else {
missingLicensesList.add(
'$name should define a static license or url in the pubspec.yaml (https://pub.dev/packages/$name)');
'The license for $name could not be fetched automaticly. $name should define a static license or url in the pubspec.yaml (https://pub.dev/packages/$name)');
missingLicenses = true;
print('----');
}
Expand Down Expand Up @@ -140,25 +161,85 @@ class Params {
print('Overriding $name license url with: $overrideLicense');
licenseUrl = overrideLicense;
}
print('LicenseUrl: $licenseUrl');
final licenseResult = await http.get(Uri.parse(licenseUrl));
if (licenseResult.statusCode != HttpStatus.ok) {
final license = await _getLicense(name, licenseUrl);
if (license == null) return null;

print('----');
return Dependency(
name: name,
version: version,
licenseUrl: isNetworkUrl(licenseUrl) ? licenseUrl : null,
license: license,
url: package.pubspec.homepage,
);
}

Future<Dependency?> _getExtraDependency(
String pubspecName, Object value) async {
if (value is! YamlMap) {
missingLicensesList.add(
'$name should define a static license or url in the pubspec.yaml (https://pub.dev/packages/$name)');
'Extra dependency: $pubspecName should define under extra_dependencies. Everything should be configured here');
missingLicenses = true;
print('----');
return null;
}
final name =
value[yamlConfigExtraLicenseName] as String?; // ignore: avoid_as
final version =
value[yamlConfigExtraLicenseVersion] as String?; // ignore: avoid_as
final url = value[yamlConfigExtraLicenseUrl] as String?; // ignore: avoid_as
final licenseUrl =
value[yamlConfigExtraLicenseLicenseUrl] as String?; // ignore: avoid_as
if (name == null) {
missingLicensesList.add(
'Extra dependency: $pubspecName should define under extra_dependencies. Everything should be configured here. `name` is missing');
missingLicenses = true;
print('----');
return null;
}
if (licenseUrl == null) {
missingLicensesList.add(
'Extra dependency: $pubspecName should define under extra_dependencies. Everything should be configured here. `license` is missing');
missingLicenses = true;
print('----');
return null;
}
final license = licenseResult.body;
final license = await _getLicense(pubspecName, licenseUrl);
if (license == null) return null;

print('----');
return Dependency(
name: name,
version: version,
licenseUrl: licenseUrl,
licenseUrl: isNetworkUrl(licenseUrl) ? licenseUrl : null,
license: license,
url: package.pubspec.homepage,
url: url,
);
}

Future<String?> _getLicense(String name, String licenseUrl) async {
print('LicenseUrl: $licenseUrl');
if (isNetworkUrl(licenseUrl)) {
final licenseResult = await http.get(Uri.parse(licenseUrl));
if (licenseResult.statusCode != HttpStatus.ok) {
missingLicensesList.add(
'$name should define a static license or url in the pubspec.yaml (https://pub.dev/packages/$name)');
missingLicenses = true;
print('----');
return null;
}
return licenseResult.body;
}
final file = File(licenseUrl);
if (!file.existsSync()) {
missingLicensesList.add(
'File: $file does not exists. $name should define a static license or url in the pubspec.yaml (https://pub.dev/packages/$name)');
missingLicenses = true;
print('----');
return null;
}
return file.readAsStringSync();
}
}

String? _getLicenseUrl(String? url) {
Expand All @@ -176,3 +257,6 @@ String? _getLicenseUrl(String? url) {
}
return null;
}

bool isNetworkUrl(String licenseUrl) =>
licenseUrl.startsWith('http://') || licenseUrl.startsWith('https://');
64 changes: 48 additions & 16 deletions example/lib/util/license.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

class License {
final String name;
final String license;
final String version;
final String url;
final String licenseUrl;
final String license;

License({
this.name,
this.version,
this.licenseUrl,
this.license,
this.url,
this.name,
this.version,
this.licenseUrl,
this.license,
this.url,
});
}

Expand All @@ -26,9 +26,8 @@ class LicenseUtil {
..add(License(
name: 'provider',
version: '^5.0.0',
url: 'null',
licenseUrl:
'https://raw.githubusercontent.com/rrousselGit/provider/master/LICENSE',
url: null,
licenseUrl: 'https://raw.githubusercontent.com/rrousselGit/provider/master/LICENSE',
license: '''MIT License
Copyright (c) 2019 Remi Rousselet
Expand All @@ -53,11 +52,9 @@ SOFTWARE.''',
))
..add(License(
name: 'shared_preferences',
version: '^2.0.3',
url:
'https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences',
licenseUrl:
'https://raw.githubusercontent.com/flutter/plugins/master/packages/shared_preferences/shared_preferences/LICENSE',
version: '^2.0.5',
url: 'https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences',
licenseUrl: 'https://raw.githubusercontent.com/flutter/plugins/master/packages/shared_preferences/shared_preferences/LICENSE',
license: '''Copyright 2013 The Flutter Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
Expand All @@ -84,6 +81,41 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
''',
));
}
))
..add(License(
name: 'Something Something',
version: '2.0.0',
url: 'https://www.google.com',
licenseUrl: null,
license: '''This is a test license''',
))
..add(License(
name: 'icapps_license',
version: null,
url: null,
licenseUrl: 'https://raw.githubusercontent.com/icapps/flutter-icapps-license/master/LICENSE',
license: '''MIT License
Copyright (c) 2019 icapps
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
''',
))
; }
}
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ packages:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
version: "2.0.5"
shared_preferences_linux:
dependency: transitive
description:
Expand Down
16 changes: 13 additions & 3 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
flutter_localizations:
sdk: flutter
provider: ^5.0.0
shared_preferences: ^2.0.3
shared_preferences: ^2.0.5

dev_dependencies:
flutter_test:
Expand All @@ -24,7 +24,17 @@ flutter:
- assets/locale/

icapps_license:
# failFast: true
# failFast: true
nullsafety: false
licenses:
icapps_license: 'https://raw.githubusercontent.com/icapps/flutter-icapps-license/master/LICENSE'
#Path override is not yet supported -> https://github.com/icapps/flutter-icapps-license/issues/17
icapps_license: https://raw.githubusercontent.com/icapps/flutter-icapps-license/master/LICENSE
extra_licenses:
something_something:
name: Something Something
version: 2.0.0
url: https://www.google.com
license: test_license
icapps_license:
name: icapps_license
license: https://raw.githubusercontent.com/icapps/flutter-icapps-license/master/LICENSE
1 change: 1 addition & 0 deletions example/test_license
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test license

0 comments on commit bd3b748

Please sign in to comment.