Skip to content

Commit e79faf8

Browse files
committed
Open package for editing
1 parent ac81df8 commit e79faf8

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

LiveWriterPluginManager/Services/ZipService.cs

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace LiveWriterPluginManager.Services
1313
public interface IZipService
1414
{
1515
Task<Plugin> UnzipFileAsync(string filePath);
16+
Task<Tuple<Manifest, string[]>> OpenPackageForEditing(string filePath);
1617
Task<bool> ZipFilesAsync(string[] files, string outputFile, Manifest manifest);
1718
}
1819

@@ -38,24 +39,18 @@ public async Task<Plugin> UnzipFileAsync(string filePath)
3839
var tcs = new TaskCompletionSource<bool>();
3940
await Task.Run(async () =>
4041
{
41-
using (var zipFile = ZipFile.Read(filePath))
42-
{
43-
zipFile.FlattenFoldersOnExtract = true;
44-
var fileName = Path.GetFileNameWithoutExtension(filePath);
45-
46-
var extractPath = Path.Combine(AppHelper.PluginsFolder, fileName);
47-
zipFile.ExtractAll(extractPath, ExtractExistingFileAction.OverwriteSilently);
42+
var fileName = Path.GetFileNameWithoutExtension(filePath);
43+
var extractPath = Path.Combine(AppHelper.PluginsFolder, fileName);
4844

49-
var manifest = await ReadManifest(extractPath);
45+
var manifest = await ExtractAndReturnManifest(extractPath, fileName);
5046

51-
result = new Plugin
52-
{
53-
Name = manifest?.Name,
54-
Path = manifest?.PluginPath
55-
};
47+
result = new Plugin
48+
{
49+
Name = manifest?.Name,
50+
Path = manifest?.PluginPath
51+
};
5652

57-
tcs.SetResult(true);
58-
}
53+
tcs.SetResult(true);
5954
});
6055

6156
await tcs.Task;
@@ -68,6 +63,43 @@ await Task.Run(async () =>
6863
return result;
6964
}
7065

66+
public async Task<Tuple<Manifest, string[]>> OpenPackageForEditing(string filePath)
67+
{
68+
if (string.IsNullOrEmpty(filePath))
69+
{
70+
return new Tuple<Manifest, string[]>(new Manifest(), new string[0]);
71+
}
72+
73+
var tcs = new TaskCompletionSource<Tuple<Manifest, string[]>>();
74+
await Task.Run(async () =>
75+
{
76+
var filename = Path.GetFileNameWithoutExtension(filePath);
77+
var extractPath = Path.Combine(Path.GetTempPath(), filename);
78+
79+
var manifest = await ExtractAndReturnManifest(filePath, extractPath);
80+
81+
var directory = new DirectoryInfo(extractPath);
82+
var files = directory.EnumerateFiles().Select(x => x.FullName).ToArray();
83+
84+
tcs.SetResult(new Tuple<Manifest, string[]>(manifest, files));
85+
});
86+
87+
var result = await tcs.Task;
88+
return result;
89+
}
90+
91+
private static async Task<Manifest> ExtractAndReturnManifest(string filePath, string extractPath)
92+
{
93+
using (var zipFile = ZipFile.Read(filePath))
94+
{
95+
zipFile.FlattenFoldersOnExtract = true;
96+
zipFile.ExtractAll(extractPath, ExtractExistingFileAction.OverwriteSilently);
97+
}
98+
99+
var manifest = await ReadManifest(extractPath);
100+
return manifest;
101+
}
102+
71103
public async Task<bool> ZipFilesAsync(string[] files, string outputFile, Manifest manifest)
72104
{
73105
var manifestJson = JsonConvert.SerializeObject(manifest);

LiveWriterPluginManager/ViewModel/CreatePackageViewModel.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,36 @@ public RelayCommand CreatePackageCommand
9797
});
9898
}
9999
}
100+
101+
public RelayCommand OpenPackageCommand
102+
{
103+
get
104+
{
105+
return new RelayCommand(async () =>
106+
{
107+
var file = _fileService.GetZipFile();
108+
var packageDetails = await _zipService.OpenPackageForEditing(file);
109+
if (packageDetails.Item1 == null || !packageDetails.Item2.Any())
110+
{
111+
return;
112+
}
113+
ManifestViewModel = new ManifestViewModel(packageDetails.Item1);
114+
115+
var files = packageDetails.Item2.Where(x => !x.Contains(Manifest.ManifestFileName)).Select(x => new FileViewModel(x));
116+
Files.Clear();
117+
foreach (var f in files)
118+
{
119+
Files.Add(f);
120+
}
121+
122+
var pluginFile = Files.FirstOrDefault(x => x.Name == ManifestViewModel.PluginFileName);
123+
if (pluginFile != null)
124+
{
125+
pluginFile.IsPluginFile = true;
126+
}
127+
});
128+
}
129+
}
100130
}
101131

102132
public class FileViewModel : ViewModelBase
@@ -157,6 +187,7 @@ public ManifestViewModel(Manifest manifest)
157187
TermsUrl = _manifest.TermsUrl;
158188
Version = _manifest.Version;
159189
TargetWriterVersion = _manifest.TargetWriterVersion;
190+
PluginFileName = _manifest.PluginFileName;
160191

161192
Messenger.Default.Register<NotificationMessage>(this, m =>
162193
{
@@ -175,6 +206,7 @@ public ManifestViewModel(Manifest manifest)
175206
public string TermsUrl { get; set; }
176207
public string Version { get; set; }
177208
public string TargetWriterVersion { get; set; }
209+
public string PluginFileName { get; set; }
178210

179211
public bool PluginFileSet => !string.IsNullOrEmpty(_manifest?.PluginFileName);
180212

0 commit comments

Comments
 (0)