Quipex

Disk/mem/redworks/programs/http/cc-get

Apr 23rd, 2012
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. ------------------------------------------------------------
  2. --- cc-get version 0.1.1
  3. --- Created by DJRanger
  4. ---
  5. --- This is very experimental, use at your own risk
  6. --- Install:
  7. --- Put this script and the cc-get-bin folder into your
  8. --- .minecraft/mods/ComputerCraft/lua/rom/programs/ folder
  9. ---
  10. ------------------------------------------------------------
  11.  
  12. VERSION = '0.1.1'
  13.  
  14. if http == nil then
  15. print('Please enable the http api.')
  16. print('Look for enableAPI_http=0 in .minecraft/config/ and change the zero to a one and then restart Minecraft')
  17. return false
  18. end
  19. json = (loadfile("/mem/redworks/programs/cc-get-bin/dkjson.lua")())
  20. shell.run('cc-get-bin/params.lua')
  21. shell.run('cc-get-bin/package.lua')
  22.  
  23. params = parse_params({...}, {version = '-v *([^ ]+)'})
  24.  
  25. urlBase = 'http://cc-get.djranger.com/cc/' .. VERSION .. '/'
  26. local action = params[1]
  27.  
  28. if not fs.exists('/bin') then
  29. fs.makeDir('/bin')
  30. fs.makeDir('/bin/lib')
  31. end
  32.  
  33. if fs.exists('/bin/packages.json') then
  34. fs.makeDir('/bin/lib');
  35. fs.move('/bin/packages.json', '/bin/lib/packages.json')
  36. end
  37.  
  38. if not fs.exists('/bin/lib/packages.json') then
  39. file = fs.open('/bin/lib/packages.json', 'w')
  40. file.write('{"packages": {}, "installed": 0}')
  41. file.close()
  42. end
  43.  
  44. local file = fs.open('/bin/lib/packages.json', 'r')
  45. data = json.decode(file:readAll())
  46. file:close()
  47.  
  48. function save_data()
  49. file = fs.open('/bin/lib/packages.json', 'w')
  50. file.write(json.encode(data))
  51. file.close()
  52. end
  53.  
  54. if not action then
  55. -- No params, show some info about the program
  56. shell.run('help', 'cc-get')
  57. elseif action == 'install' or action == 'remove' then
  58. name = params[2]
  59. if not name then
  60. print('Usage: cc-get ' .. action .. ' <program>')
  61.  
  62. elseif action == 'install' then
  63. local status, package = Package.get(name, params.version)
  64. if status then
  65. local version = ''
  66. if params.version then
  67. version = ' version ' .. package.version
  68. end
  69. term.write('Really install ' .. name .. version .. '? y/n: ')
  70. answer = read()
  71.  
  72. if answer:sub(0,1) == 'y' then
  73. status = package:install()
  74. if status == true then
  75. package.source = nil
  76. data.packages[ package.name ] = package
  77. print( package.name ..' version ' .. package.version .. ' installed' )
  78. else
  79. pritn('Something went wrong installing ' .. package.name)
  80. end
  81. end
  82. else
  83. print(package)
  84. end
  85. elseif action == 'remove' then
  86. local status, package = Package.load( name )
  87. if status then
  88. term.write('Really remove the package ' .. name .. '? y/n: ')
  89. answer = read()
  90. if answer:sub(0,1) == 'y' then
  91. package:uninstall()
  92. print('The ' .. package.name .. ' package has been removed')
  93. end
  94. else
  95. print(package)
  96. end
  97. end
  98.  
  99. elseif action == 'update' then
  100. local qs = ''
  101. for name, info in pairs(data.packages) do
  102. qs = qs .. name .. ','
  103. end
  104.  
  105. qs = qs:sub(0, -2)
  106.  
  107. result = json.decode( http.get(urlBase .. 'info/' .. qs).readAll() )
  108. local updates = 0
  109.  
  110. for name, info in pairs(result) do
  111. if data.packages[ name ].timestamp < info.timestamp then
  112. -- Need to update
  113. print('Updating ' .. name .. ' from version ' .. data.packages[ name ].version .. ' to version ' .. info.version .. '...')
  114. status, package = Package.load(name)
  115. package:update()
  116. print( package.name .. ' updated ')
  117. updates = updates + 1
  118. end
  119. end
  120.  
  121. if updates == 0 then
  122. print('Nothing needs to be updated')
  123. end
  124. elseif action == 'list' then
  125. local i = 0
  126. for name, info in pairs(data.packages) do
  127. print( name .. ' version ' .. info.version )
  128. i = i + 1
  129. end
  130.  
  131. if i == 0 then
  132. print('No packages installed')
  133. end
  134.  
  135. else
  136. print('Unknown command "' .. action .. '"')
  137. end
  138.  
  139. save_data()
Advertisement
Add Comment
Please, Sign In to add comment