" SCM. - ?"
. . , , .cs( a# project), ( ... ).
, #. MIT/X11:
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System;
namespace Tools
{
class LicenseInjector
{
public static void Main()
{
Console.WriteLine("Please enter the full root folder path (i.e. /home/usr/code/): ");
string folder = Console.ReadLine();
Console.WriteLine("Please enter full path to license data: ");
string LicensePath = Console.ReadLine();
FileStream ReadFile = new FileStream(LicensePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(ReadFile);
string license = sr.ReadToEnd();
sr.Close();
ReadFile.Close();
Recursion.RecursivePrepend(folder, license);
}
}
public static class Recursion
{
public static void RecursivePrepend(string folder, string license)
{
string [] Subdirs = Directory.GetDirectories(folder);
if(Subdirs.Length != 0)
{
foreach(string s in Subdirs)
RecursivePrepend(s, license);
}
string[] files = PatternFind.DirPatternFinder(folder, ".cs");
foreach(string s in files)
Prepend.Prepender(s, license);
}
}
public static class PatternFind
{
public static string[] DirPatternFinder(string ToLook, string Pattern)
{
Regex r = new Regex(Pattern);
string[] files = Directory.GetFiles(ToLook);
List<string> results = new List<string>();
for(int i = 0; i < files.Length; ++i)
{
Match m = r.Match(files[i]);
if(m.Success)
results.Add(files[i]);
}
return results.ToArray();
}
public static string PatternFinder(string Pattern1, string Pattern2)
{
Regex r = new Regex(Pattern1);
Match m = r.Match(Pattern2);
return m.ToString();
}
}
public static class Prepend
{
public static void Prepender(string path, string ToPut)
{
FileStream ReadFile = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(ReadFile);
string s = sr.ReadToEnd();
sr.Close();
ReadFile.Close();
bool HasLicense = false;
if(s.Substring(0, 2) == "/*")
HasLicense = true;
if(HasLicense)
{
int i = 1;
string c = "(";
while(c != "*/" && i < s.Length)
{
c = s.Substring(i, 2);
++i;
}
if(String.Compare("\n\n", s.Substring(i+1, 2)) == 0)
{
s = s.Remove(0, i+3);
}
else
{
s = s.Remove(0, i+2);
}
}
FileStream WriteFile = new FileStream(path, FileMode.Open, FileAccess.Write);
StreamWriter FileWriter = new StreamWriter(WriteFile);
FileWriter.Write(ToPut);
FileWriter.Write("\n");
FileWriter.Write(s);
FileWriter.Close();
WriteFile.Close();
return;
}
}
}