144 lines
4.7 KiB
C#
144 lines
4.7 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace YLPublishTool.Actions
|
|
{
|
|
/// <summary>
|
|
/// 复制目录
|
|
/// </summary>
|
|
[Description("复制目录")]
|
|
class CopyDirAction : IPublishAction
|
|
{
|
|
/// <summary>
|
|
/// 复制文件夹
|
|
/// </summary>
|
|
public string SrcDir { get; set; }
|
|
|
|
/// <summary>
|
|
/// 目标文件夹
|
|
/// </summary>
|
|
public string TargetDir { 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("复制目录不能为空");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(TargetDir))
|
|
{
|
|
throw new Exception("目标目录不能为空");
|
|
}
|
|
|
|
IncludeDirPattern = Helper.ConvertWildCardToRegex(IncludeDirPattern);
|
|
ExcludeDirPattern = Helper.ConvertWildCardToRegex(ExcludeDirPattern);
|
|
IncludeFilePattern = Helper.ConvertWildCardToRegex(IncludeFilePattern);
|
|
ExcludeFilePattern = Helper.ConvertWildCardToRegex(ExcludeFilePattern);
|
|
CopyDirectory(SrcDir, TargetDir);
|
|
}
|
|
|
|
private void CopyDirectory(string srcDirPath, string targetDirPath)
|
|
{
|
|
var srcDir = new DirectoryInfo(srcDirPath);
|
|
|
|
if (!srcDir.Exists)
|
|
{
|
|
throw new Exception("复制目录不存在: " + srcDirPath);
|
|
}
|
|
|
|
if (!Directory.Exists(targetDirPath))
|
|
{
|
|
Directory.CreateDirectory(targetDirPath);
|
|
}
|
|
|
|
var srcFiles = srcDir.GetFiles();
|
|
|
|
foreach (var file in srcFiles)
|
|
{
|
|
string destFile = Path.Combine(targetDirPath, file.Name);
|
|
if (IncludeFilePattern != null && !Regex.IsMatch(file.Name, IncludeFilePattern, RegexOptions.IgnoreCase))
|
|
{
|
|
continue;
|
|
}
|
|
if (ExcludeFilePattern != null && Regex.IsMatch(file.Name, ExcludeFilePattern, RegexOptions.IgnoreCase))
|
|
{
|
|
continue;
|
|
}
|
|
file.CopyTo(destFile, true);
|
|
}
|
|
|
|
var srcDirs = srcDir.GetDirectories();
|
|
|
|
foreach (var subdir in srcDirs)
|
|
{
|
|
string destDir = Path.Combine(targetDirPath, subdir.Name);
|
|
if (IncludeDirPattern != null && !Regex.IsMatch(subdir.Name, IncludeDirPattern, RegexOptions.IgnoreCase))
|
|
{
|
|
continue;
|
|
}
|
|
if (ExcludeDirPattern != null && Regex.IsMatch(subdir.Name, ExcludeDirPattern, RegexOptions.IgnoreCase))
|
|
{
|
|
continue;
|
|
}
|
|
CopyDirectory(subdir.FullName, destDir);
|
|
}
|
|
}
|
|
|
|
public void SetProperty(string name, string value)
|
|
{
|
|
switch (name)
|
|
{
|
|
case "复制目录":
|
|
SrcDir = value;
|
|
break;
|
|
case "目标目录":
|
|
TargetDir = value;
|
|
break;
|
|
case "包含子目录":
|
|
IncludeDirPattern = value;
|
|
break;
|
|
case "排除子目录":
|
|
ExcludeDirPattern = value;
|
|
break;
|
|
case "包含文件":
|
|
IncludeFilePattern = value;
|
|
break;
|
|
case "排除文件":
|
|
ExcludeFilePattern = value;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|