Class: Flutter::Pub::Spec

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-embed-flutter/flutter/pubspec.rb

Overview

The Specification provides a DSL to describe a flutter project. A project is defined as a library originating from a source. A specification can support detailed attributes for modules of code through dependencies.

Usually it is stored in ‘pubspec.yaml` file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Spec

Returns a new instance of Spec.

Parameters:

  • path (String)

    the path to the specification.



25
26
27
28
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 25

def initialize(path)
  @data = YAML.load_file path
  @defined_in_file = path
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Allows accessing top level values in ‘pubspec.yaml`, i.e. name, description, version etc.

Parameters:

  • m (Symbol)

    top level key value to access, i.e. name, description etc.

Returns:

  • depending on accessed value type in ‘pubspec.yaml`.

Raises:

  • (NoMethodError)

    if no method or custom attribute exists by the attribute name in pubspec.



179
180
181
182
183
184
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 179

def method_missing(m, *args, &block)
  if @data.include?(m.to_s)
    return @data[m.to_s]
  end
  super.method_missing(m, *args, &block)
end

Instance Attribute Details

#defined_in_fileString (readonly)

Returns the path where the specification is defined, if loaded from a file.

Returns:

  • (String)

    the path where the specification is defined, if loaded from a file.



20
21
22
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 20

def defined_in_file
  @defined_in_file
end

Class Method Details

.find(name, path) ⇒ Spec

Note:

either the flutter module or the ‘pubspec` of the flutter module can be in the path. Optionally you can provide the `pubspec` file directly.

Returns the path to ‘pubspec` with the given name and location to search.

Parameters:

  • name (String)

    the name of the project declared in ‘pubspec`.

  • path (String)

    where project or pubspec is located.

Returns:

  • (Spec)

    the ‘pubspec` with the given name if present.

Raises:

  • (StandardError)


73
74
75
76
77
78
79
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 73

def self.find(name, path)
  pubspec_path = find_file(name, path)
  raise StandardError, "Invalid path: '#{path}' for flutter module: '#{name}'." unless pubspec_path
  pubspec = Spec.new(pubspec_path)
  raise StandardError, "Invalid path: '#{path}' for flutter module: '#{name}'." unless pubspec.name == name
  return pubspec
end

.find_file(name, path) ⇒ String

Note:

either the flutter module or the ‘pubspec` of the flutter module can be in the path. Optionally you can provide the `pubspec` file directly.

Returns the path to ‘pubspec` with the given name and location to search.

Parameters:

  • name (String)

    the name of the project declared in ‘pubspec`.

  • path (String)

    where project or pubspec is located.

Returns:

  • (String)

    path to the ‘pubspec` with the given name if present.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 44

def self.find_file(name, path)
  path = File.expand_path(path, Dir.pwd)

  if File.basename(path) == Pub::SPEC_FILE
    return path
  elsif Dir.exists?(File.expand_path(name, path)) &&
   File.exists?(File.expand_path(Pub::SPEC_FILE, File.expand_path(name, path)))
    return File.expand_path(Pub::SPEC_FILE, File.expand_path(name, path))
  elsif File.exists?(File.expand_path(Pub::SPEC_FILE, path))
    return File.expand_path(Pub::SPEC_FILE, path)
  else
    return nil
  end
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

See if two Flutter::Pub::Spec instances refer to the same pubspecs.

Returns:

  • (Boolean)

    whether or not the two Flutter::Pub::Spec instances refer to the same projects.



153
154
155
156
157
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 153

def ==(other)
  self.class === other &&
   other.defined_in_file == defined_in_file &&
   other.instance_variable_get(:@data) == @data
end

#all_dependenciesArray<Dependency>

Returns the list of all the projects this specification depends upon.

Returns:

  • (Array<Dependency>)

    the list of all the projects this specification depends upon.



126
127
128
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 126

def all_dependencies
  dependencies + dev_dependencies
end

#dependenciesArray<Dependency>

Returns the list of all the projects this specification depends upon and are included in app release.

Returns:

  • (Array<Dependency>)

    the list of all the projects this specification depends upon and are included in app release.



110
111
112
113
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 110

def dependencies
  return [] unless @data.include?('dependencies')
  Dependency.create_from_hash(@data['dependencies'], self)
end

#dev_dependenciesArray<Dependency>

Returns the list of all the projects this specification depends upon only during development.

Returns:

  • (Array<Dependency>)

    the list of all the projects this specification depends upon only during development.



118
119
120
121
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 118

def dev_dependencies
  return [] unless @data.include?('dev_dependencies')
  Dependency.create_from_hash(@data['dev_dependencies'], self)
end

#hashFixnum

Returns A hash identical for equals objects.

Returns:

  • (Fixnum)

    A hash identical for equals objects.



161
162
163
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 161

def hash
  [defined_in_file, @data].hash
end

#module?Boolean

Returns If this specification is a module specification.

Returns:

  • (Boolean)

    If this specification is a module specification.



83
84
85
86
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 83

def module?
  return false unless @data.include?(Flutter::NAME)
  return @data[Flutter::NAME].is_a?(Hash) && @data[Flutter::NAME].include?('module')
end

#package_cache_pathString

Returns the path to the flutter project dependencies cache file.

Returns:

  • (String)

    the path to the flutter project dependencies cache file.



97
98
99
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 97

def package_cache_path
  File.join(project_path, Pub::TOOL_DIR, Pub::CACHE_FILE)
end

#pod_helper_pathString

Returns the path to the flutter project.

Returns:

  • (String)

    the path to the flutter project.



103
104
105
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 103

def pod_helper_path
  File.join(project_path, '.ios', Flutter::DIR_NAME, 'podhelper.rb') if module?
end

#project_pathString

Returns the path to the flutter project.

Returns:

  • (String)

    the path to the flutter project.



90
91
92
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 90

def project_path
  File.dirname(defined_in_file)
end

#pub_getConcurrent::Promises::Future, Nil

Runs ‘flutter pub get` on project directory concurrently.

Returns:

  • (Concurrent::Promises::Future, Nil)

    Nil if ‘pub get` running/completed, otherwise runs `flutter pub get` task in background and returns its future.



137
138
139
140
141
142
143
144
145
146
# File 'lib/cocoapods-embed-flutter/flutter/pubspec.rb', line 137

def pub_get
  future = @@current_pubgets[self]
  return nil if !future.nil?
  future = Concurrent::Promises.future do
    stdout, stderr, status = Open3.capture3('flutter pub get', :chdir => self.project_path)
    :result
  end
  @@current_pubgets[self] = future
  return Concurrent::Promises.zip(future, *all_dependencies.map(&:install).compact)
end