Skip to content

Commit

Permalink
feat(dfat) adds task reveal
Browse files Browse the repository at this point in the history
  • Loading branch information
VannaDii committed Jan 17, 2022
1 parent c05911b commit d2fb18f
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 50 deletions.
4 changes: 3 additions & 1 deletion dfat/bin/src/commands/aggregate.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:io';
import 'dart:convert';

import 'package:tint/tint.dart';
import 'package:path/path.dart' as path;
import 'package:collection/collection.dart';

Expand All @@ -18,6 +17,9 @@ class AggregateCommand extends DfatCommand {
@override
String get category => 'Granular';

@override
List<TaskCommand> revealTasks() => [];

final _lambdaZipMatcher =
RegExp(r"^.*/\.dist/.*\.zip$", caseSensitive: false, dotAll: true);

Expand Down
24 changes: 14 additions & 10 deletions dfat/bin/src/commands/base.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:args/command_runner.dart';
import 'package:meta/meta.dart';

import 'tasks/base.dart';
export '../enums.dart';
Expand All @@ -9,16 +8,19 @@ typedef ArgsProvider = Map<String, dynamic>? Function(String taskName);
abstract class DfatCommand extends Command<bool> {
DfatCommand({required this.tools, required this.logger});

/// Private storage for the sequence
List<TaskCommand> _sequence = [];

/// A logger instance
final Logger logger;

/// The external tools (commands and exes) required by the command
final List<String> tools;

/// The sequence of tasks run by this command
@nonVirtual
List<TaskCommand> get sequence => _sequence;
List<TaskCommand> _sequence = [];
/// Reveals all tasks potentially used by the command.
List<TaskCommand> revealTasks();

/// Sets the sequence of tasks run by this command
void useSequence(List<TaskCommand> sequence) {
_sequence = sequence;
}
Expand All @@ -28,12 +30,14 @@ abstract class DfatCommand extends Command<bool> {
(runner?.commands.values ?? []).whereType<DfatCommand>().toList();

/// All [tools] from this commands [sequence]
List<String> get allTools =>
[...tools, ...sequence.map((e) => e.requirements.tools).expand((e) => e)];
List<String> get allTools => [
...tools,
...revealTasks().map((e) => e.requirements.tools).expand((e) => e)
];

/// All [TaskRequirements.files] from this commands [sequence]
List<FileRequirement> get allFsPaths =>
[...sequence.map((e) => e.requirements.files).expand((e) => e)];
[...revealTasks().map((e) => e.requirements.files).expand((e) => e)];

/// A sorted, distinct list of [allTools] from [allCommands]
List<String> get globalTools =>
Expand All @@ -50,7 +54,7 @@ abstract class DfatCommand extends Command<bool> {
[Map<String, Map<String, dynamic>> args = const {}]) async {
bool result = true;
final _def = <String, dynamic>{};
for (var task in sequence) {
for (var task in _sequence) {
if (!result) {
break;
} else {
Expand All @@ -65,7 +69,7 @@ abstract class DfatCommand extends Command<bool> {
Future<bool> runSequenceSame(ArgsProvider provider) async {
bool result = true;
final _def = <String, dynamic>{};
for (var task in sequence) {
for (var task in _sequence) {
if (!result) {
break;
} else {
Expand Down
9 changes: 6 additions & 3 deletions dfat/bin/src/commands/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ class BuildCommand extends DfatCommand {
String get category => 'General';

@override
ArgParser get argParser => ArgParser(allowTrailingOptions: true);
ArgParser get argParser => ArgParser.allowAnything(); // Pass-thru to sequence

@override
List<TaskCommand> revealTasks() => [];

BuildCommand(Logger logger) : super(logger: logger, tools: []);

@override
Future<bool> run() async {
final runner = super.runner!;
final dockerSeq = ['docker'];
final buildSeq = ['check', 'validate', 'shared', 'lambda', 'aggregate'];
final dockerSeq = ['check', 'validate', 'shared', 'docker', 'aggregate'];
final buildSeq = ['check', 'lambda'];
final execSeq = (Utils.isInDocker ? buildSeq : dockerSeq);

final baseArgs = (argResults?.arguments ?? []).where((a) => a != name);
Expand Down
6 changes: 6 additions & 0 deletions dfat/bin/src/commands/check.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class CheckCommand extends DfatCommand {

final String inBl = ' ';

@override
List<TaskCommand> revealTasks() => [
CheckToolsTask(this, logger),
CheckFSTask(this, logger),
];

@override
Future<bool> run() async {
final blockCloser = logger.header('Checks');
Expand Down
18 changes: 10 additions & 8 deletions dfat/bin/src/commands/docker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ class DockerCommand extends DfatCommand {
@override
String get category => 'Granular';

@override
List<TaskCommand> get sequence =>
_sequence ?? [DockerBuildTask(this, logger), DockerRunTask(this, logger)];
List<TaskCommand>? _sequence;

DockerCommand(Logger logger)
: super(logger: logger, tools: Utils.isInDocker ? [] : ['docker']) {
var workDir = Directory.current.path;
Expand Down Expand Up @@ -52,6 +47,12 @@ class DockerCommand extends DfatCommand {
);
}

@override
List<TaskCommand> revealTasks() => [
DockerBuildTask(this, logger),
DockerRunTask(this, logger),
];

@override
Future<bool> run() async {
final args = argResults!;
Expand All @@ -61,14 +62,15 @@ class DockerCommand extends DfatCommand {
final String imageNameFallback = "${path.basename(rootDir)}-builder";
final String imageName = args['name'] ?? imageNameFallback;

_sequence = [];
var _sequence = <TaskCommand>[];
bool hasImage = Utils.dockerImageExists(imageName);
if (!hasImage || buildOnly || useForce) {
_sequence!.add(DockerBuildTask(this, logger));
_sequence.add(DockerBuildTask(this, logger));
}
if (!buildOnly) {
_sequence!.add(DockerRunTask(this, logger));
_sequence.add(DockerRunTask(this, logger));
}
useSequence(_sequence);
bool result = await runSequence({
DockerRunTask.taskName: {'name': imageName},
DockerBuildTask.taskName: {'name': imageName},
Expand Down
6 changes: 6 additions & 0 deletions dfat/bin/src/commands/install.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class InstallCommand extends DfatCommand {
);
}

@override
List<TaskCommand> revealTasks() => [
InstallDfatFilesTask(this, logger),
UpdateSchemasTask(this, logger),
];

@override
Future<bool> run() async {
logger.header("Install");
Expand Down
9 changes: 9 additions & 0 deletions dfat/bin/src/commands/lambda.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ class LambdaCommand extends DfatCommand {
);
}

@override
List<TaskCommand> revealTasks() => [
CleanDirTask(this, logger),
PubGetTask(this, logger),
DartTestTask(this, logger),
DartCompileTask(this, logger),
ZipArchiveTask(this, logger)
];

@override
Future<bool> run() async {
final footer = logger.header("Lambdas");
Expand Down
14 changes: 11 additions & 3 deletions dfat/bin/src/commands/shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:io';

import 'package:path/path.dart' as path;

import 'all.dart';
import 'tasks/all.dart';

class SharedCommand extends DfatCommand {
Expand All @@ -28,6 +27,15 @@ class SharedCommand extends DfatCommand {
);
}

@override
List<TaskCommand> revealTasks() => [
CleanDirTask(this, logger),
PubGetTask(this, logger),
BuildRunnerCleanTask(this, logger),
BuildRunnerBuildTask(this, logger),
DartTestTask(this, logger)
];

@override
Future<bool> run() async {
logger.header("Shared");
Expand All @@ -39,13 +47,13 @@ class SharedCommand extends DfatCommand {
useSequence([
CleanDirTask(this, logger),
PubGetTask(this, logger),
DartTestTask(this, logger),
...(isNoCacheSet
? [
BuildRunnerCleanTask(this, logger),
BuildRunnerBuildTask(this, logger)
]
: [BuildRunnerBuildTask(this, logger)])
: [BuildRunnerBuildTask(this, logger)]),
DartTestTask(this, logger),
]);

final result = await runSequence({
Expand Down
2 changes: 1 addition & 1 deletion dfat/bin/src/commands/tasks/dart_compile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DartCompileTask extends TaskCommand {
'💪 Compiling ${baseName.green()} → ${outputName?.green()}', ind);

if (!Directory(distPath).existsSync()) Directory(distPath).createSync();
final dartArgs = ['compile', 'exe', 'main.dart', '-o', relOutPath];
final dartArgs = ['compile', 'exe', 'lib/main.dart', '-o', relOutPath];
final pRes = Process.runSync('dart', dartArgs, workingDirectory: dirPath);

return Utils.handleProcessResult(pRes, logger, ' ', (code) {
Expand Down
123 changes: 105 additions & 18 deletions dfat/bin/src/commands/tasks/dart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ import 'base.dart';

class DartTestTask extends TaskCommand {
DartTestTask(DfatCommand parent, Logger logger)
: super(parent, logger, TaskRequirements());
: super(
parent,
logger,
TaskRequirements(
tools: [
'dart',
'collect_coverage',
'format_coverage',
'cobertura',
'genhtml'
],
));

static String taskName = 'dart-test';

Expand All @@ -18,6 +29,95 @@ class DartTestTask extends TaskCommand {

final inRs = ' ';

bool _runTests(bool useCoverage, String dirPath, String relOutPath) {
final usesCover = useCoverage && Utils.isCommand('collect_coverage');

final List<String> dartArgs = [
'run',
'test',
'--chain-stack-traces',
...(usesCover ? ['--coverage=coverage'] : []),
];
final tRes = Process.runSync('dart', dartArgs, workingDirectory: dirPath);

return Utils.handleProcessResult(tRes, logger, inRs + inRs);
}

bool _formatLcov(
bool useCoverage,
String baseName,
String ind,
String dirPath,
) {
final usesCover = useCoverage && Utils.isCommand('format_coverage');

// Early bail-out if there's nothing we can do here
if (!usesCover) return true;

logger.printFixed('🧪 Formatting ${baseName.green()} coverage', ind);
final List<String> formatArgs = [
'--packages=.packages',
'--base-directory=${path.normalize(dirPath)}',
'--report-on=lib',
'--lcov',
'-o',
'coverage/lcov.info',
'-i',
'coverage',
];
final fRes = Process.runSync(
'format_coverage',
formatArgs,
workingDirectory: dirPath,
);
return Utils.handleProcessResult(
fRes,
logger,
inRs + inRs,
null,
_makeBadge(dirPath).trim(),
);
}

bool _formatHtml(
bool useCoverage, String baseName, String ind, String dirPath) {
// Early bail-out if there's nothing we can do here
final hasGenHtml = Utils.isCommand('genhtml');
if (hasGenHtml) {
logger.printFixed('🧪 Marking up ${baseName.green()} coverage', ind);
final List<String> markupArgs = [
'-o',
'./coverage/report',
'./coverage/lcov.info',
];
final gRes =
Process.runSync('genhtml', markupArgs, workingDirectory: dirPath);
return Utils.handleProcessResult(
gRes,
logger,
inRs + inRs,
null,
'./$baseName/coverage/report/index.html',
);
}
return true;
}

String _makeBadge(String dirPath) {
final List<String> badgeArgs = [
'pub',
'global',
'run',
'cobertura',
'show',
'-b',
'-i',
'./coverage/lcov.info',
];
final bRes = Process.runSync('dart', badgeArgs, workingDirectory: dirPath);
return bRes.stdout;
}

@override
Future<bool> run() async {
final dirPath = targetDir;
Expand All @@ -44,23 +144,10 @@ class DartTestTask extends TaskCommand {
final useCoverage = args['coverage'] ?? true;
final relOutPath = '.dist/$outputName';

final usesCover = useCoverage &&
(Utils.getPubSpecValue(targetDir, 'dev_dependencies.coverage') ??
Utils.getPubSpecValue(targetDir, 'dependencies.coverage')) !=
null;

final List<String> dartArgs = [
'run',
'test',
'--chain-stack-traces',
...(usesCover ? ['--coverage="coverage"'] : []),
];
final pRes = Process.runSync('dart', dartArgs, workingDirectory: dirPath);
bool result = _runTests(useCoverage, dirPath, relOutPath);
if (result) result = _formatLcov(useCoverage, baseName, ind, dirPath);
if (result) result = _formatHtml(useCoverage, baseName, ind, dirPath);

return Utils.handleProcessResult(pRes, logger, ' ', (code) {
final outputFile = File(relOutPath);
final success = code == 0 && outputFile.existsSync();
if (success) Utils.chmod('+x', relOutPath);
});
return result;
}
}
Loading

0 comments on commit d2fb18f

Please sign in to comment.