Files
sdk/pkg/front_end/tool/format_cl.dart
T
Jens Johansen 6995dc4447 [CFE] format_cl helper til also formats uncomitted changes
Compiling like so:

```
$ out/ReleaseX64/dart-sdk/bin/dart compile exe pkg/front_end/tool/format_cl.dart -o ~/bin/format_cl
```

I can format my CL with

```
$ format_cl
```

This isn't new. What's changed is that it now also includes the
not-yet-committed changes.
Previously my workflow has turned out to be format, committing,
then formatting again, then committing again because the files
that I hadn't yet committed wasn't formated. They will be now.

Change-Id: I30c30d7cfa6b885cf9f80bb1a6aa3022e898fd58
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/378420
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2024-08-01 09:53:18 +00:00

108 lines
3.0 KiB
Dart

// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:convert' show LineSplitter, utf8;
import 'dart:developer' as dev show NativeRuntime;
import 'dart:io';
import '../presubmit_helper.dart' show getChangedFiles;
import '../test/utils/io_utils.dart';
Future<void> main() async {
Uri executable = getDartExecutable();
final List<String> allChangedFiles =
getChangedFiles(collectUncommitted: true);
if (allChangedFiles.isEmpty) {
print("No changes in CL.");
return;
}
final List<String> changedDartFiles = [];
for (String changedFile in allChangedFiles) {
if (changedFile.toLowerCase().endsWith(".dart")) {
changedDartFiles.add(changedFile);
}
}
if (changedDartFiles.isEmpty) {
print("No changed dart files in CL.");
return;
}
Process p = await Process.start(
executable.toFilePath(), ["format", ...changedDartFiles]);
p.stderr
.transform(utf8.decoder)
.transform(const LineSplitter())
.listen((String line) {
stderr.writeln("stderr> $line");
});
p.stdout
.transform(utf8.decoder)
.transform(const LineSplitter())
.listen((String line) {
stdout.writeln("stdout> $line");
});
exitCode = await p.exitCode;
}
Uri getDartExecutable() {
Uri executable = Uri.base.resolve(Platform.resolvedExecutable);
if (executable == Platform.script || dev.NativeRuntime.buildId != null) {
// Probably aot compiled. We need to find "dart" another way.
bool found = false;
try {
final Uri repoDir = computeRepoDirUri();
for (String candidatePath in [
"out/ReleaseX64/dart",
"out/ReleaseX64/dart-sdk/bin/dart",
"out/ReleaseX64/dart.exe",
"out/ReleaseX64/dart-sdk/bin/dart.exe",
"xcodebuild/ReleaseX64/dart",
"xcodebuild/ReleaseX64/dart-sdk/bin/dart",
]) {
Uri candidate = repoDir.resolve(candidatePath);
if (File.fromUri(candidate).existsSync()) {
executable = candidate;
found = true;
break;
}
}
} catch (e) {
print("Warning: $e");
}
if (!found) {
Uri? candidate = where("dart");
if (candidate != null) {
executable = candidate;
} else {
throw "Couldn't find a dart executable to use.";
}
}
print("Using $executable");
}
return executable;
}
Uri? where(String needle) {
String pathEnvironment = Platform.environment["PATH"] ?? '';
List<String> paths;
if (Platform.isWindows) {
paths = pathEnvironment.split(";");
} else {
paths = pathEnvironment.split(":");
}
// This isn't great but will probably work for our purposes.
List<String> extensions = ["", ".exe", ".bat", ".com"];
for (String path in paths) {
for (String extension in extensions) {
File f = new File("$path${Platform.pathSeparator}$needle$extension");
if (f.existsSync()) {
return f.uri;
}
}
}
return null;
}