Make a system call in dart? - dart

I want to execute a python or a java class from inside dart.
The following is a snippet which I have used from a stackoverflow question Java
Runtime currentRuntime = Runtime.getRuntime();
Process executeProcess = currentRuntime.exec("cmd /c c:\\somepath\\pythonprogram.py");
I would like to know how to make such calls in dart.
Basically I have a UI where in the user uploads code in java and python.I want to execute the uploaded code from the dart environment instead of creating a routine in java or python on the folder where the code is uploaded.
I personally dont know if this is possible, since dart is purely in a VM.
I want to execute the following command
java abc
from inside dart.

You can simply use Process.run.
import 'dart:io';
main() {
Process.run('java', ['abd']);
}
You can also access to stdout, stderr and exitCode through the resulting ProcessResult :
import 'dart:io';
main() {
Process.run('java', ['abd']).then((ProcessResult pr){
print(pr.exitCode);
print(pr.stdout);
print(pr.stderr);
});
}

Related

dart-define not working when running a standalone Dart program

I have a single file Dart program - let's say main.dart. I'm trying to provide some compile time environment values to it using --dart-define=env=env_value but in the Dart program, I'm always getting the default values.
This is what my Dart program looks like
void main() {
const myValue = const String.fromEnvironment("MY_VALUE", defaultValue: "DEFAULT");
print('My value: $myValue'); // Always prints "DEFAULT"
}
This is the command I'm using to run my program
dart main.dart --dart-define=MY_VALUE=SOME_VALUE
Now, when I include the exact same code from above in a Flutter app and run it with the below command, everything seems to work as expecetd but for some reason the above program always prints DEFAULT as the output on console.
flutter run --dart-define=MY_VALUE=SOME_VALUE
Is there something I'm missing when it comes to providing these values in a Dart program? I'm running macOS if that helps in any way.
If you type:
dart --help --verbose
It will give you the list of supported flags.
Usage: dart [<vm-flags>] <dart-script-file> [<script-arguments>]
Executes the Dart script <dart-script-file> with the given list of <script-arguments>.
Supported options:
...
--define=<key>=<value> or -D<key>=<value>
Define an environment declaration. To specify multiple declarations,
use multiple instances of this option.
...
So it appears that the flag you want is --define or -D, rather than --dart-define. Also note that this is considered a "vm-flag" and must come before the file name in order to work.
Therefore the following command should work:
dart --define=MY_VALUE=SOME_VALUE main.dart

Dart Functions Framework usage

I'm new to the Dart functions framework. My goal is to use this package to create several functions and deploy them to Cloud Run (in combination with Firebase, but I guess that's irrelevant to this question).
I've run the quick starts and I've read all of the contents in the docs.
The quick start mentions just one function at a time (e.g. Hello World, Cloud Events, etc..), like this:
import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';
#CloudFunction()
Response function(Request request) {
return Response.ok('Hello, World!');
}
But as you can see in the quickstarts only one function is handled in a project at a time. How about me wanting to deploy several functions? Should I:
Write several functions in the same project / file, so that the function framework compiles the 'server.dart` by itself
OR
Create a different functions_framework for each function?
Let me be more specific. Should I do the following (option 1 - which makes more sense to me):
import 'dart:math';
import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';
#CloudFunction()
Response function(Request request) {
return Response.ok('Hello, World!');
}
#CloudFunction()
Response function2(Request request) {
if (Random().nextBool()) {
return Response.ok('Hello, World!');
} else {
return Response.internalServerError();
}
}
Or should I build a different folder by running a build_runner for each function I need in my project?
Is there a difference and/or a best practice?
Thanks in advance.
EDIT. This question is related to the deployment on Cloud Run itself, and not just testing on my own PC. To test my own functions I did the following:
Run dart run build_runner build, so that it updates the server.dart file correctly (I can see that the framework does a lot behind the scenes and that the _nameToFunctionTarget is basically a router);
Run the server in two different terminals, like this: dart run bin/server.dart --port MYPORT --target MYFUNCTION (where MYPORT and MYFUNCTION are either 8080/8081 or function/function2 respectively).
I guess I'm just confused on how to correctly manage this framework once deployed on Cloud Run.
EDIT 2. I just gave up using Dart as a Serverless language or even a Backend language. There's just too much jargon even for the basic things. Any backend framework is either dead, or maintained by one single enthusiast guy (props to him!). This language has not yet received enough love from the Google Team / the community and at this moment in time is basically not possible to go fullstack on just Dart. It's a dream, but it can't be realized now. Furthermore, Dart hardly lacks a proper SDKs to use Firestore, etc., so Firebase isn't an option. I find it easier to just learn NodeJS and exploit the Firebase support for Firebase Functions written in NodeJS, and I'll wait for more support in there in the future, if there ever will be.
The documentation is a bit sparse right now (and I'm new to it also! I couldn't find any good examples, so here goes...)
You can only have a single function that is served. It should be
named 'function' (the type and name can be overriden, see the
cloudevent example dartfn generate cloudevent)
You 'could' have many of these deployed so that each does a specific thing, such as processing cloudevents above, but most people
want something more REST-like (see next)
You need to attach a Router() so that you can have the single entry point (function) handled by specific logic in your code.
Example for Rest
add to pubspec.yaml (in dependencies:) shelf_router: ^1.1.2
delegate the #CloudFunction to use the Router()
functions.dart
import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';
Router app = Router()
..get('/health', (Request request) {
return Response.ok('healthy');
})
..get('/user/<user>', (Request request, String user) {
// fetch the user... (probably return as json)
return Response.ok('hello $user');
})
..post('/user', (Request request) {
// convert request body to json and persist... (probably return as json)
return Response.ok('saved the user');
});
#CloudFunction()
Future<Response> function(Request request) => app.call(request);

How to write QuizApp using Dart Command-Line Apps?

I need to write QuizApp using Dart Command-Line Apps. There is a some example from Dartlang.org but all of the related 2013 which is very old. I am using Dart 2 and I need to write a quiz app.
I need to know how to use stdin, stdout, listen and subscription so the app doesn't quit after 1 enter.
How to write QuizApp using Dart Command-Line Apps
You don't really need any use of subscription for a simple quiz command-line application. You only need to use stdin and stdout from the dart:io package since the application does wait for input when you are using 'stdin.readLineSync()'.
I don't want to make you homework so I am only going to give a small example:
import 'dart:io';
main() {
while(true) {
stdout.writeln('Are you OK? (Yes/No)');
if (stdin.readLineSync().toLowerCase() == 'yes') {
stdout.writeln('Then stop trouble me!');
return;
} else {
stdout.write('Let me ask again! ');
}
}
}

Dart method$Class syntax

I'm new to dart, and following the tutorial provided on the Dart for the web page.
It all makes sense - apart from one piece of sytax:
final InjectorFactory injector = self.injector$Injector;
Here's the full code from the tutorial:
import 'main.template.dart' as self;
const useHashLS = false;
#GenerateInjector([
routerProvidersHash,
ClassProvider(Client, useClass: InMemoryDataService),
// Using a real back end?
// Import 'package:http/browser_client.dart' and change the
above to:
// ClassProvider(Client, useClass: BrowserClient),
])
final InjectorFactory injector = self.injector$Injector;
void main() {
runApp(ng.AppComponentNgFactory, createInjector: injector);
}
I'm a confused by the apparent .method$Class syntax. Can anyone explain to me what this means/what it's doing?
It's also underlined in Webstorm with the message The getter 'injector$Injector' isn't defined for the class 'self'. Regardless, it runs fine and works as expected.
Thanks in advance!
$ in an identifier has no special meaning. It's by convention often used for names in generated code.
Angular also uses code generation and the code will only become available after code generation was executed for example by webdev serve or webdev build.
I don't know the current state but the code might still be generated in a directory that is not analyzed by the DartAnalyzler and you might always see the error even wen the app can be run without problems.

Running an interactive Dart program from another Dart program

I have a fairly lengthy command-line program that requires user input of parameters and then processes using those parameters. What I would like to do is split the program into interactive and non-interactive. I attempted to do that, and intended to have the non-interactive program "call" the interactive program and using the results (parameters), process based on those parameters. The non-interactive part of the program displays results on the console as it processes. I have looked at Process.run and Process.start, but apparently they don't function that way. There is another similar question to this that is about 12-months old, so I thought it worthwhile asking again.
I have looked at Process.run and Process.start, but apparently they don't function that way.
Process.start is what you want here. It can do what you want, but you'll have to become a bit more comfortable with async programming if you aren't already. You'll spawn the process and then asynchronously read and write to the spawned processes stdout and stdin streams.
Your interactive program can do something like this:
// interactive.dart
import 'dart:io';
main() {
var input = stdin.readLineSync();
print(input.toUpperCase());
}
It's using stdin to read input from the command line. Then it outputs the processed result using regular print().
The non-interactive script can spawn and drive that using something like:
import 'dart:convert';
import 'dart:io';
main() {
Process.start("dart", ["interactive.dart"]).then((process) {
process.stdin.writeln("this is the input");
UTF8.decoder.fuse(new LineSplitter()).bind(process.stdout).listen((line) {
print(line);
});
});
}
It uses Process.start to spawn the interactive script. It writes to it using process.stdin. To read the resulting output it has to jump through some hoops to convert the raw byte output to strings for each line, but this is the basic idea.

Resources