Rakefile 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. portstree_map = update_portstree_map
  41. portsdir = @portsdir = Pathname.pwd
  42. distdir = portsdir.join('distfiles')
  43. task default: %i(update_githead update_svnhead)
  44. desc 'Create or update the port checksum file (distinfo)'
  45. task :makesum do
  46. in_portdir do
  47. sh "make makesum DEV_WARNING_WAIT=0 DISTDIR=#{distdir}"
  48. end
  49. end
  50. %w(md5 sha1 sha256).each do |sum|
  51. desc "Show the #{sum.upcase} sums of the distfiles"
  52. task sum do
  53. sh "mkdir -p #{distdir}" unless distdir.exist?
  54. sh "#{sum} #{distdir.join('*')}"
  55. end
  56. end
  57. desc 'Remove the ports distfiles'
  58. task :distclean do
  59. in_portdir do
  60. sh "make distclean DISTDIR=#{distdir}"
  61. end
  62. end
  63. desc 'Verify the port using portlint'
  64. task portlint: :update_githead do
  65. in_portdir do |portdir|
  66. if githead_portstree.path.join(portdir.relative_path_from(portsdir), 'Makefile').exist?
  67. sh "portlint -C"
  68. else
  69. sh "portlint -A"
  70. end
  71. end
  72. end
  73. desc 'Show the port differences'
  74. task diff: :update_svnhead do |_, args|
  75. patch = args.extras.find {|arg| arg == 'patch'}
  76. in_portdir do |portdir|
  77. new_files = Set.new(`git ls-files`.each_line.map{|line| Pathname.new(line.chomp)})
  78. in_portstree(svnhead_portstree) do |path|
  79. rel_portdir = portdir.relative_path_from(portsdir)
  80. svnhead_portdir = path.join(rel_portdir)
  81. if svnhead_portdir.exist?
  82. old_files = Set.new(`svn ls -R #{svnhead_portdir}`.each_line.reject {|line| line =~ %r{/$}}.map {|line| Pathname.new(line.chomp)})
  83. else
  84. old_files = Set.new
  85. end
  86. sh "sudo mkdir -p #{svnhead_portdir}"
  87. Dir.chdir(svnhead_portdir) do
  88. new_files.each do |file|
  89. portdir_file = portdir.join(file)
  90. sh "sudo mkdir -p #{file.dirname}" unless file.dirname.exist?
  91. sh "sudo cp #{portdir_file} #{file}"
  92. end
  93. (old_files - new_files).each do |file|
  94. sh "sudo svn add --parents #{file}"
  95. end
  96. (new_files - old_files).each do |file|
  97. sh "sudo svn remove #{file}"
  98. end
  99. unless old_files.empty?
  100. if patch
  101. sh "svn diff > #{portdir.dirname.join("#{`make -V PKGNAME`.chomp}.diff")}"
  102. else
  103. sh "svn diff"
  104. end
  105. end
  106. end
  107. if old_files.empty?
  108. if patch
  109. sh "svn diff #{rel_portdir} > #{portsdir.join("#{portdir.basename}.diff")}"
  110. else
  111. sh "svn diff #{rel_portdir}"
  112. end
  113. end
  114. end
  115. end
  116. end
  117. {githead: 'git+https', svnhead: 'svn+https'}.each do |name, method|
  118. define_method("#{name}_portstree") do
  119. portstree_map[name]
  120. end
  121. desc "Create or update the #{name} portstree"
  122. task "update_#{name}" do |_, args|
  123. force = args.extras.find {|arg| arg == 'force'}
  124. portstree = portstree_map[name]
  125. if portstree
  126. raise "unexpected portstree method for #{name}: #{portstree[:method]} (expected: #{method})" if portstree[:method] != method
  127. if force || portstree.timestamp < Date.today
  128. sh "sudo poudriere ports -u -p #{name}"
  129. end
  130. else
  131. sh "sudo poudriere ports -c -m #{method} -p #{name}"
  132. update_portstree_map(portstree_map)
  133. end
  134. end
  135. end