Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use BIC_OVERWRITE when cping static/ too #21

Merged
merged 7 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ on:

env:
IMAGE_NAME: bic
PANDOC_VERSION: 2.17.0.1
BATS_VERSION: 1.5.0
PANDOC_VERSION: 3.2.1
BATS_VERSION: 1.11.0
Comment on lines +18 to +19
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's been a couple years...


jobs:
# Run tests.
# See also https://docs.docker.com/docker-hub/builds/automated-testing/
test:
# in sync with Dockerfile
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgot to bump this when I bumped the Dockerfile 🙃


steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM ubuntu:24.04

ENV PANDOC_VERSION=2.17.0.1
ENV PANDOC_VERSION=3.2.1

RUN apt-get update && apt-get install -y curl tzdata && \
curl -sL https://github.com/jgm/pandoc/releases/download/$PANDOC_VERSION/pandoc-$PANDOC_VERSION-1-amd64.deb -o pandoc-$PANDOC_VERSION.deb && \
Expand Down
9 changes: 7 additions & 2 deletions bic
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,12 @@ build_feed() {
build_static() {
[[ ! -d "${SRC_DIR}"/static ]] && return
log "Building /static -> /"
cp -a "${SRC_DIR}"/static/. "${DEST_DIR}"
# Archive by default
local args=(-a)
# And run interactively so as to not clobber files unless explicitly disabled
[[ -z "${BIC_OVERWRITE:-}" ]] && args+=(-i)
# shellcheck disable=SC2216
echo n | cp "${args[@]}" "${SRC_DIR}"/static/. "${DEST_DIR}"
Comment on lines +373 to +374
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't love this, but wasn't able to immediately come up with another way to accomplish it https://www.shellcheck.net/wiki/SC2216

}

build() {
Expand All @@ -376,7 +381,7 @@ build() {
[[ "${SRC_DIR}" != "${__dir}" ]] && [[ -f "${SRC_DIR}"/.env ]] && src "${SRC_DIR}"/.env && log "Custom .env sourced"
DEST_DIR="${SRC_DIR}/${BUILD_DIR:-build}"
log "Using source directory: ${SRC_DIR}"
[[ -n "${BIC_OVERWRITE:-}" ]] && set +o noclobber && log "Disabled 'noclobber' redirection overwrite protection"
[[ -n "${BIC_OVERWRITE:-}" ]] && set +o noclobber && warn "Disabled overwrite protection"

# File system: what's missing and do we need to bail?
[[ -d "${SRC_DIR}"/pages && -f "${SRC_DIR}"/page.html ]] || warn "pages/ + page.html for pages"
Expand Down
12 changes: 7 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ generated site.

## Structure

For a fully-featured example, view the demo source code: <https://github.com/Pinjasaur/bic-example>
For a fully-featured example, view the [demo] source code: <https://github.com/Pinjasaur/bic-example>

```plaintext
$ tree -F --dirsfirst
Expand Down Expand Up @@ -169,10 +169,11 @@ There is an order-of-operations for how files are built, as follows:
- `feed.rss`
- static e.g. `static/*` &rarr; `/`

If you're not careful, it's possible you could overwrite an existing
file e.g. `pages/test.md` and `posts/999-test.md` both map to `/test.html`. `bic`
uses the Bash builtin `noclobber` e.g. `set -o noclobber` to help prevent these
situations. This can be disabled by setting `BIC_OVERWRITE`.
If you're not careful, it's possible you could overwrite an existing file e.g.
`pages/test.md` and `posts/999-test.md` both map to `/test.html`. `bic` uses the
Bash builtin `noclobber` e.g. `set -o noclobber` along with `--interactive` for
`cp` to protect against these scenarios. This can be disabled by setting
`BIC_OVERWRITE`. You've been warned.

## Showcase

Expand All @@ -181,6 +182,7 @@ situations. This can be disabled by setting `BIC_OVERWRITE`.
- the demo: <https://demo.bic.sh/>
- Mitch's blog: <https://fossen.dev/>
- Evan's blog: <https://evanhstanton.github.io/>
- you?

## Support

Expand Down
31 changes: 21 additions & 10 deletions lib/mo
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ mo::debugShowState() {
if [[ -z "${MO_DEBUG:-}" ]]; then
return
fi

local moState moTemp moIndex moDots

mo::escape moTemp "$MO_OPEN_DELIMITER"
Expand All @@ -258,7 +258,7 @@ mo::debugShowState() {
moState="$moState parsed: $moDots$moTemp"

moDots=...

if [[ "${#MO_UNPARSED}" -le 20 ]]; then
moDots=
fi
Expand Down Expand Up @@ -341,7 +341,7 @@ mo::content() {
mo::debug "Will read content from stdin"
mo::contentFile moContent || return 1
fi

local "$moTarget" && mo::indirect "$moTarget" "$moContent"
}

Expand Down Expand Up @@ -665,7 +665,7 @@ mo::parseBlockArray() {
moCloseDelimiterAfter=$MO_CLOSE_DELIMITER
moArrayName=${moTokens[1]}
eval "moArrayIndexes=(\"\${!${moArrayName}[@]}\")"

if [[ "${#moArrayIndexes[@]}" -lt 1 ]]; then
#: No elements
if [[ "$moInvertBlock" == "true" ]]; then
Expand Down Expand Up @@ -1002,13 +1002,24 @@ mo::isArrayIndexValid() {
# Can not use logic like this in case invalid variable names are passed.
# [[ "${!1-a}" == "${!1-b}" ]]
#
# Using logic like this gives false positives.
# [[ -v "$a" ]]
#
# Declaring a variable is not the same as assigning the variable.
# export x
# declare -p x # Output: declare -x x
# export y=""
# declare -p y # Output: declare -x y=""
# unset z
# declare -p z # Error code 1 and output: bash: declare: z: not found
#
# Returns true (0) if the variable is set, 1 if the variable is unset.
mo::isVarSet() {
if ! declare -p "$1" &> /dev/null; then
return 1
if declare -p "$1" &> /dev/null && [[ -v "$1" ]]; then
return 0
fi

return 0
return 1
}


Expand Down Expand Up @@ -1587,7 +1598,7 @@ mo::getContentUntilClose() {
MO_UNPARSED=${MO_UNPARSED:1}
mo::getContentTrim moTemp
mo::getContentWithinTag moTemp "$MO_CLOSE_DELIMITER"

if [[ "${moTagStack[0]}" == "${moTemp[1]}" ]]; then
moResultTemp="$moResultTemp${moTemp[0]}"
moTagStack=("${moTagStack[@]:1}")
Expand Down Expand Up @@ -1674,7 +1685,7 @@ mo::tokensToString() {
# Returns nothing.
mo::getContentTrim() {
local moChar moResult

moChar=${MO_UNPARSED:0:1}
moResult=""

Expand Down Expand Up @@ -1977,7 +1988,7 @@ mo::tokenizeTagContentsSingleQuote() {

# Save the original command's path for usage later
MO_ORIGINAL_COMMAND="$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)/${BASH_SOURCE[0]##*/}"
MO_VERSION="3.0.6"
MO_VERSION="3.0.7"

# If sourced, load all functions.
# If executed, perform the actions as expected.
Expand Down
Empty file.
1 change: 1 addition & 0 deletions tests/overwrite-static/entry.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{{body}}}
Empty file.
1 change: 1 addition & 0 deletions tests/overwrite-static/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{{body}}}
3 changes: 3 additions & 0 deletions tests/overwrite-static/posts/999-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Test

post
1 change: 1 addition & 0 deletions tests/overwrite-static/static/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>static</p>
Empty file added tests/static/static/foo/bar/baz
Empty file.
23 changes: 22 additions & 1 deletion tests/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@

@test "bic copies static/ correctly" {
run ./bic tests/static
[[ -f tests/static/build/humans.txt ]]
[[ "${status}" == 0 ]]
[[ -f tests/static/build/humans.txt ]]
[[ -f tests/static/build/foo/bar/baz ]]
rm -rf tests/static/build
[[ ! -d tests/static/build ]]
}
Expand Down Expand Up @@ -75,6 +76,16 @@
[[ ! -d tests/overwrite/build ]]
}

@test "bic will not overwrite by default (static)" {
run ./bic tests/overwrite-static
[[ -f tests/overwrite-static/build/test.html ]]
[[ "${status}" != 0 ]] # errored out trying to overwrite test.html
run cat tests/overwrite-static/build/test.html
[[ "${lines[0]}" == "<p>post</p>" ]]
rm -rf tests/overwrite-static/build
[[ ! -d tests/overwrite-static/build ]]
}

@test "bic will overwrite if specified" {
BIC_OVERWRITE=1 run ./bic tests/overwrite
[[ -f tests/overwrite/build/test.html ]]
Expand All @@ -85,6 +96,16 @@
[[ ! -d tests/overwrite/build ]]
}

@test "bic will overwrite if specified (static)" {
BIC_OVERWRITE=1 run ./bic tests/overwrite-static
[[ -f tests/overwrite-static/build/test.html ]]
[[ "${status}" == 0 ]] # didn't error out
run cat tests/overwrite-static/build/test.html
[[ "${lines[0]}" == "<p>static</p>" ]]
rm -rf tests/overwrite-static/build
[[ ! -d tests/overwrite-static/build ]]
}

@test "bic will honor a custom \$BUILD_DIR" {
BUILD_DIR=_site run ./bic tests/index.html
[[ -f tests/index.html/_site/index.html ]]
Expand Down
Loading