我正在创建一个Greasemonkey/UserScript脚本。由于Greasemonkey沙箱,我必须将所有内容都保存在一个文件中,但在5k+行,维护开始变得相当困难。
因此,我希望将脚本拆分到多个文件中,然后再次组合它们以进行测试和/或发布。我还必须能够添加一些逻辑:例如,发布是每种语言的(我不想为英语发布提供德语翻译,等等)。
根据实用程序员技巧8 Invest Regularly in Your Knowledge Portfolio的说法,我想学习一些语言来为我做这件事。什么是快速轻松地合并文件的好选择:makefile?Perl?RequireJS?Visual Studio macro's (我使用VS.NET编写UserScript)?还有别的吗?
发布于 2013-04-03 00:39:44
我用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
returnsourceFileName如下所示:
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是缩进级别。
发布于 2012-08-02 05:37:34
这是一个有点晚的响应,但是如果您需要做的就是合并特定的文件,那么您应该能够使用批处理脚本从命令行执行此操作。
类似于:
@ECHO off
COPY file1.txt+file2.txt combined.txthttps://stackoverflow.com/questions/8787658
复制相似问题