Skip to content

Adding csplit support for GNU/Linux distros - #50

Open
ealvar3z wants to merge 3 commits into
Dhghomon:masterfrom
ealvar3z:master
Open

ealvar3z wants to merge 3 commits into
Dhghomon:masterfrom
ealvar3z:master

Conversation

@ealvar3z

@ealvar3z ealvar3z commented Aug 2, 2020

Copy link
Copy Markdown

I made this change originally just to get it working on my machine. However, this refactored version could be beneficial to both GNU/Linux and OSX users.

** Didn't have time tonight, but if you prefer, I can also figure out WSL support.

Thank you for the book. I am enjoying it greatly.

@Dhghomon

Dhghomon commented Aug 3, 2020

Copy link
Copy Markdown
Owner

Hi @3rly , looks good to me but then again I don't know anything about Bash so my "looks good to me" really doesn't mean much! @AlexanderWillner , do you see any problems with merging this PR?

@AlexanderWillner

Copy link
Copy Markdown
Contributor

The change 1df7a35 seems wrong to me:

  1. Looking for csplit and suggest to install gcsplit
  2. I believe csplit is part of macOS by default (however, the linux and freebsd versions do not work (illegal option -- -) when I execute them manually)
  3. In the OS check gcsplit is being used on macOS

@ealvar3z

ealvar3z commented Aug 4, 2020

Copy link
Copy Markdown
Author

When I get home, I’ll investigate the why that function call it isn’t working manually for you (it passed all my tests on locally FreeBSD, Arch and a Debian-based VMs). Moreover, that command is un-modified from the original script. My additions were simply the case/switch statements.

@AlexanderWillner

Copy link
Copy Markdown
Contributor

Just to clarify: I just tested the Linux and BSD commands on MacOS. As say seem not to work, the switch statement makes sense.

@ealvar3z

ealvar3z commented Aug 5, 2020

Copy link
Copy Markdown
Author

@AlexanderWillner If you are using the coreutils package from Homebrew, there will be a conflict per the package maintainer's formula: source lines[44:80] respectively.

Commands also provided by macOS have been installed with the prefix "g".
If you need to use these commands with their normal names, you
can add a "gnubin" directory to your PATH from your bashrc like:
    PATH="$(brew --prefix)/opt/coreutils/libexec/gnubin:$PATH"

@AlexanderWillner

Copy link
Copy Markdown
Contributor

I think csplit is installed on macOS by default, however, that version needs other parameters than Linux and BSD. So testing for gcsplit, using it / suggesting installing it, make sense.

Comment thread createBookFromReadme.sh
function moveChaptersToSrcDir(){
for f in Chapter_*.md; do
mv $f src/$f
mv "$f" src/"$f"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍🏼

Comment thread createBookFromReadme.sh
Comment on lines -52 to +70
mdBook build && mdBook serve --open
mdbook build && mdbook serve --open

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This was fixed in PR #105

Comment thread createBookFromReadme.sh
# Check for binaries
function checkEnvironment(){
type gcsplit >/dev/null 2>&1 || { echo "Install 'gcsplit' first (e.g. via 'brew install coreutils')." >&2 && exit 1 ; }
type csplit >/dev/null 2>&1 || { echo "Install gcsplit' first (e.g. via 'brew install coreutils')." >&2 && exit 1 ; }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wouldn't remove the gcsplit line.

Because on macOS gcsplit is used and it cannot be caught in Environment.

What you want to do is check for os type here using an if-else and then execute the check line for that os type.

Comment thread createBookFromReadme.sh
# Splits the Readme.md file based on the header in markdown and creates chapters
# Note:
# Get gcsplit via homebrew on mac: brew install coreutils
# Get csplit via homebrew on mac: brew install coreutils

@nisrulz nisrulz Jan 5, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is again wrong. On macOS you are using the gcsplit still on line 32 in this file. So saying csplit is being installed here is wrong.

You might want to write this:

#   For macOS: Get gcsplit via homebrew, cmd: brew install coreutils
#   For Linux: Get csplit via distro's package manager

Comment thread createBookFromReadme.sh
Comment on lines +23 to +41
OS_NAME=$(uname | tr '[:upper:]' '[:lower:]')

case $OS_NAME in
linux*)
OS_NAME='linux'
csplit --prefix='Chapter_' --suffix-format='%d.md' --elide-empty-files README.md '/^## /' '{*}' -q
;;
darwin*)
OS_NAME='osx'
gcsplit --prefix='Chapter_' --suffix-format='%d.md' --elide-empty-files README.md '/^## /' '{*}' -q
;;
freebsd*)
OS_NAME='freebsd'
csplit --prefix='Chapter_' --suffix-format='%d.md' --elide-empty-files README.md '/^## /' '{*}' -q
;;
*)
OS_NAME=notset
;;
esac

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I personally donot prefer the switch statements in bash scripts. They are hard to read compared to simpler if-elif statements.

You could have done something like below and executed the specific command:

if [[ "$OSTYPE" == "linux-gnu" ]]; then
    # ...
elif [[ "$OSTYPE" == "darwin"* ]]; then
    # Mac OSX
elif [[ "$OSTYPE" == "cygwin" ]]; then
    # POSIX compatibility layer and Linux environment emulation for Windows
elif [[ "$OSTYPE" == "msys" ]]; then
    # Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
elif [[ "$OSTYPE" == "freebsd"* ]]; then
    #FreeBSD
else
    # Unknown.
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants