From 8c490ae64cf6f7d83ffb1fca5e0fe8c94455dced Mon Sep 17 00:00:00 2001 From: r1n0x Date: Tue, 23 Jul 2024 21:50:50 +0200 Subject: [PATCH] feat: github actions and provider changes --- .github/workflows/dart.yml | 38 +++++++++++++++++++++++++ README.md | 2 ++ example/type_face_provider_example.dart | 22 +++++++------- lib/src/model.dart | 4 +-- lib/src/provider.dart | 2 +- lib/src/provider/linux.dart | 26 +++++++---------- test/type_face_provider_test.dart | 32 +++++++++++++++++++++ 7 files changed, 97 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/dart.yml create mode 100644 test/type_face_provider_test.dart diff --git a/.github/workflows/dart.yml b/.github/workflows/dart.yml new file mode 100644 index 0000000..8297308 --- /dev/null +++ b/.github/workflows/dart.yml @@ -0,0 +1,38 @@ +name: Dart + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + validate_changes: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: dart-lang/setup-dart@9a04e6d73cca37bd455e0608d7e5092f881fd60 + - name: Install dependencies + run: dart pub get + - name: Verify formatting + run: dart format lib example test --output=none --set-exit-if-changed . + - name: Analyze project source + run: dart analyze example lib test + windows_tests: + runs-on: windows-latest + steps: + - uses: actions/checkout@v4 + - uses: dart-lang/setup-dart@9a04e6d73cca37bd455e0608d7e5092f881fd60 + - name: Install dependencies + run: dart pub get + - name: Run tests + run: dart test + linux_tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: dart-lang/setup-dart@9a04e6d73cca37bd455e0608d7e5092f881fd60 + - name: Install dependencies + run: dart pub get + - name: Run tests + run: dart test diff --git a/README.md b/README.md index 42cccb2..11a7f51 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ installed on user device. ## Getting started / Usage +Follow [pub.dev tutorial](https://pub.dev/packages/type_face_provider/install) for installation. + Beside linux requiring 'fc-list' dependency installed, just use it as shown in example. diff --git a/example/type_face_provider_example.dart b/example/type_face_provider_example.dart index b04c4a1..6dd2a01 100644 --- a/example/type_face_provider_example.dart +++ b/example/type_face_provider_example.dart @@ -14,17 +14,19 @@ void main() async { for (final style in typeface.styles) { print('\t\t- $style'); } - print('\tfonts:'); - for (final font in typeface.fonts) { - print('\t\t- $font'); + print('\tfull names:'); + for (final fullName in typeface.fullNames) { + print('\t\t- $fullName'); } - /// File: '/usr/share/fonts/truetype/noto/NotoSansMono-Bold.ttf' - /// typefaces: - /// - Noto Sans Mono - /// styles: - /// - Bold - /// fonts: - /// - Noto Sans Mono Bold + /// ... + /// File: '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf' + /// typefaces: + /// - DejaVu Sans Mono + /// styles: + /// - Book + /// full names: + /// - DejaVu Sans Mono + /// ... } } diff --git a/lib/src/model.dart b/lib/src/model.dart index 6a93b3e..182feaa 100644 --- a/lib/src/model.dart +++ b/lib/src/model.dart @@ -3,12 +3,12 @@ import 'dart:io'; import 'package:collection/collection.dart'; class Typeface { - const Typeface(this.names, this.file, this.styles, this.fonts); + const Typeface(this.names, this.file, this.styles, this.fullNames); final List names; final File file; final List styles; - final List fonts; + final List fullNames; } class Typefaces extends DelegatingList { diff --git a/lib/src/provider.dart b/lib/src/provider.dart index 5c34391..7c1f86c 100644 --- a/lib/src/provider.dart +++ b/lib/src/provider.dart @@ -12,7 +12,7 @@ abstract class TypefacesProvider { factory TypefacesProvider._setPlatform() { if (Platform.isLinux) { - return LinuxFontsProvider(); + return LinuxTypefacesProvider(); } else if (Platform.isWindows) { throw UnimplementedError( 'Current platform "${Platform.operatingSystem}" is not supported by $TypefacesProvider and it will be supported in the future.', diff --git a/lib/src/provider/linux.dart b/lib/src/provider/linux.dart index 6c5636d..5cb79ae 100644 --- a/lib/src/provider/linux.dart +++ b/lib/src/provider/linux.dart @@ -6,8 +6,8 @@ import 'package:type_face_provider/src/fc_list_validator.dart'; import 'package:type_face_provider/src/model.dart'; import 'package:type_face_provider/type_face_provider.dart'; -class LinuxFontsProvider extends TypefacesProvider { - LinuxFontsProvider(); +class LinuxTypefacesProvider extends TypefacesProvider { + LinuxTypefacesProvider(); static final _fcListValidator = FcListValidator(); bool? _isFcListInstalled; @@ -17,12 +17,13 @@ class LinuxFontsProvider extends TypefacesProvider { static const _filePathSectionIndex = 0; static const _nameSectionIndex = 1; static const _stylesSectionIndex = 2; + static const _fullNameSectionIndex = 3; @override Future getTypefaces() async { await throwIfFcListIsNotInstalled(); final Process process = await Process.start('fc-list', [ - '--format="%{file}$_sectionSeparator%{family}$_sectionSeparator%{style}$_lineSeparator"' + '--format="%{file}$_sectionSeparator%{family}$_sectionSeparator%{style}$_sectionSeparator%{fullname}$_lineSeparator"' ]); await process.isFinished; @@ -39,8 +40,11 @@ class LinuxFontsProvider extends TypefacesProvider { .split(',') .map((e) => e.trim()) .toList(growable: false); - List fonts = _buildFonts(names, styles); - result.add(Typeface(names, file, styles, fonts)); + final fullNames = sections[_fullNameSectionIndex] + .split(',') + .map((e) => e.trim()) + .toList(growable: false); + result.add(Typeface(names, file, styles, fullNames)); } return result; } @@ -49,18 +53,8 @@ class LinuxFontsProvider extends TypefacesProvider { _isFcListInstalled ??= await _fcListValidator.isInstalled(); if (!_isFcListInstalled!) { throw Exception( - 'In order to use $LinuxFontsProvider platform you must have \'fc-list\' installed as your system level dependency.'); - } - } - - List _buildFonts(List names, List styles) { - final List fonts = []; - for (final name in names) { - for (final style in styles) { - fonts.add("$name $style"); - } + 'In order to use $LinuxTypefacesProvider platform you must have \'fc-list\' installed as your system level dependency.'); } - return fonts; } List _parseSections(String line) { diff --git a/test/type_face_provider_test.dart b/test/type_face_provider_test.dart new file mode 100644 index 0000000..c3af2cd --- /dev/null +++ b/test/type_face_provider_test.dart @@ -0,0 +1,32 @@ +import 'dart:io'; + +import 'package:collection/collection.dart'; +import 'package:test/test.dart'; +import 'package:type_face_provider/src/provider.dart'; + +void main() { + group('Fetching system fonts', () { + test('Platform - Linux', () async { + if (!Platform.isLinux) { + markTestSkipped('Skipped - test can be only ran on Linux.'); + return; + } + + final provider = TypefacesProvider.platform; + final typefaces = (await provider.getTypefaces()); + expect(typefaces.isNotEmpty, true, + reason: "Any system must have at least one font installed!"); + final font = typefaces.firstWhereOrNull((e) => + e.file.path == '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf'); + expect(font != null, true, + reason: 'DejaVu Sans was not found on the system.'); + if (font != null) { + expect(font.names, ['DejaVu Sans Mono']); + expect(font.file.path, + '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf'); + expect(font.styles, ['Book']); + expect(font.fullNames, ['DejaVu Sans Mono']); + } + }); + }); +}