forked from krzysztofzablocki/Sourcery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
164 lines (134 loc) · 5.46 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/rake
## Most of this code is adapted from Sourcery https://github.com/AliSoftware/Sourcery/blob/master/Rakefile
require 'pathname'
require 'yaml'
require 'json'
require 'net/http'
require 'uri'
BUILD_DIR = 'build/'
## [ Utils ] ##################################################################
def version_select
# Find all Xcode 8 versions on this computer
xcodes = `mdfind "kMDItemCFBundleIdentifier = 'com.apple.dt.Xcode' && kMDItemVersion = '8.*'"`.chomp.split("\n")
if xcodes.empty?
raise "\n[!!!] You need to have Xcode 8.x to compile Sourcery.\n\n"
end
# Order by version and get the latest one
vers = lambda { |path| `mdls -name kMDItemVersion -raw "#{path}"` }
latest_xcode_version = xcodes.sort { |p1, p2| vers.call(p1) <=> vers.call(p2) }.last
%Q(DEVELOPER_DIR="#{latest_xcode_version}/Contents/Developer" TOOLCHAINS=com.apple.dt.toolchain.XcodeDefault.xctoolchain)
end
def xcpretty(cmd)
if `which xcpretty` && $?.success?
sh "set -o pipefail && #{cmd} | xcpretty -c"
else
sh cmd
end
end
def xcrun(cmd)
xcpretty "#{version_select} xcrun #{cmd}"
end
def print_info(str)
(red,clr) = (`tput colors`.chomp.to_i >= 8) ? %W(\e[33m \e[m) : ["", ""]
puts red, "== #{str.chomp} ==", clr
end
## [ Tests & Clean ] ##########################################################
desc "Run the Unit Tests"
task :tests do
print_info "Running Unit Tests"
xcrun %Q(xcodebuild -workspace Sourcery.xcworkspace -scheme Sourcery -sdk macosx test)
end
desc "Delete the build/ directory"
task :clean do
sh %Q(rm -fr build)
end
task :build do
print_info "Building project"
xcrun %Q(xcodebuild -workspace Sourcery.xcworkspace -scheme Sourcery-Release -sdk macosx -derivedDataPath #{BUILD_DIR}/tmp/)
sh %Q(rm -fr bin/Sourcery.app)
`mv #{BUILD_DIR}tmp/Build/Products/Release/Sourcery.app bin/`
sh %Q(rm -fr #{BUILD_DIR}tmp/)
end
## [ Release ] ##########################################################
namespace :release do
desc 'Create a new release on GitHub, CocoaPods and Homebrew'
task :new => [:check_versions, :build, :tests, :github, :cocoapods]
def podspec_version(file = 'Sourcery')
JSON.parse(`bundle exec pod ipc spec #{file}.podspec`)["version"]
end
def log_result(result, label, error_msg)
if result
puts "#{label.ljust(25)} \u{2705}"
else
puts "#{label.ljust(25)} \u{274C} - #{error_msg}"
end
result
end
desc 'Check if all versions from the podspecs and CHANGELOG match'
task :check_versions do
results = []
# Check if bundler is installed first, as we'll need it for the cocoapods task (and we prefer to fail early)
`which bundler`
results << log_result( $?.success?, 'Bundler installed', 'Please install bundler using `gem install bundler` and run `bundle install` first.')
# Extract version from Sourcery.podspec
version = podspec_version
puts "#{'Sourcery.podspec'.ljust(25)} \u{1F449} #{version}"
# Check if entry present in CHANGELOG
changelog_entry = system(%Q{grep -q '^## #{Regexp.quote(version)}$' CHANGELOG.md})
results << log_result(changelog_entry, "CHANGELOG, Entry added", "Please add an entry for #{version} in CHANGELOG.md")
changelog_master = system(%q{grep -qi '^## Master' CHANGELOG.md})
results << log_result(!changelog_master, "CHANGELOG, No master", 'Please remove entry for master in CHANGELOG')
exit 1 unless results.all?
print "Release version #{version} [Y/n]? "
exit 2 unless (STDIN.gets.chomp == 'Y')
end
desc 'Create a zip containing all the prebuilt binaries'
task :zip => [:clean] do
sh %Q(mkdir -p "build")
sh %Q(mkdir -p "build/Resources")
sh %Q(cp -r bin build/)
sh %Q(cp -r Templates build/)
`cp LICENSE README.md CHANGELOG.md build`
`cp Resources/daemon.gif Resources/icon-128.png build/Resources`
`cd build; zip -r -X sourcery-#{podspec_version}.zip .`
end
def post(url, content_type)
uri = URI.parse(url)
req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' => content_type})
yield req if block_given?
req.basic_auth 'krzysztofzablocki', File.read('.apitoken').chomp
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => (uri.scheme == 'https')) do |http|
http.request(req)
end
unless response.code == '201'
puts "Error: #{response.code} - #{response.message}"
puts response.body
exit 3
end
JSON.parse(response.body)
end
desc 'Upload the zipped binaries to a new GitHub release'
task :github => :zip do
v = podspec_version
changelog = `sed -n /'^## #{v}$'/,/'^## '/p CHANGELOG.md`.gsub(/^## .*$/,'').strip
print_info "Releasing version #{v} on GitHub"
puts changelog
json = post('https://api.github.com/repos/krzysztofzablocki/Sourcery/releases', 'application/json') do |req|
req.body = { :tag_name => v, :name => v, :body => changelog, :draft => false, :prerelease => false }.to_json
end
upload_url = json['upload_url'].gsub(/\{.*\}/,"?name=Sourcery-#{v}.zip")
zipfile = "build/Sourcery-#{v}.zip"
zipsize = File.size(zipfile)
print_info "Uploading ZIP (#{zipsize} bytes)"
post(upload_url, 'application/zip') do |req|
req.body_stream = File.open(zipfile, 'rb')
req.add_field('Content-Length', zipsize)
req.add_field('Content-Transfer-Encoding', 'binary')
end
end
desc 'pod trunk push Sourcery to CocoaPods'
task :cocoapods do
print_info "Pushing pod to CocoaPods Trunk"
sh 'bundle exec pod trunk push Sourcery.podspec --allow-warnings'
end
end