What is the Difference between Dart console-full and console-simple application? - dart

While learning dart I came across to these type of console application, so I just want to know What is the Difference between Dart console-full and console-simple application?

Both templates generate a Dart project with some default files:
.dart_tool/package_config.json
.gitignore
.packages
analysis_options.yaml
CHANGELOG.md
pubspec.lock
pubspec.yaml
README.md
But does then also create Dart files which depends on the type of template:
console-simple
Creates a very basic Dart project with a single Dart file named bin/<project_name>.dart with the content:
void main(List<String> arguments) {
print('Hello world!');
}
console-full
Create a more complete Dart project where we split the code into bin/, lib/, and test/. The description in the auto-generated README.md file does explain the file structure:
A sample command-line application with an entrypoint in bin/, library code
in lib/, and example unit test in test/.
The project makes use of the test package for unit testing so the project will by default include this package in the pubspec.yaml.
Following Dart files is generated to make use of the three directories:
// bin/project_name.dart
import 'package:project_name/project_name.dart' as project_name;
void main(List<String> arguments) {
print('Hello world: ${project_name.calculate()}!');
}
// lib/project_name.dart
int calculate() {
return 6 * 7;
}
// test/project_name_test.dart
import 'package:project_name/project_name.dart';
import 'package:test/test.dart';
void main() {
test('calculate', () {
expect(calculate(), 42);
});
}

Related

Run dart script outside of a project folder

I am trying to replace python with dart as a scripting language for my tools. I can create a python file anywhere, import global packages and run it but I can't seem to get this working with dart. Do I always need a pubspec.yaml file to run a dart script?
This is the script I am trying to run:
import 'package:http/http.dart' as http;
main(List<String> args) async{
var res = await http.get('https://example.com');
print(res.headers);
}
This is the error I am getting:
Error: Could not resolve the package 'http' in 'package:http/http.dart'.
test2.dart:1:8: Error: Not found: 'package:http/http.dart'
import 'package:http/http.dart' as http;
^
test2.dart:4:19: Error: Method not found: 'get'.
var res = await http.get('https://example.com');
No, you don't need a pubspec.yaml file to run a program, but it does need to somehow be able to resolve all the imports.
The pubspec.yaml file is used for obtaining the packages (from pub.dev, a git repository, etc.) but not for finding the packages at runtime. The pub program reads the pubspec.yaml file and downloads the packages mentioned in it, and maintains a packages specification file indicating where each package resolves to. By default the packages specification is in a file called .packages in the same directory as the pubspec.yaml file. The Dart runtime normally finds the packages by looking at the .packages package specification file, but there are other ways.
Here are some options:
Put a .packages file in the same directory as the Dart program, or in an ancestor directory.
Use the --packages option to specify the package specification file to use:
dart --packages=/home/username/stuff/mypackagespecfile myprogram.dart
The Dart runtime also has a --package-root option. But I haven't figured out how to make it work.
The import statements use URIs, so import 'file://...'; can also work in some cases.
Use dart2native to compile the program into an executable.
Note: Dart scripts can also start with a hash-bang line:
#!/usr/bin/env dart
import 'package:http/http.dart' as http;
main(List<String> args) async{
var res = await http.get('https://example.com');
print(res.headers);
}
Then you can make the program executable and run it without needing to type in the Dart runtime:
$ chmod a+x myprogram.dart
$ ./myprogram.dart

How do I get protobuf methods to resolve in intellij when using bazel?

I'm having an issue where I have a working java program that uses protobuf and is built using bazel, but where intellij doesn't recognize the method toByteArray.
I forked https://github.com/cgrushko/proto_library on my local machine, imported the workspace into intellij and built. I then added the following java main class:
package src;
import demo.PersonOuterClass;
public class Main {
public static void main(String argv[]) {
byte[] ba = PersonOuterClass.Person.newBuilder().setEmail("dwwd").build().toByteArray();
for (byte b : ba) {
System.out.println(b);
}
}
}
and the following bazel build rule
java_binary(
name = "Main",
main_class = "src.Main",
srcs = ["Main.java"],
deps = [":person_java_proto"]
)
The program builds and runs properly, but in intellij toByteArray() is red and intellij says that it can't resolve the method.
I suspect the issue is that generated Person extends com.google.protobuf.GeneratedMessageV3 but intellij doesn't know about GeneratedMessageV3 and that it extends a class, AbstractMessageLite, that defines the toByteArray method.
Anyone know how to fix either the bazel build target(s) or intellij so that toByteArray is resolved by the ide?
Thanks,
Tom.
I solved this problem by marking bazel-/external/java/com_google_protobuf_java/core/src/main/java as a source root. (Right click -> Mark Directory as -> Sources Root)
I fixed this on my Bazel project by adding com.google.protobuf:protobuf-java as a dependency for the Bazel library depending on protobuf.

Dart Test client- side HTML configuration

I understand that the package:unittest/unittest.dart is deprecated and the new package is package:test/test.dart.
Which are the equivalent of the library package:unittest/html_config.dart and the useHtmlConfiguration() function in the new test.dart framework.
Thanks.
Note: I am reading an outdated book ("Dart in Action"). So far I have been able to match the deprecated parts with the new standard parts of Dart.
Except now that I am reading the section of Unit-Test.
source code
.
├── PackList.dart
├── PackList.html
├── pubspec.lock
├── pubspec.yaml
├── styles.css
└── test
├── PackList_test.dart
└── PackList_test.html
I am trying to check if the value that the constructor return is not NULL.
import "package:test/test.dart";
import "../PackList.dart" as packListApp;
main() {
test("PackItem constructor", () {
var item = new packListApp.PackItem("Towel");
expect(item, isNotNull);
});
}
The source code works.
This is just an excersice to understand how the test framework works.
I expect item to be a new object.
With this properties initialized after var item = new packListApp.PackItem("Towel");
print(packItem.uiElement); //Towel
print(packItem.itemText); //div
The problem is that I don't know how do related the html part of my source code with the test.
When I run this test, I got this errors.
pub run test
00:00 +0 -1: loading test/PackList_test.dart [E]
Failed to load "test/PackList_test.dart":
Unable to spawn isolate: The built-in library 'dart:html' is not available on the stand-alone VM.
PackList.dart: line 1 pos 1: library handler failed
import "dart:html";
^
00:00 +0 -1: Some tests failed.
If I add #TestOn("dartium"), I got this message.
pub run test
No tests ran.
pub run test just runs VM tests. If you want to run browser tests, you need to explicitly tell it like
pub run test -pdartium
The my_test.html needs to contain
<link rel="x-dart-test" href="my_test.dart">
instead of a normal Dart script tag

How to perform Pub Get programatically from a Dart program

I'm building a Dart framework called Modular for backend development and this framework has a installer for convenience. At the end of the installation, I'd like to install all dependancies in the generated pubspec.yaml file. How would I do this? Thank you
import 'dart:io';
void main() {
Process.run('pub', ['get'],
runInShell: true,
workingDirectory: 'dirWherePubspec.yaml_is')
.then((ProcessResult results) {
// ...
});
}
or alternatively Process.start(...)

How to use Dart with Webstorm 6 on Windows (running Codelab from Google IO 2013)

I searched for but didn't find any other posts remotely related to my question. Essentially, I'm attempting to follow the Dart Codelab from Google IO 2013 which I found here: http://goo.gl/4E21M
I'm attempting to use the Dart plugin in Webstorm 6 which I setup using the directions here: http://blog.jetbrains.com/webide/2012/12/dart-support-in-webstorm-6/
Lastly, I'm doing this on Windows 8.
My build.dart is:
import 'package:web_ui/component_build.dart';
import 'dart:io';
import 'dart:async';
void main() {
var args = new List.from(new Options().arguments);
build(new Options().arguments, ['web/index.html'])
.then((_) => print('Build finished!'));
}
My pubspec.yaml is:
name: writer
version: 0.0.1
author: Dart Team <misc#dartlang.org>
description: This is the finished version of the application built in the Google I/O 2013 Dart Codelab.
homepage: https://github.com/dart-lang/io-2013-dart-codelab
dependencies:
intl: any
web_ui: any
Yet, when I attempt to run the step 1 code, I see in my Event Log: Error running Test: build.dart: Missing library statement in build.dart.
So that seems straight-forward enough...except I can't figure out which library statement should be there that isn't... the only line of code I removed was:
#!/usr/bin/env dart
Because I'm attempting to run this on Windows, and that is for a UNIX environment.
Any thoughts? I really appreciate any assistance you can provide getting up and running with this Codelab in Webstorm (which is LIGHT YEARS more refined then the default Dart Editor). In other words, I FAR prefer Webstorm--if I can get things up and running in it.
Thank you in advance!
Thanks to #ChrisBuckett and #PixelElephant my question was answered. In order to get the Codelab from Google IO 2013, Step 1, to run I had to include "library build;" at the top of my build.dart file. In order to see the post-build output, I had to look in the /out folder and "run" the index.html file in Chromium.
This combination worked.
My fixed build.dart file:
library build;
import 'package:web_ui/component_build.dart';
import 'dart:io';
import 'dart:async';
void main() {
var args = new List.from(new Options().arguments);
build(args, ['web/index.html'])
.then((_) => print('Build finished!'));
}

Resources