-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38a18b2
commit 2052d26
Showing
2 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:extract/app.dart' as app; | ||
|
||
void main(List<String> arguments) { | ||
print('Hello world: ${app.calculate()}!'); | ||
Future<void> main(List<String> arguments) async { | ||
final exitCode = await app.runApp(arguments); | ||
return Future.wait([ | ||
stdout.close(), | ||
stderr.close(), | ||
]).then((_) => exitCode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,65 @@ | ||
int calculate() { | ||
return 6 * 7; | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:extract/src/app_exeption.dart'; | ||
import 'package:extract/src/project_config.dart'; | ||
import 'package:file/local.dart'; | ||
import 'package:recase/recase.dart'; | ||
|
||
Future<int> runApp(List<String> arguments) async { | ||
final path = arguments.firstOrNull; | ||
if (path == null) { | ||
stderr.writeln('Usage: extract <path>'); | ||
return 1; | ||
} | ||
|
||
try { | ||
await _extract(path); | ||
stdout.writeln(r'Successfully extracted project config to $GITHUB_OUTPUT'); | ||
return 0; | ||
} on AppException catch (e) { | ||
stderr.writeln(e.message); | ||
return 1; | ||
} | ||
} | ||
|
||
Future<void> _extract(String path) async { | ||
final fileSystem = LocalFileSystem(); | ||
final projectConfigFile = fileSystem.file(path); | ||
if (!projectConfigFile.existsSync()) { | ||
throw AppException('Project config file not found at $path'); | ||
} | ||
|
||
final projectConfigContent = projectConfigFile.readAsStringSync(); | ||
|
||
final Map<String, dynamic> json; | ||
try { | ||
json = jsonDecode(projectConfigContent) as Map<String, dynamic>; | ||
} on FormatException catch (e) { | ||
throw AppException('Invalid project config file at $path: $e'); | ||
} | ||
|
||
final ProjectConfig projectConfig; | ||
try { | ||
projectConfig = ProjectConfig.fromJson(json); | ||
} on FormatException catch (e) { | ||
throw AppException('Invalid project config file at $path: $e'); | ||
} | ||
|
||
final projectConfigJson = projectConfig.toJson(); | ||
final futures = projectConfigJson.entries.map( | ||
(entry) => Process.run( | ||
'echo', | ||
[ | ||
'"${entry.key.paramCase}=${entry.value}"', | ||
'>>', | ||
r'$GITHUB_OUTPUT', | ||
], | ||
), | ||
); | ||
final results = await Future.wait(futures); | ||
final exitCodes = results.map((result) => result.exitCode); | ||
if (exitCodes.any((exitCode) => exitCode != 0)) { | ||
throw AppException(r'Failed to write project config to $GITHUB_OUTPUT'); | ||
} | ||
} |