当前位置:网站首页>[flutter 1-15] dart language -- package management tool pub details, pub G

[flutter 1-15] dart language -- package management tool pub details, pub G

2020-12-08 13:33:36 osc_ yozufu01

author | Vlad
source | Vlad ( official account :fulade_me)

What is? Pub Tools

Dart Ecosystems use packages to manage shareware , such as : Libraries and tools . We use Pub Package management tools To get Dart package . stay Pub On , You can find publicly available packages . Or from a local file system or somewhere else , such as Git Warehouse , Load available packages . No matter how the package is loaded from , Pub They all rely on version management , To help us get version compatible packages as well as SDK.
pub Tools Include management Package 、 Deploy Package And deploy command line applications .
Dart The package directory contains at least one pubspec file .
2020_12_06_pubspec
pubspec File records some dependent data about the project . In addition, there are other data, such as :Dart library , application , resources , test , picture , And examples .



Here's a pubspec An example of , The declaration in the example depends on Pub Two packages hosted on the site ( js and intl ):

name: my_app
dependencies:
  js: ^0.6.0
  intl: ^0.15.8

pub get

The project is configured with pubspec After the document , It can be executed in the project root directory pub get command :

 cd <path-to-my_app>
 pub get

pub get Command to determine which packages the current application depends on , And save them to a central system cache (central system cache) in . If the current application relies on a public package ,Pub From Pub Site The package . For one Git rely on ,Pub Meeting Clone The Git Warehouse .
Also, dependencies including packages will be downloaded . for example , If js Packet dependency test package , pub Will get at the same time js Bao He test package .

Pub Will create a .packages file ( Located in the root directory of the application ), This file maps each package name that the application depends on to the package in the system cache .
2020_12_06_packages

pub upgrade

The first time you get a dependency ,Pub Will download the latest version of dependencies and their compatibility . And then by creating lockfile Lock dependency , To always use this version . Pub Will be in pubspec Create and store a file called pubspec.lock file . It lists the specified version of each dependent package used ( The version of the current package or delivery package ).
2020_12_06_pubspec_lock
Everyone in a development project can use all the same versions of the package . Add the same to lockfile It can ensure that the deployed application uses the same version of code .

If you are ready to update the dependency to the latest version , Use command pub upgrade :

pub upgrade

The above command is used to regenerate lockfile file , And use the latest available version of the dependency package . If you only rely on an upgrade , You can specify the package to be upgraded in the command :

pub upgrade intl

The above command upgrades intl To the latest version , The other bags don't change .

Be careful pub upgrade Orders don't always put all of package Update to the latest version , as a result of pubspec Some of the documents package There's a version limit conflict between .

pub cache

Used to manage Pub The local Package cache . With this command, you can put a Package Add to cache , Or clear all cached Package And re install .
You can use pub cache add The order will be new Package Add to cache . You can also use pub cache repair The order will Package Clear from cache and re install .

pub cache add <package> [--version <constraint>] [--all]
pub cache repair

Options


add <package>    #  Install a library in your cache .

--all   #  Optional options . And  pub add  Used in combination to install all versions of a library .

--version <constraint>  #  Optional options . And  pub add  Use in combination to install the most suitable version according to restrictions .
#  for example :pub cache add barback --version "<=0.8.0 <0.110"   If you omit  --version,Pub  We will choose the most suitable one from the known version to install .

repair #  If  Pub  In the cache  Package  It's been modified and broken . for example , Someone accidentally modified the file in the dependency , But I don't know how to modify it back .pub cache repair  The command can be used for all  Package  Perform a re install to fix tampering problems .

pub deps

This command can change Package Print and output the dependency diagram to the console . This diagram includes Package The statement in pubspec In the document Directly dependent on And what these direct dependencies depend on Indirect dependencies .

pub deps [--style=<style>] [--dev] [--no-dev] [--executables]

By default, the dependency information is printed in the form of a tree .
for example , There's a project pubspec The following dependency information is declared in the file :

dependencies:
  barback: ^0.15.2
  markdown: ^0.7.2

When you execute pub deps Command, you will see the dependency diagram of the project as follows :

markdown_converter 0.0.0
|-- barback 0.15.2+6
|   |-- collection 1.1.2
|   |-- path 1.3.6
|   |-- pool 1.1.0
|   |   '-- stack_trace...
|   |-- source_span 1.2.0
|   |   '-- path...
|   '-- stack_trace 1.4.2
|       '-- path...
'-- markdown 0.7.2

Options

--style=<style>  or  -s <style> ##  Specified style output format . Specifies the style of the dependency printout .

#  share   concise 、 Trees   and   list   Three , The default is tree style .

# tree  Print dependency information in tree form . This is the default format .

# list  Print dependency information as a list .

# compact  Print dependency information as a compact list .

--dev #  Print all package dependency information , Including development time dependency . It's the default option .

--no-dev # Print all package dependencies except development time dependencies .

--executables # Print all available executable files .

pub downgrade

In the absence of additional parameters ,pub downgrade Command will get the current working directory pubspec.yaml All the dependencies listed in the file and their indirect dependencies The minimum edition .

pub downgrade [--[no-]offline] [-n|--dry-run] [dependencies...]

for example :

$ pub downgrade
Resolving dependencies... (1.2s)
+ barback 0.13.0
+ collection 0.9.1
+ path 1.2.0
+ source_maps 0.9.0
+ source_span 1.0.0
+ stack_trace 0.9.1
Changed 6 dependencies!

pub downgrade The command creates a lockfile file . If lockfile The file already exists ,Pub The file is ignored and a new lockfile file , Then all dependencies use the lowest version .

Demote specified dependencies
You can specify pub downgrade The command only minimizes the version of one dependency and does not affect the rest of the dependencies . for example :

$ pub downgrade test
Resolving dependencies...
  barback 0.15.2+2
  bot 0.27.0+2
  browser 0.10.0+2
  chrome 0.6.5
  collection 1.1.0
  path 1.3.0
  pool 1.0.1
  source_span 1.0.2
< stack_trace 0.9.2 (was 1.1.1)
  stagexl 0.10.2
< test 0.10.0 (was 0.11.4)
These packages are no longer being depended on:
- matcher 0.11.3
Changed 3 dependencies!

If you reduce the version of the specified dependency , And the dependency also has an indirect dependency , After the version change, these indirect dependencies may not fit the new version of dependency after the version change . here ,Pub Attempts to find the indirect dependency on which the highest version dependency depends within the acceptable range of the new version dependency . therefore , generally , After reducing the version of a dependency , The version of its indirect dependencies will also decrease .
Get new dependencies
If in execution pub downgrade Add a dependency to before the command pubspec In file , After the command is executed, the new dependency and other dependencies that are connected to it will be downloaded and put into .packages In file . With this pub get Command consistent .

Remove dependencies
If in pub downgrade Order from pubspec The file removed a dependency , After the command is executed, the dependency is removed from .packages Remove from file , And the related imports that the code uses for this dependency will become unavailable . All indirect dependencies that the dependency depends on are also removed , As long as these indirect dependencies are not dependent on other dependencies . This is also related to pub get Command consistent .

Offline demotion
You can still run without the Internet pub downgrade command . because Pub Will Package Download to a unified cache and store it with the rest of the system Package Share , If you need Package It's a very frequently used Package, Well, it's very likely that it's already been Package Download to the unified cache when using , At this point, you can use it directly .

however , By default ,pub downgrade The command will always try to get the dependent version online , therefore Pub You can determine if the dependency has an updated version . If you don't want to Pub Go online and check , have access to --offline The command parameter causes the command to be executed in offline mode . In offline mode ,Pub Only the local cache will be searched for available downloaded Package.

Options

--[no-]offline #  By default ,pub  It's going to go through the Internet (--no-offline). To use cached packages , Please use  --offline.
--dry-run  or  -n # Report on dependencies that will change , But it doesn't really change it .

pub publish

pub publish [--dry-run] [--force]

This command is used to put your Package Publish to pub.dev Website for others to download and rely on .
Options

--dry-run  or  -n # This option allows you to run Uploads  Package  But it doesn't really upload any files to  pub.dev  Website . This operation allows you to upload to  pub.dev  Check your upload and other related configuration before the website .

--force  or  -f  # This option allows  Pub  No more confirmation to you when uploading . Under normal circumstances , It will show you when you upload  Package  And to confirm with you .

If Package There are errors ,Pub Will exit and do not continue to upload . If it's a warning , be Package Will still be uploaded . If you want to make sure your Package No warning before uploading , Please make sure not to use --force and --dry-run Options .

pub uploader

pub uploader [options] {add/remove} <email>

The order allows pub.dev On the website Package Of the uploaders for the Package Add or remove other uploaders . It has two subcommands add and remove, You can add or delete an uploader by using the email address as the ID of an uploader . for example :

pub uploader add bob@example.com #  We have told  bob@example.com  Sent an invitation , In his / She will be added to the uploader after confirmation ( jurisdiction )
pub uploader remove bob@example.com # //  The uploader was successfully transferred from  package  Remove 

If Package There is and only one uploader , The uploader can no longer be deleted . You can delete yourself from the list of uploaders ( as long as Package There are other uploaders in the ), But once you delete yourself, you can't add yourself back .

By default , You are modifying the current working directory Package The uploader of . You can go through --package To specify which to modify Package The uploader of . for example :

pub uploader --package=transmogrify add bob@example.com 
 # //  We have told  bob@example.com  Sent an invitation , In his / She will be added to the uploader after confirmation ( jurisdiction )

adopt pub uploader add &lt;email&gt; Order to send an invitation , The invited user must Accept .

Other commands not commonly used :

pub outdated For usage, please refer to :https://zhuanlan.zhihu.com/p/138638020
pub global For usage, please refer to :https://www.jianshu.com/p/8a7f2cbac7a1


 official account

版权声明
本文为[osc_ yozufu01]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012081333114912.html