Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Created May 16, 2019 13:10
Show Gist options
  • Save CaiJingLong/82a81afd95a4b2267ad705e7962ff320 to your computer and use it in GitHub Desktop.
Save CaiJingLong/82a81afd95a4b2267ad705e7962ff320 to your computer and use it in GitHub Desktop.
根据传入的lock文件获取dart工程的依赖(不包含sdk), 以pubspec.yaml的格式输出
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
)
func main() {
params := make(map[string]interface{})
bytes, _ := ioutil.ReadFile("/Users/caijinglong/Documents/GitHub/flutter_ijkplayer/example/pubspec.lock")
_ = yaml.Unmarshal(bytes, &params)
packages := params["packages"].(map[interface{}]interface{})
fmt.Println("dependencies:")
for k, v := range packages {
name := k
var version string
itemMap := v.(map[interface{}]interface{})
switch itemMap["source"] {
case "path":
version = "\n" +
" path: " +
itemMap["description"].(map[interface{}]interface{})["path"].(string)
break
case "hosted":
version = itemMap["version"].(string)
break
case "git":
gitMap := itemMap["description"].(map[interface{}]interface{})
version = "\n git:\n" +
" url: " + gitMap["url"].(string)
if gitMap["ref"] != nil {
version = version + "\n ref: " +
gitMap["resolved-ref"].(string)
}
default:
continue
}
fmt.Printf(" %s: %s\n", name, version)
}
}
@CaiJingLong
Copy link
Author

以如下pubspec为例

name: ijkplayer_example
description: Demonstrates how to use the ijkplayer plugin.
publish_to: "none"

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  photo: ^0.3.3
  oktoast: ^2.1.6

  flutter_localizations:
    sdk: flutter
  intl: ^0.15.8
  flutter_native_image:
    git:
      url: https://github.com/btastic/flutter_native_image.git

  flutter_ijkplayer:
    path: ../

dev_dependencies:
  flutter_test:
    sdk: flutter

  intl_translation: ^0.17.3

# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:
  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true
  # To add assets to your application, add an assets section, like this:
  # assets:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg
  assets:
    - assets/
  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.io/assets-and-images/#resolution-aware.
  # For details regarding adding assets from package dependencies, see
  # https://flutter.io/assets-and-images/#from-packages
  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.io/custom-fonts/#from-packages

当 flutter packages get后 会生成一个 pubspec.lock文件

# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
packages:
  analyzer:
    dependency: transitive
    description:
      name: analyzer
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.35.4"
  args:
    dependency: transitive
    description:
      name: args
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.5.1"
  async:
    dependency: transitive
    description:
      name: async
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "2.1.0"
  boolean_selector:
    dependency: transitive
    description:
      name: boolean_selector
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.0.4"
  charcode:
    dependency: transitive
    description:
      name: charcode
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.1.2"
  collection:
    dependency: transitive
    description:
      name: collection
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.14.11"
  convert:
    dependency: transitive
    description:
      name: convert
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "2.1.1"
  crypto:
    dependency: transitive
    description:
      name: crypto
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "2.0.6"
  cupertino_icons:
    dependency: "direct main"
    description:
      name: cupertino_icons
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.1.2"
  dart_style:
    dependency: transitive
    description:
      name: dart_style
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.2.4"
  flutter:
    dependency: "direct main"
    description: flutter
    source: sdk
    version: "0.0.0"
  flutter_ijkplayer:
    dependency: "direct main"
    description:
      path: ".."
      relative: true
    source: path
    version: "0.2.3"
  flutter_localizations:
    dependency: "direct main"
    description: flutter
    source: sdk
    version: "0.0.0"
  flutter_native_image:
    dependency: "direct main"
    description:
      path: "."
      ref: HEAD
      resolved-ref: b87c2d0cda2851ea8e05b4f680042fd6d3e56af4
      url: "https://github.com/btastic/flutter_native_image.git"
    source: git
    version: "0.0.4"
  flutter_test:
    dependency: "direct dev"
    description: flutter
    source: sdk
    version: "0.0.0"
  front_end:
    dependency: transitive
    description:
      name: front_end
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.1.14"
  glob:
    dependency: transitive
    description:
      name: glob
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.1.7"
  intl:
    dependency: "direct main"
    description:
      name: intl
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.15.8"
  intl_translation:
    dependency: "direct dev"
    description:
      name: intl_translation
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.17.3"
  kernel:
    dependency: transitive
    description:
      name: kernel
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.3.14"
  matcher:
    dependency: transitive
    description:
      name: matcher
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.12.5"
  meta:
    dependency: transitive
    description:
      name: meta
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.1.6"
  oktoast:
    dependency: "direct main"
    description:
      name: oktoast
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "2.1.6"
  package_config:
    dependency: transitive
    description:
      name: package_config
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.0.5"
  path:
    dependency: transitive
    description:
      name: path
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.6.2"
  pedantic:
    dependency: transitive
    description:
      name: pedantic
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.5.0"
  petitparser:
    dependency: transitive
    description:
      name: petitparser
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "2.2.1"
  photo:
    dependency: "direct main"
    description:
      name: photo
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.3.3"
  photo_manager:
    dependency: transitive
    description:
      name: photo_manager
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.3.3"
  pub_semver:
    dependency: transitive
    description:
      name: pub_semver
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.4.2"
  quiver:
    dependency: transitive
    description:
      name: quiver
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "2.0.2"
  sky_engine:
    dependency: transitive
    description: flutter
    source: sdk
    version: "0.0.99"
  source_span:
    dependency: transitive
    description:
      name: source_span
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.5.5"
  stack_trace:
    dependency: transitive
    description:
      name: stack_trace
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.9.3"
  stream_channel:
    dependency: transitive
    description:
      name: stream_channel
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "2.0.0"
  string_scanner:
    dependency: transitive
    description:
      name: string_scanner
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.0.4"
  term_glyph:
    dependency: transitive
    description:
      name: term_glyph
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.1.0"
  test_api:
    dependency: transitive
    description:
      name: test_api
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.2.4"
  typed_data:
    dependency: transitive
    description:
      name: typed_data
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "1.1.6"
  vector_math:
    dependency: transitive
    description:
      name: vector_math
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "2.0.8"
  watcher:
    dependency: transitive
    description:
      name: watcher
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "0.9.7+10"
  yaml:
    dependency: transitive
    description:
      name: yaml
      url: "https://pub.flutter-io.cn"
    source: hosted
    version: "2.1.15"
sdks:
  dart: ">=2.2.0 <3.0.0"
  flutter: ">=0.3.0 <2.0.0"

修改lock的地址 就会将这个文件反向解析为如下格式:

dependencies:
  photo_manager: 0.3.3
  source_span: 1.5.5
  async: 2.1.0
  meta: 1.1.6
  pub_semver: 1.4.2
  glob: 1.1.7
  stack_trace: 1.9.3
  charcode: 1.1.2
  front_end: 0.1.14
  matcher: 0.12.5
  petitparser: 2.2.1
  string_scanner: 1.0.4
  typed_data: 1.1.6
  flutter_ijkplayer: 
    path: ..
  flutter_native_image: 
    git:
      url: https://github.com/btastic/flutter_native_image.git
      ref: b87c2d0cda2851ea8e05b4f680042fd6d3e56af4
  intl: 0.15.8
  term_glyph: 1.1.0
  crypto: 2.0.6
  intl_translation: 0.17.3
  stream_channel: 2.0.0
  quiver: 2.0.2
  vector_math: 2.0.8
  analyzer: 0.35.4
  convert: 2.1.1
  kernel: 0.3.14
  oktoast: 2.1.6
  path: 1.6.2
  photo: 0.3.3
  watcher: 0.9.7+10
  cupertino_icons: 0.1.2
  dart_style: 1.2.4
  package_config: 1.0.5
  pedantic: 1.5.0
  test_api: 0.2.4
  yaml: 2.1.15
  args: 1.5.1
  boolean_selector: 1.0.4
  collection: 1.14.11

@CaiJingLong
Copy link
Author

CaiJingLong commented May 16, 2019

这个用处是: 比如你的项目很庞大拥有众多package包,可以反向解析主工程的lock文件,以生成一个相对清晰的依赖引用列表

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment