Skip to content

Commit

Permalink
FIX: Fix last compiler issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gamebox committed Nov 8, 2024
1 parent 052ff02 commit d5a327d
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 127 deletions.
4 changes: 3 additions & 1 deletion bin/a_command.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "log_level.dart";

abstract class ACommand {
Future<bool> exec();
Future<bool> exec(LoggerImpl logger);
}
47 changes: 28 additions & 19 deletions bin/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'a_command.dart';
import 'compare.dart';
import 'compile.dart';
import 'init.dart';
import 'log_level.dart';

const String cHelp = 'help';
const String cVersion = 'version';
Expand All @@ -15,18 +16,18 @@ const String cInit = 'init';

ArgParser _buildParser() {
return ArgParser()
..addOption(
'logLevel',
abbr: 'l',
mandatory: false,
help: 'One of: debug, info, warn, error, fatal, quiet',
defaultsTo: "",
)
..addCommand(cHelp, ArgParser(allowTrailingOptions: true))
..addCommand(cVersion, ArgParser(allowTrailingOptions: false))
..addCommand(
cCompile,
ArgParser(allowTrailingOptions: true)
..addOption(
'logLevel',
abbr: 'l',
mandatory: false,
help: 'One of: debug, info, warn, error, fatal, quiet',
defaultsTo: "",
)
..addOption(
'dir',
abbr: 'd',
Expand Down Expand Up @@ -69,7 +70,7 @@ class HelpCommand implements ACommand {
HelpCommand({required this.parser, required this.commandUsage});

@override
Future<bool> exec() async {
Future<bool> exec(LoggerImpl _) async {
print('Usage: itch <command> [flags] [arguments]');
print("Commands");
print("========");
Expand Down Expand Up @@ -97,7 +98,7 @@ class VersionCommand implements ACommand {
VersionCommand({required this.version});

@override
Future<bool> exec() async {
Future<bool> exec(LoggerImpl _) async {
print("Itch version $version");
return true;
}
Expand All @@ -110,26 +111,34 @@ Map<String, String> commandUsage = {
cInit: "Start a new project in a new directory with a given name",
};

ACommand getCommand(Iterable<String> args, String currentVersion) {
LoggerImpl getLogger(ArgResults results) {
return LoggerImpl(level: results.option("logLevel") ?? "");
}

(ACommand, LoggerImpl) getCommand(
Iterable<String> args, String currentVersion) {
final p = _buildParser();
final result = p.parse(args);
final logger = getLogger(result);
switch (result.command?.name) {
case cVersion:
return VersionCommand(version: currentVersion);
return (VersionCommand(version: currentVersion), logger);
case cCompile:
final logLevel = result.command!.option("logLevel");
return CompileCommand(
final c = CompileCommand(
dir: result.command!.option('dir') ?? Directory('.').path,
logLevel: logLevel ?? "",
);
return (c, logger);
case cCompare:
return CompareCommand(dir: result.command!.option('dir')!);
return (CompareCommand(dir: result.command!.option('dir')!), logger);
case cInit:
return InitCommand(
projectName: result.command!.option('name')!,
dir: result.command!.option('dir')!,
return (
InitCommand(
projectName: result.command!.option('name')!,
dir: result.command!.option('dir')!,
),
logger
);
default:
return HelpCommand(parser: p, commandUsage: commandUsage);
return (HelpCommand(parser: p, commandUsage: commandUsage), logger);
}
}
3 changes: 2 additions & 1 deletion bin/compare.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:io';
import 'a_command.dart';
import 'log_level.dart';

bool compareList(dynamic fixture, dynamic built,
bool Function(dynamic, dynamic) elementCompare) {
Expand Down Expand Up @@ -186,7 +187,7 @@ class CompareCommand implements ACommand {
CompareCommand({required this.dir});

@override
Future<bool> exec() {
Future<bool> exec(LoggerImpl _) {
return runCompare(dir);
}
}
42 changes: 23 additions & 19 deletions bin/compile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ extension WhitespaceChecked on String {
}
}

class CompileCommand extends Logger implements ACommand {
class CompileCommand implements ACommand {
String dir;
String logLevel;
late LoggerImpl _logger;

CompileCommand({required this.dir, required this.logLevel}) {
super.setLogLevel('info');
}
CompileCommand({required this.dir});

@override
Future<bool> exec() async {
Future<bool> exec(LoggerImpl logger) async {
_logger = logger;
try {
runCompile(dir);
return true;
Expand All @@ -59,8 +58,8 @@ class CompileCommand extends Logger implements ACommand {
print("Compiling $filepath");
final file = File(filepath);
final contents = await file.readAsString();
final parser = Parser(contents: contents, fileName: filepath);
parser.setLogLevel(logLevel);
final parser =
Parser(contents: contents, fileName: filepath, logger: _logger);
final parseResult = parser.parse();
final parseErrors = parser.errors();
if (parseResult == null || parseErrors.isNotEmpty) {
Expand All @@ -70,15 +69,14 @@ class CompileCommand extends Logger implements ACommand {
}
return null;
} else {
print("Success");
return parseResult;
}
}

Future<String?> getProjectName() async {
final projectFile = File.fromUri(Uri.file("$dir/project.itch"));
if (!await projectFile.exists()) {
warn("No project file found at: ${projectFile.path}");
_logger.warn("No project file found at: ${projectFile.path}");
return null;
}
final contents = await projectFile.readAsString();
Expand All @@ -87,7 +85,7 @@ class CompileCommand extends Logger implements ACommand {
final matches =
RegExp(r"project ([a-zA-Z0-9'_-\s]+).").matchAsPrefix(line);
if (matches == null) {
error("""
_logger.error("""
Invalid project file:
Expected something like
Expand All @@ -107,7 +105,7 @@ $line"""
if (line.startsWith("#") || line.isWhitespace()) {
continue;
}
error("""
_logger.error("""
Unexpected content in project file:
Got
Expand All @@ -117,12 +115,13 @@ $line
But I don't know what that is...."""
.trimLeft());
}
info("Never found a valid project file or name, defaulting to 'Project'");
_logger.info(
"Never found a valid project file or name, defaulting to 'Project'");
return null;
}

Future<void> runCompile(String path) async {
await loadBlockDefs();
await loadBlockDefs(_logger);
final entries = await Directory(path)
.list(recursive: true)
.where(
Expand All @@ -145,9 +144,9 @@ But I don't know what that is...."""
}
if (error) {
print("Could not compile project due to above errors.");
return;
}
final g = gen.Generator();
g.setLogLevel(logLevel);
final g = gen.Generator(logger: _logger);
final project = g.generate(files, path);
if (project == null) {
print("ERROR");
Expand All @@ -171,9 +170,10 @@ But I don't know what that is...."""
final projectName = await getProjectName() ?? 'Project';
// clean old resources
print("Cleaning up");
final projectFilename = fileEncode(projectName);
try {
await File.fromUri(Uri.file("out.zip")).delete();
await File.fromUri(Uri.file("$projectName.sb3")).delete();
await File.fromUri(Uri.file("$projectFilename.sb3")).delete();
} on PathNotFoundException catch (_) {
// Not handling
}
Expand All @@ -186,13 +186,17 @@ But I don't know what that is...."""
return;
}
// move zip to sb3
final p2 = await Process.run("mv", ["out.zip", "$projectName.sb3"]);
final p2 = await Process.run("mv", ["out.zip", "$projectFilename.sb3"]);
if (p2.exitCode != 0) {
print("Could not create project file");
print(p.stdout);
return;
}
print("Project file created: $projectName.sb3");
print("Project file created: $projectFilename.sb3");
return;
}
}

String fileEncode(String projectName) {
return projectName.toLowerCase().replaceAll(RegExp(r"\W"), "_");
}
8 changes: 4 additions & 4 deletions bin/decoders/images.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ ImageInfo? _decodePng(List<int> imageData) {
.toList());
final md5 = crypto.md5.convert(imageData).toString();
return ImageInfo(
centerX: (height / 2).ceil(),
centerY: (width / 2).ceil(),
centerX: (width / 2).ceil(),
centerY: (height / 2).ceil(),
md5: md5,
ext: 'png',
);
Expand All @@ -89,8 +89,8 @@ ImageInfo? _decodeSvg(List<int> imageData) {
0;
final md5 = crypto.md5.convert(imageData).toString();
return ImageInfo(
centerX: (height / 2).ceil(),
centerY: (width / 2).ceil(),
centerX: (width / 2).ceil(),
centerY: (height / 2).ceil(),
md5: md5,
ext: 'svg',
);
Expand Down
Loading

0 comments on commit d5a327d

Please sign in to comment.