Skip to content

Commit

Permalink
Delegate Yaml parsing to be done within the Yaml object
Browse files Browse the repository at this point in the history
  • Loading branch information
masa-futa committed Jan 21, 2025
1 parent 328a991 commit 0269a59
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:yumemi_lints/src/models/exceptions.dart';
import 'package:yumemi_lints/src/models/exit_status.dart';
import 'package:yumemi_lints/src/models/project_type.dart';
import 'package:yumemi_lints/src/models/version.dart';
import 'package:yumemi_lints/src/models/yaml.dart';

class UpdateCommandService {
const UpdateCommandService();
Expand Down Expand Up @@ -168,8 +169,8 @@ class UpdateCommandService {
}

Version getFlutterVersion(File pubspecFile) {
final yamls = parseYamlToMap(pubspecFile.readAsStringSync());
final environment = yamls['environment'] as Map<String, dynamic>;
final yaml = Yaml.parse(pubspecFile);
final environment = yaml.yamlMap['environment'] as Map<String, dynamic>;
final flutterVersion = environment['flutter'] as String?;

if (flutterVersion == null) {
Expand All @@ -182,62 +183,9 @@ class UpdateCommandService {
return Version.parse(flutterVersion);
}

Map<String, dynamic> parseYamlToMap(String yaml) {
final lines = yaml.split('\n');
final result = <String, dynamic>{};
final stack = <String>[];
var current = result;

for (final line in lines) {
final trimmed = line.trim();
if (trimmed.isEmpty || trimmed.startsWith('#')) {
continue;
}

// Get parent element if there is an indent.
final indent = line.length - line.trimLeft().length;
while (stack.length > indent) {
stack.removeLast();
current = stack.isEmpty
? result
: current['__parent__'] as Map<String, dynamic>;
}

if (trimmed.contains(':')) {
final parts = trimmed.split(':');
final key = parts[0].trim();
final value = parts.sublist(1).join(':').trim();
final newMap = <String, dynamic>{};
// If value is empty,
// create a new Map assuming a child element and add it to current.
if (value.isEmpty) {
current[key] = newMap;
newMap['__parent__'] = current;
stack.add(key);
current = newMap;
} else {
current[key] = value;
}
}
}

_cleanParent(result);
return result;
}

// Delete the "__parent__" key.
void _cleanParent(Map<String, dynamic> map) {
map.remove('__parent__');
for (final value in map.values) {
if (value is Map<String, dynamic>) {
_cleanParent(value);
}
}
}

Version getDartVersion(File pubspecFile) {
final yamls = parseYamlToMap(pubspecFile.readAsStringSync());
final environment = yamls['environment'] as Map<String, dynamic>;
final yaml = Yaml.parse(pubspecFile);
final environment = yaml.yamlMap['environment'] as Map<String, dynamic>;
final dartVersion = environment['flutter'] as String?;

if (dartVersion == null) {
Expand Down
63 changes: 63 additions & 0 deletions packages/yumemi_lints/lib/src/models/yaml.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:io';

class Yaml {
const Yaml._(this.yamlMap);

// Convert and parse the file system into a Yaml file,
// obtain a YamlMap, and then create a Yaml object.
factory Yaml.parse(File file) {
final lines = file.readAsStringSync().split('\n');
final result = <String, dynamic>{};
final stack = <String>[];
var current = result;

for (final line in lines) {
final trimmed = line.trim();
if (trimmed.isEmpty || trimmed.startsWith('#')) {
continue;
}

// Get parent element if there is an indent.
final indent = line.length - line.trimLeft().length;
while (stack.length > indent) {
stack.removeLast();
current = stack.isEmpty
? result
: current['__parent__'] as Map<String, dynamic>;
}

if (trimmed.contains(':')) {
final parts = trimmed.split(':');
final key = parts[0].trim();
final value = parts.sublist(1).join(':').trim();
final newMap = <String, dynamic>{};
// If value is empty,
// create a new Map assuming a child element and add it to current.
if (value.isEmpty) {
current[key] = newMap;
newMap['__parent__'] = current;
stack.add(key);
current = newMap;
} else {
current[key] = value;
}
}
}
// Delete the "__parent__" key.
void cleanParent(Map<String, dynamic> map) {
map.remove('__parent__');
for (final value in map.values) {
if (value is Map<String, dynamic>) {
cleanParent(value);
}
}
}

cleanParent(result);
return Yaml._(result);
}

final YamlMap yamlMap;
}

typedef YamlMap = Map<String, dynamic>;

0 comments on commit 0269a59

Please sign in to comment.