131 lines
4.2 KiB
C#
131 lines
4.2 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace YLPublishTool.Actions
|
|
{
|
|
/// <summary>
|
|
/// 清空目录
|
|
/// </summary>
|
|
[Description("清空目录")]
|
|
class EmptyDirAction : IPublishAction
|
|
{
|
|
/// <summary>
|
|
/// 清空目录
|
|
/// </summary>
|
|
public string SrcDir { get; set; }
|
|
|
|
/// <summary>
|
|
/// 包含子目录
|
|
/// 支持*通配符,使用|做为或者符号,例:*test*|abc,优先匹配
|
|
/// 如果匹配子目录名则对子目录进行清空操作
|
|
/// </summary>
|
|
public string IncludeDirPattern { get; set; }
|
|
|
|
/// <summary>
|
|
/// 排除子目录
|
|
/// 支持*通配符,使用|做为或者符号,例:*test*|abc
|
|
/// 如果匹配子目录名则对子目录不进行清空操作
|
|
/// </summary>
|
|
public string ExcludeDirPattern { get; set; }
|
|
|
|
/// <summary>
|
|
/// 包含文件
|
|
/// 支持*通配符,使用|做为或者符号,例:*.txt|abc.dll,优先匹配
|
|
/// 如果匹配文件名则对文件进行删除操作
|
|
/// </summary>
|
|
public string IncludeFilePattern { get; set; }
|
|
|
|
/// <summary>
|
|
/// 排除文件
|
|
/// 支持*通配符,使用|做为或者符号,例:*.txt|abc.dll
|
|
/// 如果匹配文件名则对文件不进行删除操作
|
|
/// </summary>
|
|
public string ExcludeFilePattern { get; set; }
|
|
|
|
public void Execute(IPublishContext context)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(SrcDir))
|
|
{
|
|
throw new Exception("清空目录不能为空");
|
|
}
|
|
|
|
IncludeFilePattern = Helper.ConvertWildCardToRegex(IncludeFilePattern);
|
|
ExcludeFilePattern = Helper.ConvertWildCardToRegex(ExcludeFilePattern);
|
|
ExcludeDirPattern = Helper.ConvertWildCardToRegex(ExcludeDirPattern);
|
|
IncludeDirPattern = Helper.ConvertWildCardToRegex(IncludeDirPattern);
|
|
EmptyDirectory(SrcDir);
|
|
}
|
|
|
|
private void EmptyDirectory(string srcDirPath)
|
|
{
|
|
var srcDir = new DirectoryInfo(srcDirPath);
|
|
|
|
if (!srcDir.Exists)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var srcFiles = srcDir.GetFiles();
|
|
|
|
foreach (var file in srcFiles)
|
|
{
|
|
if (IncludeFilePattern != null && Regex.IsMatch(file.Name, IncludeFilePattern, RegexOptions.IgnoreCase))
|
|
{
|
|
file.Delete();
|
|
}
|
|
else if (ExcludeFilePattern == null || !Regex.IsMatch(file.Name, ExcludeFilePattern, RegexOptions.IgnoreCase))
|
|
{
|
|
file.Delete();
|
|
}
|
|
}
|
|
|
|
var srcDirs = srcDir.GetDirectories();
|
|
|
|
foreach (var subdir in srcDirs)
|
|
{
|
|
if (IncludeDirPattern != null && Regex.IsMatch(subdir.Name, IncludeDirPattern, RegexOptions.IgnoreCase))
|
|
{
|
|
EmptyDirectory(subdir.FullName);
|
|
}
|
|
else if (ExcludeDirPattern == null || !Regex.IsMatch(subdir.Name, ExcludeDirPattern, RegexOptions.IgnoreCase))
|
|
{
|
|
EmptyDirectory(subdir.FullName);
|
|
}
|
|
else
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (subdir.GetFiles().Length < 1)
|
|
{
|
|
subdir.Delete(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetProperty(string name, string value)
|
|
{
|
|
switch (name)
|
|
{
|
|
case "清空目录":
|
|
SrcDir = value;
|
|
break;
|
|
case "包含子目录":
|
|
IncludeDirPattern = value;
|
|
break;
|
|
case "排除子目录":
|
|
ExcludeDirPattern = value;
|
|
break;
|
|
case "包含文件":
|
|
IncludeFilePattern = value;
|
|
break;
|
|
case "排除文件":
|
|
ExcludeFilePattern = value;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|