Skip to content

Instantly share code, notes, and snippets.

@lot224
Last active August 29, 2015 14:11
Show Gist options
  • Save lot224/de67419729efcbf8421e to your computer and use it in GitHub Desktop.
Save lot224/de67419729efcbf8421e to your computer and use it in GitHub Desktop.
MS Build Tasks, concatenate, templates
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace eo {
public class Concatenate : Task {
public bool ExcludeHeaders { get; set; }
[Required]
public string output { get; set; }
[Required]
public ITaskItem[] Files { get; set; }
public Concatenate() {
Files = new ITaskItem[0];
}
public override bool Execute() {
FileInfo _fileOut = new FileInfo(output);
if (Files.Length > 0) {
string _header = "/*!";
_header += Environment.NewLine;
_header += " * File: {0}";
_header += Environment.NewLine;
_header += " * Date: {1}";
_header += Environment.NewLine;
_header += " */";
_header += Environment.NewLine;
_header += Environment.NewLine;
_header += "{2}";
_header += Environment.NewLine;
_header += Environment.NewLine;
if (ExcludeHeaders)
_header = "{2}" + Environment.NewLine;
StringBuilder sb = new StringBuilder();
Log.LogMessage("Building ({0}).", _fileOut.FullName);
Log.LogMessage(" {0} included files.", Files.Length);
foreach (ITaskItem item in Files) {
FileInfo FileIn = new FileInfo(item.ItemSpec);
string data = string.Empty;
try {
data = File.ReadAllText(FileIn.FullName);
} catch {
Log.LogMessage("Unable to open file ({0}),", FileIn.FullName);
}
if (data.Length == 0) {
Log.LogWarning("{0} is empty, skipping file.", FileIn.FullName);
} else {
Log.LogMessage(" => {0}", FileIn.FullName);
sb.Append(string.Format(_header, FileIn.FullName, DateTime.UtcNow.ToString("R"), data));
}
}
string nResult = sb.ToString();
try {
string path = _fileOut.DirectoryName;
System.IO.Directory.CreateDirectory(path);
File.WriteAllText(_fileOut.FullName, nResult);
} catch {
Log.LogWarning(
"Unable to write ({0}), path may not exist or file may be locked.",
_fileOut.FullName
);
return false;
}
} else {
Log.LogWarning(
"No file found to concatenate, Ignoring Request. \"{0}\" not created.",
_fileOut.Name
);
}
Log.LogMessage(Environment.NewLine);
return true;
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace eo {
public class Template : Task {
public string prefix { get; set; }
[Required]
public string output { get; set; }
[Required]
public ITaskItem[] Files { get; set; }
public Template() {
Files = new ITaskItem[0];
}
public override bool Execute() {
string template = "";
template += "angular.module(\"{0}\", []).run([\"$templateCache\", function ($templateCache) {" + Environment.NewLine;
template += " $templateCache.put(\"{0}\"," + Environment.NewLine;
template += "{1}";
template += " \"\");" + Environment.NewLine;
template += "}]);" + Environment.NewLine;
string module = "angular.module(\"eo.templates\", [{0}]);";
FileInfo _fileOut = new FileInfo(output);
Log.LogMessage(Environment.NewLine + "TEMPLATE GENERATOR");
StringBuilder outFile = new StringBuilder();
List<string>names = new List<string>();
if (Files.Length > 0) {
Log.LogMessage(" Output: {0}", _fileOut.FullName);
Log.LogMessage(" Working Dir: {0}", Environment.CurrentDirectory);
Log.LogMessage(" Prefix: {0}", prefix);
Log.LogMessage(" File List:");
string workingDir = Environment.CurrentDirectory + "\\";
foreach (ITaskItem item in Files) {
FileInfo FileIn = new FileInfo(item.ItemSpec);
string name = prefix + FileIn.FullName.Replace(workingDir, "").Replace("\\", "/");
names.Add(name);
string line;
int counter = 0;
StringBuilder sb = new StringBuilder();
System.IO.StreamReader file = new System.IO.StreamReader(FileIn.FullName);
while ((line = file.ReadLine()) != null) {
if (line.Length > 0)
sb.AppendLine(" \"" + line.Replace("\"", "\\\"") + "\\n\" +");
counter += 1;
}
file.Close();
Log.LogMessage(" Template Name: {0}, Line Count: {1}, File: {2}", name, counter.ToString(), FileIn.FullName);
outFile.AppendLine(template.Replace("{0}", name).Replace("{1}", sb.ToString()));
}
string nResult = string.Format(module, "\"" + string.Join("\",\"", names.ToArray()) + "\"") + Environment.NewLine + Environment.NewLine;
nResult += outFile.ToString();
System.IO.File.WriteAllText(_fileOut.FullName, nResult);
} else {
Log.LogWarning(" No file found to concatenate, Ignoring Request. \"{0}\" not created.", _fileOut.Name);
}
Log.LogMessage(Environment.NewLine);
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment