Skip to content

Commit e064192

Browse files
author
Soumya Mahunt
committed
docs: add code level documentation
1 parent 7e620f2 commit e064192

File tree

5 files changed

+233
-4
lines changed

5 files changed

+233
-4
lines changed

lib/cocoapods-embed-flutter/flutter/dependency.rb

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,37 @@
22

33
module Flutter
44
module Pub
5+
# Provides a DSL to describe a flutter dependency. A dependency is defined in
6+
# `pubspec.yaml` in `dependencies` or `dev_dependencies` sections.
7+
#
58
class Dependency
6-
attr_reader :name, :requirements, :parent_spec, :is_dev_dependency
9+
# @return [String] the name of the dependency.
10+
#
11+
attr_reader :name
12+
# @return [String, Hash] the requirements for
13+
# dependency as declred in parent `pubspec`.
14+
#
15+
attr_reader :requirements
16+
# @return [Spec] the parent specification where
17+
# dependency declared.
18+
#
19+
attr_reader :parent_spec
20+
# @return [Boolean] If this specification is an app specification.
21+
#
22+
attr_reader :is_dev_dependency
723

24+
# @param [String] name
25+
# the name of the specification.
26+
#
27+
# @param [String, Hash] requirements
28+
# the requirements for dependency as declred in `pubspec`
29+
#
30+
# @param [Spec] parent_specification
31+
# the parent specification where dependency declared
32+
#
33+
# @param [Boolean] is_dev_dependency
34+
# Whether the dependency only required during development
35+
#
836
def initialize(name, requirements, parent_spec, dev_dependency = false)
937
raise StandardError, 'A flutter dependency requires a name.' unless name
1038
raise StandardError, 'A flutter dependency requires a parent pubspec.' unless parent_spec.is_a?(Flutter::Pub::Spec)
@@ -14,19 +42,42 @@ def initialize(name, requirements, parent_spec, dev_dependency = false)
1442
@is_dev_dependency = dev_dependency
1543
end
1644

45+
# Returns dependencies from hash declared in `dependencies` or `dev_dependencies`
46+
# section in `pubspec.yaml` file.
47+
#
48+
# @param [Hash] hash declared in `dependencies` or `dev_dependencies`
49+
# section in `pubspec.yaml` file
50+
#
51+
# @param [Spec] parent_specification
52+
# the parent specification where dependency declared
53+
#
54+
# @param [Boolean] is_dev_dependency
55+
# Whether the dependency only required during development
56+
#
57+
# @return [Array<Dependency>] dependencies from hash declared in `dependencies`
58+
# or `dev_dependencies` section in `pubspec.yaml` file.
59+
#
1760
def self.create_from_hash(hash, parent_spec, dev_dependency = false)
1861
raise StandardError, 'A flutter dependency requires a parent pubspec.' unless parent_spec.is_a?(Flutter::Pub::Spec)
1962
hash.keys.map { |key| Dependency.new(key, hash[key], parent_spec, dev_dependency) }
2063
end
2164

65+
# @return [Boolean] If this dependency is a local flutter project.
66+
#
2267
def local?
2368
requirements.is_a?(Hash) && requirements.include?('path')
2469
end
2570

71+
# @return [Spec] for this dependency if this dependency is a local flutter project.
72+
#
2673
def spec
2774
Spec.find(name, File.expand_path(path, File.dirname(parent_spec.defined_in_file)))
2875
end
2976

77+
# Install this dependency for the parent project.
78+
#
79+
# @return void
80+
#
3081
def install
3182
spec.setup if local?
3283
end

lib/cocoapods-embed-flutter/flutter/downloader.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module Downloader
2121
# @param [Pathname,Nil] cache_path
2222
# the path used to cache pod downloads.
2323
#
24+
# @todo Implement caching for remote sources.
25+
#
2426
def self.download(
2527
request,
2628
target,

lib/cocoapods-embed-flutter/flutter/external_sources.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ module ExternalSources
1313
:http => [:flatten, :type, :sha256, :sha1, :headers].freeze,
1414
}.freeze
1515

16+
# Returns the path to `pubspec` with the given name and location to search.
17+
#
18+
# @param [String] name
19+
# the name of the project declared in `pubspec`.
20+
#
21+
# @param [Hash] options
22+
# requirement opttions for the source of project.
23+
#
24+
# @note the source of project can either be local or all the remotes
25+
# supported by `cocoapods-downloader`.
26+
#
27+
# @return [Spec] the `pubspec` with the given name satisfying
28+
# requirement options.
29+
#
1630
def self.fetchWithNameAndOptions(name, options)
1731
raise StandardError, 'A flutter module requires a name.' unless name
1832

lib/cocoapods-embed-flutter/flutter/pubspec.rb

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/cocoapods-embed-flutter/src/pub.rb

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,99 @@
44

55
module Pod
66
class Podfile
7+
# The Podfile is a specification that describes the dependencies of the
8+
# targets of one or more Xcode projects. With Embed Flutter
9+
# it is possible to declare flutter module as dependency
10+
#
11+
# A Podfile can be very simple:
12+
#
13+
# target 'MyApp'
14+
# pub 'flutter_module', :path => '../'
15+
#
16+
# An example of a more complex Podfile can be:
17+
#
18+
# platform :ios, '9.0'
19+
# inhibit_all_warnings!
20+
#
21+
# target 'MyApp' do
22+
# pub 'flutter_module', :path => '../'
23+
# end
24+
#
25+
# target 'MyAppTests' do
26+
# pub 'flutter_module_test', :path => '../'
27+
# end
28+
#
29+
# post_install do |installer|
30+
# installer.pods_project.targets.each do |target|
31+
# puts "#{target.name}"
32+
# end
33+
# end
34+
#
35+
#
36+
# @note Currently only one flutter module per target is
37+
# supported.
38+
#
739
module DSL
40+
# Specifies a flutter module dependency of the project.
41+
#
42+
# A dependency requirement is defined by the name of the module and
43+
# optionally a list of requirements.
44+
#
45+
#
46+
# ### Using the files from a local path.
47+
#
48+
# If you would like to use develop a flutter module in tandem with
49+
# its client project you can use the `path` option.
50+
#
51+
# pub 'flutter_module', :path => '../'
52+
#
53+
# Using this option Embed Flutter will assume the given folder
54+
# to be the root of the flutter module or the root of flutter module `pubspec` file
55+
# or points to the `pubspec` file itself and will link the files directly from there
56+
# in the Pods project. This means that your edits will persist to
57+
# CocoaPods installations.
58+
#
59+
# The referenced folder can be a checkout of your your favourite SCM or
60+
# even a git submodule of the current repository.
61+
#
62+
# Note that either the flutter module or the `pubspec` of the flutter module
63+
# can be in the folder. Optionally you can provide the `pubspec` file directly.
64+
#
65+
#
66+
# ### From a flutter module in the root of a library repository.
67+
#
68+
# Sometimes you may want to use the bleeding edge version of a module. Or a
69+
# specific revision. If this is the case, you can specify that with your
70+
# pub declaration.
71+
#
72+
# To use the `master` or `main` branch of the repository:
73+
#
74+
# pub 'flutter_module', :git => 'https://github.com/octokit/flutter_module.git'
75+
#
76+
#
77+
# To use a different branch of the repository:
78+
#
79+
# pub 'flutter_module', :git => 'https://github.com/octokit/flutter_module.git', :branch => 'dev'
80+
#
81+
#
82+
# To use a tag of the repository:
83+
#
84+
# pub 'flutter_module', :git => 'https://github.com/octokit/flutter_module.git', :tag => '0.7.0'
85+
#
86+
#
87+
# Or specify a commit:
88+
#
89+
# pub 'flutter_module', :git => 'https://github.com/octokit/flutter_module.git', :commit => '082f8319af'
90+
#
91+
# The flutter module or its `pubspec` file is expected to be in the
92+
# root of the repository.
93+
#
94+
#
95+
# @note This method allow a nil name and the raises to be more
96+
# informative.
97+
#
98+
# @return [void]
99+
#
8100
def pub(name = nil, *requirements)
9101
pubspec = Flutter::Pub::ExternalSources.fetchWithNameAndOptions(name, requirements)
10102
pubspec.setup

0 commit comments

Comments
 (0)