pubspec_lock 2.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 82

pubspec_lock Build Status codecov pubspec_lock version

Dart library to access and manipulate content of pubpec.lock files

The versions 2.x of this package require Dart 2.7. Consider version 1.1.0 if you intend to use the package with Dart 2.5 or 2.6.

Data classes #

PubspecLock represents data stored in pubspec.lock files.

https://pub.dev/packages/functional_data is used to provide data type facilities

class PubspecLock {
  final Iterable<SdkDependency> sdks;
  final Iterable<PackageDependency> packages;
}

SdkDependency provides sdk name and version:

@immutable
@FunctionalData()
class SdkDependency {
  /// Default constructor
  const SdkDependency({
    @required this.sdk,
    @required this.version,
  });

  final String sdk;
  final String version;
}

PackageDependency represents a package dependency, which can be:

  • a hosted dependency,
  • an SDK dependency,
  • a Git dependency, or
  • a path dependency

Please checkout https://dart.dev/tools/pub/dependencies for details.

https://pub.dev/packages/sum_types is used to deal with variations of different package dependency types in a type-safe and concise manner.

@immutable
@SumType()
class PackageDependency extends _$PackageDependency {
  const PackageDependency.sdk(SdkPackageDependency sdk) : super(sdk: sdk);
  const PackageDependency.hosted(HostedPackageDependency hosted) : super(hosted: hosted);
  const PackageDependency.git(GitPackageDependency git) : super(git: git);
  const PackageDependency.path(PathPackageDependency path) : super(path: path);

  /// Provides package dependency name
  String package() => iswitch(
        sdk: (d) => d.package,
        hosted: (d) => d.package,
        git: (d) => d.package,
        path: (d) => d.package,
      );

  /// Provides package dependency version
  String version() => iswitch(
        sdk: (d) => d.version,
        hosted: (d) => d.version,
        git: (d) => d.version,
        path: (d) => d.version,
      );

  /// Provides package dependency type -- direct, development, or transitive
  DependencyType type() => iswitch(
        sdk: (d) => d.type,
        hosted: (d) => d.type,
        git: (d) => d.type,
        path: (d) => d.type,
      );
}

Import from YAML #

The String extension method loadPubspecLockFromYaml() can be used to create a PubspecLock instance from a YAML string:

final pubspecLock = File('pubspec.lock').readAsStringSync().loadPubspecLockFromYaml();

Export to YAML #

PubspecLock.toYamlString() can be used to export PubspecLock object to a YAML string:

File('pubspec.lock').writeAsStringSync(pubspecLock.toYamlString());

Full example #

The following Dart script checks whether all Dart dependencies are taken from pub.dev.

import 'dart:io';

import 'package:pubspec_lock/pubspec_lock.dart';

// ignore_for_file: avoid_print

void main() {
  final pubspecLock = File('pubspec.lock').readAsStringSync().loadPubspecLockFromYaml();
  print('Loaded pubspec.lock with ${pubspecLock.packages.length} package dependencies:');

  final depsNotHostedByPubDev = [
    for (final package in pubspecLock.packages)
      if (!isHostedByPubDev(package))
        package
  ];

  if (depsNotHostedByPubDev.isEmpty) {
    print('SUCCESS: All dependencies are hosted by pub.dev');
  } else {
    print('WARNING: Dependencies hosted outside of pub.dev:');
    depsNotHostedByPubDev.forEach(print);
  }

  File('pubspec.lock').writeAsStringSync(pubspecLock.toYamlString());
}

bool isHostedByPubDev(PackageDependency package) => package.iswitcho(
      hosted: (package) => package.url == 'https://pub.dartlang.org',
      otherwise: () => false,
    );

[2.0.0] - 2019-12-28

  • Migration to Dart 2.7
  • Extension method on String to create PubspecLock from YAML
  • Major refactoring to improve code modularity

[1.1.0] - 2019-12-07

  • YAML formatting with json2yaml i.s.o. using ad-hoc YAML generator

[1.0.2] - 2019-11-24

  • README.md readability is improved

[1.0.1] - 2019-11-24

  • Fixed quality issues reported by pub.dev

[1.0.0] - 2019-11-24

  • PubspecLock is a data type representing data stored in pubspec.lock files.
  • Provides parser of pubspec.lock YAML string
  • Provides formatter to pubspec.lock YAML string
  • https://pub.dev/packages/functional_data is used to provide data type facilities

example/main.dart

import 'dart:io';

import 'package:pubspec_lock/pubspec_lock.dart';

// ignore_for_file: avoid_print

void main() {
  final pubspecLock = File('pubspec.lock').readAsStringSync().loadPubspecLockFromYaml();
  print('Loaded pubspec.lock with ${pubspecLock.packages.length} package dependencies:');

  final depsNotHostedByPubDev = [for (final package in pubspecLock.packages) if (!isHostedByPubDev(package)) package];

  if (depsNotHostedByPubDev.isEmpty) {
    print('SUCCESS: All dependencies are hosted by pub.dev');
  } else {
    print('WARNING: Dependencies hosted outside of pub.dev:');
    depsNotHostedByPubDev.forEach(print);
  }
}

bool isHostedByPubDev(PackageDependency package) => package.iswitcho(
      hosted: (package) => package.url == 'https://pub.dartlang.org',
      otherwise: () => false,
    );

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  pubspec_lock: ^2.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:pubspec_lock/pubspec_lock.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
64
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Jan 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Format lib/src/package_dependency/git_package_dependency/git_package_dependency.g.dart.

Run dartfmt to format lib/src/package_dependency/git_package_dependency/git_package_dependency.g.dart.

Format lib/src/package_dependency/hosted_package_dependency/hosted_package_dependency.g.dart.

Run dartfmt to format lib/src/package_dependency/hosted_package_dependency/hosted_package_dependency.g.dart.

Format lib/src/package_dependency/hosted_package_dependency/serializers.dart.

Run dartfmt to format lib/src/package_dependency/hosted_package_dependency/serializers.dart.

Fix additional 9 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/package_dependency/package_dependency.dart (Run dartfmt to format lib/src/package_dependency/package_dependency.dart.)
  • lib/src/package_dependency/package_dependency.g.dart (Run dartfmt to format lib/src/package_dependency/package_dependency.g.dart.)
  • lib/src/package_dependency/path_package_dependency/path_package_dependency.g.dart (Run dartfmt to format lib/src/package_dependency/path_package_dependency/path_package_dependency.g.dart.)
  • lib/src/package_dependency/path_package_dependency/serializers.dart (Run dartfmt to format lib/src/package_dependency/path_package_dependency/serializers.dart.)
  • lib/src/package_dependency/sdk_package_dependency/sdk_package_dependency.g.dart (Run dartfmt to format lib/src/package_dependency/sdk_package_dependency/sdk_package_dependency.g.dart.)
  • lib/src/pubspec_lock.dart (Run dartfmt to format lib/src/pubspec_lock.dart.)
  • lib/src/pubspec_lock.g.dart (Run dartfmt to format lib/src/pubspec_lock.g.dart.)
  • lib/src/sdk_dependency/sdk_dependency.g.dart (Run dartfmt to format lib/src/sdk_dependency/sdk_dependency.g.dart.)
  • lib/src/sdk_dependency/serializers.dart (Run dartfmt to format lib/src/sdk_dependency/serializers.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
functional_data ^0.2.3 0.2.3
json2yaml ^1.0.1 1.0.1
meta ^1.1.8 1.1.8
sum_types ^0.2.1 0.2.1+2
yaml ^2.2.0 2.2.0
Transitive dependencies
charcode 1.1.2
collection 1.14.12
path 1.6.4
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
Dev dependencies
build_runner ^1.7.2
dependency_validator ^1.4.2
functional_data_generator ^0.2.5
sum_types_generator ^0.2.1
test ^1.9.4
test_coverage ^0.4.0