Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 20th, 2012  |  syntax: None  |  size: 4.20 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # Spoutcraft development Rake tasks
  2. # Usage: rake -T
  3. #
  4. # These tasks assume the following folder structure:
  5. #   spoutcraft/
  6. #     lib/
  7. #       spoutcraft-api.jar  A compatible SpoutcraftAPI package
  8. #     mcp/                  Minecraft Coder Pack
  9. #       conf/               Symlink to ../conf
  10. #       lib/                Symlink to ../lib
  11. #     mojang/               Folders corresponding to Minecraft versions
  12. #       1.1/bin/            This Minecraft version's bin folder
  13. #
  14. # Your workflow will probably look something like this:
  15. #
  16. #   1. rake mcp:setup
  17. #   2. rake mcp:decompile
  18. #   3. Make some changes to the Spoutcraft source
  19. #   4. rake mcp:merge
  20. #   5. rake mcp:recompile
  21. #   6. rake mcp:reobfuscate
  22. #   7. rake mcp:client to test your changes
  23. #
  24. # Rinse and repeat!
  25.  
  26. require "fileutils"
  27. require "logger"
  28. require "open3"
  29. require "pathname"
  30. require "shellwords"
  31.  
  32. module Spoutcraft
  33.   VERSION = "1.1"
  34.  
  35.   class << self
  36.     attr_accessor :root, :logger
  37.    
  38.     def capture(command, options = {})
  39.       options = { :chdir => root }.merge(options)
  40.       debug "Executing `#{command}`..."
  41.       output, status = Open3.capture2e(command.to_s, :chdir => options[:chdir])
  42.       method = if status.exitstatus != 0 then :error else :debug end
  43.       output.each_line { |line| send(method, line.chomp) }
  44.       send method, "Exited with status #{status.exitstatus} (PID #{status.pid})"
  45.       [output, status]
  46.     end
  47.   end
  48.  
  49.   module MCP
  50.     class << self
  51.       def root
  52.         Spoutcraft.root.join("mcp")
  53.       end
  54.      
  55.       def run(command, *args)
  56.         command = root.join("#{command}.sh")
  57.         die! "Script not found: #{command}" unless command.exist?
  58.         command = "#{command} #{args.join(" ")}" unless args.empty?
  59.         Spoutcraft.capture(command, :chdir => root)
  60.       end
  61.     end
  62.   end
  63. end
  64.  
  65. def log(level, *args)
  66.   Spoutcraft.logger.send(level, args.join(" "))
  67. end
  68.  
  69. def error(*args)
  70.   log(:error, *args)
  71. end
  72.  
  73. def info(*args)
  74.   log(:info, *args)
  75. end
  76.  
  77. def debug(*args)
  78.   log(:debug, *args)
  79. end
  80.  
  81. def debug?
  82.   $DEBUG
  83. end
  84.  
  85. def die!(message = nil)
  86.   error message if message
  87.   error "Rake task stopped"
  88.   exit 1
  89. end
  90.  
  91. Spoutcraft.root = Pathname.new(File.dirname(__FILE__))
  92. Spoutcraft.logger = Logger.new(STDOUT)
  93. Spoutcraft.logger.level = if debug? then Logger::DEBUG else Logger::INFO end
  94. FileUtils = FileUtils::Verbose if debug?
  95.  
  96. namespace :mcp do
  97.   desc "Clean build artifacts"
  98.   task :clean do
  99.     Spoutcraft::MCP.run(:cleanup, "--force")
  100.     FileUtils.rm_rf(Spoutcraft::MCP.root.join("jars", "bin").to_s)
  101.   end
  102.  
  103.   desc "Copy Minecraft libraries and inject Spoutcraft resources"
  104.   task :setup do
  105.     source_path = Spoutcraft.root.join("mojang", Spoutcraft::VERSION, "bin")
  106.     dest_path = Spoutcraft::MCP.root.join("jars")
  107.     FileUtils.rm_rf(dest_path.join("bin").to_s)
  108.     FileUtils.cp_r(source_path.to_s, dest_path.to_s)
  109.    
  110.     source_path = File.join("res", "*")
  111.     dest_path = Spoutcraft::MCP.root.join("jars", "bin", "minecraft.jar")
  112.     command = "jar -uvf #{dest_path.to_s.shellescape} #{source_path}"
  113.     Spoutcraft.capture(command)
  114.   end
  115.  
  116.   desc "Decompile and unobfuscate Minecraft source code"
  117.   task :decompile do
  118.     Spoutcraft::MCP.run(:decompile)
  119.   end
  120.  
  121.   desc "Merge Spoutcraft and MCP source code and resources"
  122.   task :merge do
  123.     source_path = Spoutcraft.root.join("src")
  124.     dest_path = Spoutcraft::MCP.root.join("src")
  125.     command = "ditto -V #{source_path.to_s.shellescape} #{dest_path.to_s.shellescape}"
  126.     Spoutcraft.capture(command)
  127.   end
  128.  
  129.   desc "Recompile Minecraft source code"
  130.   task :recompile do
  131.     Spoutcraft::MCP.run(:recompile)
  132.   end
  133.  
  134.   desc "Reobfuscate compiled Minecraft source code"
  135.   task :reobfuscate do
  136.     Spoutcraft::MCP.run(:reobfuscate)
  137.   end
  138.  
  139.   desc "Run client"
  140.   task :client do
  141.     Spoutcraft::MCP.run(:startclient)
  142.   end
  143.  
  144.   desc "Run server"
  145.   task :server do
  146.     Spoutcraft::MCP.run(:startserver)
  147.   end
  148.  
  149.   desc "Merge source, recompile, and run client"
  150.   task :test => [:merge, :recompile, :client]
  151.  
  152.   namespace :update do
  153.     desc "Update MCP"
  154.     task :mcp do
  155.       Spoutcraft::MCP.run(:updatemcp)
  156.     end
  157.    
  158.     desc "Update MD5s"
  159.     task :md5 do
  160.       Spoutcraft::MCP.run(:updatemd5)
  161.     end
  162.    
  163.     desc "Update names"
  164.     task :names do
  165.       Spoutcraft::MCP.run(:updatenames)
  166.     end
  167.   end
  168. end