Skip to content

Commit 264c902

Browse files
committed
fix: Load .kicad_sym files of version 9 correctly
1 parent c042b21 commit 264c902

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

release-notes/3.0.2.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Version 3.0.2
2+
3+
## Bug fixes
4+
5+
- Load `.kicad_sym` files of version 9 correctly.

src/KiCadDbLib/Services/KiCad/LibraryReader/KiCad6LibraryReader.cs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,21 @@ public KiCad6LibraryReader(ISettingsProvider settingsProvider)
1919

2020
public async Task<string[]> GetFootprintsAsync()
2121
{
22-
var settings = await _settingsProvider.GetWorkspaceSettings()
23-
.ConfigureAwait(false);
22+
var settings = await _settingsProvider.GetWorkspaceSettings().ConfigureAwait(false);
2423

2524
var kicadFootprints = GetFootprintInfosFromDirectory(settings.FootprintsPath);
2625

27-
return kicadFootprints
28-
.Select(x => x.ToString())
29-
.ToArray();
26+
return kicadFootprints.Select(x => x.ToString()).ToArray();
3027
}
3128

3229
public async Task<string[]> GetSymbolsAsync()
3330
{
34-
var settings = await _settingsProvider.GetWorkspaceSettings()
35-
.ConfigureAwait(false);
31+
var settings = await _settingsProvider.GetWorkspaceSettings().ConfigureAwait(false);
3632

3733
var kicadSymbols = await GetSymbolInfosFromDirectoryAsync(settings.SymbolsPath)
3834
.ConfigureAwait(false);
3935

40-
return kicadSymbols
41-
.ToArray();
36+
return kicadSymbols.ToArray();
4237
}
4338

4439
private static IEnumerable<LibraryItemInfo> GetFootprintInfosFromDirectory(string directory)
@@ -48,45 +43,53 @@ private static IEnumerable<LibraryItemInfo> GetFootprintInfosFromDirectory(strin
4843
throw new DirectoryNotFoundException($"Directory \"{directory}\" not found.");
4944
}
5045

51-
return Directory.EnumerateDirectories(directory, $"*{FileExtensions.Pretty}")
46+
return Directory
47+
.EnumerateDirectories(directory, $"*{FileExtensions.Pretty}")
5248
.SelectMany(GetFootprintInfos);
5349
}
5450

5551
private static LibraryItemInfo[] GetFootprintInfos(string footprintDirectory)
5652
{
57-
var library = new DirectoryInfo(footprintDirectory).Name[..^FileExtensions.Pretty.Length];
58-
59-
return Directory.EnumerateFiles(footprintDirectory, $"*{FileExtensions.KicadMod}")
60-
.Select(Path.GetFileNameWithoutExtension)
61-
.Select(footprint => new LibraryItemInfo(library, footprint!))
62-
.ToArray();
53+
var library = new DirectoryInfo(footprintDirectory).Name[
54+
..^FileExtensions.Pretty.Length
55+
];
56+
57+
return Directory
58+
.EnumerateFiles(footprintDirectory, $"*{FileExtensions.KicadMod}")
59+
.Select(Path.GetFileNameWithoutExtension)
60+
.Select(footprint => new LibraryItemInfo(library, footprint!))
61+
.ToArray();
6362
}
6463

65-
private static async Task<IEnumerable<string>> GetSymbolInfosFromDirectoryAsync(string directory)
64+
private static async Task<IEnumerable<string>> GetSymbolInfosFromDirectoryAsync(
65+
string directory
66+
)
6667
{
6768
if (!Directory.Exists(directory))
6869
{
6970
throw new DirectoryNotFoundException($"Directory \"{directory}\" not found.");
7071
}
7172

72-
return await Directory.EnumerateFiles(directory, $"*{FileExtensions.KicadSym}")
73+
return await Directory
74+
.EnumerateFiles(directory, $"*{FileExtensions.KicadSym}")
7375
.ToAsyncEnumerable()
7476
.SelectMany(GetSymbolInfosAsync)
7577
.Select(item => item.ToString())
7678
.ToArrayAsync()
7779
.ConfigureAwait(false);
7880
}
7981

80-
private static async IAsyncEnumerable<LibraryItemInfo> GetSymbolInfosAsync(string libraryFile)
82+
private static async IAsyncEnumerable<LibraryItemInfo> GetSymbolInfosAsync(
83+
string libraryFile
84+
)
8185
{
8286
var sw = Stopwatch.StartNew();
8387
var libraryName = Path.GetFileNameWithoutExtension(libraryFile);
84-
var regex = new Regex("\\(symbol \"(.+?)\" (?!.*?extends)");
88+
var regex = new Regex("\\(symbol\\s+?\"(.+?)\"\\s+?(?!.*?extends)");
8589
var lines = await File.ReadAllTextAsync(libraryFile, Encoding.UTF8)
8690
.ConfigureAwait(false);
8791

88-
var symbols = regex.Matches(lines)
89-
.Select(match => match.Groups[1].Value);
92+
var symbols = regex.Matches(lines).Select(match => match.Groups[1].Value);
9093

9194
foreach (var symbol in symbols)
9295
{
@@ -97,4 +100,4 @@ private static async IAsyncEnumerable<LibraryItemInfo> GetSymbolInfosAsync(strin
97100
Debug.WriteLine($"Get Symbols from {libraryFile}: {sw.ElapsedMilliseconds}ms");
98101
}
99102
}
100-
}
103+
}

src/test/KiCad.UnitTest/KiCad6LibraryReaderTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public async Task Reader_Should_Read_Symbols()
3838
symbols.Should().Contain("Device:L");
3939

4040
symbols.Should().NotContain("Device:C_0_0");
41-
symbols.Should().NotContain("Device:Filter_EMI_C", because: "it only extends Device:C_Feedthrough");
41+
symbols
42+
.Should()
43+
.NotContain("Device:Filter_EMI_C", because: "it only extends Device:C_Feedthrough");
4244
}
43-
}
45+
}

0 commit comments

Comments
 (0)