1
1
require 'cocoapods-embed-flutter/flutter'
2
2
require 'yaml'
3
+ require 'open3'
4
+ require 'concurrent'
5
+ require 'cocoapods'
3
6
4
7
module Flutter
5
8
module Pub
@@ -43,7 +46,8 @@ def self.find_file(name, path)
43
46
44
47
if File . basename ( path ) == Pub ::SPEC_FILE
45
48
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 ) ) )
47
51
return File . expand_path ( Pub ::SPEC_FILE , File . expand_path ( name , path ) )
48
52
elsif File . exists? ( File . expand_path ( Pub ::SPEC_FILE , path ) )
49
53
return File . expand_path ( Pub ::SPEC_FILE , path )
@@ -88,7 +92,7 @@ def project_path
88
92
end
89
93
90
94
# @return [String] the path to the flutter project
91
- # dependencies cache file.
95
+ # dependencies cache file.
92
96
#
93
97
def package_cache_path
94
98
File . join ( project_path , Pub ::TOOL_DIR , Pub ::CACHE_FILE )
@@ -101,7 +105,7 @@ def pod_helper_path
101
105
end
102
106
103
107
# @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.
105
109
#
106
110
def dependencies
107
111
return [ ] unless @data . include? ( 'dependencies' )
@@ -123,37 +127,66 @@ def all_dependencies
123
127
dependencies + dev_dependencies
124
128
end
125
129
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.
128
131
#
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 )
131
146
end
132
147
133
- # Sets up the project installing all specified dependencies .
148
+ # See if two {Spec} instances refer to the same pubspecs .
134
149
#
135
- # @return [void]
150
+ # @return [Boolean] whether or not the two {Spec} instances refer to the
151
+ # same projects.
136
152
#
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
141
157
end
142
158
143
- # Runs `flutter pub get` on project directory.
144
- #
145
- # @return [void]
159
+ # @return [Fixnum] A hash identical for equals objects.
146
160
#
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
149
163
end
150
164
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
+ #
151
179
def method_missing ( m , *args , &block )
152
180
if @data . include? ( m . to_s )
153
181
return @data [ m . to_s ]
154
182
end
155
183
super . method_missing ( m , *args , &block )
156
184
end
185
+
186
+ private
187
+
188
+ # A hash containing all `pub get` promises.
189
+ @@current_pubgets = { }
157
190
end
158
191
end
159
192
end
0 commit comments