135 lines
4.7 KiB
C#
135 lines
4.7 KiB
C#
const string AuthorName = "Syntriax";
|
|
const string AuthorEmail = "Syntriax@gmail.com";
|
|
|
|
const string ProgramSignature = $"Signed by an automated program written by the author";
|
|
const string CommentBlockStartTag = "<CommentBlockStart>";
|
|
const string CommentBlockEndTag = "<CommentBlockEnd>";
|
|
const string CreationDateTag = "<CreationDate>";
|
|
string License = $@"MIT License
|
|
|
|
Copyright (c) {DateTime.UtcNow.Year.ToString()} {AuthorName}
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the ""Software""), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.";
|
|
string LicenseSection = $@"
|
|
------------------------------------------------------------
|
|
|
|
{License}";
|
|
|
|
string Signature =
|
|
$@"{CommentBlockStartTag}
|
|
Author: {AuthorName} <{AuthorEmail}>
|
|
Creation Date: {CreationDateTag}
|
|
{ProgramSignature}
|
|
{CommentBlockEndTag}
|
|
";
|
|
// string Signature =
|
|
// $@"{CommentBlockStartTag}
|
|
// Author: {AuthorName}
|
|
// Email: {AuthorEmail}
|
|
// Creation Date: {CreationDateTag} UTC
|
|
// {ProgramSignature}
|
|
// {LicenseSection}
|
|
// {CommentBlockEndTag}
|
|
// ";
|
|
|
|
void ExploreDirectory(string path)
|
|
{
|
|
string[] files = Directory.GetFiles(path);
|
|
string[] directories = Directory.GetDirectories(path);
|
|
|
|
foreach (string filePath in files)
|
|
Sign(filePath);
|
|
|
|
foreach (string directoryPath in directories)
|
|
ExploreDirectory(directoryPath);
|
|
}
|
|
|
|
void Sign(string filePath)
|
|
{
|
|
string[] lines = File.ReadAllLines(filePath);
|
|
|
|
foreach (string line in lines)
|
|
if (line.Contains(ProgramSignature))
|
|
return;
|
|
// lines = RemoveSignature(lines, Path.GetExtension(filePath));
|
|
|
|
string newContents = GetSignatureString(filePath);
|
|
if (string.IsNullOrEmpty(newContents))
|
|
return;
|
|
newContents += string.Join('\n', lines);
|
|
newContents += "\r\n";
|
|
Console.WriteLine($"Signing -> {filePath}");
|
|
File.WriteAllText(filePath, newContents);
|
|
}
|
|
|
|
string[] RemoveSignature(string[] lines, string extension)
|
|
{
|
|
int i = 0;
|
|
for (i = 0; i < lines.Length; i++)
|
|
if (lines[i].Contains(ProgramSignature))
|
|
break;
|
|
|
|
(string extensionCommentBlockStartTag, string extensionCommentBlockEndTag) = GetExtensionTags(extension);
|
|
int startIndex = i;
|
|
int endIndex = i;
|
|
|
|
while (!lines[startIndex].Contains(extensionCommentBlockStartTag))
|
|
startIndex--;
|
|
while (!lines[endIndex].Contains(extensionCommentBlockEndTag))
|
|
endIndex++;
|
|
|
|
List<string> result = new List<string>(lines);
|
|
result.RemoveRange(startIndex, endIndex - startIndex + 1);
|
|
return result.ToArray();
|
|
}
|
|
|
|
string GetSignatureString(string filePath)
|
|
{
|
|
DateTime dateTime = File.GetCreationTimeUtc(filePath);
|
|
string extension = Path.GetExtension(filePath);
|
|
string result = Signature;
|
|
|
|
(string extensionCommentBlockStartTag, string extensionCommentBlockEndTag) = GetExtensionTags(extension);
|
|
if (string.IsNullOrEmpty(extensionCommentBlockStartTag) || string.IsNullOrEmpty(extensionCommentBlockEndTag))
|
|
return "";
|
|
|
|
result = result.Replace(CommentBlockStartTag, extensionCommentBlockStartTag);
|
|
result = result.Replace(CommentBlockEndTag, extensionCommentBlockEndTag);
|
|
result = result.Replace(CreationDateTag, dateTime.ToLongDateString());
|
|
// result = result.Replace(CreationDateTag, $"{dateTime.ToLongDateString()} - {dateTime.ToLongTimeString()}");
|
|
return result;
|
|
}
|
|
|
|
(string extensionCommentBlockStartTag, string extensionCommentBlockEndTag) GetExtensionTags(string extension)
|
|
{
|
|
switch (extension)
|
|
{
|
|
case ".cs":
|
|
case ".jslib":
|
|
return ("/*", "*/");
|
|
case ".uxml":
|
|
return ("<!--", "-->");
|
|
default:
|
|
return ("", "");
|
|
}
|
|
}
|
|
|
|
ExploreDirectory(Environment.GetCommandLineArgs()[1]);
|
|
Console.ReadKey();
|