11require 'cocoapods-embed-flutter/flutter'
22require 'yaml'
3+ require 'open3'
4+ require 'concurrent'
5+ require 'cocoapods'
36
47module Flutter
58 module Pub
@@ -43,7 +46,8 @@ def self.find_file(name, path)
4346
4447 if File . basename ( path ) == Pub ::SPEC_FILE
4548 return path
46- elsif Dir . exists? ( File . expand_path ( name , path ) ) && File . exists? ( File . expand_path ( Pub ::SPEC_FILE , File . expand_path ( name , path ) ) )
49+ elsif Dir . exists? ( File . expand_path ( name , path ) ) &&
50+ File . exists? ( File . expand_path ( Pub ::SPEC_FILE , File . expand_path ( name , path ) ) )
4751 return File . expand_path ( Pub ::SPEC_FILE , File . expand_path ( name , path ) )
4852 elsif File . exists? ( File . expand_path ( Pub ::SPEC_FILE , path ) )
4953 return File . expand_path ( Pub ::SPEC_FILE , path )
@@ -88,7 +92,7 @@ def project_path
8892 end
8993
9094 # @return [String] the path to the flutter project
91- # dependencies cache file.
95+ # dependencies cache file.
9296 #
9397 def package_cache_path
9498 File . join ( project_path , Pub ::TOOL_DIR , Pub ::CACHE_FILE )
@@ -101,7 +105,7 @@ def pod_helper_path
101105 end
102106
103107 # @return [Array<Dependency>] the list of all the projects this
104- # specification depends upon and are included in app release.
108+ # specification depends upon and are included in app release.
105109 #
106110 def dependencies
107111 return [ ] unless @data . include? ( 'dependencies' )
@@ -123,37 +127,66 @@ def all_dependencies
123127 dependencies + dev_dependencies
124128 end
125129
126- # @return [Boolean] If the flutter project for this specification
127- # has all its dependencies installed.
130+ # Runs `flutter pub get` on project directory concurrently.
128131 #
129- def setup?
130- File . exists? ( package_cache_path ) && ( !module? || File . exists? ( pod_helper_path ) )
132+ # @return [Concurrent::Promises::Future, Nil]
133+ # {Nil} if `pub get` running/completed, otherwise
134+ # runs `flutter pub get` task in background
135+ # and returns its future.
136+ #
137+ def pub_get
138+ future = @@current_pubgets [ self ]
139+ return nil if !future . nil?
140+ future = Concurrent ::Promises . future do
141+ stdout , stderr , status = Open3 . capture3 ( 'flutter pub get' , :chdir => self . project_path )
142+ :result
143+ end
144+ @@current_pubgets [ self ] = future
145+ return Concurrent ::Promises . zip ( future , *all_dependencies . map ( &:install ) . compact )
131146 end
132147
133- # Sets up the project installing all specified dependencies .
148+ # See if two {Spec} instances refer to the same pubspecs .
134149 #
135- # @return [void]
150+ # @return [Boolean] whether or not the two {Spec} instances refer to the
151+ # same projects.
136152 #
137- def setup
138- return if setup?
139- pup_get
140- all_dependencies . each ( & :install )
153+ def == ( other )
154+ self . class === other &&
155+ other . defined_in_file == defined_in_file &&
156+ other . instance_variable_get ( :@data ) == @data
141157 end
142158
143- # Runs `flutter pub get` on project directory.
144- #
145- # @return [void]
159+ # @return [Fixnum] A hash identical for equals objects.
146160 #
147- def pup_get
148- Dir . chdir ( project_path ) { | path | system ( 'flutter pub get' , exception : true ) }
161+ def hash
162+ [ defined_in_file , @data ] . hash
149163 end
150164
165+ alias eql? ==
166+
167+ # Allows accessing top level values in `pubspec.yaml`,
168+ # i.e. name, description, version etc.
169+ #
170+ # @param [Symbol] m
171+ # top level key value to access,
172+ # i.e. name, description etc.
173+ #
174+ # @return depending on accessed value type in `pubspec.yaml`.
175+ #
176+ # @raise [NoMethodError] if no method or custom attribute exists by
177+ # the attribute name in pubspec.
178+ #
151179 def method_missing ( m , *args , &block )
152180 if @data . include? ( m . to_s )
153181 return @data [ m . to_s ]
154182 end
155183 super . method_missing ( m , *args , &block )
156184 end
185+
186+ private
187+
188+ # A hash containing all `pub get` promises.
189+ @@current_pubgets = { }
157190 end
158191 end
159192end
0 commit comments