简体   繁体   English

将C#项目的文本转换为1个文本文件

[英]Convert text of a C# project into 1 text file

So I'm doing Google Code Jam, and for their new format I have to upload my code as a single text file. 因此,我正在执行Google Code Jam,对于新格式,我必须将代码作为单个文本文件上传。

I like writing my code as properly constructed classes and multiple files even when under time pressure (I find that I save more time in clarity and my own debugging speed than I lose in wasted time.) and I want to re-use the common code. 我喜欢将代码编写为正确构造的类和多个文件,即使在时间紧迫的情况下(与浪费的时间相比,我也节省了更多的时间来保持清晰和自己的调试速度),并且我想重新使用通用代码。

Once I've got my code finished I have to convert from a series of classes in multiple files, to a single file. 完成代码后,我必须将多个文件中的一系列类转换为单个文件。

Currently I'm just manually copying and pasting all the files' text into a single file, and then manually massaging the usings and namespaces to make it all work. 目前,我只是手动将所有文件的文本复制并粘贴到单个文件中,然后手动按摩uses和名称空间以使其全部正常工作。

Is there a better option? 有更好的选择吗? Ideally a tool that will JustDoIt for me? 理想的工具是JustDoIt对我有用吗? Alternatively, if there were some predictable algorithm that I could implement that wouldn't require any manual tweaks? 或者,如果我可以实施一些可预测的算法,而无需进行任何手动调整?

  1. Write your classes so that all "using"s are inside "namespace" 编写您的类,以便所有“使用”都在“命名空间”内
  2. Write a script which collects all *.cs files and concatenates them 编写一个脚本,收集所有* .cs文件并将其串联

在此处输入图片说明

This is probably not the most optimal way to do this but this is a algorithm which can do what you need: 这可能不是执行此操作的最佳方法,但这是一种可以满足您需要的算法:

  • loop through every file and grab every line starting with " using " -> write them to a temp file/buffer 遍历每个文件并使用“ using ”开始抓取using “-”开头的每一行->将它们写入临时文件/缓冲区
  • check for duplicates and remove them 检查重复项并将其删除
  • get the position of the first ' { ' after the charsequence " namespace " 获取字符序列“ namespace ”之后的第一个“ { ”的位置
  • get the position of the last ' } ' in the file 拿到最后“的位置}在文件”
  • append the text in between these two positions onto a temp file/buffer 将这两个位置之间的文本附加到临时文件/缓冲区
  • append the second file/buffer to the first one 将第二个文件/缓冲区追加到第一个
  • write out the merged buffer 写出合并缓冲区

It is very subjective. 这是非常主观的。 I see the algorithm as the following in pseudo code: 我在伪代码中看到以下算法:

usingsLines = new HashSet<string>();
newFile = new StringBuilder();
foreeach (file in listOfFiles)
{
    var textFromFile = file.ReadToEnd();
    var usingOperators = textFromFile.GetUsings();
    var fileBody = textFromFile.GetBody();
    newFile+=fileBody ;
}
newFile = usingsLines.ToString() + newFile;
// As a result if will have something like this
// using usingsfromFirstFile;
// using usingsfromSecondFile;
// 
// namespace FirstFileNamespace
// {
// ...
// }
// 
// namespace SecondFileNamespace
// {
// ...
// }

But keep in mind this approach can lead to conflicts in namespaces if two different namespaces contan the same classes etc. To solve it you need to fix it manually, or rid of using operator and use fullnames with namespaces. 但是请记住,如果两个不同的名称空间包含相同的类等,则此方法可能导致名称空间冲突。要解决此问题,您需要手动对其进行修复,或者避免using运算符并在名称空间中使用全名。

Also these few links may be useful: Merge files , Merge file in Java 另外,以下几个链接可能很有用: 合并文件Java中的合并文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM