Beispiel #1
0
        public override string[] ExtractTargets(string target)
        {
            List <string> results = new List <string>();

            // get subdirectories and return the ones that aren't ignored
            if (Directory.Exists(target))
            {
                DirectoryInfo dir = new DirectoryInfo(target);
                AddFoldersToBeIgnored(dir);

                dir.GetDirectories()
                .Select(d => d.FullName)
                .Each(t =>
                {
                    if (!IgnoreFolders.Contains(t))
                    {
                        results.Add(t);
                    }
                });

                AddFilesToBeIgnored(dir);

                dir.GetFiles(Config.TargetFilePattern)
                .Select(f => f.FullName)
                .Each(t =>
                {
                    if (!IgnoreFiles.Contains(t))
                    {
                        results.Add(new FileInfo(t).FullName);
                    }
                });
            }
            return(results.ToArray());
        }
Beispiel #2
0
        public override bool RunTask()
        {
            FilesThatExist = Files.Where(p => File.Exists(p.ItemSpec) &&
                                         (!IgnoreFiles?.Contains(p, TaskItemComparer.DefaultComparer) ?? true)).ToArray();

            Log.LogDebugTaskItems("  [Output] FilesThatExist", FilesThatExist);

            return(true);
        }
Beispiel #3
0
 private void AddFilesToBeIgnored(DirectoryInfo dir)
 {
     if (Config.IgnoreFilePatterns != null && Config.IgnoreFilePatterns.Length > 0)
     {
         dir.GetFiles(Config.IgnoreFilePatterns)
         .Select(f => f.FullName)
         .Each(f => IgnoreFiles.Add(f));
     }
 }
Beispiel #4
0
        public void AddIgnoreFile(string fileName)
        {
            if (IgnoreFiles == null)
            {
                IgnoreFiles = new List <string>();
            }

            IgnoreFiles.Add(fileName);
        }
Beispiel #5
0
        /// <summary>
        /// 文件部署
        /// </summary>
        public override void CopyFile(object filePath)
        {
            string filePathStr = filePath.ToString();

            if (File.Exists(filePathStr))
            {
                System.Threading.Thread.Sleep(SleepSecond * 10);//休眠5000秒
                try
                {
                    string   newFilePath = filePathStr.Replace(UpdatePath, AppPath);
                    string[] fileArr     = newFilePath.Split('\\');
                    string   fileName    = fileArr[fileArr.Length - 1];
                    if (IgnoreFiles.Contains(fileArr[fileArr.Length - 1]))
                    {
                        return; //如果是忽略文件,则忽略
                    }
                    fileArr[fileArr.Length - 1] = "";
                    string newDic = string.Join("\\", fileArr);
                    if (!Directory.Exists(newDic))
                    {
                        Directory.CreateDirectory(newDic);
                    }
                    newFilePath = newDic + "\\" + fileName;


                    if (File.Exists(newFilePath))
                    {
                        /*备份到指定目录,并删除此文件*/
                        BackUpFile(newFilePath);
                        File.Delete(newFilePath);
                    }

                    try
                    {
                        File.Move(filePathStr, newFilePath);
                    }
                    catch (Exception e)
                    {
                        throw new Exception(string.Format("source:{0},target:{1},{2}", filePathStr, newFilePath, e.Message));
                    }
                    WriteMsg("移动成功:" + DateTime.Now.ToString("HH:mm:ss") + "(" + newFilePath + ")", ConsoleColor.Green);
                }
                catch (Exception e)
                {
                    WriteMsg("移动失败:" + DateTime.Now.ToString("HH:mm:ss") + "(" + filePathStr + ")" + e.ToString(), ConsoleColor.Red);
                }
                finally
                {
                    lock (Files)
                    {
                        DeleteEmptyDic(Path.GetDirectoryName(filePathStr));
                        //this.Files.Remove(filePathStr);
                    }
                }
            }
        }
        public override bool Execute()
        {
            Log.LogDebugMessage("GetFilesThatExist Task");
            Log.LogDebugTaskItems("  Files", Files);
            Log.LogDebugTaskItems("  IgnoreFiles", IgnoreFiles);

            FilesThatExist = Files.Where(p => File.Exists(p.ItemSpec) &&
                                         (!IgnoreFiles?.Contains(p, TaskItemComparer.DefaultComparer) ?? true)).ToArray();

            Log.LogDebugTaskItems("  [Output] FilesThatExist", FilesThatExist);

            return(true);
        }
Beispiel #7
0
        /// <summary>
        /// 拷贝文件
        /// </summary>
        public override void CopyFile(object filePath)
        {
            string filePathStr = filePath.ToString();

            if (File.Exists(filePathStr))
            {
                System.Threading.Thread.Sleep(SleepSecond * 1000);//休眠5000秒
                try
                {
                    string   newFilePath = filePathStr.Replace(UpdatePath, AppPath);
                    string[] fileArr     = newFilePath.Split('\\');
                    string   fileName    = fileArr[fileArr.Length - 1];
                    if (IgnoreFiles.Contains(fileArr[fileArr.Length - 1]))
                    {
                        return; //如果是忽略文件,则忽略
                    }

                    fileArr[fileArr.Length - 1] = DateTime.Now.ToString("yyyy-MM-dd");
                    string newDic = string.Join("\\", fileArr);
                    if (!Directory.Exists(newDic))
                    {
                        Directory.CreateDirectory(newDic);
                    }
                    newFilePath = newDic + "\\" + fileName;
                    string dirPath = Path.GetDirectoryName(filePathStr);
                    if (File.Exists(newFilePath))
                    {
                        File.Move(newFilePath, newFilePath + DateTime.Now.ToString("yyyy_mm_dd_HH_mm_ss") + Guid.NewGuid());
                    }
                    File.Move(filePathStr, newFilePath);
                    WriteMsg("移动成功:" + DateTime.Now.ToString("HH:mm:ss") + "(" + newFilePath + ")", ConsoleColor.Green);
                    DirectoryInfo di = new DirectoryInfo(dirPath);
                    if (di.GetDirectories().Length == 0 && di.GetFiles().Length == 0)
                    {
                        Directory.Delete(dirPath);
                    }
                }
                catch (Exception e)
                {
                    WriteMsg("移动失败:" + DateTime.Now.ToString("HH:mm:ss") + "(" + filePathStr + ")" + e.ToString(), ConsoleColor.Red);
                }
                finally
                {
                    this.Files.Remove(filePathStr);
                }
            }
        }
Beispiel #8
0
        public void RunSorter()
        {
            Files = GetFiles();
            if (Files.Count == 0)
            {
                Console.WriteLine("Files not found");
                return;
            }

            foreach (string file in Files)
            {
                string fileName = System.IO.Path.GetFileName(file);
                string fileExt  = System.IO.Path.GetExtension(fileName);

                bool notFileAndExt = string.IsNullOrWhiteSpace(fileName) || string.IsNullOrWhiteSpace(fileExt);
                Console.WriteLine("notFileAndExt: {0}", notFileAndExt);
                if (notFileAndExt)
                {
                    continue;
                }
                bool isIgnoreFile = IgnoreFiles != null && IgnoreFiles.Contains(fileName);
                Console.WriteLine("isIgnoreFile: {0}", isIgnoreFile);
                if (notFileAndExt)
                {
                    continue;
                }

                if (notFileAndExt || isIgnoreFile)
                {
                    continue;
                }

                Console.WriteLine("{0} - {1}", fileName, fileExt);
                string pathToMove = GetDirPathToMove(fileExt);
                string pathSource = System.IO.Path.Combine(pathToMove, fileName);
                pathSource = CheckSourcePath(pathSource);
                Console.WriteLine("{0} was moved to {1}.", file, pathSource);
                File.Move(file, pathSource);
            }
        }
Beispiel #9
0
        private void SetEnvironments()
        {
            {
                List <string> rawLines;
                if (Environment.Is64BitProcess)
                {
                    rawLines = FileUtility.Inst.GetFileLines(GetResourcePath(SYSTEMENVIRONMENTS_x64_FILE));
                }
                else
                {
                    rawLines = FileUtility.Inst.GetFileLines(GetResourcePath(SYSTEMENVIRONMENTS_x86_FILE));
                }

                foreach (var item in rawLines)
                {
                    int tipStartPos = item.IndexOf(',');
                    var envVariable = item.Substring(0, tipStartPos);
                    var nextSection = item.Substring(tipStartPos + 1);
                    tipStartPos = nextSection.IndexOf(',');
                    var envValue = nextSection.Substring(0, tipStartPos);
                    nextSection = nextSection.Substring(tipStartPos + 1);
                    tipStartPos = nextSection.IndexOf(',');
                    var displayName = nextSection.Substring(0, tipStartPos);
                    var envTips     = nextSection.Substring(tipStartPos + 1);

                    ToolTipMap.Add(envVariable, envTips);

                    var filePath = new Model.EnvironmentInfo();
                    filePath.VarName      = envVariable;
                    filePath.Tips         = envTips;
                    filePath.DisplayName  = displayName;
                    filePath.RelativePath = envValue;
                    filePath.ShortPath    = envValue.GetAbsolutePath(true);
                    filePath.LongPath     = envValue.GetAbsolutePath(false);

                    SystemEnvironments.Add(envVariable, filePath);
                }
            }

            {
                var rawLines = FileUtility.Inst.GetFileLines(GetResourcePath(APPENVIRONMENTS_FILE));

                foreach (var item in rawLines)
                {
                    int tipStartPos = item.IndexOf(',');

                    var envVariable = item.Substring(0, tipStartPos);
                    var envTips     = item.Substring(tipStartPos);

                    ToolTipMap.Add(envVariable, envTips);

                    var filePath = new Model.EnvironmentInfo();
                    filePath.RelativePath = envVariable;
                    filePath.ShortPath    = envVariable.GetAbsolutePath(true);
                    filePath.LongPath     = envVariable.GetAbsolutePath(false);

                    AppEnvironments.Add(envVariable, filePath);
                }
            }

            {
                var rawLines = FileUtility.Inst.GetFileLines(GetResourcePath(IGNOREFOLDERS_FILE));

                foreach (var item in rawLines)
                {
                    var expandedValue = Environment.ExpandEnvironmentVariables(item);

                    var filePath = new Model.FilePath();
                    filePath.RelativePath = item;
                    filePath.ShortPath    = expandedValue;
                    filePath.LongPath     = string.Format(@"\\?\{0}", expandedValue);

                    IgnoreFolders.Add(filePath);
                }
            }

            {
                var rawLines = FileUtility.Inst.GetFileLines(GetResourcePath(IGNOREFILES_FILE));

                foreach (var item in rawLines)
                {
                    var expandedValue = Environment.ExpandEnvironmentVariables(item);

                    var filePath = new Model.FilePathEx();
                    filePath.RelativePath = item;
                    filePath.ShortPath    = new Regex(expandedValue.Replace("\\", "\\\\"),
                                                      RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled);
                    var tempVal = string.Format(@"\\?\{0}", expandedValue);
                    filePath.LongPath = new Regex(tempVal.Replace("\\", "\\\\"),
                                                  RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled);

                    IgnoreFiles.Add(filePath);
                }
            }

            IgnoreRegKeys = FileUtility.Inst.GetFileLines(GetResourcePath(IGNOREREGISTRYKEYS_FILE));
        }
Beispiel #10
0
        /// <summary>
        /// Initializes the run options.
        /// </summary>
        /// <param name="args">The command line args.</param>
        /// <returns></returns>
        public void Init(string[] args)
        {
            //defaults
            Init();

            //parse up args
            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "-d":
                case "--data":
                    foreach (string table in args[++i].Split(','))
                    {
                        LoadData.Add(table);
                    }
                    break;

                case "--diffFile":
                    DiffFile = args[++i];
                    break;

                case "-f":
                case "--fullyScripted":
                    FullyScripted = true;
                    break;

                case "-h":
                case "--help":
                    Logger.Log("A tool to run scripts on a Microsoft SQL Server 2000 server", OutputLevel.Errors);
                    Logger.Log("Usage: SQLUpdater <args> [-t] <target> [-r] [reference]", OutputLevel.Errors);
                    Logger.Log("\t-d <name[,name]>: Tables to load data from", OutputLevel.Errors);
                    Logger.Log("\t-f: the database is fully scripted", OutputLevel.Errors);
                    Logger.Log("\t-h: display this help message and exit", OutputLevel.Errors);
                    Logger.Log("\t-l <level>: transaction level (None, TableOnly, Everything)", OutputLevel.Errors);
                    Logger.Log("\t-o <level>: output level (Errors, Differences, Updates, Reads)", OutputLevel.Errors);
                    Logger.Log("\t-s <directory>: directory to store scripts in before execution", OutputLevel.Errors);
                    Logger.Log("\t-w <name[,name]>: objects to write out changes to", OutputLevel.Errors);
                    Logger.Log("\t--diffFile <filename>: name of a single file to store a difference summary in", OutputLevel.Errors);
                    Logger.Log("\t--ignore <name[,name]>: script files to ignore", OutputLevel.Errors);
                    Logger.Log("\t--minimalDependencies: only recreate otherwise dropped objects", OutputLevel.Errors);
                    Logger.Log("\t--parserOutput <directory>: directory for debugging parser output", OutputLevel.Errors);
                    Logger.Log("\t--scriptFile <filename>: name of a single file to store scripts in in conjunction with -s", OutputLevel.Errors);
                    Logger.Log("\t--test: run in test mode without updating the database", OutputLevel.Errors);
                    Environment.Exit(0);
                    break;

                case "--ignore":
                    foreach (string name in args[++i].Split(','))
                    {
                        IgnoreFiles.Add(name);
                    }
                    break;

                case "-l":
                    TransactionLevel = (TransactionLevel)Enum.Parse(typeof(TransactionLevel), args[++i]);
                    break;

                case "-m":
                case "--minimalDependencies":
                    MinimalDependencies = true;
                    break;

                case "-o":
                case "--outputlevel":
                    OutputLevel = (OutputLevel)Enum.Parse(typeof(OutputLevel), args[++i]);
                    break;

                case "--parserOutput":
                    ParserOutput = args[++i];
                    break;

                case "-r":
                    AddConnection(args[++i], Connections.References);
                    break;

                case "-s":
                    ScriptOutputDirectory = args[++i];
                    if (!ScriptOutputDirectory.EndsWith("\\"))
                    {
                        ScriptOutputDirectory = ScriptOutputDirectory + "\\";
                    }
                    break;

                case "--scriptFile":
                    ScriptFile = args[++i];
                    break;

                case "-t":
                    AddConnection(args[++i], Connections.Targets);
                    break;

                case "--test":
                    Test = true;
                    break;

                case "-w":
                case "--write":
                    foreach (string name in args[++i].Split(','))
                    {
                        WriteObjects.Add(name);
                    }
                    break;

                default:
                    if (args[i][0] == '-' || (Connections.References.Count > 0 && Connections.Targets.Count > 0))
                    {
                        Logger.Log("Unknown argument: " + args[i], OutputLevel.Errors);
                        Environment.Exit(-1);
                    }
                    else
                    {
                        if (Connections.Targets.Count == 0)
                        {
                            AddConnection(args[i], Connections.Targets);
                        }
                        else
                        {
                            AddConnection(args[i], Connections.References);
                        }
                    }
                    break;
                }
            }
        }