1 Star 0 Fork 0

CovScript 智锐编程语言 / CSPKG

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cspkg 10.94 KB
一键复制 编辑 原始数据 按行查看 历史
李登淳 提交于 2023-04-07 16:44 . Update cspkg
#!/usr/bin/env cs
#
# Covariant Script Package Manager Launcher v1.0.1
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Copyright (C) 2017-2023 Michael Lee(李登淳)
#
# Email: lee@unicov.cn, mikecovlee@163.com
# Github: https://github.com/mikecovlee
# Website: http://covscript.org.cn
#
# Config File Structural
#
# ~/.cspkg
# cspkg.log
# cspkg.csp
# cspkg.json
# config.json
# packages.json
#
@require: 210505
import codec.json as json
import process
import regex
import curl
namespace utils
function open_json(path)
var ifs = iostream.ifstream(path)
return json.to_var(json.from_stream(ifs))
end
function save_json(val, path)
var ofs = iostream.ofstream(path)
ofs.print(json.to_string(json.from_var(val)))
end
var url_reg = regex.build("^[a-zA-z]+://([\\w.]+)[^\\s]*$")
function check_url(url)
return !utils.url_reg.match(url).empty()
end
function get_hostname(url)
var m = utils.url_reg.match(url)
if !m.empty()
return m.str(1)
else
return new string
end
end
constant low_speed_limit = 100
function http_get(url)
if !utils.check_url(url)
cspkg_log("cspkg: error when parsing URL \'" + url + "\".")
return null
end
var buff = new iostream.char_buff
var session = curl.make_session_os(buff.get_ostream())
session.set_url(url)
session.allow_redirect(true)
if config.content.exist("proxy")
session.set_proxy(config.content.proxy)
end
session.set_ssl_verify_host(false)
session.set_ssl_verify_peer(false)
session.set_connect_timeout_ms(config.content.timeout_ms.to_number())
session.set_accept_timeout_ms(config.content.timeout_ms.to_number())
session.set_low_speed_limit(low_speed_limit)
session.set_low_speed_time(config.content.timeout_ms.to_number())
if session.perform()
return buff.get_string()
else
return null
end
end
function http_get_file(url, path)
if !utils.check_url(url)
cspkg_log("cspkg: error when parsing URL \'" + url + "\".")
return -2
end
var ofs = iostream.fstream(path, iostream.openmode.bin_out)
if !ofs.good()
return -1
end
var session = curl.make_session_os(ofs)
session.set_url(url)
session.allow_redirect(true)
if config.content.exist("proxy")
session.set_proxy(config.content.proxy)
end
session.set_ssl_verify_host(false)
session.set_ssl_verify_peer(false)
session.set_connect_timeout_ms(config.content.timeout_ms.to_number())
session.set_accept_timeout_ms(config.content.timeout_ms.to_number())
session.set_low_speed_limit(low_speed_limit)
session.set_low_speed_time(config.content.timeout_ms.to_number())
return session.perform() ? 1 : 0
end
end
namespace env
var win_ucrt = true
function user_home()
if system.is_platform_windows()
return system.getenv("USERPROFILE")
else
return system.getenv("HOME")
end
end
function covscript_home()
try
return system.getenv("COVSCRIPT_HOME")
catch e; end
if system.is_platform_windows()
return env.user_home() + system.path.separator + "Documents" + system.path.separator + "CovScript"
end
if system.is_platform_linux()
return "/usr/share/covscript"
end
if system.is_platform_darwin()
return "/Applications/CovScript.app/Contents/MacOS/covscript"
end
end
function platform()
if system.is_platform_windows()
if env.win_ucrt
return "winucrt"
else
return "windows"
end
end
if system.is_platform_linux()
return "linux"
end
if system.is_platform_darwin()
return "macos"
end
end
@begin
var arch_map = {
"AMD64" : "x86_64",
"ARM64" : "arm64",
"x86" : "i386"
}.to_hash_map()
@end
function arch()
if system.is_platform_unix()
var p = process.exec("arch", {})
return p.out().getline()
else
var arch_name = system.getenv("PROCESSOR_ARCHITECTURE")
if env.arch_map.exist(arch_name)
return env.arch_map[arch_name]
else
throw runtime.exception("Unrecognizable platform name: " + arch_name)
end
end
end
end
namespace config
@begin
var default_content = {
"arch" : env.arch(),
"home" : env.covscript_home(),
"timeout_ms" : "3000",
# "proxy" : ""
"source" : "http://mirrors.covariant.cn/cspkg/index.json"
}.to_hash_map()
@end
var content = null
var cancel_prompt = false
function compose_home()
return env.user_home() + system.path.separator + ".cspkg"
end
function compose_log()
return env.user_home() + system.path.separator + ".cspkg" + system.path.separator + "cspkg.log"
end
function compose_config()
return env.user_home() + system.path.separator + ".cspkg" + system.path.separator + "config.json"
end
function compose_cspkg_info()
return env.user_home() + system.path.separator + ".cspkg" + system.path.separator + "cspkg.json"
end
function compose_cspkg_package()
return env.user_home() + system.path.separator + ".cspkg" + system.path.separator + "cspkg.csp"
end
function save()
utils.save_json(config.content, config.compose_config())
end
function create()
system.path.mkdir_p(config.compose_home())
if !system.file.exist(config.compose_config())
config.save()
end
end
function init()
if !system.path.exist(env.covscript_home() + system.path.separator + "imports")
system.path.mkdir_p(env.covscript_home() + system.path.separator + "imports")
end
if !system.file.exist(config.compose_config())
config.content = config.default_content
config.create()
else
config.content = utils.open_json(config.compose_config())
end
end
end
namespace log_impl
var log = null
function open_log()
if log_impl.log == null
system.path.mkdir_p(config.compose_home())
log_impl.log = iostream.fstream(config.compose_log(), iostream.openmode.app)
end
end
end
function cspkg_print(msg)
if !config.cancel_prompt
system.out.println(msg)
end
end
function cspkg_log(msg)
log_impl.open_log()
system.out.println(msg)
log_impl.log.println(msg)
end
# Main Function
function cspkg_main(borken)
var repos = config.content.source.split({';'})
var source = null
foreach source_url in repos
cspkg_print("Fetching source from " + utils.get_hostname(source_url) + "...")
var source_json = utils.http_get(source_url)
if source_json == null || source_json.empty()
cspkg_log("cspkg: timeout when fetching index from \"" + source_url + "\"")
return
end
source = json.to_var(json.from_string(source_json))
if !source.exist("dependencies") || !source.dependencies.exist("cspkg")
cspkg_log("cspkg: outdated source on " + utils.get_hostname(source_url))
continue
else
break
end
end
if source == null
cspkg_log("cspkg: fetching source failed.")
return
end
var remote_info = source.dependencies.cspkg
if !system.file.exist(config.compose_cspkg_info())
if !config.cancel_prompt
if borken
system.out.println("Installing cspkg...")
else
system.out.println("Fix broken cspkg installation...")
end
system.out.println("Do you want to continue?[y/n]")
if system.console.getch().tolower() != 'y'
cspkg_log("cspkg: installation canceled.")
system.exit(0)
end
end
else
var local_info = utils.open_json(config.compose_cspkg_info())
if remote_info.Version > local_info.Version
if !config.cancel_prompt
system.out.println("Detect newer version for cspkg: " + remote_info.Version)
system.out.println("Do you want to continue?[y/n]")
if system.console.getch().tolower() != 'y'
cspkg_log("cspkg: installation canceled.")
return
end
end
else
if borken
cspkg_print("Fix broken cspkg installation...")
else
return
end
end
end
var error_value = utils.http_get_file(remote_info.Target, config.compose_cspkg_package())
switch error_value
case 1
utils.save_json(remote_info, config.compose_cspkg_info())
if borken
cspkg_log("cspkg: installation successfully.")
else
cspkg_log("cspkg: local installation has been upgraded to " + remote_info.Version)
end
end
case 0
cspkg_log("cspkg: timeout when download file.")
end
case -1
cspkg_log("cspkg: unable to create file, try run with rights of administrator.")
end
end
end
# Processing CLI Args
var cspkg_options = {}
var cspkg_args = {}
var cmd_args = context.cmd_args
block
var idx = 2
while idx < cmd_args.size
if cmd_args[idx].find("--", 0) != 0
cspkg_args.push_back(cmd_args[idx])
else
break
end
++idx
end
while idx < cmd_args.size
if cmd_args[idx] == "--yes"
config.cancel_prompt = true
else
cspkg_options.push_back(cmd_args[idx])
end
++idx
end
end
config.init()
cspkg_main(!system.file.exist(config.compose_cspkg_package()))
var cspkg = context.source_import(config.compose_cspkg_package())
if cspkg == null
cspkg_log("cspkg: broken cspkg installation.")
system.exit(0)
end
if cmd_args.size == 1
cspkg.show_help(null, null)
system.exit(0)
end
cspkg.config.content = config.content
cspkg.config.default_content = config.default_content
cspkg.config.cancel_prompt = config.cancel_prompt
cspkg.main(cmd_args[1], cspkg_options, cspkg_args)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/covscript/cspkg.git
git@gitee.com:covscript/cspkg.git
covscript
cspkg
CSPKG
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891