-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement format, analyze and pub commands
- Loading branch information
Showing
5 changed files
with
271 additions
and
24 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
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,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()); | ||
} |
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
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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