load function for addons implement (#1958)

* load function for addons implement

* split load function into smaller functions, clean code

* Update sunstone-server.rb
This commit is contained in:
Diego del Corral authored and Tino Vázquez committed 2018-04-11 16:50:25 +02:00
1 parent 45e9242fa0
commit b503fccc76
1 file changed
+67
+67
View File
@@ -42,6 +42,10 @@ CONFIGURATION_FILE = ETC_LOCATION + "/sunstone-server.conf"
PLUGIN_CONFIGURATION_FILE = ETC_LOCATION + "/sunstone-plugins.yaml"
LOGOS_CONFIGURATION_FILE = ETC_LOCATION + "/sunstone-logos.yaml"
ADDONS_LOCATION = SUNSTONE_LOCATION+"/public/app/addons/dist/*"
MAIN_FILE_PATH = SUNSTONE_LOCATION+"/public/dist/main.js"
TMP_FILE = SUNSTONE_LOCATION+"/public/dist/main.tmp"
SUNSTONE_ROOT_DIR = File.dirname(__FILE__)
$: << RUBY_LIB_LOCATION
@@ -350,6 +354,68 @@ helpers do
session.clear
return [204, ""]
end
def load_addons
tmp = File.open(TMP_FILE, "w")
main = File.new(MAIN_FILE_PATH)
files = Dir[ADDONS_LOCATION].select{ |f| File.file? f }
if files.length == 0 then
return ""
end
lines = main.gets
while lines != nil
tmp << lines
if lines.include? "// start-addon-section //"
load_start_section(files, tmp)
end
if lines.include? "'addons/start'"
load_list_start(files, tmp)
end
lines = main.gets
end
FileUtils.mv(TMP_FILE, MAIN_FILE_PATH)
files.each do |file|
FileUtils.rm(file)
end
end
def load_start_section(files, tmp)
files.each do |file|
add = File.new(file)
boolist = false
add.each do |line|
if line.include? "// list-start //"
boolist = true
end
tmp << line if !boolist
end
add.close
end
end
def load_list_start(files, tmp)
files.each do |file|
add = File.new(file)
line = add.gets
while line != nil
if line.include? "// list-start //"
line=add.gets
while line != nil
tmp<<line
line=add.gets
end
end
line=add.gets
end
add.close
end
end
end
before do
@@ -454,6 +520,7 @@ end
# HTML Requests
##############################################################################
get '/' do
load_addons
content_type 'text/html', :charset => 'utf-8'
if !authorized?
return erb :login