Skip to content

Adding csplit support for GNU/Linux distros #50

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Adding csplit support for GNU/Linux distros #50

wants to merge 3 commits into from

Conversation

ealvar3z
Copy link

@ealvar3z ealvar3z commented Aug 2, 2020

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
Copy link
Owner

Dhghomon commented Aug 3, 2020

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
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
Copy link
Author

ealvar3z commented Aug 4, 2020

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
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
Copy link
Author

ealvar3z commented Aug 5, 2020

@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
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.

}

# Moves generated chapters into src directory
function moveChaptersToSrcDir(){
for f in Chapter_*.md; do
mv $f src/$f
mv "$f" src/"$f"
Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏼

Comment on lines -52 to +70
mdBook build && mdBook serve --open
mdbook build && mdbook serve --open
Copy link
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

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
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.

# Get gcsplit via homebrew on mac: brew install coreutils
# Get csplit via homebrew on mac: brew install coreutils
Copy link
Contributor

@nisrulz nisrulz Jan 5, 2021

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 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
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