Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldelaparra committed Nov 13, 2020
1 parent eee3517 commit 3033548
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 144 deletions.
34 changes: 17 additions & 17 deletions LnkSerialization.Console/LnkSerialization.Console.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>LnkSerialization</AssemblyName>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>LnkSerialization</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LnkSerialization.Core\LnkSerialization.Core.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LnkSerialization.Core\LnkSerialization.Core.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="example.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="example.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
</Project>
68 changes: 38 additions & 30 deletions LnkSerialization.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
using System.Collections.Generic;
using System.Linq;

using CommandLine;
using CommandLine.Text;

namespace LnkSerialization.Console
{
public class Program
{
[Verb("serialize", HelpText = "Serializes a folder with *.lnk files to a .json file.\nUses a custom link model for the serialization.")]
private class SerializeOptions
{
[Option('i', "inputPath", Required = true, HelpText = "Input folder with *.lnk files.\nExample: 'c:\\dirSource'")]
public string InputPath { get; set; }
[Option('o', "outputfile", Required = true, HelpText = "Output path to json filename.\nExample: 'c:\\dirBackup\\links.json'")]
public string OutputFilename { get; set; }
}

[Verb("deserialize", HelpText = "Takes a .json file with a collection of link models.\nDeserializes each link model to a *.lnk link.")]
private class DeserializeOptions
private static void DisplayHelp<T>(ParserResult<T> result, IEnumerable<Error> errs)
{
[Option('i', "inputfile", Required = true, HelpText = "Path to json filename that contains links.\nExample: 'c:\\dirBackup\\links.json'")]
public string InputFilename { get; set; }
[Option('o', "outputPath", Required = true, HelpText = "Output folder where the *.lnk files will be created.\nExample: 'c:\\dirRestore'")]
public string OutputPath { get; set; }
var helpText = HelpText.AutoBuild(result, h => {
h.AdditionalNewLineAfterOption = false;
h.Heading = "Links Serialization Util";
h.AddDashesToOption = true;
h.AutoHelp = true;
h.AutoVersion = true;
return HelpText.DefaultParsingErrorsHandler(result, h);
}, e => e);
System.Console.WriteLine(helpText);
}

private static void Main(string[] args)
Expand All @@ -38,28 +32,42 @@ private static void Main(string[] args)
.WithNotParsed(errs => DisplayHelp(parserResult, errs));
}

private static void DisplayHelp<T>(ParserResult<T> result, IEnumerable<Error> errs)
private static void RunDeserialize(DeserializeOptions options)
{
var helpText = HelpText.AutoBuild(result, h =>
{
h.AdditionalNewLineAfterOption = false;
h.Heading = $"Links Serialization Util";
h.AddDashesToOption = true;
h.AutoHelp = true;
h.AutoVersion = true;
return HelpText.DefaultParsingErrorsHandler(result, h);
}, e => e);
System.Console.WriteLine(helpText);
Core.LnkSerialization.DeserializeLinksToFolder(options.InputFilename, options.OutputPath);
}

private static void RunSerialize(SerializeOptions options)
{
Core.LnkSerialization.SerializeLinkFolder(options.InputPath, options.OutputFilename);
}

private static void RunDeserialize(DeserializeOptions options)
[Verb("serialize",
HelpText =
"Serializes a folder with *.lnk files to a .json file.\nUses a custom link model for the serialization.")]
private class SerializeOptions
{
Core.LnkSerialization.DeserializeLinksToFolder(options.InputFilename, options.OutputPath);
[Option('i', "inputPath", Required = true,
HelpText = "Input folder with *.lnk files.\nExample: 'c:\\dirSource'")]
public string InputPath { get; set; }

[Option('o', "outputfile", Required = true,
HelpText = "Output path to json filename.\nExample: 'c:\\dirBackup\\links.json'")]
public string OutputFilename { get; set; }
}

[Verb("deserialize",
HelpText =
"Takes a .json file with a collection of link models.\nDeserializes each link model to a *.lnk link.")]
private class DeserializeOptions
{
[Option('i', "inputfile", Required = true,
HelpText = "Path to json filename that contains links.\nExample: 'c:\\dirBackup\\links.json'")]
public string InputFilename { get; set; }

[Option('o', "outputPath", Required = true,
HelpText = "Output folder where the *.lnk files will be created.\nExample: 'c:\\dirRestore'")]
public string OutputPath { get; set; }
}
}
}
}
49 changes: 26 additions & 23 deletions LnkSerialization.Core/LinkModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,29 @@ public class LinkModel
{
public LinkModel() { }

public LinkModel(string filename) => FromFilename(filename);
public LinkModel(string filename)
{
FromFilename(filename);
}

public string Args { get; set; }
public string Filename { get; set; }
public string Path { get; set; }
public string WorkingDirectory { get; set; }

public void ToLinkFile(string outputPath)
{
Shortcut.CreateShortcut(Path, Args, WorkingDirectory)
.WriteToFile(System.IO.Path.Combine(outputPath, Filename));
}

private void FromArguments(string path, string args, string workingDirectory, string filename)
{
Path = path;
Args = args;
WorkingDirectory = workingDirectory;
Filename = filename;
}

private void FromFilename(string filename)
{
Expand All @@ -27,15 +49,14 @@ private void FromShortcut(Shortcut shortcut, string filename)
?? shortcut.ExtraData?.EnvironmentVariableDataBlock?.TargetUnicode
?? shortcut.ExtraData?.EnvironmentVariableDataBlock?.TargetAnsi
?? string.Empty;
if (path.IsEmpty())
{
if (path.IsEmpty()) {
var propStoreValues = shortcut
.ExtraData?
.PropertyStoreDataBlock?
.PropertyStore?
.SelectMany(x => x.PropertyStorage)
.Select(x => x.TypedPropertyValue)
.Select(x => x.Value)
.Select(x => x.Value)
?? Array.Empty<string>();

var values = propStoreValues
Expand All @@ -48,30 +69,12 @@ private void FromShortcut(Shortcut shortcut, string filename)
if (value.IsNotEmpty())
path = value;
}

var args = shortcut.StringData?.CommandLineArguments ?? string.Empty;
var workingDir = shortcut.StringData?.WorkingDir ?? string.Empty;
var name = System.IO.Path.GetFileName(filename);

FromArguments(path, args, workingDir, name);
}

private void FromArguments(string path, string args, string workingDirectory, string filename)
{
Path = path;
Args = args;
WorkingDirectory = workingDirectory;
Filename = filename;
}

public void ToLinkFile(string outputPath)
{
Shortcut.CreateShortcut(Path, Args, WorkingDirectory)
.WriteToFile(System.IO.Path.Combine(outputPath, Filename));
}

public string Path { get; set; }
public string Args { get; set; }
public string WorkingDirectory { get; set; }
public string Filename { get; set; }
}
}
39 changes: 19 additions & 20 deletions LnkSerialization.Core/LnkSerialization.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;

using ShellLink;

using Wororo.Utilities;

// Some useful links:
Expand All @@ -18,22 +16,6 @@ public static class LnkSerialization
public static string DefaultJsonFilename { get; set; } = "links.json";
public static string DefaultLinksFolder { get; set; } = @"%userProfile%\shortcuts\";

public static void SerializeLinkFolder(string folderWithLinks, string outputJsonFilename = "")
{
if (folderWithLinks.IsEmpty())
folderWithLinks = DefaultLinksFolder;

if (outputJsonFilename.IsEmpty())
outputJsonFilename = Path.Combine(folderWithLinks, DefaultJsonFilename);

if (!Directory.Exists(folderWithLinks))
return;

var lnkFiles = Directory.GetFiles(folderWithLinks, "*.lnk");
var list = lnkFiles.Select(lnkFile => new LinkModel(lnkFile));
list.SerializeJson(outputJsonFilename);
}

public static void DeserializeLinksToFolder(string jsonFilename, string outputFolder)
{
if (jsonFilename.IsEmpty())
Expand All @@ -51,14 +33,31 @@ public static void DeserializeLinksToFolder(string jsonFilename, string outputFo
outputFolder.CreatePathIfNotExists();
var links = JsonSerialization.DeserializeJson<IEnumerable<LinkModel>>(jsonFilename);

foreach (var linkModel in links)
foreach (var linkModel in links) {
linkModel.ToLinkFile(outputFolder);
}
}

public static void SerializeAsShortcutJson(string linkFile)
{
var lnkShortcut = Shortcut.ReadFromFile(linkFile);
lnkShortcut.SerializeJson($"{Path.GetFileName(linkFile)}.json");
}

public static void SerializeLinkFolder(string folderWithLinks, string outputJsonFilename = "")
{
if (folderWithLinks.IsEmpty())
folderWithLinks = DefaultLinksFolder;

if (outputJsonFilename.IsEmpty())
outputJsonFilename = Path.Combine(folderWithLinks, DefaultJsonFilename);

if (!Directory.Exists(folderWithLinks))
return;

var lnkFiles = Directory.GetFiles(folderWithLinks, "*.lnk");
var list = lnkFiles.Select(lnkFile => new LinkModel(lnkFile));
list.SerializeJson(outputJsonFilename);
}
}
}
}
86 changes: 43 additions & 43 deletions LnkSerialization.UnitTests/LnkSerialization.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LnkSerialization.Core\LnkSerialization.Core.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LnkSerialization.Core\LnkSerialization.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Resources\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
</ItemGroup>

<ItemGroup>
<None Update="Resources\d.lnk">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\dl.lnk">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\ie.lnk">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\mypc.lnk">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\s.lnk">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\snip.lnk">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="Resources\d.lnk">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\dl.lnk">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\ie.lnk">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\mypc.lnk">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\s.lnk">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\snip.lnk">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
</Project>
Loading

0 comments on commit 3033548

Please sign in to comment.