private void ProcessContent(string baseFolder, IEnumerable <FieldValue> replacements, Action <LogItem> logger)
        {
            var files = baseFolder.GetFilesByFilter(s => !Ignore(s));

            foreach (var inputfileName in files)
            {
                var extension = Path.GetExtension(inputfileName);
                if (_skipExtensions.Contains(extension))
                {
                    continue;
                }

                var segements = inputfileName.Substring(baseFolder.Length).Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
                if (segements.Any(s => Ignore(s)))
                {
                    logger(LogItem.Info($" ignored {Path.GetFileName(inputfileName)}"));
                    continue;
                }


                var encoding = GetEncoding(inputfileName);

                var tempFileName = PathHelper.GetTempName();
                var output       = new FileStream(tempFileName, FileMode.CreateNew);
                try
                {
                    using (var input = new FileStream(inputfileName, FileMode.Open))
                    {
                        if (ByteReplacer.TryReplace(input, output, replacements.Select(r => Tuple.Create(encoding.GetBytes(r.Field), encoding.GetBytes(r.Value)))))
                        {
                            input.Close();
                            output.Close();
                            File.Delete(inputfileName);
                            File.Move(tempFileName, inputfileName);
                            tempFileName = PathHelper.GetTempName();
                            output       = new FileStream(tempFileName, FileMode.CreateNew);
                            logger(LogItem.Info($"   modified {Path.GetFileName(inputfileName)}"));
                        }
                        else
                        {
                            //output.Flush();//??
                            output.Seek(0L, SeekOrigin.Begin);
                        }
                    }
                }
                finally
                {
                    if (output != null)
                    {
                        output.Close();
                        File.Delete(tempFileName);
                        output = null;
                    }
                }
            }
        }
Exemple #2
0
        public bool BuildScaffold(string targetZipName, IEnumerable <FieldValue> replacements, Action <LogItem> logger = null)
        {
            var loggerEx = logger == null
            ? new Action <Exception, LogItem>((e, i) => { if (e != null)
                                                          {
                                                              throw e;
                                                          }
                                              })
            : (e, i) => { logger(i); };

            var fields = Info?.GetScaffoldReplacements() ?? new List <FieldValuePrompt>();

            fields.Merge(replacements?.ToList(), true);
            fields.ResolvePlaceholders();

            fields.MatchAllCases();

            try
            {
                using (var zipIn = ZipFile.OpenRead(_sourceZipName))
                {
                    using (var zipOut = ZipFile.Open(targetZipName, ZipArchiveMode.Create))
                    {
                        foreach (var entry in zipIn.Entries)
                        {
                            if (entry.Name.EqualsOrdinalIgnoreCase(Constants.BoilerplateInfoFileName))
                            {
                                // skip package info
                                continue;
                            }

                            var fullname = entry.FullName;

                            foreach (var r in fields)
                            {
                                fullname = fullname.Replace(r.Field, r.Value);
                            }

                            if (GetEntryShortName(entry.FullName) != GetEntryShortName(fullname))
                            {
                                loggerEx(null, LogItem.Info($" renamed {GetEntryShortName(entry.FullName)} => {GetEntryShortName(fullname)}"));
                            }


                            if (entry.Length == 0)
                            {
                                // create directory
                                zipOut.CreateEntry(fullname, CompressionLevel.Fastest);
                                continue;
                            }

                            // find encoding
                            Encoding encoding;
                            using (var stream = entry.Open())
                            {
                                using (var reader = new StreamReader(stream))
                                {
                                    reader.Peek();
                                    encoding = reader.CurrentEncoding;
                                }
                            }

                            // replace bytes
                            using (var inStream = entry.Open())
                            {
                                using (var outStream = zipOut.CreateEntry(fullname, CompressionLevel.Optimal).Open())
                                {
                                    if (_skipExtensions.Contains(Path.GetExtension(fullname)))
                                    {
                                        CopyStream(inStream, outStream);
                                    }
                                    else if (ByteReplacer.TryReplace(inStream, outStream, fields.Select(x => Tuple.Create(encoding.GetBytes(x.Field), encoding.GetBytes(x.Value)))))
                                    {
                                        loggerEx(null, LogItem.Info($"   modified {GetEntryShortName(fullname)}"));
                                    }
                                }
                            }
                        }
                    }
                }
                loggerEx(null, LogItem.Completed("build completed"));
                return(true);
            }
            catch (Exception ex)
            {
                try
                {
                    File.Delete(targetZipName);
                }
                catch { }
                loggerEx(ex, LogItem.Error(ex.Message));
                return(false);
            }
        }