public static string UpdateProjectFile(string filePath, Manifest manifest, IDictionary <string, string> tokens = null) { if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException(nameof(filePath)); } if (tokens == null) { tokens = ReplacementToken.Create(); } ReplacementToken.Append(tokens, manifest); if (string.Equals(Path.GetFileName(filePath), "package.json", StringComparison.OrdinalIgnoreCase)) { return(UpdatePackageJson(filePath, manifest, tokens)); } if (string.Equals(Path.GetFileName(filePath), "AssemblyInfo.cs", StringComparison.OrdinalIgnoreCase)) { return(UpdateAssemblyInfo(filePath, manifest, tokens)); } else if (filePath.EndsWith("proj", StringComparison.OrdinalIgnoreCase)) { return(UpdateDotnetProjectFile(filePath, manifest, tokens)); } else { switch (Path.GetExtension(filePath).ToLowerInvariant()) { case ".vsixmanifest": return(UpdateVsixManifest(filePath, manifest, tokens)); } } throw new NotSupportedException($"'{Path.GetExtension(filePath)}' files are not supported as yet."); }
/// <summary> /// </summary> protected override void ProcessRecord() { if (File.Exists(ProjectFile) == false) { return; } string repositoryPath = Git.GetWorkingDirectory(ProjectFile); _manifest.SetVersionFormat(Git.GetCurrentBranchName(repositoryPath)); _repositories.Add(repositoryPath); IDictionary <string, string> tokens = ReplacementToken.Create(); tokens.AddGitTokens(repositoryPath); tokens["item-name"] = Path.GetFileName(ProjectFile); tokens["item-basename"] = Path.GetFileNameWithoutExtension(ProjectFile); tokens["item-directory"] = Path.GetDirectoryName(ProjectFile); tokens["item-directory-name"] = Path.GetFileName(Path.GetDirectoryName(ProjectFile)); if (ShouldProcess(ProjectFile)) { File.WriteAllText(ProjectFile, Editor.UpdateProjectFile(ProjectFile, _manifest, tokens)); if (Commit) { Git.Stage(ProjectFile, repositoryPath); } } WriteObject(ProjectFile); }
internal static string UpdatePackageJson(string filePath, Manifest manifest, IDictionary <string, string> tokens) { if (!File.Exists(filePath)) { throw new FileNotFoundException($"Could not find file at '{filePath}'."); } if (manifest == null) { throw new ArgumentNullException(nameof(manifest)); } // documentation: https://docs.npmjs.com/files/package.json string expand(string input) => ReplacementToken.Expand(input, tokens); IEnumerable <(string, object)> map = new (string, object)[]
public void TokenReplacerReplacesToken(string openToken, string closeToken, string key, string newValue) { // arrange var token = new ReplacementToken(openToken, closeToken); var testSentence = $"This is a test sentence. {token.OpenToken}{key}{token.CloseToken}. With multiple instances of {token.OpenToken}{key}{token.CloseToken}!"; var expectedResult = $"This is a test sentence. {newValue}. With multiple instances of {newValue}!"; var testDict = new Dictionary <string, string> { { key, newValue } }; // act var result = TokenReplacer.Process(testSentence, token, testDict); // assert result.ShouldBeEquivalentTo(expectedResult); }
public void Can_replace_tokens() { // Arrange const string value = "changed", format = "value: {{0}}"; var tokens = ReplacementToken.Create(); var results = new List <string>(); // Act foreach (var item in tokens.Keys) { results.Add(ReplacementToken.Expand(string.Format(format, value), tokens)); } // Assert results.ShouldNotBeEmpty(); results.ShouldAllBe(x => x == string.Format(format, x)); }