1

我正在创建一个 Greasemonkey/UserScript 脚本。由于 Greasemonkey 沙箱,我必须将所有内容保存在一个文件中,但在 5k+ 行时,维护开始变得相当困难。

因此,我想将脚本拆分为多个文件,然后将它们再次组合以进行测试和/或发布。我还必须能够添加一些逻辑:例如,版本是每种语言的(我不想为英语版本发送德语翻译等:)。

根据实用程序员提示 8 Invest Regularly in Your Knowledge Portfolio,我想学习一些语言来为我做这件事。什么是快速轻松地合并文件的好选择:makefilePerl? RequireJS? Visual Studio macro's(我使用 VS.NET 编写 UserScript)?还有什么?

4

2 回答 2

2

这是一个迟到的响应,但如果您需要做的只是合并特定文件,您应该能够从命令行使用批处理脚本来完成。

就像是:

@ECHO off
COPY file1.txt+file2.txt combined.txt
于 2012-08-01T21:37:34.067 回答
0

我使用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 是缩进级别。

于 2013-04-02T16:39:44.267 回答