How to create pubspec.yaml by command? - dart

I am learning Dart (2.9.3). I use pub global activate webdev command.
I think it will work like npm in Node.js and create pubspec.yaml automatically, but it didn't.
Do I need to add more switches (like --save) to pub global?
Thank you.

The pub global activate webdev command activates webdev package to use on your computer. This package is
A command-line tool for developing and deploying web applications with Dart.
As author wrote in usage section
webdev provides two commands: serve and build.
But neither of both creates pubspec.yaml file.
If you want to generate a project, you can use stagehand. But there is no option to generate customized pubspec.yaml file. Dart must know which packages to use in a particular project, and only this file can provide those informations.

Related

What we add under dev_dependencies in flutter

I have a silly question regarding dev_dependencies in flutter, I am planning to use mock_cloud_firestore and in its installation page it says that we need to add it in the dependencies section of pubspec.yaml
I think mock_cloud_firestore is not a production dependency but a dev dependency if so why we need to add it under dependencies?
Also what we add under dev_dependencies?
Thanks.
you can run below command flutter pub add <packagename> --dev to add under dev dependencies
and you can run below command flutter pub add <packagename> to add normal dependencies.
Both make an entry within the pubspec.yaml file under different headers.
dev_dependencies only have packages and tools which help the development process for the developer and are not really needed in production . eg. code generation etc
This issue is mentioned in Dart's official docs:
Pub supports two flavors of dependencies: regular dependencies and dev dependencies. Dev dependencies differ from regular dependencies in that dev dependencies of packages you depend on are ignored
Therefore, I believe it safer to put it under dependencies during development and remove it from pubspec when personal testing is finished. But below quote from the official doc is more clear:
The rule for deciding between a regular or dev dependency is simple: If the dependency is imported from something in your lib or bin directories, it needs to be a regular dependency. If it’s only imported from test, example, etc. it can and should be a dev dependency.

How to create and run a Dart project on the command line

I wanted to create a Dart project to play around with, but I couldn't figure out how to create one from the command line.
I tried
dart create playground.dart
I also tried
dart playground.dart
but the code I pasted in had dependency requirements.
Update
Dart now supports creating projects directly. No more need for stagehand.
dart create playground
This creates a folder named playground with a Dart project inside.
Alternate answer
1. Create a new folder
mkdir playground
2. Add a dart file with a main() function
playground.dart
void main() {
print("hello world");
}
3. Run the app
dart playground.dart
If your app has dependencies...
4. Setup configuration
Create a pubspec.yaml file
touch pubspec.yaml
Paste in any dependencies that you have
name: playground
description: Just a place to practice
version: 0.0.1
dependencies:
crypto: ^2.0.6
5. Get dependencies
pub get
The easiest way to create a new project is to use Stagehand. This is the same that IntelliJ uses when creating a new Dart project.
You can install it with this command
pub global activate stagehand
And then all you have to do to use it is
mkdir playground
cd playground
stagehand <generator-name>
These are the different generator you can use right now:
console-full - A command-line application sample.
package-simple - A starting point for Dart libraries or applications.
server-shelf - A web server built using the shelf package.
web-angular - A web app with material design components.
web-simple - A web app that uses only core Dart libraries.
web-stagexl - A starting point for 2D animation and games.

How to run angulardart application?

I just download andgularjs dart tutorial from angulardart.org . It provides me some tutorials, but I couldn't able to run those tutorials.
I am using webStorm IDE to run those application. when I am trying to run the application, I am getting following error.
enter image description here
I couldn't find package folder with in web root folder in that application.
You probably need to run pub upgrade. WebStorm provides this action in the context menu of the pubspec.yaml file, also if you open the pubspec.yaml file in the WebStorm editor links for pub actions are shown in the top-left corner of the editor. Alternatively you can run pub run from the command line of your project directory if the path is configured properly on your system so the command is found.

Dart Package Management via dart2js

I'm learning Dart and its dependency manager pub and am having a tough time seeing the "forest through the trees" here.
Say I want to use Polymer.dart in my project. So, in my project root, I create the following pubspec.yaml:
name: test_dart
description: A sample web application
dependencies:
browser: any
polymer: ">=0.9.0 <0.10.0"
I then run pub get, which goes to the pub repo and fetches the browser and polymer dependencies that I've specified. It then creates a packages directory in my project root, which now means I have a project that looks like:
MyDartProject/
pubspec.yaml
myapp.dart
packages/
browser/
...
...all the packages that ship with Polymer
Now I start coding my Dart web app (myapp.dart), which will references various Polymer and browser types/functions/etc. in its source code.
When I'm all done, I want to create a JavaScript file called myapp.js.
According to the dart2js docs, I need to run something like:
dart2js --out=myapp.js --package-root=??? myapp.dart
How do I include all the browser & polymer packages on the buildpath?
There is a "pub build" option now.
http://pub.dartlang.org/doc/pub-build.html
Use pub build when you’re ready to deploy your web app. When you run
pub build, it generates the assets for the current package and all of
its dependencies, putting them into a new directory named build.
$ cd ~/dart/helloworld
$ pub build
Building helloworld......
Built 5 files!
If the build directory already exists, pub build deletes it and then creates it again.
That should do everything you are after here. You can also launch it from the IDE by right clicking on the pubspec.yaml file and choose "pub build"
EDIT: You should also see the links in zoechi's answer.
If you run dart2js from your MyDartProject directory you don't have to provide --package-root parameter.
An alternative way is running pub build. If you use Polymer you need to add a transformers section.
see also
How to deploy a Dart Polymer app to Javascript using dart2js
How do I pass multiple entry_points to polymer transformer in pubspec.yaml?

pub install does not work in Dart

I am trying to install glmatrix using pub install. My OS is Windows 7 and I have Git installed.
Here is my pubspec.yaml
name: WebGLTut1
description: A sample application
dependencies:
glmatrix:
git: git://github.com/pjako/glmatrix.dart.git
When I run pub install I get the following error:
Running pub install ... Pub install fail, FormatException: Could not
parse "git://github.com/pjako/glmatrix.dart.git".".
I googled a while to find a solution but couldn't. Please help.
UPDATE#1:
I found the problem. There has to be a TAB/Space when specifying git: url in pubsec.yaml as follows.
glmatrix:
git: git://github.com/pjako/glmatrix.dart.git
This solved the previouse problem of FormatException. But now when I run pub install again it throws theis error.
Running pub install ... Pub install fail, Git failed.
As you identified you update, yaml files are whitespace sensitive; that's why you don't have to use lots of curly braces like json. :)
I see a few of possible problems here:
First
Glmatrix doesn't appear to have its own pubspec defined. Libraries participating in the package system much declare a pubspec file with the name property defined. The name is important because pub uses it when you declare your dependencies. Let's say you had a project called toast with the following simple pubspec:
name: butter
version: 0.0.0
Then you would need to declare your dependency to that project like so:
dependencies:
butter:
git: git://github.com/foo/toast.git
Have the project owner make the changes or fork it yourself, add the pubspec, and then reference your fork in your pubspec.
Second
Glmatrix is does not conform to the pubspec package layout conventions (your own projects should also conform to this). Although I believe that pub may handle non-conforming layouts currently, it may not in the future. So again, you can either ask the project owner to fix, or fork and fix it yourself.
Third
Since you are on windows, make sure git is in your system PATH variable. If you are using the bash shell, git will work, but it's not going to work outside of bash (like from Dart editor) until you add it to your PATH.

Resources