Coder Social home page Coder Social logo

Here documents don't work about bashly HOT 6 CLOSED

dusanbrankov avatar dusanbrankov commented on June 20, 2024
Here documents don't work

from bashly.

Comments (6)

DannyBen avatar DannyBen commented on June 20, 2024 1

Bashly cares about generating properly indented, human readable code. Heredoc had to be sacrificed.

This was discussed in a few other issues.

There are a couple of possible workarounds in the Examples section.

I am open to ideas on how to better handle this, assuming there is an elegant way.

from bashly.

DannyBen avatar DannyBen commented on June 20, 2024 1

Sure thing. Since this bothers more than a handful of people, I will see if there is anything to be done to better support this.

from bashly.

dusanbrankov avatar dusanbrankov commented on June 20, 2024

Thanks for the quick reply and the links.

I was looking for similar issues but couldn't find anything directly related to Bashly. Sorry for the duplication.

Maybe it's just me, but I find both existing solutions rather clumsy, especially the one with BASHLY_TAB_INDENT, which requires the use of Unicode-specific spaces instead of "normal" ones.

In the meantime, I've written a script that allows me to write here documents in a more natural way:

cat <<EOF
line one
line two
EOF

The following variants also work:

cat << EOF
line one
line two
EOF

cat << EOF > output.txt
hello
there
EOF

If I want to preserve whitespace, I can use <<- (probably it should be the other way around):

cat <<-EOF
line one
    line two (indented)
EOF

And here's the script to make it work:

#!/usr/bin/env bash

executable="$(awk 'NR == 1 {sub(/name:\s*/, ""); print; exit}' src/bashly.yml)"

bashly generate

# Remove leading spaces from end token
sed -i -r 's/^\s+(EOF)/\1/' "$executable"

# Remove leading spaces from docstring (not tabs)
while read -r num line; do
    if [[ -n $line ]]; then
        sed -i -r "$num s/^ +(.+)/\1/" "$executable"
    fi
done <<EOF
$(awk -v lines="$(wc -l "$executable" | awk '{print $1}')" '{\
    if (/^EOF/ || /<<- *EOF/) exit; \
    if (/<< *EOF/) lines = NR; \
    if (NR > lines) print NR, $0; \
}' "$executable")
EOF

The limitation I have with this is that I have to use EOF, but this could be solved with an environment variable, e.g. HEREDOC_TOKEN=CUSTOM.

Also, I haven't thought about \EOF and 'EOF' yet. There are probably other possible forms that I haven't covered, but I think this is a good starting point.

from bashly.

DannyBen avatar DannyBen commented on June 20, 2024

Sorry for the duplication.

It's ok.

Maybe it's just me, but I find both existing solutions rather clumsy

Not just you. I agree the workarounds are clumsy. But so is the implementation of heredoc in bash itself.
In other languages, there is a way to keep the EOF marker indented.

I am sure there is a way to make bashly more heredoc-friendly, I just haven't found it yet... :)

from bashly.

dusanbrankov avatar dusanbrankov commented on June 20, 2024

Not just you. I agree the workarounds are clumsy. But so is the implementation of heredoc in bash itself. In other languages, there is a way to keep the EOF marker indented.

Totally agree with you. Bash is just bad in so many ways :)

I will close this for now as this is a known issue with existing workarounds.

Thanks again for your help.

Best,
Dušan

from bashly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.