flutter_wrapper icon indicating copy to clipboard operation
flutter_wrapper copied to clipboard

Document a clean way to disallow running the project without flutterw

Open neiljaywarner opened this issue 3 years ago • 5 comments

This is an awesome solution but the biggest challenge is onboarding/communication, especially with Intellij/Android studio users.

Please consider a good way to a) Push the flutterw usage to repo or similar like vscode, even if it's not quite as solid b) crash/close our app with a reasonable error message if not running with flutterw - ideally by checking pubspec.yaml and/or commit hash in flutterw or something but if nothing else via a darat define or env variable or command line parameter or something

neiljaywarner avatar Apr 11 '22 16:04 neiljaywarner

Good idea!

FYI: VScode is picking up the SDK in directory .flutter automatically

General - Inject information

The only way to inject information into flutter apps is via --dart-define. You could write a run.sh script to inject some build parameter and depending on that crash or not crash the app.

# run.sh

sh flutterw run --dart-define=USES_FLUTTERW=true "$@"

And then crash the app if that flag isn't present

final isUsingFlutterw = const String.fromEnvironment('USES_FLUTTERW') == 'true';

if (isDebug && !isUsingFlutterw) {
  throw "You're not using fluttew!";
}

Usage:

./run.sh --debug -d macos

Use sidekick

Because bash scripts are kind of hard to maintain, I migrated my apps to use sidekick. We use this command to inject some defaults (like the web renderer or some credentials).

import 'package:sidekick_core/sidekick_core.dart';

class RunCommand extends ForwardCommand {
  @override
  final String description =
      'Runs the application with the API credentials as well as the preset for chrome and the html renderer.';

  @override
  String get name => 'run';

  @override
  Future<void> run() async {
    print(green('Running the application...'));
    flutterw(
      [
        'run',
        '--dart-define=BACKEND_API_KEY=${vault.backendApiKey}',
        '--dart-define=USES_FLUTTERW=true',
        '--web-renderer=canvaskit',
        if (rest != null) ...argResults?.rest,
        mainProject.libDir.file('main.dart').absolute.path,
      ],
      workingDirectory: mainProject.root,
    );
  }
}

From there, the same as with the bash script:

final isUsingFlutterw = const String.fromEnvironment('USES_FLUTTERW') == 'true';

if (isDebug && !isUsingFlutterw) {
  throw "You're not using fluttew!";
}

Usage

<our-cli> run  --debug -d macos

passsy avatar Apr 11 '22 18:04 passsy

@passsy what is the "$@" for in the below

sh flutterw run --dart-define=USES_FLUTTERW=true "$@"

neiljaywarner avatar Apr 11 '22 20:04 neiljaywarner

nevermind, i see https://stackoverflow.com/questions/9994295/what-does-mean-in-a-shell-script

neiljaywarner avatar Apr 11 '22 20:04 neiljaywarner

@passsy this didn't work for me without the singlequotes around hte 'true'. of course, your promptness and quality of your reply was fantastic :)

neiljaywarner avatar Apr 11 '22 21:04 neiljaywarner

Sorry, did not run the code before posting 🎸

@passsy what is the "$@" for in the below

Q.E.D. That's why I switched to dart scripts instead of bash. Less magic ✨

passsy avatar Apr 11 '22 23:04 passsy