Skip to content

Commit

Permalink
Add ws command line interface. (#23)
Browse files Browse the repository at this point in the history
* Add ws command for Wordsmith command line interface.

* Add alpine assets builder.

* Remove misusage OptionParser missing_options

* Fix PKG_NAME

* Add ws pipe examples

* Update ws usage in README.md

* Refactor use STDIN.info.type.pipe? to check pipe

* Update README.md

Co-authored-by: Jeremy Woertink <jeremywoertink@gmail.com>

* Update README.md

Co-authored-by: Jeremy Woertink <jeremywoertink@gmail.com>

* Update src/ws.cr

Co-authored-by: Jeremy Woertink <jeremywoertink@gmail.com>

* Fix bugs in alpline assets workflow

* Try improve banner usage for ws

* Bump version to 0.3.2

* Add workflow for build GNU X86_64 static binary

* Add workflow for build static binary for Apple Darwin.

* Update .github/workflows/apple_darwin_release.yml

Co-authored-by: Jeremy Woertink <jeremywoertink@gmail.com>

* Revert version changes and add authors in shard.yml

Co-authored-by: Jeremy Woertink <jeremywoertink@gmail.com>
  • Loading branch information
zw963 and jwoertink authored Aug 31, 2022
1 parent aca338c commit 438eef7
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 2 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/alpine_x86_64_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
on:
push:
tags:
- "v*.*.*"

jobs:
build:
runs-on: ubuntu-latest
container:
image: crystallang/crystal:latest-alpine
steps:
- name: Cache shards
uses: actions/cache@v2
with:
path: ~/.cache/shards
key: ${{ runner.os }}-shards-${{ hashFiles('shard.yml') }}
restore-keys: ${{ runner.os }}-shards-
- name: Download source
uses: actions/checkout@v2
- name: Install shards
run: shards check || shards install
- name: Check formatting
run: crystal tool format --check
- name: Run tests
run: crystal spec --order=random --error-on-warnings
- name: package information
run: |
echo "BINARY_NAME=ws" >> $GITHUB_ENV
echo "PKG_ARCH=x86_64" >> $GITHUB_ENV
echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: release binary
id: release
run: |
shards build --production --release --progress --static --no-debug --link-flags="-s -Wl,-z,relro,-z,now"
ASSERT_NAME=${BINARY_NAME}-${RELEASE_VERSION}-${PKG_ARCH}-unknown-linux-musl.tar.gz
tar zcf ${ASSERT_NAME} bin/${BINARY_NAME} LICENSE
echo ::set-output name=ASSERT_NAME::${ASSERT_NAME}
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
${{ steps.release.outputs.ASSERT_NAME }}
43 changes: 43 additions & 0 deletions .github/workflows/apple_darwin_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
on:
push:
tags:
- "v*.*.*"

jobs:
build:
runs-on: macos-latest
steps:
- name: Cache shards
uses: actions/cache@v2
with:
path: ~/.cache/shards
key: ${{ runner.os }}-shards-${{ hashFiles('shard.yml') }}
restore-keys: ${{ runner.os }}-shards-
- name: Download source
uses: actions/checkout@v2
- name: Install Crystal
uses: crystal-lang/install-crystal@v1
- name: Install shards
run: shards check || shards install
- name: Check formatting
run: crystal tool format --check
- name: Run tests
run: crystal spec --order=random --error-on-warnings
- name: package information
run: |
echo "BINARY_NAME=ws" >> $GITHUB_ENV
echo "PKG_ARCH=x86_64" >> $GITHUB_ENV
echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: release binary
id: release
run: |
shards build --production --release --progress --no-debug
ASSERT_NAME=${BINARY_NAME}-${RELEASE_VERSION}-${PKG_ARCH}-apple-darwin.tar.gz
tar zcf ${ASSERT_NAME} bin/${BINARY_NAME} LICENSE
echo ::set-output name=ASSERT_NAME::${ASSERT_NAME}
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
${{ steps.release.outputs.ASSERT_NAME }}
43 changes: 43 additions & 0 deletions .github/workflows/gnu_x86_64_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
on:
push:
tags:
- "v*.*.*"

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Cache shards
uses: actions/cache@v2
with:
path: ~/.cache/shards
key: ${{ runner.os }}-shards-${{ hashFiles('shard.yml') }}
restore-keys: ${{ runner.os }}-shards-
- name: Download source
uses: actions/checkout@v2
- name: Install Crystal
uses: crystal-lang/install-crystal@v1
- name: Install shards
run: shards check || shards install
- name: Check formatting
run: crystal tool format --check
- name: Run tests
run: crystal spec --order=random --error-on-warnings
- name: package information
run: |
echo "BINARY_NAME=ws" >> $GITHUB_ENV
echo "PKG_ARCH=x86_64" >> $GITHUB_ENV
echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: release binary
id: release
run: |
shards build --production --release --progress --static --no-debug --link-flags="-s -Wl,-z,relro,-z,now"
ASSERT_NAME=${BINARY_NAME}-${RELEASE_VERSION}-${PKG_ARCH}-unknown-linux-gnu.tar.gz
tar zcf ${ASSERT_NAME} bin/${BINARY_NAME} LICENSE
echo ::set-output name=ASSERT_NAME::${ASSERT_NAME}
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
${{ steps.release.outputs.ASSERT_NAME }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/lib/
/bin/
/.shards/

/ws
# Libraries don't need dependency lock
# Dependencies will be locked in application that uses them
/shard.lock
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,42 @@ Wordsmith::Inflector.foreign_key("Person") # "person_id"
Wordsmith::Inflector.parameterize("Admin/product") # "admin-product"
```

Wordsmith comes with a `ws` CLI utility which allows you to process words from the command line. You can download it directly from the [releases page](https://github.com/luckyframework/wordsmith/releases).

```sh
╰─ $ ./ws
Usage: ws <option> WORD

Wordsmith is a library for pluralizing, singularizing and doing
other fun and useful things with words.

Command `ws` is the command line version of Wordsmith, not all
features of Wordsmith are implemented, for precompiled binary,
please download from github releases page.

https://github.com/luckyframework/wordsmith/releases

some examples:

$: ws -s people # => person
$: ws -p person # => people

You can use it with pipe:

$: echo "WordSmith" |ws -u |ws -d # => word-smith

more examples, please check https://github.com/luckyframework/wordsmith#usage

-s WORD, --singularize=WORD Return the singular version of the word.
-p WORD, --pluralize=WORD Return the plural version of the word.
-c WORD, --camelize=WORD Return the camel-case version of that word.
-C WORD, --camelize-downcase=WORD
Return the camel-case version of that word, but the first letter not capitalized.
-u WORD, --underscore=WORD Convert a given camel-case word to it's underscored version.
-d WORD, --dasherize=WORD Convert a given underscore-separated word to the same word, separated by dashes.
-h, --help Show this help
```
## Custom inflections
If something isn't pluralizing correctly, it's easy to customize.
Expand Down
4 changes: 4 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ authors:
- Paul Smith <paulcsmith0218@gmail.com>
- Flinn Mueller <theflinnster@gmail.com>

targets:
ws:
main: src/ws.cr

crystal: ">= 1.4.0"

license: MIT
Expand Down
1 change: 0 additions & 1 deletion src/wordsmith.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "./wordsmith/**"
require "./wordsmith/inflector/**"

# Wordsmith is a library for pluralizing, ordinalizing, singularizing and doing other fun and useful things with words.
module Wordsmith
Expand Down
95 changes: 95 additions & 0 deletions src/ws.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require "option_parser"
require "colorize"
require "./wordsmith"

if STDIN.info.type.pipe?
ARGV << STDIN.gets.not_nil!
else
ARGV << "--help" if ARGV.empty?
end

OptionParser.parse do |parser|
parser.banner = <<-USAGE
Usage: ws <option> WORD
Wordsmith is a library for pluralizing, singularizing and doing
other fun and useful things with words.
Command `ws` is the command line version of Wordsmith, not all
features of Wordsmith are implemented, for precompiled binary,
please download from github releases page.
https://github.com/luckyframework/wordsmith/releases
some examples:
$: #{"ws -s people".colorize(:light_yellow)} # => person
$: #{"ws -p person".colorize(:light_yellow)} # => people
You can use it with pipe:
$: #{"echo \"WordSmith\" |ws -u |ws -d".colorize(:light_yellow)} # => word-smith
more examples, please check https://github.com/luckyframework/wordsmith#usage
USAGE

parser.on(
"-s WORD",
"--singularize=WORD",
"Return the singular version of the word."
) do |word|
puts Wordsmith::Inflector.singularize(word)
end

parser.on(
"-p WORD",
"--pluralize=WORD",
"Return the plural version of the word."
) do |word|
puts Wordsmith::Inflector.pluralize(word)
end

parser.on(
"-c WORD",
"--camelize=WORD",
"Return the camel-case version of that word."
) do |word|
puts Wordsmith::Inflector.camelize(word)
end

parser.on(
"-C WORD",
"--camelize-downcase=WORD",
"Return the camel-case version of that word, but the first letter not capitalized."
) do |word|
puts Wordsmith::Inflector.camelize(word)
end

parser.on(
"-u WORD",
"--underscore=WORD",
"Convert a given camel-case word to it's underscored version."
) do |word|
puts Wordsmith::Inflector.underscore(word)
end

parser.on(
"-d WORD",
"--dasherize=WORD",
"Convert a given underscore-separated word to the same word, separated by dashes."
) do |word|
puts Wordsmith::Inflector.dasherize(word)
end

parser.on("-h", "--help", "Show this help") do
puts parser
exit
end

parser.invalid_option do |flag|
STDERR.puts "ERROR: #{flag} is not a valid option.\n\n"
STDERR.puts parser
exit 1
end
end

0 comments on commit 438eef7

Please sign in to comment.