protected override async Task <CommandLineOperationSettings> GetSettings(ResolveContext context) { Assert.IsNotNull(context); string inputPath = context.GetInputPath(); string workingDir = context.GetWorkingDirectory(); string path = Path.Join(workingDir, inputPath, FileName); using FileStream st = File.Open(path, FileMode.Open, FileAccess.Read); FileBasedCommandLineOperationSettings settings = await JsonFormatter.Deserialize <FileBasedCommandLineOperationSettings>(st).ConfigureAwait(false); CommandLineOperationSettings res = new CommandLineOperationSettings { WorkingDirectory = settings.WorkingDirectory, Shell = settings.Shell }; foreach (string v in settings.Scripts) { CommandLineTemplate item = new CommandLineTemplate { Raw = v }; res.Scripts.Add(item); } return(res); }
public async Task Basic() { CommandLineTemplate builder = new CommandLineTemplate(); builder.Commands.Add("gcc"); builder.Arguments.Add("a.c"); builder.Raw = "--version"; _ = builder.WithFlag("ff", "-") .WithFlag("O2", "-") .WithOption("o", "a.out", "-") .WithOption("cc", 1, "--") .WithOption("cc", "a", "--") .WithoutFlag("-ff") .WithoutOption("--cc"); Assert.AreEqual(0, builder.GetVariables().Count); Assert.AreEqual("gcc a.c -O2 -o a.out --version", string.Join(' ', await builder.Resolve(new ResolveContext()))); }
private void GenerateTemplatedCommandLine(CommandLineBuilder builder) { // Match all instances of [asdf], where "asdf" can be any combination of any // characters *except* a [ or an ]. i.e., if "[ [ sdf ]" is passed, then we will // match "[ sdf ]" string matchString = @"\[[^\[\]]+\]"; Regex regex = new Regex(matchString, RegexOptions.ECMAScript); MatchCollection matches = regex.Matches(CommandLineTemplate); int indexOfEndOfLastSubstitution = 0; foreach (Match match in matches) { if (match.Length == 0) { continue; } // Because we match non-greedily, in the case where we have input such as "[[[[[foo]", the match will // be "[foo]". However, if there are multiple '[' in a row, we need to do some escaping logic, so we // want to know what the first *consecutive* square bracket was. int indexOfFirstBracketInMatch = match.Index; // Indexing using "indexOfFirstBracketInMatch - 1" is safe here because it will always be // greater than indexOfEndOfLastSubstitution, which will always be 0 or greater. while (indexOfFirstBracketInMatch > indexOfEndOfLastSubstitution && CommandLineTemplate[indexOfFirstBracketInMatch - 1].Equals('[')) { indexOfFirstBracketInMatch--; } // Append everything we know we want to add -- everything between where the last substitution ended and // this match (including previous '[' that were not initially technically part of the match) begins. if (indexOfFirstBracketInMatch != indexOfEndOfLastSubstitution) { builder.AppendTextUnquoted(CommandLineTemplate.Substring(indexOfEndOfLastSubstitution, indexOfFirstBracketInMatch - indexOfEndOfLastSubstitution)); } // Now replace every "[[" with a literal '['. We can do this by simply counting the number of '[' between // the first one and the start of the match, since by definition everything in between is an '['. // + 1 because match.Index is also a bracket. int openBracketsInARow = match.Index - indexOfFirstBracketInMatch + 1; if (openBracketsInARow % 2 == 0) { // even number -- they all go away and the rest of the match is appended literally. for (int i = 0; i < openBracketsInARow / 2; i++) { builder.AppendTextUnquoted("["); } builder.AppendTextUnquoted(match.Value.Substring(1, match.Value.Length - 1)); } else { // odd number -- all but one get merged two at a time, and the rest of the match is substituted. for (int i = 0; i < (openBracketsInARow - 1) / 2; i++) { builder.AppendTextUnquoted("["); } // Determine which property the user has specified in the template. string propertyName = match.Value.Substring(1, match.Value.Length - 2); if (String.Equals(propertyName, "AllOptions", StringComparison.OrdinalIgnoreCase)) { // When [AllOptions] is specified, we append all switch-type options. CommandLineBuilder tempBuilder = new CommandLineBuilder(true); GenerateStandardCommandLine(tempBuilder, true); builder.AppendTextUnquoted(tempBuilder.ToString()); } else if (String.Equals(propertyName, "AdditionalOptions", StringComparison.OrdinalIgnoreCase)) { BuildAdditionalArgs(builder); } else if (IsPropertySet(propertyName)) { CommandLineToolSwitch property = _activeCommandLineToolSwitches[propertyName]; // verify the dependencies if (VerifyDependenciesArePresent(property) && VerifyRequiredArgumentsArePresent(property, false)) { CommandLineBuilder tempBuilder = new CommandLineBuilder(true); GenerateCommandsAccordingToType(tempBuilder, property, false); builder.AppendTextUnquoted(tempBuilder.ToString()); } } else if (!PropertyExists(propertyName)) { // If the thing enclosed in square brackets is not in fact a property, we // don't want to replace it. builder.AppendTextUnquoted('[' + propertyName + ']'); } } indexOfEndOfLastSubstitution = match.Index + match.Length; } builder.AppendTextUnquoted(CommandLineTemplate.Substring(indexOfEndOfLastSubstitution, CommandLineTemplate.Length - indexOfEndOfLastSubstitution)); }