-
Notifications
You must be signed in to change notification settings - Fork 383
[cronet_http] ✨ Add Cronet embedded tool #853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
brianquinlan
merged 9 commits into
dart-lang:master
from
AlexV525:add-cronet-embedded-tool
Feb 17, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c08d02e
✨ Add the preparation tool
AlexV525 cc61e44
🚚 Move scripts
AlexV525 09cbf7f
🔥 Remove useless groups
AlexV525 332ac10
🚀 Use `yaml_edit` to update pubspec
AlexV525 7181f17
🔊 Log when patching the implementation version
AlexV525 591d6ca
✨ Add README replacement
AlexV525 2f2f917
📝 Improve the README
AlexV525 a121848
🎨 Improve codes
AlexV525 ada3209
🎨 Address review
AlexV525 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,12 @@ | ||
An Android Flutter plugin that provides access to the | ||
[Cronet](https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/package-summary) | ||
HTTP client. | ||
|
||
This package is identical to [`package:cronet_http`](https://pub.dev/packages/cronet_http) | ||
except that it embeds | ||
[Cronet](https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/package-summary) | ||
rather than using the version included with | ||
[Google Play Services](https://developers.google.com/android/guides/overview). | ||
This increases the uncompressed size of the application by approximately 8MB. | ||
|
||
See more details about cronet_http at: https://pub.dev/packages/cronet_http. |
This file contains hidden or 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 hidden or 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,104 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
AlexV525 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// The cronet_http directory is used to produce two packages: | ||
AlexV525 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// - `cronet_http`, which uses the Google Play Services version of Cronet. | ||
/// - `cronet_http_embedded`, which embeds Cronet. | ||
/// | ||
/// The default configuration of this code is to use the | ||
/// Google Play Services version of Cronet. | ||
/// | ||
/// The script transforms the configuration into one that embeds Cronet by: | ||
/// 1. Modifying the Gradle build file to reference the embedded Cronet. | ||
/// 2. Modifying the *name* and *description* in `pubspec.yaml`. | ||
/// 3. Replacing `README.md` with `README_EMBEDDED.md`. | ||
/// | ||
/// After running this script, `flutter pub publish` | ||
/// can be run to update package:cronet_http_embedded. | ||
/// | ||
/// NOTE: This script modifies the above files in place. | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:http/http.dart' as http; | ||
import 'package:xml/xml.dart'; | ||
import 'package:yaml_edit/yaml_edit.dart'; | ||
|
||
late final Directory _packageDirectory; | ||
|
||
const _gmsDependencyName = 'com.google.android.gms:play-services-cronet'; | ||
const _embeddedDependencyName = 'org.chromium.net:cronet-embedded'; | ||
const _packageName = 'cronet_http_embedded'; | ||
const _packageDescription = 'An Android Flutter plugin that ' | ||
'provides access to the Cronet HTTP client. ' | ||
'Identical to package:cronet_http except that it embeds Cronet ' | ||
'rather than relying on Google Play Services.'; | ||
final _cronetVersionUri = Uri.https( | ||
'dl.google.com', | ||
'android/maven2/org/chromium/net/group-index.xml', | ||
); | ||
|
||
void main() async { | ||
if (Directory.current.path.endsWith('tool')) { | ||
_packageDirectory = Directory.current.parent; | ||
} else { | ||
_packageDirectory = Directory.current; | ||
} | ||
|
||
final latestVersion = await _getLatestCronetVersion(); | ||
updateCronetDependency(latestVersion); | ||
updatePubSpec(); | ||
updateReadme(); | ||
} | ||
|
||
Future<String> _getLatestCronetVersion() async { | ||
final response = await http.get(_cronetVersionUri); | ||
final parsedXml = XmlDocument.parse(response.body); | ||
final embeddedNode = parsedXml.children | ||
.singleWhere((e) => e is XmlElement) | ||
.children | ||
.singleWhere((e) => e is XmlElement && e.name.local == 'cronet-embedded'); | ||
final stableVersionReg = RegExp(r'^\d+.\d+.\d+$'); | ||
final versions = embeddedNode.attributes | ||
.singleWhere((e) => e.name.local == 'versions') | ||
.value | ||
.split(',') | ||
.where((e) => stableVersionReg.stringMatch(e) == e); | ||
return versions.last; | ||
} | ||
|
||
/// Update android/build.gradle | ||
void updateCronetDependency(String latestVersion) { | ||
final fBuildGradle = File('${_packageDirectory.path}/android/build.gradle'); | ||
final gradleContent = fBuildGradle.readAsStringSync(); | ||
final implementationRegExp = RegExp( | ||
'^\\s*implementation [\'"]' | ||
'$_gmsDependencyName' | ||
':\\d+.\\d+.\\d+[\'"]', | ||
multiLine: true, | ||
); | ||
final newImplementation = '$_embeddedDependencyName:$latestVersion'; | ||
print('Patching $newImplementation'); | ||
AlexV525 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final newGradleContent = gradleContent.replaceAll( | ||
implementationRegExp, | ||
' implementation $newImplementation', | ||
); | ||
fBuildGradle.writeAsStringSync(newGradleContent); | ||
} | ||
|
||
/// Update pubspec.yaml | ||
void updatePubSpec() { | ||
final fPubspec = File('${_packageDirectory.path}/pubspec.yaml'); | ||
final yamlEditor = YamlEditor(fPubspec.readAsStringSync()) | ||
..update(['name'], _packageName) | ||
..update(['description'], _packageDescription); | ||
fPubspec.writeAsStringSync(yamlEditor.toString()); | ||
} | ||
|
||
/// Move README_EMBEDDED.md to replace README.md | ||
void updateReadme() { | ||
File('${_packageDirectory.path}/README.md').deleteSync(); | ||
File('${_packageDirectory.path}/README_EMBEDDED.md') | ||
.renameSync('${_packageDirectory.path}/README.md'); | ||
} |
1 change: 1 addition & 0 deletions
1
pkgs/cronet_http/run_pigeon.sh → pkgs/cronet_http/tool/run_pigeon.sh
100755 → 100644
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got freaked out. It's only a dev_dependency. 😌