예제 #1
0
        internal static Include Include(string baseDirectory, string culture, string fileName, bool useCulture, IDictionary replace)
        {
            baseDirectory = PSDocumentOption.GetRootedPath(baseDirectory);
            var absolutePath = Path.IsPathRooted(fileName) ? fileName : Path.Combine(baseDirectory, (useCulture ? culture : string.Empty), fileName);
            var result       = new Include
            {
                Path = absolutePath
            };

            if (result.Exists)
            {
                var text = File.ReadAllText(absolutePath);
                if (replace != null && replace.Count > 0)
                {
                    foreach (var key in replace.Keys)
                    {
                        var k = key?.ToString();
                        var v = replace[key]?.ToString();
                        text = text.Replace(k, v);
                    }
                }
                result.Content = text;
            }
            return(result);
        }
예제 #2
0
 internal DocumentResult(DocumentContext documentContext, string markdown)
 {
     _Markdown    = markdown;
     InstanceName = documentContext.InstanceName;
     Culture      = documentContext.Culture;
     OutputPath   = PSDocumentOption.GetRootedPath(documentContext.OutputPath);
     FullName     = GetFullName();
     Metadata     = documentContext.Metadata;
     Data         = documentContext.Data;
 }
예제 #3
0
        public override void Process(RunspaceContext context, IEnumerable input)
        {
            var culture    = context.Builder.Document.Culture;
            var rootedPath = PSDocumentOption.GetRootedPath(context.Pipeline.Option.Output.Path);
            var outputPath = !string.IsNullOrEmpty(culture) && context.Pipeline.Option.Output?.Culture?.Length > 1 ?
                             Path.Combine(rootedPath, culture) : rootedPath;

            context.DocumentContext.InstanceName = context.Builder.Document.Name;
            context.DocumentContext.OutputPath   = outputPath;
        }
예제 #4
0
        private void ProcessTemplateFile(string templateFile)
        {
            try
            {
                var rootedTemplateFile = PSDocumentOption.GetRootedPath(templateFile);

                // Check if metadata property exists
                if (!TryTemplateFile(rootedTemplateFile, out string version, out JObject metadata))
                {
                    return;
                }

                var templateLink = new TemplateLink(rootedTemplateFile, version);

                // Populate remaining properties
                if (TryStringProperty(metadata, PROPERTYNAME_NAME, out string name))
                {
                    templateLink.Name = name;
                }

                if (TryStringProperty(metadata, PROPERTYNAME_DESCRIPTION, out string description))
                {
                    templateLink.Description = description;
                }

                if (TryMetadata(metadata, out Hashtable meta))
                {
                    templateLink.Metadata = meta;
                }

                // var pso = PSObject.AsPSObject(templateLink);
                // var instanceNameProp = new PSNoteProperty("InstanceName", string.Concat(templateLink.Name, "_v", templateLink.Version.Major));
                // pso.Members.Add(new PSMemberSet("PSDocs", new PSMemberInfo[] { instanceNameProp }));

                Writer.WriteObject(templateLink, false);
            }
            catch (InvalidOperationException ex)
            {
                Writer.WriteError(ex, nameof(InvalidOperationException), ErrorCategory.InvalidOperation, templateFile);
            }
            catch (FileNotFoundException ex)
            {
                Writer.WriteError(ex, nameof(FileNotFoundException), ErrorCategory.ObjectNotFound, templateFile);
            }
            catch (PipelineException ex)
            {
                Writer.WriteError(ex, nameof(PipelineException), ErrorCategory.WriteError, templateFile);
            }
        }
예제 #5
0
 internal InputFileInfo(string basePath, string path)
 {
     if (path.IsUri())
     {
         FullName = path;
         IsUrl    = true;
         return;
     }
     path          = PSDocumentOption.GetRootedPath(path);
     FullName      = path;
     BasePath      = basePath;
     Name          = System.IO.Path.GetFileName(path);
     Extension     = System.IO.Path.GetExtension(path);
     DirectoryName = System.IO.Path.GetDirectoryName(path);
     Path          = ExpressionHelpers.NormalizePath(basePath, FullName);
     _TargetType   = string.IsNullOrEmpty(Extension) ? System.IO.Path.GetFileNameWithoutExtension(path) : Extension;
 }
예제 #6
0
        public static Include Include(string baseDirectory, string culture, string fileName, bool useCulture)
        {
            baseDirectory = PSDocumentOption.GetRootedPath(baseDirectory);
            var absolutePath = Path.IsPathRooted(fileName) ? fileName : Path.Combine(baseDirectory, (useCulture ? culture : string.Empty), fileName);

            if (!File.Exists(absolutePath))
            {
                throw new FileNotFoundException(PSDocsResources.IncludeNotFound, absolutePath);
            }

            var text = File.ReadAllText(absolutePath);

            return(new Include
            {
                Path = absolutePath,
                Content = text,
            });
        }
예제 #7
0
        private SourceFile[] GetFiles(string path, string helpPath, string moduleName = null)
        {
            var result     = new List <SourceFile>();
            var rootedPath = PSDocumentOption.GetRootedPath(path);
            var extension  = Path.GetExtension(rootedPath);

            if (extension == ".ps1" || extension == ".yaml" || extension == ".yml")
            {
                if (!File.Exists(rootedPath))
                {
                    throw new FileNotFoundException(PSDocsResources.SourceNotFound, rootedPath);
                }

                if (helpPath == null)
                {
                    helpPath = Path.GetDirectoryName(rootedPath);
                }

                result.Add(new SourceFile(rootedPath, null, GetSourceType(rootedPath), helpPath));
            }
            else if (System.IO.Directory.Exists(rootedPath))
            {
                var files = System.IO.Directory.EnumerateFiles(rootedPath, "*.Doc.*", SearchOption.AllDirectories);
                foreach (var file in files)
                {
                    if (Include(file))
                    {
                        if (helpPath == null)
                        {
                            helpPath = Path.GetDirectoryName(file);
                        }

                        result.Add(new SourceFile(file, moduleName, GetSourceType(file), helpPath));
                    }
                }
            }
            else
            {
                return(null);
            }
            return(result.ToArray());
        }
예제 #8
0
        private static void WriteToFile(IDocumentResult result, PSDocumentOption option, IPipelineWriter writer, ShouldProcess shouldProcess)
        {
            var rootedPath = PSDocumentOption.GetRootedPath(option.Output.Path);
            var filePath   = !string.IsNullOrEmpty(result.Culture) && option.Output?.Culture?.Length > 1 ?
                             Path.Combine(rootedPath, result.Culture, result.Name) : Path.Combine(rootedPath, result.Name);
            var parentPath = Directory.GetParent(filePath);

            if (!parentPath.Exists && shouldProcess(target: parentPath.FullName, action: PSDocsResources.ShouldCreatePath))
            {
                Directory.CreateDirectory(path: parentPath.FullName);
            }
            if (shouldProcess(target: rootedPath, action: PSDocsResources.ShouldWriteFile))
            {
                var encoding = GetEncoding(option.Markdown.Encoding.Value);
                File.WriteAllText(filePath, result.ToString(), encoding);

                // Write file info instead
                var fileInfo = new FileInfo(filePath);
                writer.WriteObject(fileInfo, false);
            }
        }
예제 #9
0
        private static void WriteToFile(IDocumentResult result, PSDocumentOption option, IPipelineWriter writer, ShouldProcess shouldProcess)
        {
            // Calculate paths
            var fileName   = string.Concat(result.InstanceName, result.Extension);
            var outputPath = PSDocumentOption.GetRootedPath(result.OutputPath);
            var filePath   = Path.Combine(outputPath, fileName);
            var parentPath = Directory.GetParent(filePath);

            if (!parentPath.Exists && shouldProcess(target: parentPath.FullName, action: PSDocsResources.ShouldCreatePath))
            {
                Directory.CreateDirectory(path: parentPath.FullName);
            }

            if (shouldProcess(target: outputPath, action: PSDocsResources.ShouldWriteFile))
            {
                var encoding = GetEncoding(option.Markdown.Encoding.Value);
                File.WriteAllText(filePath, result.ToString(), encoding);

                // Write file info instead
                var fileInfo = new FileInfo(filePath);
                writer.WriteObject(fileInfo, false);
            }
        }