headroom-0.3.1.0: License Header Manager

Copyright(c) 2019-2020 Vaclav Svejcar
LicenseBSD-3-Clause
Maintainervaclav.svejcar@gmail.com
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Headroom.FileSupport

Contents

Description

This module is the heart of Headroom as it contains functions for working with the license headers and the source code files.

Synopsis

File info extraction

extractFileInfo Source #

Arguments

:: FileType

type of the detected file

-> CtHeaderConfig

license header configuration

-> Maybe TemplateMeta

metadata extracted from template

-> Text

text used for detection

-> FileInfo

resulting file info

Extracts info about the processed file to be later used by the header detection/manipulation functions.

License header manipulation

addHeader Source #

Arguments

:: FileInfo

info about file where header is added

-> Text

text of the new header

-> Text

text of the file where to add the header

-> Text

resulting text with added header

Adds given header at position specified by the FileInfo. Does nothing if any header is already present, use replaceHeader if you need to override it.

dropHeader Source #

Arguments

:: FileInfo

info about the file from which the header will be dropped

-> Text

text of the file from which to drop the header

-> Text

resulting text with dropped header

Drops header at position specified by the FileInfo from the given text. Does nothing if no header is present.

replaceHeader Source #

Arguments

:: FileInfo

info about the file in which to replace the header

-> Text

text of the new header

-> Text

text of the file where to replace the header

-> Text

resulting text with replaced header

Replaces existing header at position specified by the FileInfo in the given text. Basically combines addHeader with dropHeader. If no header is present, then the given one is added to the text.

License header detection

findHeader Source #

Arguments

:: CtHeaderConfig

appropriate header configuration

-> Text

text in which to detect the header

-> Maybe (Int, Int)

header position (startLine, endLine)

Finds header position in given text, where position is represented by line number of first and last line of the header (numbered from zero). Based on the HeaderSyntax specified in given HeaderConfig, this function delegates its work to either findBlockHeader or findLineHeader.

>>> :set -XFlexibleContexts
>>> :set -XTypeFamilies
>>> let hc = HeaderConfig ["hs"] 0 0 [] [] (BlockComment "{-" "-}")
>>> findHeader hc "foo\nbar\n{- HEADER -}\nbaz"
Just (2,2)

findBlockHeader Source #

Arguments

:: Text

starting pattern (e.g. {- or /*)

-> Text

ending pattern (e.g. -} or */)

-> [Text]

lines of text in which to detect the header

-> Int

line number offset (adds to resulting position)

-> Maybe (Int, Int)

header position (startLine + offset, endLine + offset)

Finds header in the form of multi-line comment syntax, which is delimited with starting and ending pattern.

>>> findBlockHeader "{-" "-}" ["", "{- HEADER -}", "", ""] 0
Just (1,1)

findLineHeader Source #

Arguments

:: Text

prefix pattern (e.g. -- or //)

-> [Text]

lines of text in which to detect the header

-> Int

line number offset (adds to resulting position)

-> Maybe (Int, Int)

header position (startLine + offset, endLine + offset)

Finds header in the form of single-line comment syntax, which is delimited with the prefix pattern.

>>> findLineHeader "--" ["", "a", "-- first", "-- second", "foo"] 0
Just (2,3)

firstMatching Source #

Arguments

:: [Regex]

regex used for matching

-> [Text]

input lines

-> Maybe Int

matching line number

Finds very first line that matches the given regex (numbered from zero).

>>> import Headroom.Data.Regex (re)
>>> :set -XQuasiQuotes
>>> firstMatching [[re|^foo|]] ["some text", "foo bar", "foo baz", "last"]
Just 1

lastMatching Source #

Arguments

:: [Regex]

regex used for matching

-> [Text]

input lines

-> Maybe Int

matching line number

Finds very last line that matches the given regex (numbered from zero).

>>> import Headroom.Data.Regex (re)
>>> :set -XQuasiQuotes
>>> lastMatching [[re|^foo|]] ["some text", "foo bar", "foo baz", "last"]
Just 2

splitInput Source #

Arguments

:: [Regex]

patterns for first split

-> [Regex]

patterns for second split

-> Text

text to split

-> ([Text], [Text], [Text])

result lines as ([before1stSplit], [middle], [after2ndSplit])

Splits input lines into three parts:

  1. list of all lines located before the very last occurence of one of the conditions from the first condition list
  2. list of all lines between the first and last lists
  3. list of all lines located after the very first occurence of one of the conditions from the second condition list

If both first and second patterns are empty, then all lines are returned in the middle list.

>>> import Headroom.Data.Regex (re)
>>> :set -XQuasiQuotes
>>> splitInput [[re|->|]] [[re|<-|]] "text\n->\nRESULT\n<-\nfoo"
(["text","->"],["RESULT"],["<-","foo"])
>>> splitInput [] [[re|<-|]] "text\n->\nRESULT\n<-\nfoo"
([],["text","->","RESULT"],["<-","foo"])
>>> splitInput [] [] "one\ntwo"
([],["one","two"],[])