public void ContainsFile()
        {
            void Template(string scriptPath, string folderName, string fileName, bool result)
            {
                EngineState s = EngineTests.CreateEngineState();
                string      pbOriginScript = Path.Combine("%TestBench%", "EncodedFile", scriptPath);
                string      originScript   = StringEscaper.Preprocess(s, pbOriginScript);

                Script sc = s.Project.LoadScriptRuntime(originScript, new LoadScriptRuntimeOptions());

                Assert.AreEqual(EncodedFile.ContainsFile(sc, folderName, fileName), result);
            }

            Template("ExtractFileTests.script", "FolderExample", "Type1.jpg", true);
            Template("ExtractFileTests.script", "FolderExample", "ShouldFail", false);
            Template("ExtractFileTests.script", "ShouldFail", "Type2.7z", false);
            Template("CompleteBlank.script", "ShouldFail", "ShouldFail", false);
        }
Exemple #2
0
        /*
         * WB082 Behavior
         * ExtractFile : DestDir must be Directory, create if not exists.
         * Ex) (...),README.txt,%BaseDir%\Temp\Hello
         *   -> No Hello : Create directory "Hello" and extract files into new directory.
         *   -> Hello is a file : Failure
         *   -> Hello is a directory : Extract files into directory.
         *
         * ExtractAllFiles
         * Ex) (...),Fonts,%BaseDir%\Temp\Hello
         *   -> No Hello : Failure
         *   -> Hello is a file : Failure
         *   -> Hello is a directory : Extract files into directory.
         *
         * PEBakery Behavior
         * ExtractFile/ExtractAllFiles : DestDir must be Directory, create if not exists.
         * Ex) (...),README.txt,%BaseDir%\Temp\Hello
         *   -> No Hello : Create directory "Hello" and extract files into new directory.
         *   -> Hello is a file : Failure
         *   -> Hello is a directory : Extract files into directory.
         */

        public static List <LogInfo> ExtractFile(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_ExtractFile info = cmd.Info.Cast <CodeInfo_ExtractFile>();

            string scriptFile = StringEscaper.Preprocess(s, info.ScriptFile);
            string dirName    = StringEscaper.Preprocess(s, info.DirName);
            string fileName   = StringEscaper.Preprocess(s, info.FileName);
            string destDir    = StringEscaper.Preprocess(s, info.DestDir); // Should be directory name

            Script sc = Engine.GetScriptInstance(s, s.CurrentScript.RealPath, scriptFile, out _);

            // Check if encoded file exist
            if (!EncodedFile.ContainsFile(sc, dirName, fileName))
            {
                return(LogInfo.LogErrorMessage(logs, $"Encoded file [{dirName}\\{fileName}] not found in script [{sc.RealPath}]."));
            }

            // Filter dest path
            if (!StringEscaper.PathSecurityCheck(destDir, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            if (!Directory.Exists(destDir)) // DestDir already exists
            {
                if (File.Exists(destDir))   // Error, cannot proceed
                {
                    return(LogInfo.LogErrorMessage(logs, $"File [{destDir}] is not a directory."));
                }

                Directory.CreateDirectory(destDir);
            }

            s.MainViewModel.SetBuildCommandProgress("ExtractFile Progress", 1);
            try
            {
                object             progressLock = new object();
                IProgress <double> progress     = new Progress <double>(x =>
                {
                    lock (progressLock)
                    {
                        s.MainViewModel.BuildCommandProgressValue = x;
                        if (x < EncodedFile.Base64ReportFactor)
                        { // [Stage 1] Base64
                            s.MainViewModel.BuildCommandProgressText = $"Reading \"{fileName}\" from script\r\n({x * 100:0.0}%)";
                        }
                        else
                        { // [Stage 2] Decompress
                            s.MainViewModel.BuildCommandProgressText = $"Decompressing \"{fileName}\"\r\n({x * 100:0.0}%)";
                        }
                    }
                });

                string destPath = Path.Combine(destDir, fileName);
                using (FileStream fs = new FileStream(destPath, FileMode.Create, FileAccess.Write))
                {
                    EncodedFile.ExtractFile(sc, dirName, fileName, fs, progress);
                }
            }
            finally
            {
                s.MainViewModel.ResetBuildCommandProgress();
            }

            logs.Add(new LogInfo(LogState.Success, $"Encoded file [{fileName}] was extracted to [{destDir}]"));

            return(logs);
        }
Exemple #3
0
        public static List <LogInfo> ExtractAndRun(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_ExtractAndRun info = cmd.Info.Cast <CodeInfo_ExtractAndRun>();

            string scriptFile = StringEscaper.Preprocess(s, info.ScriptFile);
            string dirName    = StringEscaper.Preprocess(s, info.DirName);
            string fileName   = StringEscaper.Preprocess(s, info.FileName);

            Script sc = Engine.GetScriptInstance(s, s.CurrentScript.RealPath, scriptFile, out _);

            // Check if encoded file exist
            if (!EncodedFile.ContainsFile(sc, dirName, fileName))
            {
                return(LogInfo.LogErrorMessage(logs, $"Encoded file [{dirName}\\{fileName}] not found in script [{sc.RealPath}]."));
            }

            string tempDir  = FileHelper.GetTempDir();
            string tempPath = Path.Combine(tempDir, fileName);

            s.MainViewModel.SetBuildCommandProgress("ExtractAndRun Progress", 1);
            try
            {
                object             progressLock = new object();
                IProgress <double> progress     = new Progress <double>(x =>
                {
                    lock (progressLock)
                    {
                        s.MainViewModel.BuildCommandProgressValue = x;
                        if (x < EncodedFile.Base64ReportFactor)
                        { // [Stage 1] Base64
                            s.MainViewModel.BuildCommandProgressText = $"Reading \"{fileName}\" from script\r\n({x * 100:0.0}%)";
                        }
                        else
                        { // [Stage 2] Decompress
                            s.MainViewModel.BuildCommandProgressText = $"Decompressing \"{fileName}\"\r\n({x * 100:0.0}%)";
                        }
                    }
                });

                using (FileStream fs = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
                {
                    EncodedFile.ExtractFile(sc, dirName, info.FileName, fs, progress);
                }
            }
            finally
            {
                s.MainViewModel.ResetBuildCommandProgress();
            }

            string _params = null;

            using (Process proc = new Process())
            {
                proc.EnableRaisingEvents = true;
                proc.StartInfo           = new ProcessStartInfo
                {
                    FileName        = tempPath,
                    UseShellExecute = true,
                };

                if (!string.IsNullOrEmpty(info.Params))
                {
                    _params = StringEscaper.Preprocess(s, info.Params);
                    proc.StartInfo.Arguments = _params;
                }

                proc.Exited += (object sender, EventArgs e) =>
                {
                    if (Directory.Exists(tempDir))
                    {
                        Directory.Delete(tempDir, true);
                    }

                    // ReSharper disable once AccessToDisposedClosure
                    proc.Dispose();
                };

                proc.Start();
            }

            if (_params == null)
            {
                logs.Add(new LogInfo(LogState.Success, $"Extracted and executed [{fileName}]"));
            }
            else
            {
                logs.Add(new LogInfo(LogState.Success, $"Extracted and executed [{fileName} {_params}]"));
            }

            return(logs);
        }