Skip to content

Commit

Permalink
fixed crash on yaml-deserializion error / initial support for multi-y…
Browse files Browse the repository at this point in the history
…aml files
  • Loading branch information
donid committed Apr 12, 2021
1 parent afcbc83 commit a4fbb45
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 53 deletions.
150 changes: 113 additions & 37 deletions src/WingetRepoBrowser/MainForm.Designer.cs

Large diffs are not rendered by default.

44 changes: 41 additions & 3 deletions src/WingetRepoBrowser/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using WingetRepoBrowserCore;



//todo:
// support multiple repo paths (; separated)
// implement/find custom sort algorithm for version-column
Expand Down Expand Up @@ -154,7 +155,18 @@ private void simpleButtonSearch_Click(object sender, EventArgs e)

foreach (string yamlFile in yamlFiles)
{
ManifestPackage_1_0_0 package = Helpers.ReadYamlFile(yamlFile);
ManifestPackage_1_0_0 package;
try
{
package = Helpers.ReadYamlFile(yamlFile);
}
catch (YamlDotNet.Core.YamlException ex)
{
layoutControlItemMessages.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
memoEditMessages.Text += yamlFile + ": " + GetMessage(ex) + Environment.NewLine;
continue;
}

if (package.ManifestType == "singleton")
{
_manifestVMs.Add(new ManifestPackageVM(package, yamlFile));
Expand Down Expand Up @@ -189,6 +201,15 @@ private void simpleButtonSearch_Click(object sender, EventArgs e)
simpleButtonCreateSubFoldersForSelected.Enabled = true;
}

private static string GetMessage(YamlDotNet.Core.YamlException ex)
{
string inner = null;
if (ex.InnerException != null)
{
inner = " (" + ex.InnerException.Message + ")";
}
return ex.Message + inner;
}

private void repositoryItemButtonEditUrl_ButtonClick(object sender, ButtonPressedEventArgs e)
{
Expand Down Expand Up @@ -276,18 +297,33 @@ private static List<NewDownload> FindNewDownloads(Dictionary<string, string> pac
exists = Directory.Exists(versionFolder);
}
}
if (manifestPackage.ManifestType == "singleton" && manifestPackage.Version != "latest")//TODO!!!!
if (manifestPackage.Version != "latest")//TODO!!!!
{
if (!exists)
{
NewDownload dl = new NewDownload() { ManifestPackage = manifestPackage.Package, VersionFolder = versionFolder, FilePath = manifestPackage.FilePath };
NewDownload dl = new NewDownload()
{
ManifestPackage = manifestPackage.Package,
VersionFolder = versionFolder,
FilePath = manifestPackage.FilePath,
InstallerPackageFilePath = GetInstallerPackageFilePath(manifestPackage)
};
result.Add(dl);
}
}
}
}
return result;
}
static string GetInstallerPackageFilePath(ManifestPackageVM manifestPackage)
{
if (manifestPackage.InstallerPackage == null)
{
return null;
}
return Helpers.GetInstallerPackageFilePath(manifestPackage.FilePath);
}


static string ReplaceInvalidChars(string filename)
{
Expand Down Expand Up @@ -342,6 +378,8 @@ class NewDownload
public ManifestPackage_1_0_0 ManifestPackage { get; set; }
public string VersionFolder { get; set; }
public string FilePath { get; set; }
//public ManifestPackage_1_0_0 InstallerPackage { get; set; }
public string InstallerPackageFilePath { get; set; }

}

Expand Down
3 changes: 2 additions & 1 deletion src/WingetRepoBrowser/ManifestInstallerVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public ManifestInstallerVM(ManifestInstaller_1_0_0 installers)
public string Scope { get { return _installers.Scope; } }


public string SystemAppId { get { return _installers.ProductCode; } }
public string ProductCode { get { return _installers.ProductCode; } }
public string UpgradeBehavior { get { return _installers.UpgradeBehavior; } }

//public ManifestSwitches Switches { get { return _installers.Switches; } }

Expand Down
15 changes: 10 additions & 5 deletions src/WingetRepoBrowser/ManifestPackageVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public class ManifestPackageVM

MultiFileYaml _multiFileYaml;
ManifestPackage_1_0_0 _defaultLocalePackage;

ManifestPackage_1_0_0 _installerPackage;
internal ManifestPackage_1_0_0 InstallerPackage { get { return _installerPackage; } }


public ManifestPackageVM(ManifestPackage_1_0_0 package, string filePath, MultiFileYaml multiFileYaml)
{
Expand All @@ -26,7 +29,7 @@ public ManifestPackageVM(ManifestPackage_1_0_0 package, string filePath, MultiFi
_multiFileYaml = multiFileYaml;
_defaultLocalePackage = _multiFileYaml.Packages.FirstOrDefault(p => p.PackageLocale == package.DefaultLocale);
_installerPackage = _multiFileYaml.Packages.FirstOrDefault(p => p.ManifestType == "installer");
_installerVMs = _installerPackage.Installers.Select(item => new ManifestInstallerVM(item)).ToArray();
_installerVMs = _installerPackage?.Installers.Select(item => new ManifestInstallerVM(item)).ToArray();
}

public ManifestPackageVM(ManifestPackage_1_0_0 package, string filePath)
Expand Down Expand Up @@ -63,6 +66,8 @@ ManifestPackage_1_0_0 GetInstallerPackage()
public string License { get { return GetDefaultPackage().License; } }

public string LicenseUrl { get { return GetDefaultPackage().LicenseUrl; } }
public string PrivacyUrl { get { return GetDefaultPackage().PrivacyUrl; } }
public string PublisherUrl { get { return GetDefaultPackage().PublisherUrl; } }

public string MinOSVersion { get { return GetInstallerPackage().MinimumOSVersion; } }

Expand All @@ -85,7 +90,7 @@ static string SafeJoin(string separator, IEnumerable<string> arr)
public string InstallerType { get { return GetInstallerPackage().InstallerType; } }

public ManifestInstallerVM[] Installers { get { return _installerVMs; } }
public int InstallersCount { get { return _installerVMs.Length; } }
public int InstallersCount { get { return _installerVMs == null ? 0 : _installerVMs.Length; } }


public string FileExtensions { get { return SafeJoin("|", _package.FileExtensions); } }
Expand All @@ -94,9 +99,9 @@ static string SafeJoin(string separator, IEnumerable<string> arr)

public string Commands { get { return SafeJoin("|", _package.Commands); } }

public string InstallersArch { get { return SafeJoin("|", GetInstallerPackage().Installers.Select(item => item.Architecture)); } }
public string InstallersLanguage { get { return SafeJoin("|", GetInstallerPackage().Installers.Select(item => item.InstallerLocale)); } }
public string InstallersInstallerType { get { return SafeJoin("|", GetInstallerPackage().Installers.Select(item => item.InstallerType)); } }
public string InstallersArch { get { return SafeJoin("|", GetInstallerPackage().Installers?.Select(item => item.Architecture)); } }
public string InstallersLanguage { get { return SafeJoin("|", GetInstallerPackage().Installers?.Select(item => item.InstallerLocale)); } }
public string InstallersInstallerType { get { return SafeJoin("|", GetInstallerPackage().Installers?.Select(item => item.InstallerType)); } }

public string ManifestSwitchInteractive { get { return GetInstallerPackage().InstallerSwitches?.Interactive; } }
public string ManifestSwitchSilent { get { return GetInstallerPackage().InstallerSwitches?.Silent; } }
Expand Down
50 changes: 43 additions & 7 deletions src/WingetRepoBrowser/NewDownloadsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,16 @@ private void ProcessOneNewDownload(NewDownload newDownload)
string versionFolder = newDownload.VersionFolder;
// create a fresh instance, otherwise the changes we will make would be visible in the GUI-grid
ManifestPackage_1_0_0 targetManifestPackage = Helpers.ReadYamlFile(newDownload.FilePath);
ManifestInstaller_1_0_0[] installers = targetManifestPackage.Installers;

//TODO select specific installer when winget supports multiple installers
foreach (ManifestInstaller_1_0_0 manifestInstaller in targetManifestPackage.Installers)
ManifestPackage_1_0_0 installerPackage = null;
if (newDownload.InstallerPackageFilePath != null)
{
installerPackage = Helpers.ReadYamlFile(newDownload.InstallerPackageFilePath);
installers = installerPackage.Installers;
}

foreach (ManifestInstaller_1_0_0 manifestInstaller in installers)
{
string downloadUrl = manifestInstaller.InstallerUrl;
string downloadFileName = _installerDownloader.GetFileNameFromUrl(downloadUrl, out Uri responseUri);
Expand All @@ -89,7 +96,15 @@ private void ProcessOneNewDownload(NewDownload newDownload)
{
AddLogLineBackground("Creating directory: " + versionFolder);
Directory.CreateDirectory(versionFolder);
string downloadFilePath = Path.Combine(versionFolder, downloadFileName);
string downloadFolder = versionFolder;
if (newDownload.InstallerPackageFilePath != null)
{
string archAndLocale = $"{manifestInstaller.Architecture ?? "null"}_{manifestInstaller.InstallerLocale ?? "null"}";
downloadFolder = Path.Combine(versionFolder, archAndLocale);
Directory.CreateDirectory(downloadFolder);
}

string downloadFilePath = Path.Combine(downloadFolder, downloadFileName);
AddLogLineBackground("Downloading: " + downloadUrl + " -> " + downloadFilePath);
_installerDownloader.DownloadFile(downloadUrl, downloadFilePath);
if (backgroundWorker1.CancellationPending)
Expand Down Expand Up @@ -123,16 +138,37 @@ private void ProcessOneNewDownload(NewDownload newDownload)
}
}
}
//save modified manifest to download-folder
string yamlTargetFilename = Path.GetFileName(newDownload.FilePath);
string yamlTargetFilePath = Path.Combine(versionFolder, yamlTargetFilename);

if (Directory.Exists(versionFolder))
{
//save modified manifest to download-folder
string yamlTargetFilename = Path.GetFileName(newDownload.FilePath);
string yamlTargetFilePath = Path.Combine(versionFolder, yamlTargetFilename);

Helpers.WriteYamlFile(yamlTargetFilePath, targetManifestPackage);
Trace.WriteLine("modified: " + newDownload.FilePath + " " + yamlTargetFilePath);
if (newDownload.InstallerPackageFilePath == null)
{
Trace.WriteLine("modified: " + newDownload.FilePath + " " + yamlTargetFilePath);
}
else
{
string yamlInstallerTargetFilePath = Helpers.GetInstallerPackageFilePath(yamlTargetFilePath);
Helpers.WriteYamlFile(yamlInstallerTargetFilePath, installerPackage);
Trace.WriteLine("modified: " + newDownload.InstallerPackageFilePath + " " + yamlInstallerTargetFilePath);

string defaultLocale = targetManifestPackage.DefaultLocale;
string yamlLocaleTargetFilePath = GetYamlLocaleFilePath(yamlTargetFilePath, defaultLocale);
string yamlLocaleSourceFilePath = GetYamlLocaleFilePath(newDownload.FilePath, defaultLocale);
File.Copy(yamlLocaleSourceFilePath, yamlLocaleTargetFilePath);
}
}
}

private static string GetYamlLocaleFilePath(string yamlFilePath, string locale)
{
return Path.ChangeExtension(yamlFilePath, ".locale." + locale + ".yaml");
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
AddLogLine((string)e.UserState);
Expand Down
4 changes: 4 additions & 0 deletions src/WingetRepoBrowserCore/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,9 @@ public static string ToHex(Byte[] bytes)
return stringBuilder.ToString();
}

public static string GetInstallerPackageFilePath(string filePath)
{
return Path.ChangeExtension(filePath, ".installer.yaml"); ;
}
}
}
3 changes: 3 additions & 0 deletions src/WingetRepoBrowserCore/ManifestInstaller_1_0_0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,8 @@ public class ManifestInstaller_1_0_0
/// "description": "PackageFamilyName for appx or msix installer. Could be used for correlation of packages across sources"
/// </summary>
public string PackageFamilyName { get; set; }


public string UpgradeBehavior { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/WingetRepoBrowserCore/ManifestPackage_1_0_0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public class ManifestPackage_1_0_0
/// </summary>
public string License { get; set; }


/// <summary>
/// valid secure URL to license
/// example: https://docs.microsoft.com/en-us/MicrosoftTeams/assign-teams-licenses
Expand All @@ -106,6 +107,10 @@ public class ManifestPackage_1_0_0
/// </summary>
public string LicenseUrl { get; set; }

public string PrivacyUrl { get; set; }
public string PublisherUrl { get; set; }


/// <summary>
/// version numbering format for minimum version of Windows supported
/// example: 10.0.0.0
Expand Down

0 comments on commit a4fbb45

Please sign in to comment.