在多个文件中维护同一段代码
我在一个项目中有一个不寻常的环境,其中有许多文件,每个文件都是独立的独立脚本。脚本所需的所有代码都必须位于一个文件中,并且我无法使用包含等引用外部文件。
所有这些文件中有一个公共函数用于执行授权,即每个文件中的最后一个函数。如果这个函数发生任何变化(就像现在时不时发生的那样),它必须在所有文件中发生变化,而且有很多文件。
最初,我考虑将授权函数保留在一个单独的文件中,并运行一个批处理过程,通过将 auth 文件与其他文件组合来生成最终文件。然而,这在调试时非常麻烦,因为为此目的需要将 auth 函数放在主文件中。因此,我总是在包含组合文件的文件夹中进行测试和调试,然后必须将更改复制回未组合的文件。
有人能想办法解决这个问题吗?即在多个文件中维护相同的代码片段。
I have an unusual environment in a project where I have many files that are each independent standalone scripts. All of the code required by the script must be in the one file and I can't reference outside files with includes etc.
There is a common function in all of these files that does authorization that is the last function in each file. If this function changes at all (as it does now and then) it has to changed in all the files and there are plenty of them.
Initially I was thinking of keeping the authorization function in a separate file and running a batch process that produced the final files by combining the auth file with each of the others. However, this is extremely cumbersome when debugging because the auth function needs to be in the main file for this purpose. So I'd always be testing and debugging in the folder with the combined file and then have to copy changes back to the uncombined files.
Can anyone think of a way to solve this problem? i.e. maintain an identical fragment of code in multiple files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定“出于此目的,auth 函数需要位于主文件中”是什么意思,但典型的 Unix 解决方案可能是在这里使用 make(1) 和 cpp(1) 。
I'm not sure what you mean by "the auth function needs to be in the main file for this purpose", but a typical Unix solution might be to use make(1) and cpp(1) here.
不确定您使用的环境/编辑器,但您可以做的一件事是使用预构建事件。创建一个定义导入区域的开始标签/结束标签,然后在预构建事件中复制标签之间的公共代码,然后编译...
//$start-tag-common-auth
......代码在这里......
//$end-tag-common-auth
在预构建事件中只需找到这些标签,并将它们替换为导入代码,然后完成编译。
VS 支持 pre-post 构建事件,这些事件可以调用外部进程,但不直接与环境交互(如批处理文件或脚本)。
Not sure what environment/editor your using but one thing you can do is to use prebuild events. create a start-tag/end-tag which defines the import region, and then in the prebuild event copy the common code between the tags and then compile...
//$start-tag-common-auth
..... code here .....
//$end-tag-common-auth
In your prebuild event just find those tags, and replace them with the import code and then finish compiling.
VS supports pre-post build events which can call external processes, but do not directly interact with the environment (like batch files or scripts).
不要将身份验证代码保存在单独的文件中,而是将现有脚本之一指定为主脚本。使用它来编辑/调试/处理身份验证代码。然后添加一个构建/批处理过程,就像您所说的那样,将身份验证代码从主脚本复制到所有其他脚本中。
这样您仍然可以随时调试和使用主脚本,您不必担心另一个文件,并且您的构建/部署过程使所有内容保持同步。
您可以使用 @Priyank Bolia 建议的技术来轻松查找/替换所需的代码位。
Instead of keeping the authentication code in a separate file, designate one of your existing scripts as the primary or master script. Use this one to edit/debug/work on the authentication code. Then add a build/batch process like you are talking about that copies the authentication code from the master script into all of the other scripts.
That way you can still debug and work with the master script at any time, you don't have to worry about one more file, and your build/deploy process keeps everything in sync.
You can use a technique like @Priyank Bolia suggested to make it easy to find/replace the required bit of code.
我能想到的一种丑陋的方式是:
在所有文件中包含原始代码,并用标记将其包围,例如:
此代码块可以由构建过程从一个通用代码文件自动替换。
I ugly way I can think of:
have the original code in all the files, and surround it with markers like:
This code block can be replaced automatically by the build process, from one common code file.