1

Is it possible to pass some argument to a dart application when running it using pub serve? What I'm trying to do is have an application use some mocked services while I'm developing it, but then when it's deployed I'd like to replace mocked services with real ones. For example:

const bool DEBUG = true;

class AppModule extends Module {
  AppModule() {
    type(PaymentService, implementedBy: DEBUG ? PaypalPaymentService : MockPaymentService );
  }
}

I'd like this DEBUG parameter to somehow come form the environment and to be easily configurable when running the application using pub serve. Which is the best way to achieve this?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
markovuksanovic
  • 15,676
  • 13
  • 46
  • 57

1 Answers1

1

You could check the URL. If host is 127.0.0.1 your in the development environment, otherwise it's prod.

Another idea is to use a transformer that injects something when mode is debug. Not sure if that is really possible this way.

$ pub help serve
Run a local web development server.

Usage: pub serve
-h, --help               Print usage information for this command.
    --port               The port to listen on.
                         (defaults to "8080")

    --[no-]dart2js       Compile Dart to JavaScript.
                         (defaults to on)

    --[no-]force-poll    Force the use of a polling filesystem watcher.
    --mode               Mode to run transformers in.
                         (defaults to "debug")
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • URL won't work as I could be running the application in the mode where real services are used even on localhost. Transformers might work but I can't find an example of how to write one. Any pointers? The documentation on pub does not show how to write one, only how to use it. – markovuksanovic Jan 18 '14 at 23:56
  • There was a discussion recently that a documentation should be written how to build a transformer but haven't seen one yet. (https://groups.google.com/a/dartlang.org/forum/#!topic/misc/d4jrnPzFV2w) – Günter Zöchbauer Jan 18 '14 at 23:57
  • Also when I run pub serve how would I specify if this is a 'production' mode or 'debug'? I was looking for something like `pub serve --mode=debug`. – markovuksanovic Jan 19 '14 at 00:20
  • I added the output from `pub help serve` to my answer. `--mode=debug` is default. – Günter Zöchbauer Jan 19 '14 at 00:23
  • Oh... sorry, I didn't realize that pub already has a flag named 'mode'. I was looking for passing a custom flag (coincidentally I used the name mode which is already exists). Maybe a better example would be `pub serve --app_mode=debug'. I'm thinking of setting a meta tag in html and then reading that to set the mode... Then I could use transformer to actually change the value, during deploy process, if needed.... – markovuksanovic Jan 19 '14 at 00:30
  • I find the existing `mode` parameter convenient as it defaults to `debug` for `pub serve` and `release` for `pub build` so you don't have to bother setting a value. But of course in your use case other options may fit better. – Günter Zöchbauer Jan 19 '14 at 00:33