首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为了合并源文件,应该学习哪种(批处理)语言?

为了合并源文件,应该学习哪种(批处理)语言?
EN

Stack Overflow用户
提问于 2012-01-09 19:24:58
回答 2查看 101关注 0票数 1

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

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-03 00:39:44

我用Autohotkey推出了我自己的解决方案。我最好使用其他的东西,但这是ahk的源代码:

代码语言:javascript
复制
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如下所示:

代码语言:javascript
复制
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是缩进级别。

票数 0
EN

Stack Overflow用户

发布于 2012-08-02 05:37:34

这是一个有点晚的响应,但是如果您需要做的就是合并特定的文件,那么您应该能够使用批处理脚本从命令行执行此操作。

类似于:

代码语言:javascript
复制
@ECHO off
COPY file1.txt+file2.txt combined.txt
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8787658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档