Example #1
0
        /// <summary>
        /// Implements the template on the provided path
        /// </summary>
        /// <param name="ImplementationPath">The path to implement the template on</param>
        /// <param name="Data">Data to replace on the files while preprocessing</param>
        /// <returns>void</returns>
        public async Task ImplementAsync(String ImplementationPath, IDictionary <String, String> Data)
        {
            ImplementationPath = Path.GetFullPath(ImplementationPath);
            // Checks if the implementation folder exists
            Directory.CreateDirectory(ImplementationPath);

            // List all files and ignores the ones that should be
            var files = GetFiles(TPath, TConfig.ignore
                                 .Concat(new String[1] {
                "template.json"
            }));

            // Copies all files to their targets
            var copier = new FileCopier( );

            copier.FileCopied += Copier_FileCopied;
            await copier.CopyFilesAsync(
                files.Select(file => Path.Combine(TPath, file)),
                files.Select(file => Path.Combine(ImplementationPath, file))
                );

            // List files to process
            var processable = GetFiles(ImplementationPath, TConfig.ignore
                                       .Concat(TConfig.processIgnore)
                                       .Concat(new String[1] {
                "template.json"
            }));

            // Processes the files
            for (var i = 0; i < processable.Length; i++)
            {
                ProcessFile(Path.Combine(ImplementationPath, processable[i]), Data);
                FileProcessed?.Invoke(processable[i], i, processable.Length);
            }

            foreach (var command in TConfig.setupTasks)
            {
                TaskRunning?.Invoke(command);
                Process task = null;
                try
                {
                    task = Process.Start(new ProcessStartInfo
                    {
                        FileName         = command.Before(' '),
                        Arguments        = command.After(' '),
                        WindowStyle      = ProcessWindowStyle.Hidden,
                        CreateNoWindow   = false,
                        UseShellExecute  = true,
                        LoadUserProfile  = true,
                        WorkingDirectory = ImplementationPath
                    });
                    // Runs the process for 5 minutes in max

                    task.WaitForExit(TConfig.tasksTimeout);

                    if (task.ExitCode != 0)
                    {
                        throw new Exception($"Exit code was not 0: {task.ExitCode}.");
                    }

                    var elapsed = task.ExitTime - task.StartTime;
                    if (elapsed.TotalMilliseconds >= TConfig.tasksTimeout - 2)
                    {
                        throw new Exception($"Process achieved the timeout: {elapsed.ToString ( )}");
                    }
                }
                catch (Exception ex)
                {
                    TaskInterrupted?.Invoke(command, ex);
                }
                finally
                {
                    task.Close( );
                    task.Dispose( );
                }
            }
        }