CC: Tweaked

CC: Tweaked

42M Downloads

Resuable code within rom

kepler155c opened this issue ยท 0 comments

commented

CC ROM code is, in many cases, not reusable and teaches bad programming practices if used as a reference.

Suggestion: Make use of require to enforce good programming practices (ie. code reuse, modular design).

Following is an example of how the pastebin program would be restructured to allow for code reuse. Using this convention, users would be make use of the pastebin functionality (via require) within their code without having to copy/paste the relevant code into their own programs. One side-effect is that their code would not have to be modified if future changes to pastebin occur.

rom/programs/http/pastebin.lua
if not http then
  printError( "Set http_enable to true in ComputerCraft.cfg" )
  printError( "Pastebin requires http API" )
  return
end

local function printUsage()
  print( "Usages:" )
  print( "pastebin put <filename>" )
  print( "pastebin get <code> <filename>" )
  print( "pastebin run <code> <arguments>" )
end

local pastebin = require('cc.pastebin')

local tArgs = { ... }
if #tArgs < 2 or not pastebin[tArgs[1]] then
  printUsage()
  return
end

write( "Connecting to pastebin.com... " )

local sCommand = tArgs[1]
if sCommand == "put" then
  -- Upload a file to pastebin.com
  -- Determine file to upload
  local sFile = tArgs[2]
  local sPath = shell.resolve( sFile )
  if not fs.exists( sPath ) or fs.isDir( sPath ) then
    printError( "No such file" )
    return
  end

  local sCode, msg = pastebin.put(sPath)

  if sCode then
    print( "Uploaded as ".. sCode )
    print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
  else
    printError(msg)
  end

elseif sCommand == "get" then
  -- Download a file from pastebin.com

  -- Determine file to download
  local sCode = tArgs[2]
  local sFile = tArgs[3] or printUsage()
  local sPath = shell.resolve( sFile )
  if fs.exists( sPath ) then
    printError( "File already exists" )
    return
  end

  local err, msg = pastebin.get(sCode, sPath)
  if err then
    printError(msg)
  else
    print( "Downloaded as "..sFile )
  end

elseif sCommand == "run" then
  local err, msg = pastebin.run(tArgs[2], table.unpack(tArgs, 3))
  if err then
    printError(msg)
  end
end
rom/.../pastebin.lua (the require)
local pastebin = { }

local function get(paste)
  local response = http.get(
    "http://pastebin.com/raw.php?i="..textutils.urlEncode( paste )
  )

  if not response then
    return false, 'failed'
  end

  local sResponse = response.readAll()
  response.close()
  return sResponse
end

-- Upload a file to pastebin.com
-- Determine file to upload
function pastebin.put(sPath)
  if not fs.exists( sPath ) or fs.isDir( sPath ) then
    return false, "No such file"
  end

  -- Read in the file
  local sName = fs.getName( sPath )
  local file = fs.open( sPath, "r" )
  if not file then
    return false, 'Unable to open file'
  end
  local sText = file.readAll()
  file.close()

  -- POST the contents to pastebin
  local key = "0ec2eb25b6166c0c27a394ae118ad829"
  local response = http.post(
    "http://pastebin.com/api/api_post.php", 
    "api_option=paste&"..
    "api_dev_key="..key.."&"..
    "api_paste_format=lua&"..
    "api_paste_name="..textutils.urlEncode(sName).."&"..
    "api_paste_code="..textutils.urlEncode(sText)
  )

  if not response then
    return false, 'Failed'
  end
  local sResponse = response.readAll()
  response.close()

  return string.match( sResponse, "[^/]+$" )
end

function pastebin.get(sCode, sPath)
  if fs.exists( sPath ) then
    return false, "File already exists"
  end

  -- GET the contents from pastebin
  local res, msg = get(sCode)
  if not res then
    return res, msg
  end

  local file = fs.open( sPath, "w" )
  if not file then
    return false, 'Failed to open file'
  end
  file.write( res )
  file.close()

  return true
end

function pastebin.run(sCode, ...)
  local res, msg = get(sCode)
  if not res then
    return res, msg
  end
  local func, err = load(res, sCode, "t", _ENV)
  if not func then
    return func, err
  end
  return pcall(func, ...)
end

return pastebin

Of course, there would be work needed to modify the existing programs and to ensure backwards compatibility. But, this change would significantly increase the usability of the built-in code.

Coding conventions would, of course, need to be established beforehand.