Skip to content

Commit

Permalink
implement format, analyze and pub commands
Browse files Browse the repository at this point in the history
  • Loading branch information
klavs committed Oct 10, 2020
1 parent 98ec0d5 commit 3afd053
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ uni pub global activate uni

#### Available commands
- [x] `uni doctor`: Show information about the installed tooling.
- [ ] `uni analyze`: Analyze the project's Dart code.
- [x] `uni analyze`: Analyze the project's Dart code.
- [ ] `uni compile`: Compile Dart to various formats.
- [ ] `uni create`: Create a new project.
- [ ] `uni format`: Idiomatically format Dart source code.
- [x] `uni format`: Idiomatically format Dart source code.
- [ ] `uni migrate`: Perform a null safety migration on a project or package.
- [ ] `uni pub`: Work with packages.
- [x] `uni pub`: Work with packages.
- [ ] `uni run`: Run the program.
- [ ] `uni test`: Run tests in this package.

Expand Down
77 changes: 56 additions & 21 deletions lib/src/commands/commands.dart
Original file line number Diff line number Diff line change
@@ -1,42 +1,77 @@
import "dart:io";

import "package:args/args.dart";
import "package:args/command_runner.dart";
import "package:path/path.dart" as path;
import "package:pubspec/pubspec.dart";
import "package:uni/src/context.dart";
import "package:uni/src/tooling.dart";
import "package:uni/src/uni_commands.dart" as uni;

Future<Context> getContext(Directory dir) async {
if (dir.path == dir.parent.path) {
return DartContext();
}

final pubspecPath = path.join(dir.path, "pubspec.yaml");

if (await File(pubspecPath).exists()) {
final pubspec = await PubSpec.loadFile(pubspecPath);

return pubspec.allDependencies.containsKey("flutter")
? FlutterContext()
: DartContext();
}

return getContext(dir.parent);
}

abstract class PassthroughCommand extends Command<void> {
final uni.UniCommand uniCommand;

@override
final String name;
String get name => uniCommand.name;

@override
final String description;
String get description => uniCommand.description;

@override
ArgParser argParser = ArgParser.allowAnything();

PassthroughCommand({
this.name,
this.description,
});
PassthroughCommand(this.uniCommand);

@override
Future<void> run() async {
final context = await getContext(Directory.current);

final tooling = Tooling.fromName(
globalResults["tooling"] as String,
);

final conf = await tooling.getToolFor(
uniCommand,
context,
);

await conf.tool.run(
[
...conf.args,
...argResults.arguments,
],
stdout: stdout,
stderr: stderr,
);
}
}

class PubCommand extends PassthroughCommand {
PubCommand()
: super(
name: "pub",
description: "Work with packages.",
);
PubCommand() : super(const uni.PubCommand());
}

class AnalyzeCommand extends PassthroughCommand {
AnalyzeCommand()
: super(
name: "analyze",
description: "Analyze the project's Dart code.",
);
AnalyzeCommand() : super(const uni.AnalyzeCommand());
}

class FormatCommand extends PassthroughCommand {
FormatCommand()
: super(
name: "format",
description: "Idiomatically format Dart source code.",
);
FormatCommand() : super(const uni.FormatCommand());
}
129 changes: 129 additions & 0 deletions lib/src/tooling.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import "package:pub_semver/pub_semver.dart";
import "package:uni/src/context.dart";
import "package:uni/src/tool.dart";
import "package:uni/src/tools/dart_tool.dart";
import "package:uni/src/tools/dartanalyzer_tool.dart";
import "package:uni/src/tools/dartdoc_tool.dart";
import "package:uni/src/tools/dartfmt_tool.dart";
import "package:uni/src/tools/flutter_tool.dart";
import "package:uni/src/tools/pub_tool.dart";
import "package:uni/src/uni_commands.dart";

class ToolConfig {
final Tool tool;
final List<String> args;

const ToolConfig({
this.tool,
this.args,
});
}

abstract class Tooling {
final String name;
Expand Down Expand Up @@ -36,6 +49,11 @@ abstract class Tooling {
}

Future<bool> isSupported();

Future<ToolConfig> getToolFor(
UniCommand command,
Context context,
);
}

class AdaptiveTooling extends Tooling {
Expand All @@ -53,6 +71,50 @@ class AdaptiveTooling extends Tooling {
]).any(
(supported) => supported,
);

@override
Future<ToolConfig> getToolFor(
UniCommand command,
Context context,
) async {
if (context is FlutterContext) {
if (await const FlutterTooling().isSupported()) {
return const FlutterTooling().getToolFor(
command,
context,
);
}

return null;
}

if (context is DartContext) {
if (await const DartTooling().isSupported()) {
return const DartTooling().getToolFor(
command,
context,
);
}

if (await const LegacyTooling().isSupported()) {
return const LegacyTooling().getToolFor(
command,
context,
);
}

if (await const FlutterTooling().isSupported()) {
return const FlutterTooling().getToolFor(
command,
context,
);
}

return null;
}

return null;
}
}

class DartTooling extends Tooling {
Expand All @@ -68,6 +130,23 @@ class DartTooling extends Tooling {
VersionConstraint.parse("^2.10.0").allows(
await const DartTool().getVersion(),
);

@override
Future<ToolConfig> getToolFor(
UniCommand command,
Context context,
) async {
if (context is DartContext) {
const dartTool = DartTool();

return ToolConfig(
tool: dartTool,
args: command.getArgsFor(dartTool),
);
}

return null;
}
}

class FlutterTooling extends Tooling {
Expand All @@ -79,6 +158,19 @@ class FlutterTooling extends Tooling {

@override
Future<bool> isSupported() async => const FlutterTool().isAvailable();

@override
Future<ToolConfig> getToolFor(
UniCommand command,
Context context,
) async {
const flutterTool = FlutterTool();

return ToolConfig(
tool: flutterTool,
args: command.getArgsFor(flutterTool),
);
}
}

class LegacyTooling extends Tooling {
Expand All @@ -97,4 +189,41 @@ class LegacyTooling extends Tooling {
].every(
(tool) => tool.isAvailable(),
);

@override
Future<ToolConfig> getToolFor(
UniCommand command,
Context context,
) async {
if (context is DartContext) {
if (command is PubCommand) {
const pubTool = PubTool();

return ToolConfig(
tool: pubTool,
args: command.getArgsFor(pubTool),
);
}

if (command is FormatCommand) {
const dartFmtTool = DartFmtTool();

return ToolConfig(
tool: dartFmtTool,
args: command.getArgsFor(dartFmtTool),
);
}

if (command is AnalyzeCommand) {
const dartAnalyzerTool = DartAnalyzerTool();

return ToolConfig(
tool: dartAnalyzerTool,
args: command.getArgsFor(dartAnalyzerTool),
);
}
}

return null;
}
}
81 changes: 81 additions & 0 deletions lib/src/uni_commands.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import "package:uni/src/tool.dart";
import "package:uni/src/tools/dart_tool.dart";
import "package:uni/src/tools/dartanalyzer_tool.dart";
import "package:uni/src/tools/dartfmt_tool.dart";
import "package:uni/src/tools/flutter_tool.dart";
import "package:uni/src/tools/pub_tool.dart";

abstract class UniCommand {
final String name;
final String description;

const UniCommand({
this.name,
this.description,
});

List<String> getArgsFor(Tool tool);
}

class PubCommand extends UniCommand {
const PubCommand()
: super(
name: "pub",
description: "Work with packages.",
);

@override
List<String> getArgsFor(Tool tool) {
if (tool is DartTool || tool is FlutterTool) {
return ["pub"];
}

if (tool is PubTool) {
return [];
}

return null;
}
}

class AnalyzeCommand extends UniCommand {
const AnalyzeCommand()
: super(
name: "analyze",
description: "Analyze the project's Dart code.",
);

@override
List<String> getArgsFor(Tool tool) {
if (tool is DartTool || tool is FlutterTool) {
return ["analyze"];
}

if (tool is DartAnalyzerTool) {
return [];
}

return null;
}
}

class FormatCommand extends UniCommand {
const FormatCommand()
: super(
name: "format",
description: "Idiomatically format Dart source code.",
);

@override
List<String> getArgsFor(Tool tool) {
if (tool is DartTool || tool is FlutterTool) {
return ["format"];
}

if (tool is DartFmtTool) {
return [];
}

return null;
}
}
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ dependencies:
directed_graph: ^0.2.3
meta: ^1.0.0
process_run: ^0.11.0
path: ^1.6.2
pub_semver: ^1.4.4
pubspec: ^0.1.2
dev_dependencies:
gql_pedantic: ^1.0.1
executables:
Expand Down

0 comments on commit 3afd053

Please sign in to comment.