Browse Source

Code cleanup and minimization

environments/development/deployments/48
Drew Short 7 years ago
parent
commit
701182da1e
  1. 73
      src/spm/spm.moon

73
src/spm/spm.moon

@ -1,3 +1,6 @@
-- Variables for component names that are called more than once
internet_component = "internet"
component = require("component") component = require("component")
event = require("event") event = require("event")
fs = require("filesystem") fs = require("filesystem")
@ -10,31 +13,50 @@ wget = loadfile("/bin/wget.lua")
gpu = component.gpu gpu = component.gpu
if not component.isAvailable("internet")
if not component.isAvailable(internet_component)
io.stderr\write("This program requires an internet card to run.") io.stderr\write("This program requires an internet card to run.")
return return
internet = require("internet")
internet = require(internet_component)
githubContentRoot = "https://raw.githubusercontent.com/"
defaultRepoLocationConfig = "{URL_BASE}/repos.cfg" defaultRepoLocationConfig = "{URL_BASE}/repos.cfg"
localInstalledPackagesFile = "/etc/spdata.svd" localInstalledPackagesFile = "/etc/spdata.svd"
localConfigFile = "/etc/spm.cfg" localConfigFile = "/etc/spm.cfg"
-- Common Error Messages
err_internet_connection = "Unable to connect to the Internet.\n"
err_package_does_not_exist = "Package does not exist"
err_package_has_not_been_installed = "Package has not been installed."
class Repository class Repository
new: (name, src, isGithub) =>
new: (name, src, isLegacy) =>
@name = name @name = name
@src = src @src = src
@isGithub = isGithub
@isLegacy = isLegacy
getRepoPath: =>
if @isGithub
return @src .. "/master"
@src
getPackagePath: =>
if @isLegacy
return @src .. "/master/programs.cfg"
@src .. "/programs.cfg"
getPackages: => getPackages: =>
getPackages(@\getRepoPath!)
getPackages(@\getPackagePath!)
class Package
new: (name, longName, files, dependencies, description, authors, note, hidden, repo, version, minified) =>
@name = name
@longName = longName
@files = files
@dependencies = dependencies
@description = description
@authors = authors
@note = note
@hidden = hidden
@repo = version
@minified = minified
args, options = shell.parse(...) args, options = shell.parse(...)
@ -71,7 +93,7 @@ getRepositories = (repoLocation) ->
success, raw_repositories = pcall(getContent, repoLocation) success, raw_repositories = pcall(getContent, repoLocation)
if not success if not success
io.stderr\write("Could not connect to the Internet. Please ensure you have an Internet connection.")
io.stderr\write(err_internet_connection)
return -1 return -1
deserialized_repositories = serial.unserialize(raw_repositories) deserialized_repositories = serial.unserialize(raw_repositories)
@ -89,18 +111,19 @@ getRepositories = (repoLocation) ->
print("Repository malformed/unsupported: " .. repositoryName) print("Repository malformed/unsupported: " .. repositoryName)
continue continue
isLegacy = false
src = repository.src src = repository.src
isGithub = false
if src == nil if src == nil
src = githubContentRoot .. repository.repo
isGithub = true
repositories[repositoryName] = Repository(repositoryName, src, isGithub)
src = "https://raw.githubusercontent.com/" .. repository.repo
isLegacy = true
repositories[repositoryName] = Repository(repositoryName, src, isLegacy)
repositories repositories
-- Get Packages for src -- Get Packages for src
getPackages = (src) -> getPackages = (src) ->
success, packages = pcall(getContent, src .. "/programs.cfg")
success, packages = pcall(getContent, src)
if not success or not packages if not success or not packages
return -1 return -1
@ -180,7 +203,7 @@ listPackages = (filter) ->
success, repositories = pcall(getRepositories, defaultRepoLocationConfig) success, repositories = pcall(getRepositories, defaultRepoLocationConfig)
if not success or repositories == -1 if not success or repositories == -1
io.stderr\write("Unable to connect to the Internet.\n")
io.stderr\write(err_internet_connection)
return return
elseif repositories == nil elseif repositories == nil
print("Error while trying to receive repository list") print("Error while trying to receive repository list")
@ -242,7 +265,7 @@ getInformation = (requestedPackage) ->
success, repositories = pcall(getRepositories, defaultRepoLocationConfig) success, repositories = pcall(getRepositories, defaultRepoLocationConfig)
if not success or repositories == -1 if not success or repositories == -1
io.stderr\write("Unable to connect to the Internet.\n")
io.stderr\write(err_internet_connection)
return return
for _, repository in pairs(repositories) for _, repository in pairs(repositories)
@ -266,7 +289,7 @@ provideInfo = (pack) ->
pack = string.lower(pack) pack = string.lower(pack)
info = getInformation(pack) info = getInformation(pack)
if not info if not info
print("Package does not exist")
print(err_package_does_not_exist)
return return
done = false done = false
@ -321,7 +344,7 @@ installPackage = (requestedPackage, installPath, doUpdate) ->
packageInfo, repositoryPath = getInformation(requestedPackage) packageInfo, repositoryPath = getInformation(requestedPackage)
if not packageInfo if not packageInfo
print("Package does not exist")
print(err_package_does_not_exist)
return return
if doUpdate if doUpdate
@ -358,7 +381,7 @@ installPackage = (requestedPackage, installPath, doUpdate) ->
print("Package has already been installed") print("Package has already been installed")
return return
elseif not locallyInstalledPackages[requestedPackage] and doUpdate elseif not locallyInstalledPackages[requestedPackage] and doUpdate
print("Package has not been installed.")
print(err_package_has_not_been_installed)
print("If it has, uninstall it manually and reinstall it.") print("If it has, uninstall it manually and reinstall it.")
return return
@ -424,7 +447,7 @@ installPackage = (requestedPackage, installPath, doUpdate) ->
copyPackage = (requestedPackage, copyLocation) -> copyPackage = (requestedPackage, copyLocation) ->
packageInformation, repositoryLocation = getInformation(requestedPackage) packageInformation, repositoryLocation = getInformation(requestedPackage)
if not packageInformation if not packageInformation
print("Package does not exist")
print(err_package_does_not_exist)
return return
locallyInstalledFiles = readFromLocalInstallFile() locallyInstalledFiles = readFromLocalInstallFile()
@ -435,7 +458,7 @@ copyPackage = (requestedPackage, copyLocation) ->
table.remove(locallyInstalledFiles, 1) table.remove(locallyInstalledFiles, 1)
if not locallyInstalledFiles[requestedPackage] if not locallyInstalledFiles[requestedPackage]
print("Package has not been installed.")
print(err_package_has_not_been_installed)
print("It must be installed to be copied.") print("It must be installed to be copied.")
return return
@ -458,7 +481,7 @@ copyPackage = (requestedPackage, copyLocation) ->
uninstallPackage = (requestedPackage) -> uninstallPackage = (requestedPackage) ->
packageInformation, repositoryLocation = getInformation(requestedPackage) packageInformation, repositoryLocation = getInformation(requestedPackage)
if not packageInformation if not packageInformation
print("Package does not exist")
print(err_package_does_not_exist)
return return
locallyInstalledFiles = readFromLocalInstallFile() locallyInstalledFiles = readFromLocalInstallFile()
@ -469,7 +492,7 @@ uninstallPackage = (requestedPackage) ->
table.remove(locallyInstalledFiles, 1) table.remove(locallyInstalledFiles, 1)
if not locallyInstalledFiles[requestedPackage] if not locallyInstalledFiles[requestedPackage]
print("Package has not been installed.")
print(err_package_has_not_been_installed)
print("If it has, you have to remove it manually.") print("If it has, you have to remove it manually.")
return return

Loading…
Cancel
Save