33// See the LICENSE file in the project root for more information
44
55using System . IO . Abstractions ;
6+ using System . Text . RegularExpressions ;
67using Elastic . Markdown . IO ;
78using Microsoft . Extensions . Logging ;
8- using System . Text . RegularExpressions ;
99
1010namespace Documentation . Builder . Cli ;
1111
1212internal class Move ( IFileSystem fileSystem , DocumentationSet documentationSet , ILoggerFactory loggerFactory )
1313{
1414 private readonly ILogger _logger = loggerFactory . CreateLogger < Move > ( ) ;
15- private readonly List < ( string filePath , string originalContent , string newContent ) > _changes = [ ] ;
15+ private readonly List < ( string filePath , string originalContent , string newContent ) > _changes = [ ] ;
1616 private const string ChangeFormatString = "Change \e [31m{0}\e [0m to \e [32m{1}\e [0m at \e [34m{2}:{3}:{4}\e [0m" ;
1717
1818 public async Task < int > Execute ( string ? source , string ? target , bool isDryRun , Cancel ctx = default )
@@ -30,46 +30,47 @@ public async Task<int> Execute(string? source, string? target, bool isDryRun, Ca
3030
3131 var ( _, sourceMarkdownFile ) = documentationSet . MarkdownFiles . Single ( i => i . Value . FilePath == sourcePath ) ;
3232
33- var soureContent = await fileSystem . File . ReadAllTextAsync ( sourceMarkdownFile . FilePath , ctx ) ;
33+ var sourceContent = await fileSystem . File . ReadAllTextAsync ( sourceMarkdownFile . FilePath , ctx ) ;
3434
3535 var markdownLinkRegex = new Regex ( @"\[([^\]]*)\]\(((?:\.{0,2}\/)?[^:)]+\.md(?:#[^)]*)?)\)" , RegexOptions . Compiled ) ;
36- var matches = markdownLinkRegex . Matches ( soureContent ) ;
3736
38- var change = Regex . Replace ( soureContent , markdownLinkRegex . ToString ( ) , match =>
37+ var change = Regex . Replace ( sourceContent , markdownLinkRegex . ToString ( ) , match =>
3938 {
4039 var originalPath = match . Value . Substring ( match . Value . IndexOf ( '(' ) + 1 , match . Value . LastIndexOf ( ')' ) - match . Value . IndexOf ( '(' ) - 1 ) ;
41- // var anchor = originalPath.Contains('#') ? originalPath[originalPath.IndexOf('#')..] : "";
42-
43- string newPath ;
4440
41+ var newPath = originalPath ;
4542 var isAbsoluteStylePath = originalPath . StartsWith ( '/' ) ;
46- if ( isAbsoluteStylePath )
43+ if ( ! isAbsoluteStylePath )
4744 {
48- newPath = originalPath ;
49- } else {
5045 var targetDirectory = Path . GetDirectoryName ( targetPath ) ! ;
5146 var sourceDirectory = Path . GetDirectoryName ( sourceMarkdownFile . FilePath ) ! ;
5247 var fullPath = Path . GetFullPath ( Path . Combine ( sourceDirectory , originalPath ) ) ;
5348 var relativePath = Path . GetRelativePath ( targetDirectory , fullPath ) ;
5449 newPath = relativePath ;
5550 }
5651 var newLink = $ "[{ match . Groups [ 1 ] . Value } ]({ newPath } )";
57- var lineNumber = soureContent . Substring ( 0 , match . Index ) . Count ( c => c == '\n ' ) + 1 ;
58- var columnNumber = match . Index - soureContent . LastIndexOf ( '\n ' , match . Index ) ;
59- _logger . LogInformation ( string . Format ( ChangeFormatString , match . Value , newLink ,
60- sourceMarkdownFile . SourceFile . FullName , lineNumber , columnNumber ) ) ;
61-
52+ var lineNumber = sourceContent . Substring ( 0 , match . Index ) . Count ( c => c == '\n ' ) + 1 ;
53+ var columnNumber = match . Index - sourceContent . LastIndexOf ( '\n ' , match . Index ) ;
54+ _logger . LogInformation (
55+ string . Format (
56+ ChangeFormatString ,
57+ match . Value ,
58+ newLink ,
59+ sourceMarkdownFile . SourceFile . FullName ,
60+ lineNumber ,
61+ columnNumber
62+ )
63+ ) ;
6264 return newLink ;
6365 } ) ;
6466
65- _changes . Add ( ( sourceMarkdownFile . FilePath , soureContent , change ) ) ;
67+ _changes . Add ( ( sourceMarkdownFile . FilePath , sourceContent , change ) ) ;
6668
6769 foreach ( var ( _, markdownFile ) in documentationSet . MarkdownFiles )
6870 {
6971 await ProcessMarkdownFile (
7072 sourcePath ,
7173 targetPath ,
72- isDryRun ,
7374 markdownFile ,
7475 ctx
7576 ) ;
@@ -79,19 +80,20 @@ await ProcessMarkdownFile(
7980 return 0 ;
8081
8182 var targetDirectory = Path . GetDirectoryName ( targetPath ) ;
82- fileSystem . Directory . CreateDirectory ( targetDirectory ! ) ; // CreateDirectory automatically creates all necessary parent directories
83+ fileSystem . Directory . CreateDirectory ( targetDirectory ! ) ;
8384 fileSystem . File . Move ( sourcePath , targetPath ) ;
84- // Write changes to disk
85- try {
86- foreach ( var ( filePath , _, newContent ) in _changes )
87- await fileSystem . File . WriteAllTextAsync ( filePath , newContent , ctx ) ;
88- } catch ( Exception ) {
89- foreach ( var ( filePath , originalContent , _) in _changes )
90- await fileSystem . File . WriteAllTextAsync ( filePath , originalContent , ctx ) ;
91- fileSystem . File . Move ( targetPath , sourcePath ) ;
92- throw ;
93- }
94-
85+ try
86+ {
87+ foreach ( var ( filePath , _, newContent ) in _changes )
88+ await fileSystem . File . WriteAllTextAsync ( filePath , newContent , ctx ) ;
89+ }
90+ catch ( Exception )
91+ {
92+ foreach ( var ( filePath , originalContent , _) in _changes )
93+ await fileSystem . File . WriteAllTextAsync ( filePath , originalContent , ctx ) ;
94+ fileSystem . File . Move ( targetPath , sourcePath ) ;
95+ throw ;
96+ }
9597 return 0 ;
9698 }
9799
@@ -140,7 +142,6 @@ private bool ValidateInputs(string? source, string? target)
140142 private async Task ProcessMarkdownFile (
141143 string source ,
142144 string target ,
143- bool isDryRun ,
144145 MarkdownFile value ,
145146 Cancel ctx )
146147 {
@@ -149,9 +150,9 @@ private async Task ProcessMarkdownFile(
149150 var pathInfo = GetPathInfo ( currentDir , source , target ) ;
150151 var linkPattern = BuildLinkPattern ( pathInfo ) ;
151152
152- if ( System . Text . RegularExpressions . Regex . IsMatch ( content , linkPattern ) )
153+ if ( Regex . IsMatch ( content , linkPattern ) )
153154 {
154- var newContent = ReplaceLinks ( content , linkPattern , pathInfo . absoluteStyleTarget , target , value ) ;
155+ var newContent = ReplaceLinks ( content , linkPattern , pathInfo . absoluteStyleTarget , target , value ) ;
155156 _changes . Add ( ( value . FilePath , content , newContent ) ) ;
156157 }
157158 }
@@ -162,12 +163,12 @@ private async Task ProcessMarkdownFile(
162163 string targetPath
163164 )
164165 {
165- var relativeSource = Path . GetRelativePath ( currentDir , sourcePath ) . Replace ( ' \\ ' , '/' ) ;
166- var relativeSourceWithDotSlash = Path . Combine ( "." , relativeSource ) . Replace ( ' \\ ' , '/' ) ;
167- var relativeToDocsFolder = Path . GetRelativePath ( documentationSet . SourcePath . FullName , sourcePath ) . Replace ( ' \\ ' , '/' ) ;
168- var absolutStyleSource = $ "/{ relativeToDocsFolder } ". Replace ( ' \\ ' , '/' ) ;
166+ var relativeSource = Path . GetRelativePath ( currentDir , sourcePath ) ;
167+ var relativeSourceWithDotSlash = Path . Combine ( "." , relativeSource ) ;
168+ var relativeToDocsFolder = Path . GetRelativePath ( documentationSet . SourcePath . FullName , sourcePath ) ;
169+ var absolutStyleSource = $ "/{ relativeToDocsFolder } ";
169170 var relativeToDocsFolderTarget = Path . GetRelativePath ( documentationSet . SourcePath . FullName , targetPath ) ;
170- var absoluteStyleTarget = $ "/{ relativeToDocsFolderTarget } ". Replace ( ' \\ ' , '/' ) ;
171+ var absoluteStyleTarget = $ "/{ relativeToDocsFolderTarget } ";
171172 return (
172173 relativeSource ,
173174 relativeSourceWithDotSlash ,
@@ -186,9 +187,8 @@ private string ReplaceLinks(
186187 string absoluteStyleTarget ,
187188 string target ,
188189 MarkdownFile value
189- )
190- {
191- return System . Text . RegularExpressions . Regex . Replace (
190+ ) =>
191+ Regex . Replace (
192192 content ,
193193 linkPattern ,
194194 match =>
@@ -207,15 +207,22 @@ MarkdownFile value
207207 else
208208 {
209209 // Relative link
210- var relativeTarget = Path . GetRelativePath ( Path . GetDirectoryName ( value . FilePath ) ! , target ) . Replace ( ' \\ ' , '/' ) ;
210+ var relativeTarget = Path . GetRelativePath ( Path . GetDirectoryName ( value . FilePath ) ! , target ) ;
211211 newLink = $ "[{ match . Groups [ 1 ] . Value } ]({ relativeTarget } { anchor } )";
212212 }
213213
214214 var lineNumber = content . Substring ( 0 , match . Index ) . Count ( c => c == '\n ' ) + 1 ;
215215 var columnNumber = match . Index - content . LastIndexOf ( '\n ' , match . Index ) ;
216- _logger . LogInformation ( string . Format ( ChangeFormatString , match . Value , newLink , value . SourceFile . FullName ,
217- lineNumber , columnNumber ) ) ;
216+ _logger . LogInformation (
217+ string . Format (
218+ ChangeFormatString ,
219+ match . Value ,
220+ newLink ,
221+ value . SourceFile . FullName ,
222+ lineNumber ,
223+ columnNumber
224+ )
225+ ) ;
218226 return newLink ;
219227 } ) ;
220- }
221228}
0 commit comments