我使用Autohotkey推出了自己的解决方案。我最好使用其他东西,但这里是ahk
来源:
inputFile := "sourceFileName"
savePath := "C:\Temp\"
saveAs := "targetFileName"
workingDirectory = %A_WorkingDir%
SetWorkingDir, %A_ScriptDir%
ParseFile(fileName, indentCount)
{
if not FileExist(fileName)
MsgBox Couldn't find: %fileName%
replacedFile =
Loop, Read, %fileName%
{
replacedFile .= ParseLine(A_LoopReadLine, indentCount) . "`r"
}
StringTrimRight, replacedFile, replacedFile, 1
return %replacedFile%
}
ParseLine(line, indentCount)
{
found =
FoundInclude := RegExMatch(line, "(^\s*)?//\<!--@@INCLUDE "".*"" INDENT=\d //--\>", found)
if FoundInclude
{
; //<!--@@INCLUDE "importit.txt" INDENT=X //-->
toIncludeFileName := RegExReplace(found, "^\s*")
StringMid, toIncludeFileName, toIncludeFileName, 18
closingQuotePosition := InStr(toIncludeFileName, """")
StringMid, newIndent, toIncludeFileName, closingQuotePosition + 9
StringMid, newIndent, newIndent, 1, 1
StringMid, toIncludeFileName, toIncludeFileName, 1, closingQuotePosition - 1
If toIncludeFileName
{
toIncludeContent := ParseFile(toIncludeFileName, newIndent)
StringReplace, line, line, %found%, %toIncludeContent%
}
else
{
StringReplace, line, line, %found%
}
}
else if indentCount
{
Loop %indentCount%
{
;line := " " . line
line := A_TAB . line
}
}
return %line%
}
; Keep backups of merges?
IfExist, %savePath%%saveAs%
{
backupCount := 0
backupFileName = %savePath%%saveAs%
while FileExist(backupFileName)
{
backupFileName = backup\%saveAs%%backupCount%
backupCount++
}
FileMove, %savePath%%saveAs%, %backupFileName%
FileCopy, %inputFile%, %backupFileName%_source
}
formattedOutput := ParseFile(inputFile, 0)
;fullFileName = %savePath%%SaveAs%
;MsgBox, %A_FileEncoding%
;file := FileOpen, fullFileName, "w"
FileEncoding, UTF-8-RAW
FileAppend, %formattedOutput%, %savePath%%SaveAs%
SetWorkingDir, workingDirectory
return
sourceFileName 如下所示:
function ready() {
var version = "//<!--@@INCLUDE "version.txt" INDENT=0 //-->";
// User config
var user_data = {};
//<!--@@INCLUDE "config\settings.js" INDENT=1 //-->
... more code ...
}
所以包含文件的语法是://<!--@@INCLUDE "fileToInclude" INDENT=X //-->
X 是缩进级别。