@@ -10,14 +10,41 @@ module Pub
1010 TOOL_DIR = '.dart_tool' . freeze
1111 CACHE_FILE = 'package_config.json' . freeze
1212
13+ # The Specification provides a DSL to describe a flutter project.
14+ # A project is defined as a library originating from a source.
15+ # A specification can support detailed attributes for modules of code
16+ # through dependencies.
17+ #
18+ # Usually it is stored in `pubspec.yaml` file.
19+ #
1320 class Spec
21+ # @return [String] the path where the specification is defined, if loaded
22+ # from a file.
23+ #
1424 attr_reader :defined_in_file
1525
26+ # @param [String] path
27+ # the path to the specification.
28+ #
1629 def initialize ( path )
1730 @data = YAML . load_file path
1831 @defined_in_file = path
1932 end
2033
34+ # Returns the path to `pubspec` with the given name and location to search.
35+ #
36+ # @param [String] name
37+ # the name of the project declared in `pubspec`.
38+ #
39+ # @param [String] path
40+ # where project or pubspec is located.
41+ #
42+ # @note either the flutter module or the `pubspec` of the flutter module
43+ # can be in the path. Optionally you can provide the `pubspec`
44+ # file directly.
45+ #
46+ # @return [String] path to the `pubspec` with the given name if present.
47+ #
2148 def self . find_file ( name , path )
2249 path = File . expand_path ( path , Dir . pwd )
2350
@@ -32,6 +59,20 @@ def self.find_file(name, path)
3259 end
3360 end
3461
62+ # Returns the path to `pubspec` with the given name and location to search.
63+ #
64+ # @param [String] name
65+ # the name of the project declared in `pubspec`.
66+ #
67+ # @param [String] path
68+ # where project or pubspec is located.
69+ #
70+ # @note either the flutter module or the `pubspec` of the flutter module
71+ # can be in the path. Optionally you can provide the `pubspec`
72+ # file directly.
73+ #
74+ # @return [Spec] the `pubspec` with the given name if present.
75+ #
3576 def self . find ( name , path )
3677 pubspec_path = find_file ( name , path )
3778 raise StandardError , "Invalid path: '#{ path } ' for flutter module: '#{ name } '." unless pubspec_path
@@ -40,47 +81,76 @@ def self.find(name, path)
4081 return pubspec
4182 end
4283
84+ # @return [Boolean] If this specification is a module specification.
85+ #
4386 def module?
4487 return false unless @data . include? ( Flutter ::NAME )
4588 return @data [ Flutter ::NAME ] . is_a? ( Hash ) && @data [ Flutter ::NAME ] . include? ( 'module' )
4689 end
4790
91+ # @return [String] the path to the flutter project.
92+ #
4893 def project_path
4994 File . dirname ( defined_in_file )
5095 end
5196
97+ # @return [String] the path to the flutter project
98+ # dependencies cache file.
99+ #
52100 def package_cache_path
53101 File . join ( project_path , Pub ::TOOL_DIR , Pub ::CACHE_FILE )
54102 end
55103
104+ # @return [String] the path to the flutter project.
105+ #
56106 def pod_helper_path
57107 File . join ( project_path , '.ios' , Flutter ::DIR_NAME , 'podhelper.rb' ) if module?
58108 end
59109
110+ # @return [Array<Dependency>] the list of all the projects this
111+ # specification depends upon and are included in app release.
112+ #
60113 def dependencies
61114 return [ ] unless @data . include? ( 'dependencies' )
62- Flutter :: Pub :: Dependency . create_from_hash ( @data [ 'dependencies' ] , self )
115+ Dependency . create_from_hash ( @data [ 'dependencies' ] , self )
63116 end
64117
118+ # @return [Array<Dependency>] the list of all the projects this
119+ # specification depends upon only during development.
120+ #
65121 def dev_dependencies
66122 return [ ] unless @data . include? ( 'dev_dependencies' )
67- Flutter :: Pub :: Dependency . create_from_hash ( @data [ 'dev_dependencies' ] , self )
123+ Dependency . create_from_hash ( @data [ 'dev_dependencies' ] , self )
68124 end
69125
126+ # @return [Array<Dependency>] the list of all the projects this
127+ # specification depends upon.
128+ #
70129 def all_dependencies
71130 dependencies + dev_dependencies
72131 end
73132
133+ # @return [Boolean] If the flutter project for this specification
134+ # has all its dependencies installed.
135+ #
74136 def setup?
75137 File . exists? ( package_cache_path ) && ( !module? || File . exists? ( pod_helper_path ) )
76138 end
77139
140+ # Sets up the project installing all specified dependencies.
141+ #
142+ # @return void
143+ #
78144 def setup
79145 return if setup?
80146 pup_get
81147 all_dependencies . each ( &:install )
82- end
148+ end
83149
150+ # Runs `flutter pub get` on project directory.
151+ #
152+ # @return void
153+ #
84154 def pup_get
85155 Dir . chdir ( project_path ) { |path | system ( 'flutter pub get' , exception : true ) }
86156 end
0 commit comments