1+ desc "Runs the specs [EMPTY]"
2+ task :spec do
3+ # Provide your own implementation
4+ end
5+
6+ task :version do
7+ git_remotes = `git remote` . strip . split ( "\n " )
8+
9+ if git_remotes . count > 0
10+ puts "-- fetching version number from github"
11+ sh 'git fetch'
12+
13+ remote_version = remote_spec_version
14+ end
15+
16+ if remote_version . nil?
17+ puts "There is no current released version. You're about to release a new Pod."
18+ version = "0.0.1"
19+ else
20+ puts "The current released version of your pod is " + remote_spec_version . to_s ( )
21+ version = suggested_version_number
22+ end
23+
24+ puts "Enter the version you want to release (" + version + ") "
25+ new_version_number = $stdin. gets . strip
26+ if new_version_number == ""
27+ new_version_number = version
28+ end
29+
30+ replace_version_number ( new_version_number )
31+ end
32+
33+ desc "Release a new version of the Pod"
34+ task :release do
35+
36+ puts "* Running version"
37+ sh "rake version"
38+
39+ unless ENV [ 'SKIP_CHECKS' ]
40+ if `git symbolic-ref HEAD 2>/dev/null` . strip . split ( '/' ) . last != 'master'
41+ $stderr. puts "[!] You need to be on the `master' branch in order to be able to do a release."
42+ exit 1
43+ end
44+
45+ if `git tag` . strip . split ( "\n " ) . include? ( spec_version )
46+ $stderr. puts "[!] A tag for version `#{ spec_version } ' already exists. Change the version in the podspec"
47+ exit 1
48+ end
49+
50+ puts "You are about to release `#{ spec_version } `, is that correct? [y/n]"
51+ exit if $stdin. gets . strip . downcase != 'y'
52+ end
53+
54+ puts "* Running specs"
55+ sh "rake spec"
56+
57+ puts "* Linting the podspec"
58+ sh "pod lib lint"
59+
60+ # Then release
61+ sh "git commit #{ podspec_path } CHANGELOG.md -m 'Release #{ spec_version } '"
62+ sh "git tag -a #{ spec_version } -m 'Release #{ spec_version } '"
63+ sh "git push origin master"
64+ sh "git push origin --tags"
65+ sh "pod push master #{ podspec_path } "
66+ end
67+
68+ # @return [Pod::Version] The version as reported by the Podspec.
69+ #
70+ def spec_version
71+ require 'cocoapods'
72+ spec = Pod ::Specification . from_file ( podspec_path )
73+ spec . version
74+ end
75+
76+ # @return [Pod::Version] The version as reported by the Podspec from remote.
77+ #
78+ def remote_spec_version
79+ require 'cocoapods-core'
80+
81+ if spec_file_exist_on_remote?
82+ remote_spec = eval ( `git show origin/master:#{ podspec_path } ` )
83+ remote_spec . version
84+ else
85+ nil
86+ end
87+ end
88+
89+ # @return [Bool] If the remote repository has a copy of the podpesc file or not.
90+ #
91+ def spec_file_exist_on_remote?
92+ test_condition = `if git rev-parse --verify --quiet origin/master:#{ podspec_path } >/dev/null;
93+ then
94+ echo 'true'
95+ else
96+ echo 'false'
97+ fi`
98+
99+ 'true' == test_condition . strip
100+ end
101+
102+ # @return [String] The relative path of the Podspec.
103+ #
104+ def podspec_path
105+ podspecs = Dir . glob ( '*.podspec' )
106+ if podspecs . count == 1
107+ podspecs . first
108+ else
109+ raise "Could not select a podspec"
110+ end
111+ end
112+
113+ # @return [String] The suggested version number based on the local and remote version numbers.
114+ #
115+ def suggested_version_number
116+ if spec_version != remote_spec_version
117+ spec_version . to_s ( )
118+ else
119+ next_version ( spec_version ) . to_s ( )
120+ end
121+ end
122+
123+ # @param [Pod::Version] version
124+ # the version for which you need the next version
125+ #
126+ # @note It is computed by bumping the last component of the versino string by 1.
127+ #
128+ # @return [Pod::Version] The version that comes next after the version supplied.
129+ #
130+ def next_version ( version )
131+ version_components = version . to_s ( ) . split ( "." ) ;
132+ last = ( version_components . last . to_i ( ) + 1 ) . to_s
133+ version_components [ -1 ] = last
134+ Pod ::Version . new ( version_components . join ( "." ) )
135+ end
136+
137+ # @param [String] new_version_number
138+ # the new version number
139+ #
140+ # @note This methods replaces the version number in the podspec file with a new version number.
141+ #
142+ # @return void
143+ #
144+ def replace_version_number ( new_version_number )
145+ text = File . read ( podspec_path )
146+ text . gsub! ( /(s.version( )*= ")#{ spec_version } (")/ , "\\ 1#{ new_version_number } \\ 3" )
147+ File . open ( podspec_path , "w" ) { |file | file . puts text }
148+ end
0 commit comments