前几天 写的博客 多项目 函数库、类库 统一为一个版本的方法中提到 使用 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 两个版本,整套程序都是一个独立的文件。
下图演示了 解析过程:
完整的测试代码 可以 访问:oneasp.rar 下载
核心代码 如下:
' ====================================================' 类名:One' 作者:mqycn' 博客:http://www.miao*qi*yu*an.cn' 源码:http://www.*mia*oqiy*uan.cn/p/one-php' 说明:多项目 函数库、类库 统一为一个版本的方法' ====================================================Class OneAspPrivate FSOPrivate Root Private Sub Class_Initialize()Set FSO = Server.CreateObject("Scripting.FileSystemObject")End Sub Private Sub Class_Terminate()Set FSO = NothingEnd Sub Public Function Run(ByVal sourceFile, ByVal saveFile)Run = "<hr><b>Input:</b>" & sourceFile & "<br><b>Result:</b>" & Save(saveFile, Include(sourceFile))End FunctionPublic Function Include(ByVal path)Dim tmpPath, tmpItem, arrPath, indextmpPath = ""arrPath = Split(path, "/")For index = 0 To UBound(arrPath) - 1tmpItem = arrPath(index)tmpPath = tmpPath & tmpItem & "/"NextInclude = Parse(tmpPath, arrPath(UBound(arrPath)))End Function Private Function Parse(ByVal root, ByVal fileName)Call SetRoot(root)Dim htmlhtml = OpenRead(fileName) Dim preg, pregResultSet preg = New Regexppreg.pattern = "<!--#include file=""([^""]*)""-->"preg.global = Truepreg.ignorecase = True Dim htmlIncludeSet pregResult = preg.execute(html)For Each htmlInclude In pregResulthtml = Replace(html, htmlInclude, Include(root & htmlInclude.submatches(0)))Next Parse = "<% '" & root & fileName & " Start %" & ">" & vbCrLf & html & vbCrLf & "<%'" & root & fileName & " End %" & ">" & vbCrLfEnd Function Private Function SetRoot(ByVal rootPath)If Right(rootPath, 1) <> "/" Then rootPath = rootPath & "/"Root = rootPathEnd Function Private Function RealPath(ByVal path)RealPath = Server.Mappath(Root & path)End Function Private Function OpenRead(ByVal path)Dim txtFileSet txtFile = FSO.OpenTextFile(RealPath(path))OpenRead = txtFile.ReadAll()txtFile.closeOn Error GoTo 0End Function Public Function Save(ByVal path, ByVal body)Dim txtFileSet txtFile = FSO.CreateTextFile(Server.Mappath(path))txtFile.write bodytxtFile.closeSet txtFile = NothingSave = pathEnd Function End Class
使用也非常简单,使用 Call new OneAsp.run(开发版, 打包版),可以 dev.asp 中的所有包含的代码 打包到 index.asp,如果只想获取解析的内容,可以使用 Response.Write Server.Htmlencode(Call new OneAsp.include(开发版))