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; } }
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 RunnableProjectTemplate(JObject raw, IGenerator generator, IConfiguredTemplateSource source, ITemplateSourceFile configFile, IRunnableProjectConfig config) { config.SourceFile = configFile; ConfigFile = configFile; Generator = generator; Source = source; Config = config; DefaultName = config.DefaultName; Name = config.Name; Identity = config.Identity ?? config.Name; ShortName = config.ShortName; Author = config.Author; Tags = config.Tags; Classifications = config.Classifications; GroupIdentity = config.GroupIdentity; _raw = raw; }
public string GetInstallPackageId(IConfiguredTemplateSource source, string location) { if (Path.GetExtension(location).ToUpperInvariant() == ".NUPKG") { using (IDisposable <ITemplateSourceFolder> root = RootFor(source, location)) { ITemplateSourceFile nuspec = root.Value.EnumerateFiles("*.nuspec", SearchOption.TopDirectoryOnly).FirstOrDefault(); if (nuspec != null) { string id, version; GetPackageIdAndVersion(nuspec, out id, out version); return(id); } } } return(null); }
private static string[] JTokenToCollection(JToken token, ITemplateSourceFile sourceFile, string[] defaultSet) { if (token == null) { return(defaultSet); } else if (token.Type == JTokenType.String) { using (Stream excludeList = sourceFile.Parent.OpenFile(token.ToObject <string>())) using (TextReader reader = new StreamReader(excludeList, Encoding.UTF8, true, 4096, true)) { return(reader.ReadToEnd().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)); } } else { return(token.ToObject <string[]>()); } }
private IEnumerable <ITemplate> GetTemplatesFromDir(IConfiguredTemplateSource source, ITemplateSourceFolder folder) { foreach (ITemplateSourceEntry entry in folder.Children) { if (entry.Kind == TemplateSourceEntryKind.File && entry.Name.Equals(".netnew.json", StringComparison.OrdinalIgnoreCase)) { RunnableProjectTemplate tmp = null; try { ITemplateSourceFile file = (ITemplateSourceFile)entry; JObject srcObject = ReadConfigModel(file); tmp = new RunnableProjectTemplate(srcObject, this, source, file, srcObject.ToObject <IRunnableProjectConfig>(new JsonSerializer { Converters = { new RunnableProjectConfigConverter() } })); } catch { } if (tmp != null) { yield return(tmp); } } else if (entry.Kind == TemplateSourceEntryKind.Folder) { foreach (ITemplate template in GetTemplatesFromDir(source, (ITemplateSourceFolder)entry)) { yield return(template); } } } }
public async Task <bool> CheckForUpdatesAsync(string location) { if (Path.GetExtension(location).ToUpperInvariant() == ".NUPKG") { using (IDisposable <ITemplateSourceFolder> root = RootFor(location)) { ITemplateSourceFile nuspec = root.Value.EnumerateFiles("*.nuspec", SearchOption.TopDirectoryOnly).FirstOrDefault(); if (nuspec != null) { string id, version; GetPackageIdAndVersion(nuspec, out id, out version); NuGetUtil.Init(); string newerVersion = await NuGetUtil.GetCurrentVersionOfPackageAsync(id, version); return(newerVersion != null); } } } return(false); }
public virtual IEnumerable <ITemplateSourceFile> EnumerateFiles(string pattern, SearchOption searchOption) { foreach (ITemplateSourceEntry child in Children) { if (child.Kind == TemplateSourceEntryKind.File) { ITemplateSourceFile childFile = (ITemplateSourceFile)child; if (IsMatch(childFile, pattern)) { yield return(childFile); } } else { if (searchOption == SearchOption.AllDirectories) { foreach (ITemplateSourceFile file in ((ITemplateSourceFolder)child).EnumerateFiles(pattern, SearchOption.AllDirectories)) { yield return(file); } } } } }
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); }
protected virtual bool TryGetFlushThreshold(ITemplateSourceFile sourceFile, out int threshold) { threshold = -1; return(false); }
protected virtual bool TryGetBufferSize(ITemplateSourceFile sourceFile, out int bufferSize) { bufferSize = -1; return(false); }
internal void Evaluate(IParameterSet parameters, VariableCollection rootVariableCollection, ITemplateSourceFile configFile) { List <FileSource> sources = new List <FileSource>(); bool stable = _simpleConfigModel.Symbols == null; Dictionary <string, bool> computed = new Dictionary <string, bool>(); while (!stable) { stable = true; foreach (KeyValuePair <string, ISymbolModel> symbol in _simpleConfigModel.Symbols) { if (symbol.Value.Type == "computed") { ComputedSymbol sym = (ComputedSymbol)symbol.Value; bool value = CppStyleEvaluatorDefinition.EvaluateFromString(sym.Value, rootVariableCollection); bool currentValue; stable &= computed.TryGetValue(symbol.Key, out currentValue) && currentValue == value; rootVariableCollection[symbol.Key] = value; computed[symbol.Key] = value; } } } foreach (ExtendedFileSource source in _simpleConfigModel.Sources) { List <string> includePattern = JTokenToCollection(source.Include, SourceFile, new[] { "**/*" }).ToList(); List <string> excludePattern = JTokenToCollection(source.Exclude, SourceFile, new[] { "/[Bb]in/", "/[Oo]bj/", ".netnew.json", "**/*.filelist" }).ToList(); List <string> copyOnlyPattern = JTokenToCollection(source.CopyOnly, SourceFile, new[] { "**/node_modules/**/*" }).ToList(); if (source.Modifiers != null) { foreach (SourceModifier modifier in source.Modifiers) { if (CppStyleEvaluatorDefinition.EvaluateFromString(modifier.Condition, rootVariableCollection)) { includePattern.AddRange(JTokenToCollection(modifier.Include, SourceFile, new string[0])); excludePattern.AddRange(JTokenToCollection(modifier.Exclude, SourceFile, new string[0])); copyOnlyPattern.AddRange(JTokenToCollection(modifier.CopyOnly, SourceFile, new string[0])); } } } Dictionary <string, string> renames = new Dictionary <string, string>(); // Console.WriteLine(_simpleConfigModel.NameParameter); string val; if (parameters.ParameterValues.TryGetValue(_simpleConfigModel.NameParameter, out val)) { foreach (ITemplateSourceEntry entry in configFile.Parent.EnumerateFileSystemInfos("*", SearchOption.AllDirectories)) { string tmpltRel = entry.PathRelativeTo(configFile.Parent); string outRel = tmpltRel.Replace(_simpleConfigModel.SourceName, val); renames[tmpltRel] = outRel; // Console.WriteLine($"Mapping {tmpltRel} -> {outRel}"); } } sources.Add(new FileSource { CopyOnly = copyOnlyPattern.ToArray(), Exclude = excludePattern.ToArray(), Include = includePattern.ToArray(), Source = source.Source ?? "./", Target = source.Target ?? "./", Rename = renames }); } _sources = sources; }
public IRunnableProjectConfig ReprocessWithParameters(IParameterSet parameters, VariableCollection rootVariableCollection, ITemplateSourceFile configFile, IOperationProvider[] providers) { return(_simpleConfigModel.ReprocessWithParameters(parameters, rootVariableCollection, configFile, providers)); }
public IRunnableProjectConfig ReprocessWithParameters(IParameterSet parameters, VariableCollection rootVariableCollection, ITemplateSourceFile configFile, IOperationProvider[] operations) { EvaluatedSimpleConfig config = new EvaluatedSimpleConfig(this); config.Evaluate(parameters, rootVariableCollection, configFile); return(config); }