Skip to content

Commit

Permalink
refactor: clean up manager & subclasses
Browse files Browse the repository at this point in the history
> add `ConsolePrinter` as required arg d9df525
> clean up `FinderManager` &  `ReplacerManager`. Reduce bloated code which made it hard to follow code
> add file number when calling reset in tracker 909ad98
> add initial final touches that makes the managers complete and ready for tests
> customize enums & typedefs for manager use
  • Loading branch information
kekavc24 committed Dec 21, 2023
1 parent 909ad98 commit bf76550
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 123 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pubspec.lock
doc/api/

# Files generated during & for tests
.vscode/
.test_coverage.dart
coverage/
.test_runner.dart
Expand Down
151 changes: 71 additions & 80 deletions lib/src/core/yaml_transformers/managers/finder_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,63 @@ class FinderManager extends TransformerManager implements ManageByCount {
FinderManager._({
required super.files,
required super.aggregator,
required KeysToFind keysToFind,
required ValuesToFind valuesToFind,
required PairsToFind pairsToFind,
required FinderType finderType,
}) : _finderType = finderType,
_keysToFind = keysToFind,
_valuesToFind = valuesToFind,
_pairsToFind = pairsToFind;
required super.printer,
KeysToFind? keysToFind,
ValuesToFind? valuesToFind,
PairsToFind? pairsToFind,
FinderType? finderType,
}) : _finderType = finderType ?? FinderType.byValue,
_keysToFind = keysToFind ?? (keys: [], orderType: OrderType.loose),
_valuesToFind = valuesToFind ?? [],
_pairsToFind = pairsToFind ?? {};

factory FinderManager.forFinder({
FinderManager.fullSetup({
required List<FileOutput> files,
required Aggregator aggregator,
required ConsolePrinter printer,
required KeysToFind keysToFind,
required ValuesToFind valuesToFind,
required PairsToFind pairsToFind,
FinderType? finderType,
}) {
return FinderManager._(
files: files,
aggregator: aggregator,
finderType: finderType ?? FinderType.byValue,
keysToFind: keysToFind,
valuesToFind: valuesToFind,
pairsToFind: pairsToFind,
);
}
required FinderType finderType,
}) : this._(
files: files,
aggregator: aggregator,
printer: printer,
keysToFind: keysToFind,
valuesToFind: valuesToFind,
pairsToFind: pairsToFind,
finderType: finderType,
);

factory FinderManager.findValues({
FinderManager.findValues({
required List<FileOutput> files,
required Aggregator aggregator,
required ConsolePrinter printer,
required ValuesToFind valuesToFind,
}) {
return FinderManager._(
files: files,
aggregator: aggregator,
keysToFind: (keys: [], orderType: OrderType.loose),
valuesToFind: valuesToFind,
pairsToFind: {},
finderType: FinderType.byValue,
);
}
}) : this._(
files: files,
aggregator: aggregator,
printer: printer,
valuesToFind: valuesToFind,
);

factory FinderManager.findeKeys({
FinderManager.findKeys({
required List<FileOutput> files,
required Aggregator aggregator,
required ConsolePrinter printer,
required KeysToFind keysToFind,
}) {
return FinderManager._(
files: files,
aggregator: aggregator,
keysToFind: keysToFind,
valuesToFind: [],
pairsToFind: {},
finderType: FinderType.byValue,
);
}
}) : this._(
files: files,
aggregator: aggregator,
printer: printer,
keysToFind: keysToFind,
);

late FinderType _finderType;
final FinderType _finderType;

late KeysToFind _keysToFind;
late ValuesToFind _valuesToFind;
late PairsToFind _pairsToFind;
final KeysToFind _keysToFind;
final ValuesToFind _valuesToFind;
final PairsToFind _pairsToFind;

/// Obtains the current generator to be used/ currently in use by
/// this manager
Expand All @@ -82,36 +77,40 @@ class FinderManager extends TransformerManager implements ManageByCount {
? transformAll(resetTracker: true)
: transformByCount(
aggregator.count!,
applyToEach: aggregator.applyToEach,
applyToEachArg: aggregator.applyToEachArg,
applyToEachFile: aggregator.applyToEachFile,
);
}

/// Prefills the tracker with keys for accurate value tracking
void _prefillTracker() {
for (final data in keysToPrefill()) {
if (data.keys.isEmpty) continue;

_tracker.prefill(data.keys, origin: data.origin);
}
}

@override
Future<void> transform() async {

// TODO: Add match formatter/aggregator
for (final match in getGenerator()) {}
// Loop all matches and add to printer
for (final match in getGenerator()) {
_printer.addValuesFound(match.currentFile, match.data);
}
}

@override
Iterable<FindManagerOutput> transformByCount(
int count, {
required bool applyToEach,
bool applyToEachFile = true,
required bool applyToEachArg,
required bool applyToEachFile,
}) sync* {
// Prefill tracker
_prefillTracker();

/// If we are not applying to each file and neither are we applying to
/// to each argument. Return just count
if (!applyToEachFile && !applyToEach) {
if (!applyToEachFile && !applyToEachArg) {
yield* transformAll(resetTracker: true).take(count);
}

Expand All @@ -129,7 +128,7 @@ class FinderManager extends TransformerManager implements ManageByCount {
///
// we never reset the tracker, terminate once arg conditions are met
else if (!applyToEachFile && applyToEach) {
else if (!applyToEachFile && applyToEachArg) {
yield* transformAll(resetTracker: false).takeWhile(
(value) => !value.reachedLimit,
);
Expand All @@ -148,22 +147,18 @@ class FinderManager extends TransformerManager implements ManageByCount {
var countForEachFile = <int, int>{};

/// When `applyToEach` argument is true, we track current active file &
/// whether we reach the limit for count for each argument and yielded the
/// last value that triggered the match
/// whether we reached the limit for count for each argument and yielded
/// the last value that triggered the match
var currentFile = 0;

/// We create our custom queue with all files
var customQueue = QueueList.from(yamlQueue);

// Setup all our variables for tracking exiting current transformation
if (!applyToEach) {
countForEachFile = yamlQueue.asMap().keys.fold(
<int, int>{},
(previousValue, element) {
previousValue.addAll({element: 0});
return previousValue;
},
);
if (!applyToEachArg) {
countForEachFile = <int, int>{}..addEntries(
yamlQueue.mapIndexed((index, element) => MapEntry(index, 0)),
);
}

/// Our queue will act as the reference for controlling the loop
Expand All @@ -178,7 +173,7 @@ class FinderManager extends TransformerManager implements ManageByCount {
///
/// When `applyToEach` argument is false, we break loop if count for
/// matches generated for this file has been reached
if (!applyToEach) {
if (!applyToEachArg) {
if (countForEachFile[match.currentFile] == count) break;

// Increment its count if loop wasn't broken
Expand Down Expand Up @@ -233,15 +228,16 @@ class FinderManager extends TransformerManager implements ManageByCount {
required bool resetTracker,
QueueList<YamlMap>? customQueue,
}) sync* {
final numOfFiles = yamlQueue.length;
final localQueue = customQueue ?? QueueList.from(yamlQueue);

do {
// Index of current file
final currentFile = yamlQueue.length - localQueue.length;
final currentFile = numOfFiles - localQueue.length;

// Reset tracker if not first run, since we havent completed it.
if (localQueue.length != yamlQueue.length && resetTracker) {
super.resetTracker();
super.resetTracker(currentFile - 1);
}

final yamlMap = localQueue.removeFirst(); // Get file, remove from list
Expand All @@ -260,23 +256,18 @@ class FinderManager extends TransformerManager implements ManageByCount {
);
}
} while (localQueue.isNotEmpty);

// Reset the tracker and put last tracker into the history
if (resetTracker) super.resetTracker(numOfFiles - 1);
}

@override
List<PrefillData> keysToPrefill() {
final keys = <PrefillData>[];

if (_keysToFind.keys.isNotEmpty) {
keys.add((keys: _keysToFind.keys, origin: Origin.key));
}
if (_valuesToFind.isNotEmpty) {
keys.add((keys: _valuesToFind, origin: Origin.value));
}
if (_pairsToFind.isNotEmpty) {
keys.add((keys: _pairsToFind.entries.toList(), origin: null));
}

return keys;
return <PrefillData>[
(keys: _keysToFind.keys, origin: Origin.key),
(keys: _valuesToFind, origin: Origin.value),
(keys: _pairsToFind.entries.toList(), origin: null),
];
}

/// Get finder for this manager
Expand Down
22 changes: 19 additions & 3 deletions lib/src/core/yaml_transformers/managers/manager.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:collection/collection.dart';
import 'package:magical_version_bump/src/core/yaml_transformers/console_printer/console_printer.dart';
import 'package:magical_version_bump/src/core/yaml_transformers/managers/tranform_tracker/transform_tracker.dart';
import 'package:magical_version_bump/src/core/yaml_transformers/yaml_transformer.dart';
import 'package:magical_version_bump/src/utils/enums/enums.dart';
Expand All @@ -14,7 +15,9 @@ abstract class TransformerManager {
TransformerManager({
required List<FileOutput> files,
required Aggregator aggregator,
ConsolePrinter? printer,
}) : _aggregator = aggregator,
_printer = printer ?? ConsolePrinter(format: aggregator.viewFormat),
_yamlQueue = QueueList.from(files.map((e) => e.fileAsMap)),
_tracker = TransformTracker(limit: aggregator.count);

Expand All @@ -24,6 +27,10 @@ abstract class TransformerManager {
/// A custom Aggregator for this transformer
final Aggregator _aggregator;

/// A console printer for each manager that will is called by each
/// command's handler to print to console all aggregated info
final ConsolePrinter _printer;

/// Tracker for keeping track of transformations made.
final TransformTracker _tracker;

Expand All @@ -36,6 +43,8 @@ abstract class TransformerManager {
/// Tracker in use by this manager
TransformTracker get tracker => _tracker;

ConsolePrinter get printer => _printer;

/// Increments the count of a tracked value being transformed in the
/// tracker using a [ String ]
void incrementWithStrings(List<dynamic> values, {required Origin origin}) {
Expand All @@ -56,7 +65,7 @@ abstract class TransformerManager {
}

/// Resets tracker and saves current state to history
void resetTracker() => _tracker.reset();
void resetTracker(int fileNumber) => _tracker.reset(fileNumber: fileNumber);

/// Initializes transformer manager
Future<void> transform();
Expand All @@ -83,9 +92,16 @@ abstract interface class ManageByCount {
///
/// [ count ] - denotes number of values to extract
///
/// [ applyToEach ] - denotes whether each unique matcher should be
/// [ applyToEachArg ] - denotes whether each unique matcher should be
/// transformed by this count
void transformByCount(int count, {required bool applyToEach});
///
/// [ applyToEachFile ] - denotes whether each file should be transformed
/// by count
void transformByCount(
int count, {
required bool applyToEachArg,
required bool applyToEachFile,
});

/// Transforms all
void transformAll({required bool resetTracker});
Expand Down
Loading

0 comments on commit bf76550

Please sign in to comment.