Rakefile 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # FreeBSD Ports
  2. #
  3. # Douglas Thrift
  4. #
  5. # Rakefile
  6. require 'date'
  7. require 'ostruct'
  8. require 'pathname'
  9. require 'pry'
  10. require 'set'
  11. Color = Pry::Helpers::Text
  12. def in_portdir(&block)
  13. portdir = Pathname.new(Rake.original_dir)
  14. raise "#{portdir} is not a port directory" unless portdir.relative_path_from(@portsdir).to_s =~ %r{[^/]+/[^/]+}
  15. Dir.chdir(portdir) do
  16. block.call(portdir)
  17. end
  18. end
  19. def in_portstree(portstree, &block)
  20. portstree_zfs = `zfs get -H -o name mountpoint #{portstree.path}`.chomp
  21. sh "sudo zfs snapshot #{portstree_zfs}@clean"
  22. Dir.chdir(portstree.path) do
  23. block.call(portstree.path)
  24. end
  25. ensure
  26. sh "sudo zfs rollback #{portstree_zfs}@clean"
  27. sh "sudo zfs destroy #{portstree_zfs}@clean"
  28. end
  29. def update_portstree_map(map = {})
  30. `poudriere ports -l -q`.each_line do |line|
  31. line.chomp!
  32. if line =~ /^([^ ]+) +([^ ]+) +([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}) +([^ ]+)$/
  33. map[$1.to_sym] = OpenStruct.new(method: $2, timestamp: DateTime.parse($3), path: Pathname.new($4))
  34. else
  35. raise "unexpected line: #{line}"
  36. end
  37. end
  38. map
  39. end
  40. def clean_version(version)
  41. if version =~ /^(\d+\.\d+-RELEASE)(?:-p\d+)$/
  42. $1
  43. else
  44. version
  45. end
  46. end
  47. portstree_map = update_portstree_map
  48. portsdir = @portsdir = Pathname.pwd
  49. distdir = portsdir.join('distfiles')
  50. task default: %i(update_githead update_svnhead)
  51. desc 'Create or update the port checksum file (distinfo)'
  52. task :makesum do
  53. in_portdir do
  54. sh "make makesum DEV_WARNING_WAIT=0 DISTDIR=#{distdir}"
  55. end
  56. end
  57. %w(md5 sha1 sha256).each do |sum|
  58. desc "Show the #{sum.upcase} sums of the distfiles"
  59. task sum do
  60. sh "mkdir -p #{distdir}" unless distdir.exist?
  61. sh "#{sum} #{distdir.join('*')}"
  62. end
  63. end
  64. desc 'Remove the ports distfiles'
  65. task :distclean do
  66. in_portdir do
  67. sh "make distclean DISTDIR=#{distdir}"
  68. end
  69. end
  70. desc 'Verify the port using portlint'
  71. task portlint: :update_githead do
  72. in_portdir do |portdir|
  73. if githead_portstree.path.join(portdir.relative_path_from(portsdir), 'Makefile').exist?
  74. sh "portlint -C"
  75. else
  76. sh "portlint -A"
  77. end
  78. end
  79. end
  80. desc 'Show the port differences'
  81. task diff: :update_svnhead do |_, args|
  82. patch = args.extras.find {|arg| arg == 'patch'}
  83. in_portdir do |portdir|
  84. new_files = Set.new(`git ls-files`.each_line.map {|line| Pathname.new(line.chomp)})
  85. in_portstree(svnhead_portstree) do |path|
  86. rel_portdir = portdir.relative_path_from(portsdir)
  87. svnhead_portdir = path.join(rel_portdir)
  88. if svnhead_portdir.exist?
  89. old_files = Set.new(`svn ls -R #{svnhead_portdir}`.each_line.reject {|line| line =~ %r{/$}}.map {|line| Pathname.new(line.chomp)})
  90. else
  91. old_files = Set.new
  92. end
  93. sh "sudo mkdir -p #{svnhead_portdir}"
  94. Dir.chdir(svnhead_portdir) do
  95. new_files.each do |file|
  96. portdir_file = portdir.join(file)
  97. sh "sudo mkdir -p #{file.dirname}" unless file.dirname.exist?
  98. sh "sudo cp #{portdir_file} #{file}"
  99. end
  100. (new_files - old_files).each do |file|
  101. sh "sudo svn add --parents #{file}"
  102. end
  103. (old_files - new_files).each do |file|
  104. sh "sudo svn remove #{file}"
  105. end
  106. unless old_files.empty?
  107. if patch
  108. sh "svn diff > #{portdir.dirname.join("#{`make -V PKGNAME`.chomp}.diff")}"
  109. else
  110. sh "svn diff"
  111. end
  112. end
  113. end
  114. if old_files.empty?
  115. if patch
  116. sh "svn diff #{rel_portdir} > #{portsdir.join("#{portdir.basename}.diff")}"
  117. else
  118. sh "svn diff #{rel_portdir}"
  119. end
  120. end
  121. end
  122. end
  123. end
  124. desc 'Test the port with poudriere'
  125. task :testport, %i(jailname flavor) => :update_githead do |_, args|
  126. this_version = clean_version(`uname -r`.chomp)
  127. this_arch = `uname -m`.chomp
  128. this_jailname = nil
  129. `poudriere jail -l -q`.each_line do |line|
  130. line.chomp!
  131. if line =~ /^([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+).+$/
  132. jailname, version, arch, method = $1, $2, $3, $4
  133. if clean_version(version) == this_version && arch == this_arch && method == 'ftp'
  134. this_jailname = jailname
  135. break
  136. end
  137. else
  138. raise "unexpected line: #{line}"
  139. end
  140. end
  141. args.with_defaults(jailname: this_jailname)
  142. in_portdir do |portdir|
  143. files = `git ls-files`.each_line.map {|line| Pathname.new(line.chomp)}
  144. in_portstree(githead_portstree) do |path|
  145. rel_portdir = portdir.relative_path_from(portsdir)
  146. githead_portdir = path.join(rel_portdir)
  147. sh "sudo mkdir -p #{githead_portdir}"
  148. Dir.chdir(githead_portdir) do
  149. files.each do |file|
  150. portdir_file = portdir.join(file)
  151. sh "sudo mkdir -p #{file.dirname}" unless file.dirname.exist?
  152. sh "sudo cp #{portdir_file} #{file}"
  153. end
  154. end
  155. sh "sudo poudriere testport -j #{args.jailname} -o #{rel_portdir}#{args.flavor && "@#{args.flavor}"} -p githead"
  156. end
  157. end
  158. end
  159. {githead: 'git+https', svnhead: 'svn+https'}.each do |name, method|
  160. define_method("#{name}_portstree") do
  161. portstree_map[name]
  162. end
  163. desc "Create or update the #{name} portstree"
  164. task "update_#{name}" do |_, args|
  165. force = args.extras.find {|arg| arg == 'force'}
  166. portstree = portstree_map[name]
  167. if portstree
  168. raise "unexpected portstree method for #{name}: #{portstree[:method]} (expected: #{method})" if portstree[:method] != method
  169. if force || portstree.timestamp < Date.today
  170. sh "sudo poudriere ports -u -p #{name}"
  171. end
  172. else
  173. sh "sudo poudriere ports -c -m #{method} -p #{name}"
  174. update_portstree_map(portstree_map)
  175. end
  176. end
  177. end