Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit a0c1604

Browse files
author
David Morgan
committed
Fix generation across multiple files, allow reuse of generated identifiers.
1 parent 363b947 commit a0c1604

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.0.5
4+
5+
- Fix generation across multiple files, allow reuse of generated identifiers.
6+
37
## 0.0.4
48

59
- Fail on dynamic fields.

enum_class/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: enum_class
2-
version: 0.0.4
2+
version: 0.0.5
33
description: >
44
Dart classes as enums. This library is the runtime dependency.
55
authors:

enum_class_generator/lib/enum_class_generator.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class EnumClassGenerator extends Generator {
1818
Set<String> _usedGeneratedIdentifiers = new Set<String>();
1919

2020
Future<String> generate(Element element) async {
21+
// Generated identifiers only have to be unique per library, reset for
22+
// each new library.
23+
if (element is LibraryElement) {
24+
_usedGeneratedIdentifiers = new Set<String>();
25+
}
2126
if (element is! ClassElement) {
2227
return null;
2328
}

enum_class_generator/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: enum_class_generator
2-
version: 0.0.4
2+
version: 0.0.5
33
description: >
44
Dart classes as enums. This library is the dev dependency.
55
authors:
@@ -12,7 +12,7 @@ environment:
1212
dependencies:
1313
analyzer: '>=0.26.1 <1.0.0'
1414
built_collection: '^1.0.0'
15-
enum_class: '^0.0.4'
15+
enum_class: '^0.0.5'
1616
source_gen: '>=0.4.3 <1.0.0'
1717
quiver: '>=0.21.0 <0.22.0'
1818

enum_class_generator/test/enum_class_generator_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ void main() {
6363
expect(await generate(correctInput), endsWith(correctOutput));
6464
});
6565

66+
test('produces two correct output for two correct inputs', () async {
67+
expect(
68+
await generateTwo(correctInput,
69+
correctInput.replaceAll('test_enum', 'test_enum_two')),
70+
endsWith(correctOutput.replaceAll('test_enum', 'test_enum_two')));
71+
});
72+
6673
test('allows part statement with double quotes', () async {
6774
expect(
6875
await generate(correctInput.replaceAll(
@@ -570,6 +577,30 @@ Future<String> generate(String source) async {
570577
return outputFile.existsSync() ? outputFile.readAsStringSync() : '';
571578
}
572579

580+
Future<String> generateTwo(String source, String source2) async {
581+
final tempDir =
582+
Directory.systemTemp.createTempSync('enum_class_generator.dart.');
583+
final packageDir = new Directory(tempDir.path + '/packages')..createSync();
584+
final enumClassDir = new Directory(packageDir.path + '/enum_class')
585+
..createSync();
586+
final enumClassFile = new File(enumClassDir.path + '/enum_class.dart')
587+
..createSync();
588+
enumClassFile.writeAsStringSync(enumClassSource);
589+
590+
final libDir = new Directory(tempDir.path + '/lib')..createSync();
591+
final sourceFile = new File(libDir.path + '/test_enum.dart');
592+
sourceFile.writeAsStringSync(source);
593+
final sourceFile2 = new File(libDir.path + '/test_enum_two.dart');
594+
sourceFile2.writeAsStringSync(source2);
595+
596+
await build([], [new EnumClassGenerator()],
597+
projectPath: tempDir.path, librarySearchPaths: <String>['lib']);
598+
final outputFile = new File(libDir.path + '/test_enum.g.dart');
599+
final outputFile2 = new File(libDir.path + '/test_enum_two.g.dart');
600+
return (outputFile.existsSync() ? outputFile.readAsStringSync() : '') +
601+
(outputFile2.existsSync() ? outputFile2.readAsStringSync() : '');
602+
}
603+
573604
const String enumClassSource = r'''
574605
library enum_class;
575606

0 commit comments

Comments
 (0)