forked from FubuMvcArchive/buildsupport
/
nuget.rb
246 lines (206 loc) · 7.84 KB
/
nuget.rb
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
namespace :nuget do
buildsupport_path = File.dirname(__FILE__)
nuget = "#{buildsupport_path}/nuget.exe"
nugroot = File.expand_path(ENV['NUGET_HUB'] || "/nugs")
desc "Build the nuget package"
task :build do
rm Dir.glob("#{ARTIFACTS}/*.nupkg")
FileList["packaging/nuget/*.nuspec"].each do |spec|
sh "#{nuget} pack #{spec} -o #{ARTIFACTS} -Version #{BUILD_NUMBER} -Symbols"
end
end
desc "update dependencies from local machine"
task :pull, [:package] do |task, args|
Nuget.each_installed_package do |package|
next if args[:package] && Nuget.package_name(package).downcase != args[:package].downcase
src_package = File.join nugroot, Nuget.package_name(package)
if File.directory? src_package
['lib','tools'].each do |folder|
dst = File.join package, folder
src = File.join src_package, folder
if File.directory? src
clean_dir dst
cp_r src + "/.", dst, :verbose => false
after_nuget_update(Nuget.package_name(package), dst) if respond_to? :after_nuget_update
end
end
puts "pulled from #{src_package}"
else
puts "could not find #{src_package}"
end
end
end
desc "Updates dependencies from nuget.org"
task :update => [:update_packages, :clean, :update_callback]
task :update_packages do
FileList["**/*.sln"].each do |proj|
sh "#{nuget} update #{proj}"
end
FileList["**/packages.config"].each do |proj|
sh "#{nuget} install #{proj} -OutputDirectory #{Nuget.package_root}"
end
end
task :update_callback do
after_nuget_update(nil, Nuget.package_root) if respond_to? :after_nuget_update
end
desc "pushes dependencies to central location on local machine for nuget:pull from other repos"
task :push, [:package] => :build do |task, args|
FileList["#{ARTIFACTS}/*.nupkg"].exclude(".symbols.nupkg").each do |file|
next if args[:package] && Nuget.package_name(file).downcase != args[:package].downcase
destination = File.join nugroot, Nuget.package_name(file)
clean_dir destination
unzip_file file, destination
puts "pushed to #{destination}"
end
end
def clean_dir(path)
mkdir_p path, :verbose => false
rm_r Dir.glob(File.join(path, "*.*")), :verbose => false
end
def unzip_file (file, destination)
require 'rubygems'
require 'zip/zip'
Zip::ZipFile.open(file) { |zip_file|
zip_file.each { |f|
f_path=File.join(destination, f.name)
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
}
}
end
desc "Pushes nuget packages to the official feed"
task :release, [:package] do |t, args|
require 'open-uri'
release_path = "#{buildsupport_path}/nuget_release"
clean_dir release_path
artifact_url = "http://teamcity.codebetter.com/guestAuth/repository/downloadAll/#{@teamcity_build_id}/.lastSuccessful/artifacts.zip"
puts "downloading artifacts from #{artifact_url}"
artifact = open(artifact_url)
unzip_file artifact.path, release_path
FileList["#{release_path}/*.nupkg"].exclude(".symbols.nupkg").each do |nupkg|
next if args[:package] && Nuget.package_name(nupkg).downcase != args[:package].downcase
sh "#{nuget} push #{nupkg}" do |ok, res|
puts "May not have published #{nupkg}" unless ok
end
end
end
task :clean do
require 'rexml/document'
repo_paths = Nuget.repositories
packages = repo_paths.map{|repo| Nuget.packages(repo)}.reduce(:|)
Nuget.each_installed_package do |package|
name = Nuget.package_name(package)
tracked = packages.select{|p| p.include? name}
if tracked.any? # only remove folders for older versions of tracked packages
should_delete = !tracked.detect{|t| package.end_with? t[name]}
rm_rf package if should_delete
end
end
end
end
namespace :ripple do
@ripple_root = File.expand_path(File.join(File.dirname(__FILE__), "../../") + "/ripple")
@progress_file = "#{@ripple_root}/packages/ripple.txt"
task :local => ["nuget:pull", :ci, "nuget:push"]
task :public => [:gitupdate, "nuget:update", :ci, :commit, :manualsteps]
task :gitupdate do
#raise "Uncommitted changes in #{Dir.pwd}" unless `git status --porcelain`.strip.empty?
sh "git checkout master && git pull --ff-only" do |ok, status|
raise "Cannot pull latest into #{Dir.pwd}. You need to rebase or merge origin/master" unless ok
end
end
task :commit do
status = `git status --porcelain`
changes = status.split("\n").select{|l| l.start_with? "??"}.map{|l| l.match(/packages\/(.*)\//)[1]}
if changes.any?
msg = "Updated packages: #{changes.join(", ")}"
sh "git add -A"
sh "git commit -m \"#{msg}\""
else
puts "no changes to commit in #{Dir.pwd}"
end
end
task :manualsteps do
puts """
*******************************
1) Push your changes to github.
2) Optionally kick off the teamcity build manually (if it hasn't started automatically yet)
3) Wait for teamcity build to finish
4) Run rake ripple:publish to continue after publishing nuget packages publicly
or rake ripple:continue to continue without publishing publicly
"""
sh "start http://teamcity.codebetter.com/viewType.html?buildTypeId=#{@teamcity_build_id}"
end
task :continue do
job = get_next_job
if job
puts "mark #{job} as complete"
mark_complete job
run_in @ripple_root, "rake ripple:nextjob"
else
puts "no current job to continue"
end
end
task :publish => ["nuget:release", :continue]
def mark_complete(job)
jobs = get_ripple_jobs
raise "Job does not exist: #{job} -- cannot mark it complete" unless jobs.include? job
init_ripple_jobs(jobs - [job])
end
def get_next_job
jobs = get_ripple_jobs
return nil unless jobs.any?
jobs[0]
end
def get_ripple_jobs
File.readlines(@progress_file).map{|x| x.chomp}.reject{|x| x.empty?}
end
def init_ripple_jobs(jobs)
File.open(@progress_file, "w") do |file|
jobs.each{|job| file.puts job}
end
end
end
module Nuget
def self.each_installed_package
FileList[File.join(package_root, "*")].exclude{|f| File.file?(f)}.each do |package|
yield package
end
end
def self.packages(package_config)
xml = REXML::Document.new File.read package_config
xml.get_elements('packages/package').map do |p|
{ p.attributes['id'] => p.attributes['version'] }
end
end
def self.repositories
#get packages.config from nuget's repositories.config file in the package root
repo_file_path = File.join package_root, "repositories.config"
if FileTest.exists? repo_file_path
file = REXML::Document.new File.read repo_file_path
return file.get_elements("repositories/repository").map{|node| File.expand_path(File.join(package_root, node.attributes["path"]))}
end
#otherwise glob all the packages.config in the source directory
Dir.glob("{source,src}/**/packages.config")
end
def self.package_root
root = nil
packroots = Dir.glob("{source,src}/packages")
return packroots.last if packroots.length > 0
Dir.glob("{source,src}").each do |d|
packroot = File.join d, "packages"
FileUtils.mkdir_p(packroot)
root = packroot
end
root
end
def self.package_name(filename)
File.basename(filename, ".nupkg").gsub(/[\d.]+$/, "")
end
def self.tool(package, tool)
File.join(Dir.glob(File.join(package_root,"#{package}.*")).sort.last, "tools", tool)
end
end
def run_in(working_dir, cmd)
sh "buildsupport/run_in_path.cmd #{working_dir.gsub('/','\\')} #{cmd}"
end