Skip to content

Commit

Permalink
Remove .Net Core 1.0 requirement for Loc and fix mac build (#841)
Browse files Browse the repository at this point in the history
* Remove .Net Core 1.0 requirement for Loc and fix mac build
Use StringResourceTool to remove .Net Core 1.0 dependency for Loc
Fixed Mac build by copying the code used in internal repo for build.
- This now mostly matches internal, so expect this should work OK.
  • Loading branch information
kevcunnane committed Aug 1, 2019
1 parent ec09ad2 commit e0cce70
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 74 deletions.
Binary file removed bin/nuget/Microsoft.DataTools.SrGen.1.0.2.nupkg
Binary file not shown.
File renamed without changes.
161 changes: 90 additions & 71 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -554,87 +554,106 @@ Task("SetPackageVersions")
Task("SRGen")
.Does(() =>
{
var projects = System.IO.Directory.GetFiles(sourceFolder, "*.csproj", SearchOption.AllDirectories).ToList();
var locTemplateDir = System.IO.Path.Combine(sourceFolder, "../localization");
foreach(var project in projects) {
var projectDir = System.IO.Path.GetDirectoryName(project);
var localizationDir = System.IO.Path.Combine(projectDir, "Localization");
var projectName = (new System.IO.DirectoryInfo(projectDir)).Name;
var projectNameSpace = projectName + ".Localization";
var projectStrings = System.IO.Path.Combine(localizationDir, "sr.strings");
if (!System.IO.File.Exists(projectStrings))
{
Information("Project {0} doesn't contain 'sr.strings' file", projectName);
continue;
}
// save current working directory to restore at end of task
var taskStartedWorkingDirectory = System.IO.Directory.GetCurrentDirectory();
try
{
var projects = System.IO.Directory.GetFiles(sourceFolder, "*.csproj", SearchOption.AllDirectories).ToList();
var locTemplateDir = System.IO.Path.Combine(sourceFolder, "../localization");
foreach(var project in projects) {
var projectDir = System.IO.Path.GetDirectoryName(project);
// set current directory to this project so relative paths can be reliably
// used. This is to address an issue with quoting differences between Windows
// and MacOS. On MacOS the dotnet command ends up calling SRGen with quotations around
// the arguments stripped off. This causes SRGen to interpret the arg values as options
System.IO.Directory.SetCurrentDirectory(projectDir);
// build remaining paths relative to the project directory
var localizationDir = System.IO.Path.Combine(".", "Localization");
var projectName = (new System.IO.DirectoryInfo(projectDir)).Name;
var projectNameSpace = projectName + ".Localization";
var projectStrings = System.IO.Path.Combine(localizationDir, "sr.strings");
if (!System.IO.File.Exists(projectStrings))
{
Information("Project {0} doesn't contain 'sr.strings' file", projectName);
continue;
}
var srgenPath = System.IO.Path.Combine(toolsFolder, "Microsoft.DataTools.SrGen", "lib", "netcoreapp1.0", "srgen.dll");
var outputResx = System.IO.Path.Combine(localizationDir, "sr.resx");
var inputXliff = System.IO.Path.Combine(localizationDir, "transXliff");
var outputXlf = System.IO.Path.Combine(localizationDir, "sr.xlf");
var outputCs = System.IO.Path.Combine(localizationDir, "sr.cs");
var srgenPath = System.IO.Path.Combine(toolsFolder, "Microsoft.Data.Tools.StringResourceTool", "tools", "netcoreapp2.2", "any", "srgen.dll");
var outputResx = System.IO.Path.Combine(localizationDir, "sr.resx");
var inputXliff = System.IO.Path.Combine(localizationDir, "transXliff");
var outputXlf = System.IO.Path.Combine(localizationDir, "sr.xlf");
var outputCs = System.IO.Path.Combine(localizationDir, "sr.cs");
// Delete preexisting resx and designer files
if (System.IO.File.Exists(outputResx))
{
System.IO.File.Delete(outputResx);
}
if (System.IO.File.Exists(outputCs))
{
System.IO.File.Delete(outputCs);
}
// Delete preexisting resx and designer files
if (System.IO.File.Exists(outputResx))
{
System.IO.File.Delete(outputResx);
}
if (System.IO.File.Exists(outputCs))
{
System.IO.File.Delete(outputCs);
}
if (!System.IO.Directory.Exists(inputXliff))
{
System.IO.Directory.CreateDirectory(inputXliff);
}
if (!System.IO.Directory.Exists(inputXliff))
{
System.IO.Directory.CreateDirectory(inputXliff);
}
// Run SRGen
var dotnetArgs = string.Format("{0} -or \"{1}\" -oc \"{2}\" -ns \"{3}\" -an \"{4}\" -cn SR -l CS -dnx \"{5}\"",
srgenPath, outputResx, outputCs, projectName, projectNameSpace, projectStrings);
Information("{0}", dotnetArgs);
Run(dotnetcli, dotnetArgs)
.ExceptionOnError("Failed to run SRGen.");
// Update XLF file from new Resx file
var doc = new XliffParser.XlfDocument(outputXlf);
doc.UpdateFromSource();
var outputXlfFile = doc.Files.Single();
foreach (var unit in outputXlfFile.TransUnits)
{
unit.Target = unit.Source;
}
doc.Save();
// Run SRGen
var dotnetArgs = string.Format("{0} -or \"{1}\" -oc \"{2}\" -ns \"{3}\" -an \"{4}\" -cn SR -l CS -dnx \"{5}\"",
srgenPath, outputResx, outputCs, projectName, projectNameSpace, projectStrings);
Information("{0}", dotnetcli);
Information("{0}", dotnetArgs);
Run(dotnetcli, dotnetArgs)
.ExceptionOnError("Failed to run SRGen.");
// Update XLF file from new Resx file
var doc = new XliffParser.XlfDocument(outputXlf);
doc.UpdateFromSource();
var outputXlfFile = doc.Files.Single();
foreach (var unit in outputXlfFile.TransUnits)
{
unit.Target = unit.Source;
}
doc.Save();
// Update ResX files from new xliff files
var xlfDocNames = System.IO.Directory.GetFiles(inputXliff, "*.xlf", SearchOption.AllDirectories).ToList();
foreach(var docName in xlfDocNames)
{
// load our language XLIFF
var xlfDoc = new XliffParser.XlfDocument(docName);
var xlfFile = xlfDoc.Files.Single();
// Update ResX files from new xliff files
var xlfDocNames = System.IO.Directory.GetFiles(inputXliff, "*.xlf", SearchOption.AllDirectories).ToList();
foreach(var docName in xlfDocNames)
{
// load our language XLIFF
var xlfDoc = new XliffParser.XlfDocument(docName);
var xlfFile = xlfDoc.Files.Single();
// load a language template
var templateFileLocation = System.IO.Path.Combine(locTemplateDir, System.IO.Path.GetFileName(docName) + ".template");
var templateDoc = new XliffParser.XlfDocument(templateFileLocation);
var templateFile = templateDoc.Files.Single();
// load a language template
var templateFileLocation = System.IO.Path.Combine(locTemplateDir, System.IO.Path.GetFileName(docName) + ".template");
var templateDoc = new XliffParser.XlfDocument(templateFileLocation);
var templateFile = templateDoc.Files.Single();
// iterate through our tranlation units and prune invalid units
foreach (var unit in xlfFile.TransUnits)
{
// if a unit does not have a target it is invalid
if (unit.Target != null) {
templateFile.AddTransUnit(unit.Id, unit.Source, unit.Target, 0, 0);
}
}
// iterate through our tranlation units and prune invalid units
foreach (var unit in xlfFile.TransUnits)
{
// if a unit does not have a target it is invalid
if (unit.Target != null) {
templateFile.AddTransUnit(unit.Id, unit.Source, unit.Target, 0, 0);
}
}
// export modified template to RESX
var newPath = System.IO.Path.Combine(localizationDir, System.IO.Path.GetFileName(docName));
templateDoc.SaveAsResX(newPath.Replace("xlf","resx"));
// export modified template to RESX
var newPath = System.IO.Path.Combine(localizationDir, System.IO.Path.GetFileName(docName));
templateDoc.SaveAsResX(newPath.Replace("xlf","resx"));
}
}
}
finally
{
// restore the original working directory
System.IO.Directory.SetCurrentDirectory(taskStartedWorkingDirectory);
}
});

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions scripts/archiving.cake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#load "runhelpers.cake"

using System;
using System.IO.Compression;
using System.Text.RegularExpressions;

Expand Down
7 changes: 4 additions & 3 deletions scripts/packages.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Cake" version="0.17.0" />
<package id="Newtonsoft.Json" version="8.0.3" />
<package id="Microsoft.DataTools.SrGen" version="1.0.2" />
<package id="Cake" version="0.30.0" />
<package id="Newtonsoft.Json" version="9.0.1" />
<package id="Microsoft.Data.Tools.StringResourceTool" version="1.0.0" />
<package id="Mono.TextTransform" version="1.0.0" />
<package id="mssql.XliffParser" version="0.5.3" />
<package id="mssql.ResX" version="0.2.0" />
</packages>

0 comments on commit e0cce70

Please sign in to comment.