Skip to content

Commit a905ec1

Browse files
committed
Use URL representation to store template repositories
Template repositories were stored in / cloned into `~/<localAppData>/<profile-name>/<branch>` This could lead to problems as profiles could share the same name across projects while pointing to different repositories. This is solved now by making the repository URL, i.e. a file system friendly representation of it, the path identifier to clone templates: `~/<localAppData>/<url>/<branch>`
1 parent 0f33728 commit a905ec1

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed
Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
namespace Moryx.Cli.Templates.Extensions
1+
using System.Text;
2+
using System.Text.RegularExpressions;
3+
4+
namespace Moryx.Cli.Templates.Extensions
25
{
36
public static class StringExtensions
47
{
5-
public static List<string> ToCleanList(this string str) {
8+
public static List<string> ToCleanList(this string str)
9+
{
610
return str
711
.Split(',')
812
.Select(s => s.Trim())
913
.Where(s => s.Length > 0)
10-
.ToList(); }
14+
.ToList();
15+
}
1116

12-
public static string ReplaceFileExtension(this string str, string oldStr, string newStr) {
17+
public static string ReplaceFileExtension(this string str, string oldStr, string newStr)
18+
{
1319
var position = str.LastIndexOf(oldStr);
1420
if (position == -1)
1521
{
@@ -19,5 +25,22 @@ public static string ReplaceFileExtension(this string str, string oldStr, string
1925
}
2026

2127
public static string StateBase(this string str) => str + "StateBase";
28+
29+
public static string FileSystemCompatible(this string url)
30+
{
31+
var pattern = @"[\:\/\?\#\[\]\!\$\&\'\(\)\*\+\,\;\=]";
32+
url = Regex.Replace(url, pattern, "_");
33+
var result = new StringBuilder();
34+
var previous = '\0';
35+
foreach (char current in url)
36+
{
37+
if (current != '_' || (current == '_' && current != previous))
38+
{
39+
result.Append(current);
40+
previous = current;
41+
}
42+
}
43+
return result.ToString();
44+
}
2245
}
2346
}

src/Moryx.Cli.Templates/Models/TemplateSettings.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Moryx.Cli.Templates.Models
1+
using Moryx.Cli.Templates.Extensions;
2+
3+
namespace Moryx.Cli.Templates.Models
24
{
35
public class TemplateSettings
46
{
@@ -8,7 +10,7 @@ public class TemplateSettings
810
public required string AppName { get; set; }
911
public string ProfileName { get; set; } = "default";
1012
public required string TargetDirectory { get; set; }
11-
public string SourceDirectory { get => Path.Combine(TemplatesRoot(), ProfileName, Branch); }
13+
public string SourceDirectory { get => Path.Combine(TemplatesRoot(), Repository.FileSystemCompatible(), Branch); }
1214

1315

1416
public string TemplatesRoot()

src/Moryx.Cli.Templates/TemplateRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static int Clone(TemplateSettings settings, Action<string>? onStatus = nu
2222
{
2323
return ExecCommanLine($"git -C {targetDir} pull", _ => onStatus?.Invoke(_));
2424
}
25-
return -1;
25+
return 0;
2626
}
2727

2828
public static int ExecCommanLine(string command, Action<string> onStatus)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Moryx.Cli.Templates.Extensions;
2+
3+
namespace Moryx.Cli.Tests
4+
{
5+
public class FileSystemCompatibleUrlTests
6+
{
7+
8+
[SetUp]
9+
public void Setup()
10+
{
11+
}
12+
13+
[TestCase("https://github.com/microsoft/vscode.git", "https_github.com_microsoft_vscode.git")]
14+
[TestCase("ssh://[email protected]:microsoft/vscode.git", "[email protected]_microsoft_vscode.git")]
15+
public void CheckUrlConversion(string input, string expected)
16+
{
17+
var result = input.FileSystemCompatible();
18+
19+
Assert.That(result, Is.EqualTo(expected));
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)