public Stream OpenFile(string path)
        {
            int lastSep = path.IndexOfAny(new[] { '/', '\\' });
            IDisposable <ITemplateSourceFolder> rootFolder = Root;

            if (lastSep == -1)
            {
                ITemplateSourceFile sourceFile = (ITemplateSourceFile)rootFolder.Value.Children.FirstOrDefault(x => string.Equals(path, x.Name, StringComparison.OrdinalIgnoreCase));
                return(new CoDisposableStream(sourceFile.OpenRead(), rootFolder));
            }

            string part = path.Substring(0, lastSep);
            ITemplateSourceFolder sourceFolder = (ITemplateSourceFolder)rootFolder.Value.Children.FirstOrDefault(x => string.Equals(part, x.Name, StringComparison.OrdinalIgnoreCase));

            while (lastSep > 0)
            {
                int start = lastSep + 1;
                lastSep = path.IndexOfAny(new[] { '/', '\\' }, lastSep + 1);

                if (lastSep < 0)
                {
                    part = path.Substring(start);
                    ITemplateSourceFile sourceFile = (ITemplateSourceFile)sourceFolder.Children.FirstOrDefault(x => string.Equals(part, x.Name, StringComparison.OrdinalIgnoreCase));
                    return(new CoDisposableStream(sourceFile.OpenRead(), rootFolder));
                }

                part         = path.Substring(start, lastSep - start);
                sourceFolder = (ITemplateSourceFolder)sourceFolder.Children.FirstOrDefault(x => string.Equals(part, x.Name, StringComparison.OrdinalIgnoreCase));
            }

            rootFolder.Dispose();
            throw new FileNotFoundException("Unable to find file", path);
        }
        public VsTemplate(ITemplateSourceFile source, IConfiguredTemplateSource templateSource, IGenerator generator)
        {
            SourceFile = source;
            Source     = templateSource;
            Generator  = generator;

            using (Stream src = source.OpenRead())
            {
                XDocument doc = XDocument.Load(src);
                DefaultName = doc.Root.Descendants().FirstOrDefault(x => x.Name.LocalName == "DefaultName")?.Value;
                XElement idElement = doc.Root.Descendants().FirstOrDefault(x => x.Name.LocalName == "TemplateID");
                IEnumerable <XElement> customParameters = doc.Root.Descendants().Where(x => x.Name.LocalName == "CustomParameter");
                List <CustomParameter> declaredParams   = new List <CustomParameter>();

                foreach (XElement parameter in customParameters)
                {
                    string name = parameter.Attributes().First(x => x.Name.LocalName == "Name").Value;
                    name = name.Substring(1, name.Length - 2);

                    declaredParams.Add(new CustomParameter
                    {
                        Name         = name,
                        DefaultValue = parameter.Attributes().First(x => x.Name.LocalName == "Value").Value
                    });
                }

                CustomParameters = declaredParams;
                Name             = idElement.Value;
                VsTemplateFile   = doc;
            }
        }
예제 #3
0
 private JObject ReadConfigModel(ITemplateSourceFile file)
 {
     using (Stream s = file.OpenRead())
         using (TextReader tr = new StreamReader(s, true))
             using (JsonReader r = new JsonTextReader(tr))
             {
                 return(JObject.Load(r));
             }
 }
 private void GetPackageIdAndVersion(ITemplateSourceFile nuspec, out string id, out string version)
 {
     using (Stream s = nuspec.OpenRead())
         using (TextReader reader = new StreamReader(s, Encoding.UTF8, true, 8192, true))
         {
             XDocument doc      = XDocument.Load(reader);
             XElement  metadata = doc.Descendants().First(x => x.Name.LocalName == "metadata");
             id      = metadata.Descendants().First(x => x.Name.LocalName == "id").Value;
             version = metadata.Descendants().First(x => x.Name.LocalName == "version").Value;
         }
 }
        private static void ProcessFile(Orchestrator self, ITemplateSourceFile sourceFile, string sourceRel, string targetDir, IGlobalRunSpec spec, IProcessor fallback, IReadOnlyDictionary <IPathMatcher, IProcessor> specializations)
        {
            IProcessor runner = specializations.FirstOrDefault(x => x.Key.IsMatch(sourceRel)).Value ?? fallback;

            string targetRel;

            if (!spec.TryGetTargetRelPath(sourceRel, out targetRel))
            {
                targetRel = sourceRel;
            }

            string targetPath = Path.Combine(targetDir, targetRel);

            //TODO: Update context with the current file & such here

            int bufferSize,
                flushThreshold;

            bool   customBufferSize     = self.TryGetBufferSize(sourceFile, out bufferSize);
            bool   customFlushThreshold = self.TryGetFlushThreshold(sourceFile, out flushThreshold);
            string fullTargetDir        = Path.GetDirectoryName(targetPath);

            Directory.CreateDirectory(fullTargetDir);

            try
            {
                using (Stream source = sourceFile.OpenRead())
                    using (FileStream target = File.Create(targetPath))
                    {
                        if (!customBufferSize)
                        {
                            runner.Run(source, target);
                        }
                        else
                        {
                            if (!customFlushThreshold)
                            {
                                runner.Run(source, target, bufferSize);
                            }
                            else
                            {
                                runner.Run(source, target, bufferSize, flushThreshold);
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                throw new Exception($"Error while processing file {sourceFile.FullPath}.\nCheck InnerException for details", ex);
            }
        }
        public IRunnableProjectConfig ReprocessWithParameters(IParameterSet parameters, VariableCollection rootVariableCollection, ITemplateSourceFile configFile, IOperationProvider[] operations)
        {
            IProcessor             processor = Processor.Create(new EngineConfig(rootVariableCollection), operations);
            IRunnableProjectConfig m;

            using (Stream configStream = configFile.OpenRead())
                using (Stream targetStream = new MemoryStream())
                {
                    processor.Run(configStream, targetStream);
                    targetStream.Position = 0;

                    using (TextReader tr = new StreamReader(targetStream, true))
                        using (JsonReader r = new JsonTextReader(tr))
                        {
                            JObject model = JObject.Load(r);
                            m = model.ToObject <ConfigModel>();
                        }
                }

            return(m);
        }