前几天 写的博客 多项目 函数库、类库 统一为一个版本的方法中提到 使用 one.php 将整个项目打包成 一个 php 文件,有网友 让我 整个asp版本的,今天下午抽空写了个 one.asp,使用方式基本一致,这次 增加了路径计算的功能,可以引用不同路径。
举个简单的应用场景,开发一个小的api系统,支持 xml、json输出。区别是 基础版(dev/dev.asp)只支持 access,vip版本(dev/vip.asp)支持 access + sql server。这样vip版本就需要在现在的基础上 增加 sqlserver的支持,增加额外的 配置项。开发过程只需要 按正常开发即可。
开发完毕后,通过 one.asp 打包成 index.asp 和 vip.asp 两个版本,整套程序都是一个独立的文件。
下图演示了 解析过程:
完整的测试代码 可以 访问: 下载
核心代码 如下:
' ====================================================
' 类名:one
' 作者:mqycn
' 博客:http://www.miaoqiyuan.cn
' 源码:http://www.miaoqiyuan.cn/p/one-php
' 说明:多项目 函数库、类库 统一为一个版本的方法
' ====================================================
class oneasp
private fso
private root
private sub class_initialize()
set fso = server.createobject("scripting.filesystemobject")
end sub
private sub class_terminate()
set fso = nothing
end sub
public function run(byval sourcefile, byval savefile)
run = "<hr><b>input:</b>" & sourcefile & "<br><b>result:</b>" & save(savefile, include(sourcefile))
end function
public function include(byval path)
dim tmppath, tmpitem, arrpath, index
tmppath = ""
arrpath = split(path, "/")
for index = 0 to ubound(arrpath) - 1
tmpitem = arrpath(index)
tmppath = tmppath & tmpitem & "/"
next
include = parse(tmppath, arrpath(ubound(arrpath)))
end function
private function parse(byval root, byval filename)
call setroot(root)
dim html
html = openread(filename)
dim preg, pregresult
set preg = new regexp
preg.pattern = "<!--#include file=""([^""]*)""-->"
preg.global = true
preg.ignorecase = true
dim htmlinclude
set pregresult = preg.execute(html)
for each htmlinclude in pregresult
html = replace(html, htmlinclude, include(root & htmlinclude.submatches(0)))
next
parse = "<% '" & root & filename & " start %" & ">" & vbcrlf & html & vbcrlf & "<% '" & root & filename & " end %" & ">" & vbcrlf
end function
private function setroot(byval rootpath)
if right(rootpath, 1) <> "/" then rootpath = rootpath & "/"
root = rootpath
end function
private function realpath(byval path)
realpath = server.mappath(root & path)
end function
private function openread(byval path)
dim txtfile
set txtfile = fso.opentextfile(realpath(path))
openread = txtfile.readall()
txtfile.close
on error goto 0
end function
public function save(byval path, byval body)
dim txtfile
set txtfile = fso.createtextfile(server.mappath(path))
txtfile.write body
txtfile.close
set txtfile = nothing
save = path
end function
end class
使用也非常简单,使用 call new oneasp.run(开发版, 打包版),可以 dev.asp 中的所有包含的代码 打包到 index.asp,如果只想获取解析的内容,可以使用 response.write server.htmlencode(call new oneasp.include(开发版))



发表评论