Skip to content

Instantly share code, notes, and snippets.

@spidgorny
Created October 9, 2019 16:22
Show Gist options
  • Save spidgorny/ed475cbbe303e1c09c0f6c9f9f57dcad to your computer and use it in GitHub Desktop.
Save spidgorny/ed475cbbe303e1c09c0f6c9f9f57dcad to your computer and use it in GitHub Desktop.
import 'dart:convert';
import "dart:io";
import 'package:html/dom.dart';
import 'package:html/parser.dart' as parser;
import "package:http/http.dart" as http;
import 'package:http/http.dart';
import "package:yaml/yaml.dart";
main() async {
File file = new File('pubspec.yaml');
String yamlString = file.readAsStringSync();
Map yaml = loadYaml(yamlString);
YamlMap dependencies = yaml['dependencies'];
// print(dependencies);
for (var key in dependencies.keys) {
var version = dependencies[key];
if (version is String) {
var versionNumber = version.replaceFirst('^', '');
print(key + ': ' + versionNumber);
Response html = await http.get('https://pub.dev/packages/' + key);
Document document = parser.parse(html.body);
var jsonScript =
document.querySelector('script[type="application/ld+json"]');
var json = jsonDecode(jsonScript.innerHtml);
// print(json['version']);
if (json['version'].toString().compareTo(versionNumber) > 0) {
print(' => ' + json['version']);
}
}
}
}
It reads pubspec.yaml and check for the latest version of each dependency.
Example:
cupertino_icons: 0.1.2
location: 2.3.5
flutter_local_notifications: 0.8.4
http: 0.12.0+1
=> 0.12.0+2
haversine: 1.0.2
flutter_map: 0.1.4
=> 0.7.3
map_native: 0.0.11
url_launcher: 5.1.4
flutter_html_view: 0.5.12
flutter_html: 0.8.2
background_fetch: 0.2.0
=> 0.3.2
package_info: 0.4.0+2
=> 0.4.0+6
liquid_pull_to_refresh: 1.1.1
provider: 3.0.0
=> 3.1.0
yaml: any
google_maps_flutter: 0.5.21+7
@shlima
Copy link

shlima commented Mar 9, 2020

I've modified a little bit your script:

  1. you can specify a pubspec.yml file you want
  2. prints changelog URL
  3. checks dev_dependencies too

Just puts the script into project/bin/outdated.dart and run dart bin/outdated.dart pubspec.yml

/// @refs https://gist.github.com/spidgorny/ed475cbbe303e1c09c0f6c9f9f57dcad
import 'dart:convert';
import "dart:io";
import 'package:html/dom.dart';
import 'package:html/parser.dart' as parser;
import "package:http/http.dart" as http;
import 'package:http/http.dart';
import "package:yaml/yaml.dart";

main(List<String> arguments) async {
    exitCode = 0;
    String pubspec = arguments[0] ?? "pubspec.yml";

    final Map yaml = loadYaml(File(pubspec).readAsStringSync());
    final YamlMap dependencies = yaml["dependencies"];
    final YamlMap devDependencies = yaml["dev_dependencies"];

    await _check(dependencies);
    if (devDependencies != null) await _check(devDependencies);
}

_check(YamlMap tree) async {
   tree.forEach((name, version) async {
       final String verNum = version.toString().replaceFirst('^', '');
       String pubNum;

       try {
           pubNum = await _pubGet(name);
       } catch(e) {
           print("⚠️  Failed to check $name");
           return;
       }

       if (verNum == pubNum) return;

       print("$name $verNum => $pubNum (${_changeLogUrl(name)})");
   });
}

Future<String> _pubGet(String name) async {
    final Response html = await http.get("https://pub.dev/packages/$name");
    final Document document = parser.parse(html.body);
    final Element jsonScript = document.querySelector('script[type="application/ld+json"]');
    final dynamic json = jsonDecode(jsonScript.innerHtml);
    return json["version"].toString();
}

String _changeLogUrl(String name) {
    return "https://pub.dev/packages/$name#-changelog-tab-";
}

@shlima
Copy link

shlima commented Mar 9, 2020

Outputs:

cupertino_icons 0.1.2 => 0.1.3 (https://pub.dev/packages/cupertino_icons#-changelog-tab-)
simple_animations 1.3.8 => 1.3.9 (https://pub.dev/packages/simple_animations#-changelog-tab-)
percent_indicator 2.1.1 => 2.1.1+1 (https://pub.dev/packages/percent_indicator#-changelog-tab-)
flutter_secure_storage 3.3.1 => 3.3.1+1 (https://pub.dev/packages/flutter_secure_storage#-changelog-tab-)
shared_preferences 0.5.6 => 0.5.6+2 (https://pub.dev/packages/shared_preferences#-changelog-tab-)
crypto 2.1.3 => 2.1.4 (https://pub.dev/packages/crypto#-changelog-tab-)
flutter_i18n 0.10.0 => 0.10.1 (https://pub.dev/packages/flutter_i18n#-changelog-tab-)
pull_to_refresh 1.5.7 => 1.5.8 (https://pub.dev/packages/pull_to_refresh#-changelog-tab-)
⚠️  Failed to check flutter_test
test any => 1.14.2 (https://pub.dev/packages/test#-changelog-tab-)
⚠️  Failed to check flutter
⚠️  Failed to check flutter_driver

@spidgorny
Copy link
Author

@shlima Great upgrade. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment