Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run a polymer app via "Run in Dartium" from console?

I want to call a polymer webapp directly via command line or via 'Process' in a dart file.

I know when running it via the dart editor, a server on port 8080 is created and listening to requests for the /web folder.

but when launching

dartium/chrome.exe path/To/Index.html

from console its simply loading the files inside the browser but wont start a server for the client.

via file:://path/to/file.html [no 'dart is not runnning' warning, but no polymer content] or 127.x.x.x.x.x.x.x.x:xxxxxxxx/app/index.html will obviously tell me

'This webpage is not available'

like image 645
H.R. Avatar asked Mar 19 '23 02:03

H.R.


2 Answers

DartEditor lauches pub serve. You can do this manually without Darteditor (since Dart 1.5 AFAIK). Just launch

pub serve

from within your Polymer app package directory.
Inside your console app launch the browser with the URL that loads the page from this server.

You could also include web server functionality into your console application that serves the Polymer app to your browser.

pub help serve

lists the available options.

like image 173
Günter Zöchbauer Avatar answered Mar 23 '23 17:03

Günter Zöchbauer


You can try this script as an example how to call a polymer webapp directly via 'Process' in a dart file.

This example also includes launch of default browser.

import "dart:async";
import "dart:io";
import "package:path/path.dart" as pathos;

void main(List<String> args) {
  String app;
  String file;
  switch (args.length) {
    case 1:
      app = args[0];
      break;
    case 2:
      app = args[0];
      file = args[1];
      break;
    default:
      print("Usage: pubserve.dart app_path [file_name]");
      exit(0);
  }

  if(!new Directory(app).existsSync()) {
    print("Directory not exists: $app");
    exit(-1);
  }

  pubServe(app, file).then((exitCode) {
    exit(exitCode);
  });
}

Future<int> pubServe(String app, String file) {
  var sdk = Platform.environment["DART_SDK"];
  if (sdk == null) {
    print("Dart SDK not found");
    return new Future(() => -1);
  }

  var executable = pathos.join(sdk, "bin", "pub");
  var pattern = r"^Serving (?:.*) web on (.*)$";
  var regexp = new RegExp(pattern);
  return Process.start(executable, ["serve"], runInShell: true,
      workingDirectory: app).then((process) {
    process.stdout.listen((data) {
      var string = new String.fromCharCodes(data);
      for (var c in data) {
        stdout.writeCharCode(c);
      }

      var match = regexp.matchAsPrefix(string);
      if (match != null) {
        var url = match.group(1);
        if (file != null) {
          url += "/$file";
        }

        Timer.run(() => runBrowser(url));
      }
    });

    process.stderr.pipe(stderr);
    stdin.pipe(process.stdin);
    return process.exitCode.then((exitCode) {
      return exitCode;
    });
  });
}

void runBrowser(String url) {
  var fail = false;
  switch (Platform.operatingSystem) {
    case "linux":
      Process.run("x-www-browser", [url]);
      break;
    case "macos":
      Process.run("open", [url]);
      break;
    case "windows":
      Process.run("explorer", [url]);
      break;
    default:
      fail = true;
      break;
  }

  if (!fail) {
    //print("Start browsing...");
  }
}

P.S.

NOTE:

If you run this script from Dart Editor, Editor will never stops execution of subprocess (pub serve in our case) when you stop current script in Dart Editor.

This is not related only to this script. Editor always keep subprocesses alive.

If you run it from cmd-line it terminates pub serve correctly.

like image 31
mezoni Avatar answered Mar 23 '23 17:03

mezoni