Skip to content

Commit

Permalink
Display number of files loaded.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Ivon committed Mar 9, 2021
1 parent a672685 commit f48fa8f
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Deploy/buildlpx.cmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@set version=6.5.0
@set version=6.5.1
@set zip="%ProgramFiles%\7-Zip\7z.exe"
@set output="CsvLINQPadDriver.%version%.lpx6"

Expand Down
3 changes: 2 additions & 1 deletion Src/CsvLINQPadDriver/CodeGen/CsvCSharpCodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Security;
Expand Down Expand Up @@ -41,7 +42,7 @@ public static (string Code, IReadOnlyCollection<IGrouping<string, (string Type,
var groups = csvTables
.Select(table => GenerateTableRowDataTypeClass(table, _properties.HideRelationsFromDump))
.GroupBy(typeCode => typeCode.Type)
.ToList();
.ToImmutableList();

return ($@"
using System;
Expand Down
17 changes: 12 additions & 5 deletions Src/CsvLINQPadDriver/CsvDataContextDriver.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

using CsvLINQPadDriver.DataDisplay;
using CsvLINQPadDriver.Helpers;
using Humanizer;

using LINQPad;
using LINQPad.Extensibility.DataContext;

using CsvLINQPadDriver.DataDisplay;
using CsvLINQPadDriver.Helpers;

namespace CsvLINQPadDriver
{
// ReSharper disable once ClassNeverInstantiated.Global
Expand All @@ -24,8 +27,12 @@ public class CsvDataContextDriver : DynamicDataContextDriver
public override string Author =>
"Martin Dobroucký (dobrou@gmail.com), Ivan Ivon (ivan.ivon@gmail.com)";

public override string GetConnectionDescription(IConnectionInfo cxInfo) =>
FileUtils.GetLongestCommonPrefixPath(new CsvDataContextDriverProperties(cxInfo).ParsedFiles);
public override string GetConnectionDescription(IConnectionInfo cxInfo)
{
var parsedFiles = new CsvDataContextDriverProperties(cxInfo).ParsedFiles.ToImmutableList();
var parsedFilesCount = parsedFiles.Count;
return $"{FileUtils.GetLongestCommonPrefixPath(parsedFiles)}{(parsedFilesCount > 1 ? $" ({"file".ToQuantity(parsedFilesCount)})" : string.Empty)}";
}

public override bool ShowConnectionDialog(IConnectionInfo cxInfo, ConnectionDialogOptions connectionDialogOptions)
{
Expand Down Expand Up @@ -56,7 +63,7 @@ public override ICustomMemberProvider GetCustomDisplayMemberProvider(object obje

public override IEnumerable<string> GetNamespacesToAdd(IConnectionInfo cxInfo)
{
yield return typeof(StringExtensions).Namespace!;
yield return typeof(Helpers.StringExtensions).Namespace!;
}

public override List<ExplorerItem> GetSchemaAndBuildAssembly(IConnectionInfo cxInfo, AssemblyName assemblyToBuild, ref string nameSpace, ref string typeName) =>
Expand Down
2 changes: 1 addition & 1 deletion Src/CsvLINQPadDriver/CsvDataContextDriverProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public string Files
public IEnumerable<string> ParsedFiles =>
Regex.Split(Files, @"[\r\n]+")
.Select(fileName => fileName.Trim())
.Where(fileName => !string.IsNullOrEmpty(fileName))
.GetFilesOnly()
.Distinct(StringComparer.InvariantCultureIgnoreCase);

public string CsvSeparator
Expand Down
8 changes: 4 additions & 4 deletions Src/CsvLINQPadDriver/CsvLINQPadDriver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/i2van/CsvLINQPadDriver</RepositoryUrl>
<PackageIcon>NuGetIcon.png</PackageIcon>
<PackageReleaseNotes>Added single class generation for the similar CSV files.</PackageReleaseNotes>
<PackageReleaseNotes>Display number of files loaded.</PackageReleaseNotes>
<Copyright>Copyright © Martin Dobroucký 2013-2014, Ivan Ivon 2021</Copyright>
<AssemblyVersion>6.5.0.0</AssemblyVersion>
<FileVersion>6.5.0.0</FileVersion>
<Version>6.5.0</Version>
<AssemblyVersion>6.5.1.0</AssemblyVersion>
<FileVersion>6.5.1.0</FileVersion>
<Version>6.5.1</Version>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<ApplicationIcon>Connection.ico</ApplicationIcon>
</PropertyGroup>
Expand Down
7 changes: 4 additions & 3 deletions Src/CsvLINQPadDriver/DataDisplay/CsvRowMemberProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -42,8 +43,8 @@ protected record ProviderData(IList<PropertyInfo> Properties, IList<FieldInfo> F
protected static ProviderData GetProviderData(Type objectType)
{
var param = Parameter(typeof(object));
var properties = objectType.GetProperties().Where(IsMemberVisible).ToList();
var fields = objectType.GetFields().Where(IsMemberVisible).ToList();
var properties = objectType.GetProperties().Where(IsMemberVisible).ToImmutableList();
var fields = objectType.GetFields().Where(IsMemberVisible).ToImmutableList();

return new ProviderData(
properties,
Expand All @@ -61,7 +62,7 @@ static bool IsMemberVisible(MemberInfo member) =>
(member.MemberType & (MemberTypes.Field | MemberTypes.Property)) != 0 &&
member.GetCustomAttributes(typeof(HideFromDumpAttribute), true).Length == 0;
}

public static ICustomMemberProvider? GetCsvRowMemberProvider(object objectToDisplay)
{
var objectType = objectToDisplay.GetType();
Expand Down
8 changes: 4 additions & 4 deletions Src/CsvLINQPadDriver/DataModel/CsvDataModelGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -27,12 +28,11 @@ private CsvDatabase CreateModel()
{
var files = FileUtils
.EnumFiles(_csvDataContextDriverProperties.ParsedFiles)
.Select(f => f.Trim())
.ToArray();
.ToImmutableList();

var baseDir = FileUtils.GetLongestCommonPrefixPath(files);

var csvDatabase = new CsvDatabase(baseDir, CreateTables().ToList());
var csvDatabase = new CsvDatabase(baseDir, CreateTables().ToImmutableList());

MakeCodeNamesUnique(csvDatabase.Tables);

Expand Down Expand Up @@ -81,7 +81,7 @@ IEnumerable<CsvTable> CreateTables()
CodeName = CodeGenHelper.GetSafeCodeName(col.value),
DisplayName = string.Empty
})
.ToList();
.ToImmutableList();

yield return new CsvTable(file, csvSeparator, columns, new List<CsvRelation>())
{
Expand Down
18 changes: 10 additions & 8 deletions Src/CsvLINQPadDriver/Helpers/FileUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -179,18 +180,18 @@ public static char CsvDetectSeparator(string fileName, string[]? csvData = null)

public static string GetLongestCommonPrefixPath(IEnumerable<string> paths)
{
var pathsValid = GetFiles(paths).ToArray();
var pathsValid = paths.GetFilesOnly().ToImmutableList();

// Get longest common path prefix.
var filePaths = pathsValid.FirstOrDefault()?.Split(Path.DirectorySeparatorChar) ?? new string[0];

return Enumerable.Range(1, filePaths.Length)
.Select(i => string.Join(Path.DirectorySeparatorChar, filePaths.Take(i).ToArray()))
.Select(i => string.Join(Path.DirectorySeparatorChar, filePaths.Take(i).ToImmutableList()))
.LastOrDefault(prefix => pathsValid.All(path => path.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))) ?? string.Empty;
}

public static IEnumerable<string> EnumFiles(IEnumerable<string> paths) =>
GetFiles(paths)
GetFilesOnly(paths)
.SelectMany(EnumFiles)
.Distinct(StringComparer.Ordinal);

Expand Down Expand Up @@ -221,12 +222,13 @@ private static CsvParser CreateCsvParser(string fileName, char csvSeparator)
return new CsvParser(new StreamReader(fileName, Encoding.Default, true, csvConfiguration.BufferSize / sizeof(char)), csvConfiguration);
}

private static IEnumerable<string> GetFiles(IEnumerable<string> paths) =>
public static IEnumerable<string> GetFilesOnly(this IEnumerable<string> paths) =>
paths
.Select(p => p.Trim())
.Where(p => !p.StartsWith("#"))
.Where(p => !string.IsNullOrWhiteSpace(p));

private static string[] EnumFiles(string path)
private static IEnumerable<string> EnumFiles(string path)
{
try
{
Expand All @@ -253,18 +255,18 @@ private static string[] EnumFiles(string path)

if (!Directory.Exists(baseDir))
{
return new string[0];
return Enumerable.Empty<string>();
}

return Directory
.EnumerateFiles(baseDir, file, file.Contains("**") ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.ToArray();
.ToImmutableList();
}
catch(Exception exception)
{
CsvDataContextDriver.WriteToLog($"File enumeration failed for {path}", exception);

return new string[0];
return Enumerable.Empty<string>();
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions Src/CsvLINQPadDriver/SchemaBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -49,9 +50,9 @@ internal static List<ExplorerItem> GetSchemaAndBuildAssembly(ICsvDataContextDriv
{
foreach (var tableCodeGroup in tableCodeGroups.Where(codeGroup => codeGroup.Count() > 1))
{
var codeNames = tableCodeGroup.Select(g => g.CodeName).ToList();
var codeNames = tableCodeGroup.Select(g => g.CodeName).ToImmutableList();

var total = $"({codeNames.Count} total)";
var total = $"({codeNames.Count} of {csvDataContextDriverProperties.ParsedFiles.Count()})";

schema.Insert(index++, new ExplorerItem($"{codeNames.First()} similar files {total} joined data", ExplorerItemKind.Schema, ExplorerIcon.View)
{
Expand Down
5 changes: 3 additions & 2 deletions Tests/CsvLINQPadDriverTest/SchemaBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -52,15 +53,15 @@ public void GetSchemaAndBuildAssembly_CreatedAssembly_AsExpected((ICsvDataContex
contextAssemblyName,
ref nameSpace,
ref contextTypeName
).ToList();
).ToImmutableList();

// Debug info to console.
explorerItems.ForEach(item => Console.WriteLine(item.DragText));

// Check returned explorer tree.
explorerItems.Should().HaveCount(3);

explorerItems = explorerItems.Where(i => i.Kind == ExplorerItemKind.QueryableObject).ToList();
explorerItems = explorerItems.Where(i => i.Kind == ExplorerItemKind.QueryableObject).ToImmutableList();

explorerItems.Select(i => i.DragText)
.Should()
Expand Down

0 comments on commit f48fa8f

Please sign in to comment.