Skip to content

Instantly share code, notes, and snippets.

@sapsari
Last active July 23, 2019 06:19
Show Gist options
  • Save sapsari/fbe93f1f245d012954b61f3744471f88 to your computer and use it in GitHub Desktop.
Save sapsari/fbe93f1f245d012954b61f3744471f88 to your computer and use it in GitHub Desktop.
buildalyzer temp project creation for transform
public class WorkspaceManagerCore : Buildalyzer.Construction.IProjectTransformer
{
public void Transform(XDocument x)
{
// Your transform code
}
static string GetUniqueFilename(string filename)
{
string basename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(filename),
System.IO.Path.GetFileNameWithoutExtension(filename));
string uniquefilename = $"{basename}_TEMP_{basename.GetHashCode()}{System.IO.Path.GetExtension(filename)}";
return uniquefilename;
}
public Workspace CreateWorkspace(string solutionPath)
{
var stringBuilder = new System.Text.StringBuilder();
var writer = new System.IO.StringWriter(stringBuilder);
var analyzerOptions = new AnalyzerManagerOptions
{
LogWriter = writer,
};
var managerOriginal = new AnalyzerManager(solutionPath, analyzerOptions);
var projectPath = managerOriginal.Projects.First().Value.ProjectFile.Path;
var duplicateProjectPath = GetUniqueFilename(projectPath);
var doc = XDocument.Load(projectPath);
Transform(doc);
doc.Save(duplicateProjectPath);
var managerDuplicate = new AnalyzerManager(analyzerOptions);
var analyzerDuplicate = managerDuplicate.GetProject(duplicateProjectPath);
var workspace = analyzerDuplicate.GetWorkspace();
var build = analyzerDuplicate.Build();
if (build.OverallSuccess)
{
Log("Buildalyzer success");
}
else
{
Log("Buildalyzer failure");
Log(stringBuilder.ToString());
}
System.IO.File.Delete(duplicateProjectPath);
return workspace;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment