Skip to content

Commit

Permalink
add option --out-dir to packge vector_graphics_compiler (#215)
Browse files Browse the repository at this point in the history
* add option --out-dir to packge vector_graphics_compiler

* reflect code to use path package

* add unit test to option --out-dir

* Lower path constraint

---------

Co-authored-by: Dan Field <[email protected]>
  • Loading branch information
FursanAbdulhak and dnfield committed Sep 21, 2023
1 parent 2b8860b commit c1782b8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:vector_graphics_compiler/src/svg/colors.dart';
import 'package:vector_graphics_compiler/vector_graphics_compiler.dart';

import 'util/isolate_processor.dart';
import 'package:path/path.dart' as p;

final ArgParser argParser = ArgParser()
..addOption(
Expand Down Expand Up @@ -68,6 +69,11 @@ final ArgParser argParser = ArgParser()
'Only includes files that end with .svg. '
'Cannot be combined with --input or --output.',
)
..addOption(
'out-dir',
help: 'The output directory path '
'use it with --input-dir to specific the output dirictory',
)
..addOption(
'input',
abbr: 'i',
Expand Down Expand Up @@ -144,7 +150,19 @@ Future<void> main(List<String> args) async {
if (!file.path.endsWith('.svg')) {
continue;
}
final String outputPath = '${file.path}.vec';

String outputPath = '${file.path}.vec';

// to specfic the output directory when parse multi svg
if (results.wasParsed('out-dir')) {
final Directory outDir = Directory(results['out-dir'] as String);
//to add the output dirctory if it exist
if (!outDir.existsSync()) {
outDir.createSync();
}
outputPath = p.join(outDir.path, '${p.basename(file.path)}.vec');
}

pairs.add(Pair(file.path, outputPath));
}
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/vector_graphics_compiler/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:
path_parsing: ^1.0.1
xml: ^6.3.0
vector_graphics_codec: 1.1.8
path: ^1.8.0

dev_dependencies:
flutter_lints: ^1.0.0
Expand Down
50 changes: 50 additions & 0 deletions packages/vector_graphics_compiler/test/cli_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import '../bin/util/isolate_processor.dart';
import '../bin/vector_graphics_compiler.dart' as cli;
import 'package:path/path.dart' as p;
import 'package:collection/collection.dart';

void main() {
final File output = File('test_data/example.vec');
Expand Down Expand Up @@ -86,4 +89,51 @@ void main() {
}
}
});

test('out-dir option works', () async {
const String inputTestDir = 'test_data';

const String outTestDir = 'output_vec';

try {
await cli.main(<String>[
'--input-dir',
inputTestDir,
'--out-dir',
outTestDir,
]);

bool passed = false;

final Directory inputDir = Directory(inputTestDir);
final Directory outDir = Directory(outTestDir);

if (inputDir.existsSync() && outDir.existsSync()) {
final List<String> inputTestFiles = inputDir
.listSync(recursive: true)
.whereType<File>()
.where((File element) => element.path.endsWith('svg'))
.map((File e) => p.basenameWithoutExtension(e.path))
.toList();

final List<String> outTestFiles = outDir
.listSync(recursive: true)
.whereType<File>()
.where((File element) => element.path.endsWith('vec'))
.map((File e) =>
p.withoutExtension(p.basenameWithoutExtension(e.path)))
.toList();

if (listEquals(inputTestFiles, outTestFiles)) {
passed = true;
}
}

expect(passed, true);
} finally {
if (Directory(outTestDir).existsSync()) {
Directory(outTestDir).deleteSync(recursive: true);
}
}
});
}

0 comments on commit c1782b8

Please sign in to comment.