Skip to content

Commit

Permalink
feat(LibraryReader): implemented an object that can read project prop…
Browse files Browse the repository at this point in the history
…erties from a .library file

Removed the previous heurics and instead tried to figure out how the format works
  • Loading branch information
iadonkey committed Dec 8, 2023
1 parent 80f7a2b commit c6c6d60
Show file tree
Hide file tree
Showing 33 changed files with 501 additions and 249 deletions.
27 changes: 0 additions & 27 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,3 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



This software contains code of third-parties.

LibraryPropertyReader.cs:
=========================

Copyright (c) 2023 Andrew Burks

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 34 additions & 3 deletions TwinpackRegistry/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using Twinpack.Models;
Expand All @@ -25,6 +26,19 @@ public class UpdateOptions

[Option('r', "name", Required = false, Default = "Twinpack-Registry", HelpText = "")]
public string RegistryName { get; set; }

[Option('d', "dry-run", Required = false, Default = false, HelpText = "")]
public bool DryRun { get; set; }

[Option('D', "dump", Required = false, Default = false, HelpText = "")]
public bool Dump { get; set; }
}

[Verb("dump", HelpText = "")]
public class DumpOptions
{
[Option('p', "path", Required = false, Default = "Twinpack-Registry", HelpText = "")]
public string Path { get; set; }
}

private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
Expand All @@ -49,14 +63,31 @@ static int Main(string[] args)

try
{
return Parser.Default.ParseArguments<UpdateOptions>(args)
return Parser.Default.ParseArguments<UpdateOptions, DumpOptions>(args)
.MapResult(
(UpdateOptions opts) =>
{
Login(opts.Username, opts.Password);
new TwinpackRegistry(_twinpackServer).DownloadAsync(opts.RegistryOwner, opts.RegistryName).GetAwaiter().GetResult();
_twinpackServer.PushAsync(TwinpackUtils.PlcProjectsFromConfig(compiled: false, target: "TC3.1"), "Release", "main", "TC3.1", null, false).GetAwaiter().GetResult();
new TwinpackRegistry(_twinpackServer).DownloadAsync(opts.RegistryOwner, opts.RegistryName, dump: opts.Dump).GetAwaiter().GetResult();

if(!opts.DryRun)
_twinpackServer.PushAsync(TwinpackUtils.PlcProjectsFromConfig(compiled: false, target: "TC3.1"), "Release", "main", "TC3.1", null, false).GetAwaiter().GetResult();

return 0;
},
(DumpOptions opts) =>
{
foreach(var file in Directory.GetFiles(opts.Path))
{
try
{
using (var memoryStream = new MemoryStream(File.ReadAllBytes(file)))
using (var zipArchive = new ZipArchive(memoryStream))
{
var libraryInfo = LibraryReader.Read(File.ReadAllBytes(file), dumpFilenamePrefix: file);
}
} catch(Exception) { }
}
return 0;
},
errs => 1);
Expand Down
4 changes: 2 additions & 2 deletions TwinpackRegistry/TwinpackRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async Task<byte[]> DownloadAsync(string url)
}
}

public async Task DownloadAsync(string repositoryOwner, string repositoryName, string cachePath = null)
public async Task DownloadAsync(string repositoryOwner, string repositoryName, bool dump=false)
{
var config = new Config()
{
Expand All @@ -98,7 +98,7 @@ public async Task DownloadAsync(string repositoryOwner, string repositoryName, s
{
var license = await RetrieveLicenseAsync(client, owner, repo);
var library = await DownloadAsync(asset.BrowserDownloadUrl);
var libraryInfo = LibraryPropertyReader.Read(library);
var libraryInfo = LibraryReader.Read(library, dumpFilenamePrefix: $"{repositoryOwner}_{repositoryName}_{asset.Name}.library");
var filePath = $@"{TwinpackServer.DefaultLibraryCachePath}\{target}";
var iconFileName = $@"{filePath}\{libraryInfo.Title}_{libraryInfo.Version}.png";
var libraryFileName = $@"{filePath}\{libraryInfo.Title}_{libraryInfo.Version}.library";
Expand Down
11 changes: 11 additions & 0 deletions TwinpackShared/Exceptions/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public LibraryNotFoundException(string reference, string version, string message
}
}

public class LibraryInvalid : Exception
{
public LibraryInvalid(string message) : base(message)
{
}

public LibraryInvalid(string fileName, string message) : base(fileName + ", " +message)
{
}
}

public class LicenseFileNotFoundException : Exception
{
public string Reference { get; private set; }
Expand Down
148 changes: 0 additions & 148 deletions TwinpackShared/LibraryPropertyReader.cs

This file was deleted.

Loading

0 comments on commit c6c6d60

Please sign in to comment.