Skip to content
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

chore: use install executable to obtain libpact #3

Merged
merged 5 commits into from Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .codecov.yml
@@ -0,0 +1,5 @@
coverage:
status:
patch:
default:
target: 80%
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -23,8 +23,14 @@ jobs:
- uses: dart-lang/setup-dart@v1
- name: Install dependencies
run: dart pub get
- name: Obtain libpact
run: dart run pact_dart:install
env:
PACT_DART_LIB_DOWNLOAD_PATH: .
- name: Run unit tests
run: dart test --coverage="coverage" test/
env:
PACT_DART_LIB_DOWNLOAD_PATH: .
- name: Format coverage
run: dart run coverage:format_coverage --lcov --in=coverage --out=coverage.lcov --packages=.packages --report-on=lib
- uses: codecov/codecov-action@v2
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 0.3.0

- chore: use install executable to obtain libpact

## 0.2.0

- feat: implement dsl for creating pact matchers
Expand Down
5 changes: 1 addition & 4 deletions README.md
Expand Up @@ -9,14 +9,11 @@ This library provides a Dart DSL for generating Pact contracts. It implements [P

### Installation

Simply, install the package using `pub`:

```bash
dart pub add pact_dart
dart run pact_dart:install
```

Currently, the `libpact_ffi` dependency is included for x86_64 macOS, Linux and Windows.

### Example

```dart
Expand Down
65 changes: 65 additions & 0 deletions bin/install.dart
@@ -0,0 +1,65 @@
/// Dart has no way to install native libraries during package installation
///
/// Something to keep an eye on: https://github.com/dart-lang/pub/issues/39
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:path/path.dart' as path;

String getPlatformFileType() {
switch (Platform.operatingSystem) {
case 'macos':
return 'dylib';

case 'linux':
return 'so';

case 'windows':
return 'dll';

default:
throw UnsupportedError(
'Sorry! ${Platform.operatingSystem} is unsupported by pact_dart.');
}
}

String getLibDirectory() {
final env = Platform.environment['PACT_DART_LIB_DOWNLOAD_PATH'];
if (env != null && env.isNotEmpty) {
return env;
}

return '/usr/local/lib';
}

Uri generateDependencyLink(String name, String version, String fileType) {
final operatingSystem =
Platform.operatingSystem == 'macos' ? 'osx' : Platform.operatingSystem;

final path =
'/pact-foundation/pact-reference/releases/download/$name-v$version/$name-$operatingSystem-x86_64.$fileType.gz';

return Uri(scheme: 'https', host: 'github.com', path: path);
}

Future<void> downloadDependency(String name, String version) async {
final fileType = getPlatformFileType();
final dependencyLink = generateDependencyLink(name, version, fileType);

final res = await http.get(dependencyLink);
final library = GZipCodec().decode(res.bodyBytes);

final libDir = getLibDirectory();
final libPath = path.join(libDir, '$name.$fileType');

final writeStream = File(libPath.toString()).openWrite();
writeStream.add(library);

await writeStream.close();
}

void main() async {
final dependencyName = Platform.isWindows ? 'pact_ffi' : 'libpact_ffi';
final dependencyVersion = '0.0.1';

await downloadDependency(dependencyName, dependencyVersion);
}
Binary file removed dependencies/libpact_ffi-linux-x86_64.so
Binary file not shown.
Binary file removed dependencies/pact_ffi-windows-x86_64.dll
Binary file not shown.
25 changes: 17 additions & 8 deletions lib/src/ffi/dylib.dart
Expand Up @@ -2,20 +2,29 @@ import 'dart:io';
import 'dart:ffi';
import 'package:path/path.dart' as path;

String getlibraryName() {
String getLibDirectory() {
final env = Platform.environment['PACT_DART_LIB_DOWNLOAD_PATH'];
if (env != null && env.isNotEmpty) {
return env;
}

return '/usr/local/lib';
}

String getLibName() {
if (Platform.isLinux) {
return 'libpact_ffi-linux-x86_64.so';
return 'libpact_ffi.so';
}
if (Platform.isMacOS) return 'libpact_ffi-osx-x86_64.dylib';
if (Platform.isWindows) return 'pact_ffi-windows-x86_64.dll';
if (Platform.isMacOS) return 'libpact_ffi.dylib';
if (Platform.isWindows) return 'pact_ffi.dll';

throw Exception('Package does not support the current platform');
}

DynamicLibrary openLibrary() {
final libraryName = getlibraryName();
final libraryPath =
path.join(Directory.current.path, 'dependencies', libraryName);
final name = getLibName();
final libDir = getLibDirectory();
final libPath = path.join(libDir, name);

return DynamicLibrary.open(libraryPath);
return DynamicLibrary.open(libPath);
}
Binary file not shown.
4 changes: 3 additions & 1 deletion pubspec.yaml
@@ -1,6 +1,6 @@
name: pact_dart
description: A Dart DSL for creating pact contracts
version: 0.2.0
version: 0.3.0
homepage: https://github.com/matthewshirley/pact_dart

environment:
Expand All @@ -15,3 +15,5 @@ dev_dependencies:
http: ^0.13.3
pedantic: ^1.10.0
test: ^1.17.10
executables:
install: