IMPORT-一个简单快速的Bash和其他Unix shell模块系统

2020-07-17 07:39:58

#!/bin/sh#MacOS上默认只有`shasum`,#但高山上默认只有`sha1sum`__import_shasum=";$(command-v sha1sum)";||__import_shasum=";$(command-v shasum)";||{r=$?ECHO";IMPORT:不存在\`shasum\`或\`sha1sum\`命令";>;&;2退出";$r";}[-n";${import_debug-}";]&;&;ECHO&34;IMPORT:Using';$__IMPORT_SHASUM';>;&;2import_Usage(){ECHO";用法:IMPORT\";org/repo/mod.sh\";>;&;2 ECHO";>;&;&;2 ECHO&34;文档:https://import.pw";>;&;2回声";核心模块:https://github.com/importpw";&>&;2 ECHO&34;>;&;2 ECHO&34;示例:";>;&;2 ECHO";IMPORT\";ASSERT\";#IMPORT最新提交的';Assert";模块>;&;2 ECHO";IMPORT\";[email protected]\";#从GitHub存储库导入';assert";>;&;2 ECHO";IMPORT/HELLO\";#IMPORT的标记\`2.1.3\`;https://git.io/fAWiz\";#IMPORT#IMPORT#IMPORT";>;&;#IMPORT#IMPORT\";>;&;#IMPORT#IMPORT\";&>&;#IMPORT#IMPORT";&>&;#IMPORT#IMPORT从完全限定的URL&34;&>&>#IMPORT#IMPORT。2返回2}import_parse_location(){LOCAL LOCATION=";$1";LOCAL LOCATION_HEADER=";";#LOCAL LOCATION_HEADER=";";#print`x-import-warning`标头grep-i';^x-import-warning:';<;";$Headers";|While IFS=';';read-r line;do echo|awk-F";:";';{print$2}';|TR-d\\r)";>;&;2完成#查找最终的`Location`或`Content-Location`标题location_Header=";$(grep-i&39;^location\|^content-location:';<;";$Headers";|Tail-N1)";if[-n"。$LOCATION_HEADER";|awk-F";:";';{print$2}';|TR-d\\r)";fi echo";$location";}import(){local url=";$*";local url_path=";";如果[-z";$url";];则import_use返回FI[-n&]&;&;echo&34;import:导入';$url";>;&;2#如果这是相对导入,则需要基于#父导入的解析URL位置。案例";$url";位于(./*)url=";$(目录名称";$__import_location";)/$url";(../*)url=";$(目录名称";$__IMPORT_LOCATION";)/$url";esac#导入缓存的基目录。#默认为`$HOME/.import_cache`。#可以通过设置`IMPORT_CACHE`变量进行配置。Local cache=";${IMPORT_CACHE-${HOME}/.import-cache}";#如果用户正在执行隐式导入,则应用默认服务器!echo";$url";|grep";://";>;/dev/null&;&;!ECHO";$url";|AWK-F/';{PRINT$1}';|AWK-F@';|grep';\.&39;>;/dev/NULL;然后url=";${IMPORT_SERVER-https://import.pw}/$url";[-n";${IMPORT_DEBUG-}";]&;&;ECHO";导入:规范化的URL';$url';";>;&;2fi url_path=";$(echo";$url";|sed';s/\:\/';)";LOCAL CACHE_PATH=";$CACHE/LINKS/$url_path";if[!-e";$cache_path";]||[-。则#确保包含此导入的符号链接的目录存在。local dir dir=";$(dirname";$url_path";)";local link_dir=";$cache/link/$dir";mkdir-p";$link_dir";$cache/data";";$cache/locations/$dir";>;&;2||return#解析缓存和链接目录缓存=";$((CD";$CACHE";&;&;pwd))";||return link_dir=";$(cd";$link_dir";&;&;pwd))";||return cache_path=";$cache/link/$url_path";#将请求的文件下载到临时位置,以便计算shasum#以确定正确的最终文件本地位置=";";本地tmpfile=";$cache_path.tmp";local tmpheader=";$cache_path.header";local locfile=";$cache/locations/$url_path";local qs=";$url";|grep';\?';>;/dev/null;则qs=";&;";fi[-n";${import_debug-}";]&;&;echo";导入:HTTP GET$url_with_qs";>;&;2curl-sfLS\--netrc-可选\--转储标头";$tmpheader";\${import_curl_opts-}\";$url_with_qs";>;";$Echo";导入:无法下载:$url_with_qs";>;&;2 rm-f";$tmpfile";";$tmpheader";||true return";$r";}#既然HTTP请求已解析,请解析";location";location=";$(import_parse_location";$url";&#。${import_debug-}&34;]&;&;ECHO";导入:解析的位置';$url';-&>&39;$LOCATION';";&;&;2 ECHO";$LOCATION&