dotnet-resgen should use a file to accept argument values

This commit is contained in:
Pranav K 2016-01-29 16:59:09 -08:00 committed by Pavel Krymets
parent 7a203c02e1
commit 7a03c91350
3 changed files with 148 additions and 96 deletions

View file

@ -314,14 +314,12 @@ namespace Microsoft.DotNet.Tools.Compiler
if (success)
{
success &= GenerateCultureResourceAssemblies(context.ProjectFile, dependencies, outputPath);
success &= GenerateCultureResourceAssemblies(context.ProjectFile, dependencies, intermediateOutputPath, outputPath);
}
return PrintSummary(diagnostics, sw, success);
}
private static void RunScripts(ProjectContext context, string name, Dictionary<string, string> contextVariables)
{
foreach (var script in context.ProjectFile.Scripts.GetOrEmpty(name))
@ -370,16 +368,18 @@ namespace Microsoft.DotNet.Tools.Compiler
{
if (ResourceUtility.IsResxFile(resgenFile.InputFile))
{
var arguments = new[]
{
resgenFile.InputFile,
$"-o:{resgenFile.OutputFile}",
$"-v:{project.Version.Version}"
};
var rsp = Path.Combine(intermediateOutputPath, $"dotnet-resgen-resx.rsp");
File.WriteAllLines(rsp, arguments);
var result =
Command.CreateDotNet("resgen",
new string[]
{
$"{resgenFile.InputFile}",
"-o",
$"{resgenFile.OutputFile}",
"-v",
$"{project.Version.Version}"
})
Command.CreateDotNet("resgen", new[] { $"@{rsp}" })
.ForwardStdErr()
.ForwardStdOut()
.Execute();
@ -400,11 +400,14 @@ namespace Microsoft.DotNet.Tools.Compiler
return true;
}
private static bool GenerateCultureResourceAssemblies(Project project, List<LibraryExport> dependencies, string outputPath)
private static bool GenerateCultureResourceAssemblies(
Project project,
List<LibraryExport> dependencies,
string intermediateOutputPath,
string outputPath)
{
var referencePaths = CompilerUtil.GetReferencePathsForCultureResgen(dependencies);
var resgenReferenceArgs = referencePaths.Select(referencePath => $"-r \"{referencePath}\"").ToList();
var resgenReferenceArgs = referencePaths.Select(path => $"-r:{path}").ToList();
var cultureResgenFiles = CompilerUtil.GetCultureResources(project, outputPath);
foreach (var resgenFile in cultureResgenFiles)
@ -419,15 +422,17 @@ namespace Microsoft.DotNet.Tools.Compiler
var arguments = new List<string>();
arguments.AddRange(resgenReferenceArgs);
arguments.Add($"-o \"{resgenFile.OutputFile}\"");
arguments.Add($"-c {resgenFile.Culture}");
arguments.Add($"-v {project.Version.Version}");
arguments.AddRange(resgenFile.InputFileToMetadata.Select(fileToMetadata => $"\"{fileToMetadata.Key}\",{fileToMetadata.Value}"));
arguments.Add($"-o:{resgenFile.OutputFile}");
arguments.Add($"-c:{resgenFile.Culture}");
arguments.Add($"-v:{project.Version.Version}");
arguments.AddRange(resgenFile.InputFileToMetadata.Select(fileToMetadata => $"{fileToMetadata.Key},{fileToMetadata.Value}"));
var rsp = Path.Combine(intermediateOutputPath, $"dotnet-resgen.rsp");
File.WriteAllLines(rsp, arguments);
var result = Command.CreateDotNet("resgen", arguments)
.ForwardStdErr()
.ForwardStdOut()
.Execute();
var result = Command.CreateDotNet("resgen", new[] { $"@{rsp}" })
.ForwardStdErr()
.ForwardStdOut()
.Execute();
if (result.ExitCode != 0)
{
return false;

View file

@ -2,10 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Linq;
using Microsoft.Dnx.Runtime.Common.CommandLine;
using Microsoft.DotNet.Cli.Compiler.Common;
using System.CommandLine;
using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.Tools.Resgen
@ -16,68 +13,54 @@ namespace Microsoft.DotNet.Tools.Resgen
{
DebugHelper.HandleDebugSwitch(ref args);
var app = new CommandLineApplication(false);
app.Name = "resgen";
app.FullName = "Resource compiler";
app.Description = "Microsoft (R) .NET Resource Generator";
app.HelpOption("-h|--help");
var help = false;
string helpText = null;
var returnCode = 0;
var ouputFile = app.Option("-o", "Output file name", CommandOptionType.SingleValue);
var culture = app.Option("-c", "Ouput assembly culture", CommandOptionType.SingleValue);
var version = app.Option("-v", "Ouput assembly version", CommandOptionType.SingleValue);
var references = app.Option("-r", "Compilation references", CommandOptionType.MultipleValue);
var inputFiles = app.Argument("<input>", "Input files", true);
app.OnExecute(() =>
var resgenCommand = new ResgenCommand();
try
{
if (!inputFiles.Values.Any())
ArgumentSyntax.Parse(args, syntax =>
{
Reporter.Error.WriteLine("No input files specified");
return 1;
}
syntax.ApplicationName = "Resource compiler";
var intputResourceFiles = inputFiles.Values.Select(ParseInputFile).ToArray();
var outputResourceFile = ResourceFile.Create(ouputFile.Value());
syntax.HandleHelp = false;
syntax.HandleErrors = false;
switch (outputResourceFile.Type)
{
case ResourceFileType.Dll:
using (var outputStream = outputResourceFile.File.Create())
{
var metadata = new AssemblyInfoOptions();
metadata.Culture = culture.Value();
metadata.AssemblyVersion = version.Value();
syntax.DefineOption("o|output", ref resgenCommand.OutputFileName, "Output file name");
syntax.DefineOption("c|culture", ref resgenCommand.AssemblyCulture, "Ouput assembly culture");
syntax.DefineOption("v|version", ref resgenCommand.AssemblyVersion, "Ouput assembly version");
syntax.DefineOption("h|help", ref help, "Help for compile native.");
ResourceAssemblyGenerator.Generate(intputResourceFiles,
outputStream,
metadata,
Path.GetFileNameWithoutExtension(outputResourceFile.File.Name),
references.Values.ToArray()
);
}
break;
case ResourceFileType.Resources:
using (var outputStream = outputResourceFile.File.Create())
{
if (intputResourceFiles.Length > 1)
{
Reporter.Error.WriteLine("Only one input file required when generating .resource output");
return 1;
}
ResourcesFileGenerator.Generate(intputResourceFiles.Single().Resource, outputStream);
}
break;
default:
Reporter.Error.WriteLine("Resx output type not supported");
return 1;
}
syntax.DefineOptionList("r", ref resgenCommand.CompilationReferences, "Compilation references");
syntax.DefineParameterList("args", ref resgenCommand.Args, "Input files");
return 0;
});
helpText = syntax.GetHelpText();
});
}
catch (ArgumentSyntaxException exception)
{
Console.Error.WriteLine(exception.Message);
help = true;
returnCode = 1;
}
if (resgenCommand.Args.Count == 0)
{
Reporter.Error.WriteLine("No input files specified");
help = true;
returnCode = 1;
}
if (help)
{
Console.WriteLine(helpText);
return returnCode;
}
try
{
return app.Execute(args);
return resgenCommand.Execute();
}
catch (Exception ex)
{
@ -90,22 +73,5 @@ namespace Microsoft.DotNet.Tools.Resgen
}
}
private static ResourceSource ParseInputFile(string arg)
{
var seperatorIndex = arg.IndexOf(',');
string name = null;
string metadataName = null;
if (seperatorIndex > 0)
{
name = arg.Substring(0, seperatorIndex);
metadataName = arg.Substring(seperatorIndex + 1);
}
else
{
name = arg;
metadataName = arg;
}
return new ResourceSource(ResourceFile.Create(name), metadataName);
}
}
}

View file

@ -0,0 +1,81 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.DotNet.Cli.Compiler.Common;
using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.Tools.Resgen
{
public partial class ResgenCommand
{
public string OutputFileName = null;
public string AssemblyCulture = null;
public string AssemblyVersion = null;
public IReadOnlyList<string> CompilationReferences = null;
public IReadOnlyList<string> Args = null;
public int Execute()
{
var intputResourceFiles = Args.Select(ParseInputFile).ToArray();
var outputResourceFile = ResourceFile.Create(OutputFileName);
switch (outputResourceFile.Type)
{
case ResourceFileType.Dll:
using (var outputStream = outputResourceFile.File.Create())
{
var metadata = new AssemblyInfoOptions
{
Culture = AssemblyCulture,
AssemblyVersion = AssemblyVersion,
};
ResourceAssemblyGenerator.Generate(intputResourceFiles,
outputStream,
metadata,
Path.GetFileNameWithoutExtension(outputResourceFile.File.Name),
CompilationReferences.ToArray());
}
break;
case ResourceFileType.Resources:
using (var outputStream = outputResourceFile.File.Create())
{
if (intputResourceFiles.Length > 1)
{
Reporter.Error.WriteLine("Only one input file required when generating .resource output");
return 1;
}
ResourcesFileGenerator.Generate(intputResourceFiles.Single().Resource, outputStream);
}
break;
default:
Reporter.Error.WriteLine("Resx output type not supported");
return 1;
}
return 0;
}
private static ResourceSource ParseInputFile(string arg)
{
var seperatorIndex = arg.IndexOf(',');
string name;
string metadataName;
if (seperatorIndex > 0)
{
name = arg.Substring(0, seperatorIndex);
metadataName = arg.Substring(seperatorIndex + 1);
}
else
{
name = arg;
metadataName = arg;
}
return new ResourceSource(ResourceFile.Create(name), metadataName);
}
}
}