Our team is trying to implement a master csv file for all the language translations we have to enable our systems team to maintain our resource files. I'm creating a dll that will be called before build to convert said csv file into their respective resx and designer files. I've got generating the resx files and designer files completed already but am struggling to add the new resx files that are generated to the resource project properly. I looked at this solution to add a file to the project but I can't add the dependencies right.
Here's the code I'm trying to use
var p =
Microsoft.Build.Evaluation
.ProjectCollection.GlobalProjectCollection
.LoadedProjects.FirstOrDefault(pr => pr.FullPath == projFilePath);
if (p == null)
p = new Microsoft.Build.Evaluation.Project(projFilePath);
p.AddItem("Folder", @"C:\projects\BabDb\test\test2");
p.AddItem("Compile", @"C:\projects\BabDb\test\test2\Class1.cs");
p.Save();
To anyone who might stumble on to this question, here's how I ended up fixing it
var resourceProject = Microsoft.Build.Evaluation
.ProjectCollection.GlobalProjectCollection
.LoadedProjects.FirstOrDefault(pr => pr.FullPath == projFilePath);
if (resourceProject == null)
resourceProject = new Microsoft.Build.Evaluation.Project(projFilePath);
//add resx file if it doesn't exist in the project
if (resourceProject.Items.FirstOrDefault(i => i.EvaluatedInclude == path) == null)
{
string designerPath = path.Replace(".resx", ".Designer.cs");
//Add metadata when the resx file has a designer file
resourceProject.AddItem("EmbeddedResource", path);
if (withDesigner)
{
var projectItem = resourceProject.Items.FirstOrDefault(i => i.EvaluatedInclude == path);
projectItem.SetMetadataValue("Generator", "ResXFileCodeGenerator");
projectItem.SetMetadataValue("LastGenOutput", Path.GetFileName(designerPath));
resourceProject.AddItem("Compile", designerPath);
var designerItem = resourceProject.Items.FirstOrDefault(i => i.EvaluatedInclude == designerPath);
designerItem.SetMetadataValue("AutoGen", "True");
designerItem.SetMetadataValue("DesignTime", "True");
designerItem.SetMetadataValue("DependentUpon", Path.GetFileName(path));
}
resourceProject.Save();
}