Skip to content

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
smallhadroncollider committed Jan 17, 2019
2 parents 5b9643e + 4958488 commit 7016350
Show file tree
Hide file tree
Showing 40 changed files with 1,844 additions and 0 deletions.
73 changes: 73 additions & 0 deletions .bin/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
resolver="lts-13.2"
ghcv="8.6.3"

branch=$(git rev-parse --abbrev-ref HEAD)

if [ $branch != "master" ]; then
echo "\033[0;31mMust be on master branch\033[0m"
exit
fi

if [ ! -d ".stack-work" ]; then
echo "\033[0;31mMust be run in project root\033[0m"
exit
fi

if [ -z "$1" ]; then
echo "\033[0;31mVersion number required\033[0m"
exit
fi

# make sure latest changes pushed to github
git push --all && git push --tags

# clean out previous build attempts
rm -rf .stack-work/install

# sort out releases directory
rm -rf "releases/$1/brok"
mkdir -p "releases/$1/brok"

# Mac
stack build --ghc-options -O3

tar -czvf "releases/$1/brok-$1_x86-64-mac.tar.gz" --directory=".stack-work/install/x86_64-osx/$resolver/$ghcv/bin" "brok"


# Linux
stack docker pull
stack build --docker --ghc-options -O3

LINUX_DIR=$(ls .stack-work/install | grep linux)

tar -czvf "releases/$1/brok-$1_x86-64-linux.tar.gz" --directory=".stack-work/install/$LINUX_DIR/$resolver/$ghcv/bin" "brok"

mkdir -p "releases/$1/brok/DEBIAN"
mkdir -p "releases/$1/brok/usr/local/bin"

cp ".stack-work/install/$LINUX_DIR/$resolver/$ghcv/bin/brok" "releases/$1/brok/usr/local/bin"

echo "Package: brok
Version: $1
Maintainer: Mark Wales
Architecture: amd64
Description: Finds broken links in text files" > "releases/$1/brok/DEBIAN/control"

docker run -v "$PWD/releases/$1":/usr/src/app -w /usr/src/app debian dpkg-deb --build brok

mv "releases/$1/brok.deb" "releases/$1/brok-$1_x86-64-linux.deb"

# Release Template
rm -rf "releases/$1/brok"

open "releases/$1"
open "https://github.com/smallhadroncollider/brok/releases/new"

echo "-

### Installation

- Mac/Linux: download binary and place it in a directory in your \`\$PATH\` (e.g. \`/usr/local/bin\`)
- Debian (including Ubuntu): download the \`.deb\` file and run \`dpkg -i brok-$1_x86-64-linux.deb\`" | pbcopy

echo "Release info copied to clipboard"
1 change: 1 addition & 0 deletions .bin/ghci
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stack ghci
1 change: 1 addition & 0 deletions .bin/ghcid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
~/.local/bin/ghcid --command "stack ghci brok:lib brok:test:brok-test --ghci-options=-fobject-code" --test "main"
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PATH_add .bin
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.stack-work/
brok.cabal
/.brokdb
*~
3 changes: 3 additions & 0 deletions .hindent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
indent-size: 4
line-length: 100
force-trailing-newline: false
16 changes: 16 additions & 0 deletions .stylish-haskell.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
steps:
- simple_align:
cases: true
records: true

- imports:
align: group

- tabs:
spaces: 4

columns: 100

language_extensions:
- OverloadedStrings
- NoImplicitPrelude
30 changes: 30 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright Small Hadron Collider / Mark Wales (c) 2019

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Small Hadron Collider / Mark Wales nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# brök

Find broken links in text documents

![Demo](https://files.smallhadroncollider.com/brok-0.1.gif)

Similar idea to [awesome_bot](https://github.com/dkhamsing/awesome_bot), but with different output options.

Currently only supports `http://` and `https://` prefixed URLs

## Install

```bash
stack build && stack install
```

## Usage

### Basic Usage

Check all links in a single text file:

```bash
brok test.md
```

Or in multiple files:

```bash
brok test.md links.tex
```

If you're using this as part of a test suite, you probably only need the errors:

```
brok text.md links.tex > /dev/null
```

### Options

#### Cache

By default brök will cache successes for a day. It will always recheck errors.

If you want to adjust the cache length, you can enter the number of seconds after which the cache invalidates:

```bash
# cache for a week
brok --cache 604800 test.md links.tex
```

#### Ignore URLs

You can tell brök to ignore URLs with specified prefixes:

```bash
# ignore facebook and amazon URLS
brok --ignore "http://facebook.com" "http://amazon.com" test.md links.tex
```

#### Interval

By default brök waits for 100ms between URL checks. You can change the delay:

```bash
# wait for 1 second between checks
brok --interval 1000 test.md links.tex
```
7 changes: 7 additions & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Main where

import Brok (brok)
import ClassyPrelude (IO)

main :: IO ()
main = brok
65 changes: 65 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: brok
version: 0.1.0.0
github: "smallhadroncollider/brok"
license: BSD3
author: "Small Hadron Collider"
maintainer: "[email protected]"
copyright: "2019 Small Hadron Collider"

extra-source-files:
- README.md

# Metadata used when publishing your package
synopsis: Finds broken links in text files
# category: Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/smallhadroncollider/brok#readme>

default-extensions:
- OverloadedStrings
- NoImplicitPrelude

dependencies:
- base >= 4.7 && < 5
- classy-prelude
- file-embed

library:
source-dirs: src
dependencies:
- ansi-terminal
- attoparsec
- directory
- http-conduit
- http-client
- text
- time

executables:
brok:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- brok

tests:
brok-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- brok
- tasty
- tasty-discover
- tasty-hunit
- tasty-expected-failure
66 changes: 66 additions & 0 deletions src/Brok.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

module Brok
( brok
) where

import ClassyPrelude

import Data.FileEmbed (embedFile)
import System.Exit (exitFailure, exitSuccess)

import Brok.IO.CLI (errorMessage, header, replace)
import Brok.IO.DB (getCached, setCached)
import Brok.IO.Document (readContent)
import Brok.IO.Http (check)
import Brok.IO.Output (output)
import Brok.Options (parse)
import Brok.Parser.Links (links)
import qualified Brok.Types.Config as C (Config, cache, files, ignore, interval)
import Brok.Types.Link (getURL, isSuccess)
import Brok.Types.Next (Next (..))
import Brok.Types.Result (cachedLinks, ignoredLinks, justLinks, linkIOMap, parseLinks,
pathToResult)

go :: C.Config -> IO ()
go config
-- read files
= do
content <- sequence (readContent . pathToResult <$> C.files config)
-- find links in each file
let parsed = parseLinks links <$> content
-- check cached successes
cached <- getCached (C.cache config)
let uncached = cachedLinks cached . ignoredLinks (C.ignore config) <$> parsed
-- check links in each file
header "Checking URLs"
putStrLn ""
checked <- sequence (linkIOMap (check (C.interval config)) <$> uncached)
replace "Fetching complete"
-- display results
putStrLn ""
header "Results"
anyErrors <- sequence $ output <$> checked
-- cache successes
setCached (C.cache config) $ getURL <$> filter isSuccess (concat (justLinks <$> checked))
-- exit with appropriate status code
if foldl' (||) False anyErrors
then void exitFailure
else void exitSuccess

showHelp :: IO ()
showHelp = putStr $ decodeUtf8 $(embedFile "template/usage.txt")

-- entry point
brok :: IO ()
brok = do
config <- parse <$> getArgs
case config of
Right (Continue cnf) -> go cnf
Right Help -> showHelp
Left _ -> do
errorMessage "Invalid format"
showHelp
void exitFailure
Loading

0 comments on commit 7016350

Please sign in to comment.