Skip to content

Commit

Permalink
feat: Implement app
Browse files Browse the repository at this point in the history
  • Loading branch information
blendthink committed Jan 30, 2024
1 parent 38a18b2 commit 2052d26
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
10 changes: 8 additions & 2 deletions app/bin/app.dart
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);
}
66 changes: 64 additions & 2 deletions app/lib/app.dart
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');
}
}

0 comments on commit 2052d26

Please sign in to comment.