IAmYourFaja
IAmYourFaja

Reputation: 56934

Dart package layout

I just read Dart's suggested packagout layout spec and have a few questions regarding it:

  1. The spec says that I should put all 3rd party packages into a lib directory. But running pub get or pub upgrade will fetch those packages into a packages directory. So which is it?!? Am I supposed to run pub get, pull down packages into packages, and then manually copy them over to lib? That seems inefficient!
  2. The spec says that packages that should be a part of an API should go in lib, but that internal packages (not a part of the API) should go in lib/src. So if my project depends on some 3rd party package called transmogrify, how do I known which transmogrify packages are a part of the public API and should be placed in lib, and which ones are "private" and should be placed in lib/src? This seems tedious!
  3. Can a Dart app have 2+ main() methods? Why/why not?
  4. Just want confirmation that the following files should not be checked into a VCS:
    • packages/**
    • build/** (produced by pub build)
    • pubspec.lock
    • Any others?

Upvotes: 1

Views: 273

Answers (2)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657871

1) you don't put 3rd party packages into a lib directory.

If you want to build a reusable library package you put the files building the library in the lib directory.

3rd party packages are just referenced in the dependencies or dev_dependencies section in the pubspec.yaml file. The rest is maintained by pub.

2) should be partly answered by 1). You put your public surface of your library package into lib. Library internals that should not be exposed as public API can/should go into a lib sub directory.

3) I think this depends how you specify a Dart app. A Dart package can certainly have several main functions. You can't have more entry pages in the web directory where each has a main.

You can have more Dart script files in bin, test, example where each can have a main.

You start an app by opening an entry page in the browser or running a script file with dart myscript.dart so this clearly defines what gets called.

4)

  • you don't check in packages/
  • you don't check in build/
  • you do check in pubspec.lock for application packages
  • you don't check in pubspec.lock for library packages also take a look at this question In Dart and Pub, should I add pubspec.lock to my .gitignore?
  • I have several in .gitignore but they seem not to be used anymore (at least not by pub *)
    • out
    • deploy
    • _from_packages

I somehow have the impression that you are mixing up the concepts of libraries and packages. Just to clarify: You can have more than one library inside on package. You can (or have to) import individual libraries of a package individually if you want to use them.

Upvotes: 2

TaylorR
TaylorR

Reputation: 4013

  1. When you run pub-get, it pulls all required packages into your packages directory. Leave the structures untouched.
  2. My suggestion is to use the package names you can find in "Add package" when you open the pubspec.yaml file in Dart Editor.
  3. No. main() is the program entry point. So one program, one entry.
  4. That is pretty much you have listed. To add one: .buildlog.

Upvotes: 2

Related Questions