https://github.com/alexei-sintotski/pubspec_lock
Dart library to access and manipulate content of pubpec.lock files
https://github.com/alexei-sintotski/pubspec_lock
dart flutter pub pubspec
Last synced: 2 months ago
JSON representation
Dart library to access and manipulate content of pubpec.lock files
- Host: GitHub
- URL: https://github.com/alexei-sintotski/pubspec_lock
- Owner: alexei-sintotski
- License: mit
- Created: 2019-10-13T10:14:25.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-08-21T13:51:18.000Z (almost 2 years ago)
- Last Synced: 2025-03-23T22:37:36.479Z (3 months ago)
- Topics: dart, flutter, pub, pubspec
- Language: Dart
- Homepage:
- Size: 116 KB
- Stars: 5
- Watchers: 1
- Forks: 9
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# pubspec_lock  [](https://pub.dev/packages/pubspec_lock)
Dart library to access and manipulate content of pubpec.lock files## 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 sdks;
final Iterable 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 dependencyPlease 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,
);
```