Skip to content

Commit

Permalink
Prefer single quotes in Dart
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed Sep 13, 2024
1 parent 50abced commit f867738
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 134 deletions.
4 changes: 4 additions & 0 deletions flutter_package/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
include: package:lints/core.yaml

linter:
rules:
- prefer_single_quotes
50 changes: 25 additions & 25 deletions flutter_package/bin/rinf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,51 @@ import 'src/internet.dart';

Future<void> main(List<String> args) async {
if (args.isEmpty) {
print("No operation is provided.");
print("Use `rinf --help` to see all available operations.");
print('No operation is provided.');
print('Use `rinf --help` to see all available operations.');
return;
}

final rinfConfig = await loadVerifiedRinfConfig("pubspec.yaml");
final rinfConfig = await loadVerifiedRinfConfig('pubspec.yaml');
await checkConnectivity();

switch (args[0]) {
case "config":
case 'config':
print(rinfConfig);
break;
case "template":
case 'template':
await applyRustTemplate(messageConfig: rinfConfig.message);
break;
case "message":
if (args.contains("--watch") || args.contains("-w")) {
case 'message':
if (args.contains('--watch') || args.contains('-w')) {
await watchAndGenerateMessageCode(messageConfig: rinfConfig.message);
} else {
await generateMessageCode(messageConfig: rinfConfig.message);
}
break;
case "wasm":
if (args.contains("--release") || args.contains("-r")) {
case 'wasm':
if (args.contains('--release') || args.contains('-r')) {
await buildWebassembly(isReleaseMode: true);
} else {
await buildWebassembly();
}
break;
case "--help":
case "-h":
print("Usage: rinf [arguments]");
print("Arguments:");
print(" -h, --help Shows this usage information.");
print(" config Shows current Rinf configuration"
"\n resolved from `pubspec.yaml`"
"\n with defaults applied.");
print(" template Applies Rust template"
"\n to current Flutter project.");
print(" message Generates message code from `.proto` files.");
print(" -w, --watch Continuously watches `.proto` files.");
print(" wasm Builds webassembly module.");
print(" -r, --release Builds in release mode.");
case '--help':
case '-h':
print('Usage: rinf [arguments]');
print('Arguments:');
print(' -h, --help Shows this usage information.');
print(' config Shows current Rinf configuration'
'\n resolved from `pubspec.yaml`'
'\n with defaults applied.');
print(' template Applies Rust template'
'\n to current Flutter project.');
print(' message Generates message code from `.proto` files.');
print(' -w, --watch Continuously watches `.proto` files.');
print(' wasm Builds webassembly module.');
print(' -r, --release Builds in release mode.');
default:
print("No such operation is available.");
print("Use `rinf --help` to see all available operations.");
print('No such operation is available.');
print('Use `rinf --help` to see all available operations.');
}
}
2 changes: 1 addition & 1 deletion flutter_package/bin/src/common.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extension UriJoin on Uri {
Uri join(String path) {
if (path.isEmpty || path == "/") {
if (path.isEmpty || path == '/') {
// By default, `resolve` method returns root directory
// when provided string is empty or a slash.
// We need to override this.
Expand Down
20 changes: 10 additions & 10 deletions flutter_package/bin/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RinfConfigMessage {
if (!MESSAGE_CONFIG_KEYS.contains(key)) {
throw Exception(
"Unknown key '$key' in rinf message configuration.\n"
"Available keys are: $MESSAGE_CONFIG_KEYS",
'Available keys are: $MESSAGE_CONFIG_KEYS',
);
}
}
Expand All @@ -49,14 +49,14 @@ class RinfConfigMessage {
$KEY_RUST_SERDE: $rustSerde''';
}

static const KEY_INPUT_DIR = "input_dir";
static const KEY_RUST_OUTPUT_DIR = "rust_output_dir";
static const KEY_DART_OUTPUT_DIR = "dart_output_dir";
static const KEY_RUST_SERDE = "rust_serde";
static const KEY_INPUT_DIR = 'input_dir';
static const KEY_RUST_OUTPUT_DIR = 'rust_output_dir';
static const KEY_DART_OUTPUT_DIR = 'dart_output_dir';
static const KEY_RUST_SERDE = 'rust_serde';

static const DEFAULT_INPUT_DIR = "messages/";
static const DEFAULT_RUST_OUTPUT_DIR = "native/hub/src/messages/";
static const DEFAULT_DART_OUTPUT_DIR = "lib/messages/";
static const DEFAULT_INPUT_DIR = 'messages/';
static const DEFAULT_RUST_OUTPUT_DIR = 'native/hub/src/messages/';
static const DEFAULT_DART_OUTPUT_DIR = 'lib/messages/';
static const DEFAULT_RUST_SERDE = false;

static const MESSAGE_CONFIG_KEYS = [
Expand All @@ -81,7 +81,7 @@ class RinfConfig {
if (!RINF_CONFIG_KEYS.contains(key)) {
throw Exception(
"Unknown key '$key' in rinf configuration.\n"
"Available keys are: $RINF_CONFIG_KEYS",
'Available keys are: $RINF_CONFIG_KEYS',
);
}
}
Expand All @@ -100,7 +100,7 @@ rinf:
$message''';
}

static const KEY_MESSAGE = "message";
static const KEY_MESSAGE = 'message';
static const RINF_CONFIG_KEYS = [KEY_MESSAGE];
}

Expand Down
96 changes: 48 additions & 48 deletions flutter_package/bin/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ Future<void> applyRustTemplate({
final packagePath = package.root;

// Check if current folder is a Flutter app project.
final specFile = File.fromUri(flutterProjectPath.join("pubspec.yaml"));
final specFile = File.fromUri(flutterProjectPath.join('pubspec.yaml'));
final isFlutterProject = await specFile.exists();
if (!isFlutterProject) {
print("This folder doesn't look like a Flutter project.");
return;
}
final pubspec = loadYaml(await specFile.readAsString());
final String? publishTo = pubspec['publish_to'];
if (publishTo != "none") {
print("Flutter package development is not supported by Rinf.");
if (publishTo != 'none') {
print('Flutter package development is not supported by Rinf.');
return;
}

// Copy basic folders needed for Rust to work
final templateSource = packagePath.join("template/native/");
final templateDestination = flutterProjectPath.join("native/");
final templateSource = packagePath.join('template/native/');
final templateDestination = flutterProjectPath.join('native/');
await copyDirectory(templateSource, templateDestination);
final messagesSource = packagePath.join("template/messages/");
final messagesDestination = flutterProjectPath.join("messages/");
final messagesSource = packagePath.join('template/messages/');
final messagesDestination = flutterProjectPath.join('messages/');
await copyDirectory(messagesSource, messagesDestination);

// Create workspace `Cargo.toml`
Expand Down Expand Up @@ -93,7 +93,7 @@ resolver = "2"
var readmeSplitted = readmeContent.split('\n\n');
readmeSplitted = readmeSplitted.map((s) => s.trim()).toList();
if (!readmeContent.contains(guideSectionTitle)) {
final text = '''
final text = """
$guideSectionTitle
This project leverages Flutter for GUI and Rust for the backend logic,
Expand Down Expand Up @@ -135,7 +135,7 @@ flutter run
For detailed instructions on writing Rust and Flutter together,
please refer to Rinf's [documentation](https://rinf.cunarist.com).
''';
""";
readmeSplitted.add(text);
}
await readmeFile.writeAsString(readmeSplitted.join('\n\n') + '\n');
Expand All @@ -144,12 +144,12 @@ please refer to Rinf's [documentation](https://rinf.cunarist.com).
await Process.run('dart', ['pub', 'add', 'protobuf']);

// Modify `./lib/main.dart`
final mainFile = File.fromUri(flutterProjectPath.join("lib/main.dart"));
final mainFile = File.fromUri(flutterProjectPath.join('lib/main.dart'));
if (await mainFile.exists()) {
await Process.run('dart', ['format', './lib/main.dart']);
var mainText = await mainFile.readAsString();
if (!mainText.contains('messages/exports.dart')) {
final lines = mainText.split("\n");
final lines = mainText.split('\n');
final lastImportIndex = lines.lastIndexWhere(
(line) => line.startsWith('import '),
);
Expand All @@ -161,7 +161,7 @@ please refer to Rinf's [documentation](https://rinf.cunarist.com).
lastImportIndex + 2,
"import './messages/exports.dart';",
);
mainText = lines.join("\n");
mainText = lines.join('\n');
}
if (!mainText.contains('initializeRust(assignRustSignal)')) {
mainText = mainText.replaceFirst(
Expand All @@ -179,7 +179,7 @@ please refer to Rinf's [documentation](https://rinf.cunarist.com).

await generateMessageCode(silent: true, messageConfig: messageConfig);

print("🎉 Rust template is now ready! 🎉");
print('🎉 Rust template is now ready! 🎉');
}

Future<void> copyDirectory(Uri source, Uri destination) async {
Expand All @@ -204,38 +204,38 @@ Future<void> copyDirectory(Uri source, Uri destination) async {
Future<void> buildWebassembly({bool isReleaseMode = false}) async {
// Ensure Rust toolchain.
if (isInternetConnected) {
print("Ensuring Rust toolchain for the web." +
"\nThis is done by installing it globally on the system.");
print('Ensuring Rust toolchain for the web.' +
'\nThis is done by installing it globally on the system.');
final processResults = <ProcessResult>[];
processResults.add(await Process.run("rustup", [
"toolchain",
"install",
"nightly",
processResults.add(await Process.run('rustup', [
'toolchain',
'install',
'nightly',
]));
processResults.add(await Process.run("rustup", [
"+nightly",
"component",
"add",
"rust-src",
processResults.add(await Process.run('rustup', [
'+nightly',
'component',
'add',
'rust-src',
]));
processResults.add(await Process.run("rustup", [
"+nightly",
"target",
"add",
"wasm32-unknown-unknown",
processResults.add(await Process.run('rustup', [
'+nightly',
'target',
'add',
'wasm32-unknown-unknown',
])); // For actual compilation
processResults.add(await Process.run("rustup", [
"target",
"add",
"wasm32-unknown-unknown",
processResults.add(await Process.run('rustup', [
'target',
'add',
'wasm32-unknown-unknown',
])); // For Rust-analyzer
processResults.add(await Process.run("cargo", [
"install",
"wasm-pack",
processResults.add(await Process.run('cargo', [
'install',
'wasm-pack',
]));
processResults.add(await Process.run("cargo", [
"install",
"wasm-bindgen-cli",
processResults.add(await Process.run('cargo', [
'install',
'wasm-bindgen-cli',
]));
processResults.forEach((processResult) {
if (processResult.exitCode != 0) {
Expand All @@ -244,7 +244,7 @@ Future<void> buildWebassembly({bool isReleaseMode = false}) async {
}
});
} else {
print("Skipping ensurement of Rust toolchain for the web.");
print('Skipping ensurement of Rust toolchain for the web.');
}

// Prepare the webassembly output path.
Expand All @@ -253,7 +253,7 @@ Future<void> buildWebassembly({bool isReleaseMode = false}) async {
final outputPath = flutterProjectPath.uri.join(subPath);

// Build the webassembly module.
print("Compiling Rust with `wasm-pack` to `web` target...");
print('Compiling Rust with `wasm-pack` to `web` target...');
final compileCommand = await Process.run(
'wasm-pack',
[
Expand All @@ -278,35 +278,35 @@ Future<void> buildWebassembly({bool isReleaseMode = false}) async {
print(compileCommand.stderr.toString().trim());
throw Exception('Unable to compile Rust into webassembly');
}
print("Saved `.wasm` and `.js` files to `$subPath`.");
print('Saved `.wasm` and `.js` files to `$subPath`.');

// Guide the developer how to run Flutter web server with web headers.
print("To run the Flutter web server, use:");
print('To run the Flutter web server, use:');
final commandLineDivider = await getCommandLineDivider();
final commandLines = [
'flutter run',
'--web-header=Cross-Origin-Opener-Policy=same-origin',
'--web-header=Cross-Origin-Embedder-Policy=require-corp'
];
print(commandLines.join(" ${commandLineDivider}\n"));
print(commandLines.join(' ${commandLineDivider}\n'));

print("🎉 Webassembly module is now ready! 🎉");
print('🎉 Webassembly module is now ready! 🎉');
}

Future<String> getCommandLineDivider({bool isReleaseMode = false}) async {
if (Platform.isWindows) {
// Windows environment, check further for PowerShell or CMD
if (Platform.environment['SHELL'] == null) {
// Likely PowerShell environment
return "`";
return '`';
// // Likely Command Prompt (cmd.exe)
// return "^";
} else {
// Bash or some other shell
return "\\";
return '\\';
}
} else {
// Bash or some other shell
return "\\";
return '\\';
}
}
Loading

0 comments on commit f867738

Please sign in to comment.