Skip to content

Commit 4db1792

Browse files
authored
Merge pull request #122 from dotnet-campus/t/lindexi/TargetFramework
优化 UOS 的 deb 打包 优化提示和符号链接失败时降级使用文件拷贝
2 parents f10173c + 725fdfd commit 4db1792

File tree

1 file changed

+77
-11
lines changed

1 file changed

+77
-11
lines changed

DebUOS/Packaging.DebUOS/DebUOSPackageFileStructCreator.cs

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
using System.Globalization;
1+
using System;
2+
using System.Globalization;
23
using System.IO;
34
using System.Text;
45
using System.Text.Json;
56
using System.Text.RegularExpressions;
7+
68
using Microsoft.Extensions.Logging;
9+
710
using Packaging.DebUOS.Contexts;
811
using Packaging.DebUOS.Contexts.Configurations;
912
using Packaging.DebUOS.Exceptions;
13+
1014
using Walterlv.IO.PackageManagement;
1115

1216
namespace Packaging.DebUOS;
@@ -52,7 +56,14 @@ public void CreatePackagingFolder(DebUOSConfiguration configuration)
5256
// 删除旧的文件夹,防止打包使用到旧文件
5357
if (Directory.Exists(packingFolder))
5458
{
55-
Directory.Delete(packingFolder, true);
59+
if (OperatingSystem.IsWindows())
60+
{
61+
PackageDirectory.Delete(packingFolder);
62+
}
63+
else
64+
{
65+
Directory.Delete(packingFolder, true);
66+
}
5667
}
5768

5869
Directory.CreateDirectory(packingFolder);
@@ -76,11 +87,9 @@ public void CreatePackagingFolder(DebUOSConfiguration configuration)
7687
var filesFolder = Path.Join(appIdFolder, "files");
7788
Directory.CreateDirectory(filesFolder);
7889
var applicationBin = Path.Join(filesFolder, "bin");
79-
// 符号比拷贝速度快
80-
var symbol = Directory.CreateSymbolicLink(applicationBin, projectPublishFolder);
81-
if (!symbol.Exists)
90+
if (!FolderUtils.CreateSymbolLinkOrCopyFolder(projectPublishFolder, applicationBin))
8291
{
83-
throw new PackagingException($"创建符号链接失败,从 '{projectPublishFolder}' '{applicationBin}'");
92+
throw new PackagingException($"将发布输出文件拷贝到安装包打包文件夹失败,从 '{projectPublishFolder}' 复制到 '{applicationBin}' 失败");
8493
}
8594

8695
// opt\apps\AppId\entries
@@ -194,13 +203,20 @@ public void CreatePackagingFolder(DebUOSConfiguration configuration)
194203
(configuration.Png512x512IconFile, "512x512"),
195204
})
196205
{
197-
if (File.Exists(iconFile))
206+
if (!string.IsNullOrEmpty(iconFile))
198207
{
199-
var pngFile = Path.Join(iconsFolder, "hicolor", resolution, "apps", $"{appId}.png");
200-
Directory.CreateDirectory(Path.GetDirectoryName(pngFile)!);
201-
File.Copy(iconFile, pngFile);
208+
if (File.Exists(iconFile))
209+
{
210+
var pngFile = Path.Join(iconsFolder, "hicolor", resolution, "apps", $"{appId}.png");
211+
Directory.CreateDirectory(Path.GetDirectoryName(pngFile)!);
212+
File.Copy(iconFile, pngFile);
202213

203-
anyIconFileExist = true;
214+
anyIconFileExist = true;
215+
}
216+
else
217+
{
218+
Logger.LogWarning($"配置了 {resolution} 的图标文件路径,但是找不到图标文件 图标文件={iconFile} 图标文件绝对路径={Path.GetFullPath(iconFile)}");
219+
}
204220
}
205221
}
206222

@@ -340,4 +356,54 @@ public void CreatePackagingFolder(DebUOSConfiguration configuration)
340356
File.Copy(configuration.DebPreinstFile, preinstFile);
341357
}
342358
}
359+
360+
static class FolderUtils
361+
{
362+
public static bool CreateSymbolLinkOrCopyFolder(string sourceFolder, string destinationFolder)
363+
{
364+
try
365+
{
366+
try
367+
{
368+
if (OperatingSystem.IsWindows())
369+
{
370+
// 在 Win 下可以创建 JunctionPoint 联接试试
371+
// 这是不需要权限的,比 Directory.CreateSymbolicLink 更好
372+
var ioResult = PackageDirectory.Link(sourceFolder, destinationFolder);
373+
374+
if (ioResult)
375+
{
376+
// 创建成功
377+
return true;
378+
}
379+
}
380+
}
381+
catch (Exception)
382+
{
383+
// 失败了,继续尝试
384+
}
385+
386+
try
387+
{
388+
// 符号比拷贝速度快
389+
var symbol = Directory.CreateSymbolicLink(destinationFolder, sourceFolder);
390+
if (symbol.Exists)
391+
{
392+
return true;
393+
}
394+
}
395+
catch (Exception)
396+
{
397+
// 创建符号失败了,失败了就尝试拷贝一下吧
398+
}
399+
400+
var result = PackageDirectory.Copy(sourceFolder, destinationFolder, true);
401+
return result;
402+
}
403+
catch (Exception e)
404+
{
405+
throw new PackagingException($"从 '{sourceFolder}' 复制到 '{destinationFolder}' 失败", e);
406+
}
407+
}
408+
}
343409
}

0 commit comments

Comments
 (0)