Compare commits
3 Commits
e351f2e924
...
f8a61e9290
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f8a61e9290 | ||
|
|
ef57d56b2c | ||
|
|
7a344b82dd |
64
.claude/rules/bash-style.md
Normal file
64
.claude/rules/bash-style.md
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
---
|
||||||
|
paths:
|
||||||
|
- "**/*.sh"
|
||||||
|
- "bashrc"
|
||||||
|
- "bin/*"
|
||||||
|
---
|
||||||
|
|
||||||
|
# Bash Readability Style
|
||||||
|
|
||||||
|
The primary goal is code that is **easy to understand at a glance**. Write bash that reads naturally, follows established conventions, and doesn't require the reader to puzzle over clever tricks. Favor clarity and well-known idioms over brevity.
|
||||||
|
|
||||||
|
Complements CLAUDE.md. Does not repeat rules already defined there.
|
||||||
|
|
||||||
|
## Formatting
|
||||||
|
|
||||||
|
- 2-space indentation, no tabs
|
||||||
|
- `then`/`do` on same line: `if cond; then` / `for x in list; do`
|
||||||
|
- Blank lines between logical sections; no blanks between tightly coupled statements
|
||||||
|
- Long pipe chains: one command per line with leading `|`
|
||||||
|
|
||||||
|
## Naming
|
||||||
|
|
||||||
|
- `UPPER_CASE` — exported env vars and constants
|
||||||
|
- `snake_case` — local variables and function names
|
||||||
|
- `verb_noun` pattern for functions: `set_ps1_prompt`, `docker_aws_login`
|
||||||
|
- Meaningful names, no single letters except loop counters (`i`, `f`)
|
||||||
|
|
||||||
|
## Functions
|
||||||
|
|
||||||
|
```bash
|
||||||
|
function do_thing() {
|
||||||
|
local name="$1"
|
||||||
|
local count="${2:-0}"
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- Always `function name() {` declaration style
|
||||||
|
- Always `local` for function variables, assigned on declaration line
|
||||||
|
- Name parameters immediately: `local filename="$1"` — no raw `$1` in logic
|
||||||
|
- Default values via `${VAR:-default}`
|
||||||
|
- Guard clauses with early `return` over deep nesting
|
||||||
|
- Keep functions focused — single responsibility
|
||||||
|
|
||||||
|
## Syntax Preferences
|
||||||
|
|
||||||
|
- `[[ ]]` over `[ ]` for conditionals
|
||||||
|
- `(( ))` for arithmetic comparisons
|
||||||
|
- `$(command)` for substitution, never backticks
|
||||||
|
- Double quotes around expansions: `"$var"`, `"${array[@]}"`
|
||||||
|
- Single quotes for literals that must not expand
|
||||||
|
- `printf` over `echo` when output contains escapes or format strings
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
- Long options for clarity: `--recursive` not `-r` (common test flags `-f`, `-d`, `-e` are fine)
|
||||||
|
- Redirect stderr: `> /dev/null 2>&1`
|
||||||
|
- `# shellcheck shell=bash` at top of sourced (non-executable) files
|
||||||
|
|
||||||
|
## Comments
|
||||||
|
|
||||||
|
- Minimal — explain *why*, not *what*
|
||||||
|
- URL reference when pattern comes from external source
|
||||||
|
- No banner/divider comments
|
||||||
@@ -7,6 +7,7 @@ insert_final_newline = true
|
|||||||
end_of_line = lf
|
end_of_line = lf
|
||||||
indent_size = 4
|
indent_size = 4
|
||||||
indent_style = space
|
indent_style = space
|
||||||
|
max_line_length = 120
|
||||||
|
|
||||||
[*.md]
|
[*.md]
|
||||||
trim_trailing_whitespace = false
|
trim_trailing_whitespace = false
|
||||||
@@ -14,7 +15,6 @@ indent_size = 2
|
|||||||
|
|
||||||
[*.{java,kt,kts}]
|
[*.{java,kt,kts}]
|
||||||
continuation_indent_size = 4
|
continuation_indent_size = 4
|
||||||
max_line_length = 120
|
|
||||||
wildcard_import_limit = 99
|
wildcard_import_limit = 99
|
||||||
|
|
||||||
[*.xml]
|
[*.xml]
|
||||||
|
|||||||
35
CLAUDE.md
35
CLAUDE.md
@@ -1,6 +1,7 @@
|
|||||||
# CLAUDE.md
|
# Linux Workspace
|
||||||
|
|
||||||
Dotfiles & workstation automation for Linux, macOS, WSL. `bashrc` loads `rc/*.sh` (ordered by prefix) and adds `bin/` to PATH. `$LWS` points to repo root.
|
Dotfiles & workstation automation for Linux, macOS, WSL. `bashrc` loads `rc/*.sh` (ordered by prefix)
|
||||||
|
and adds `bin/` to PATH. `$LWS` points to repo root.
|
||||||
|
|
||||||
## Directory Layout
|
## Directory Layout
|
||||||
|
|
||||||
@@ -9,19 +10,34 @@ Dotfiles & workstation automation for Linux, macOS, WSL. `bashrc` loads `rc/*.sh
|
|||||||
- `bin/` — Scripts auto-added to PATH. New CLI tools go here.
|
- `bin/` — Scripts auto-added to PATH. New CLI tools go here.
|
||||||
- `scripts/` — Setup scripts NOT on PATH (git-config, docker fixes, gnome config).
|
- `scripts/` — Setup scripts NOT on PATH (git-config, docker fixes, gnome config).
|
||||||
- `conf/` — Tool configs (vim, WireGuard, WSL).
|
- `conf/` — Tool configs (vim, WireGuard, WSL).
|
||||||
- `functions.sh` — Core lib: `append_path`, `prepend_path`, `set_uniq_path`, `source_directory_sh`, `is_fast_init`/`is_slow_init`, WSL detection, debug logging.
|
- `functions.sh` — Core lib (see below).
|
||||||
|
|
||||||
|
## Key Helpers (`functions.sh`)
|
||||||
|
|
||||||
|
- `can_run <cmd>` — check if command is available
|
||||||
|
- `is_slow_init` / `is_fast_init` — check `LWS_FAST` mode
|
||||||
|
- `is_interactive_shell` — true when `[[ $- == *i* ]]`
|
||||||
|
- `is_wsl` — detect WSL environment
|
||||||
|
- `true_false <val>` — normalize `1/true/yes` to boolean
|
||||||
|
- `prepend_path` / `append_path` / `prepend_path_try` / `append_path_try` — safe PATH manipulation
|
||||||
|
- `source_try <file>` — source file if it exists
|
||||||
|
- `source_directory_sh <dir>` — source all `*.sh` in a directory
|
||||||
|
- `xlog <msg>` — debug log (only in interactive + `LWS_DEBUG`)
|
||||||
|
|
||||||
## Conventions
|
## Conventions
|
||||||
|
|
||||||
All new bash scripts MUST use:
|
All new bash scripts should use:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
set -E -o errexit -o nounset -o pipefail
|
set -E -o errexit -o nounset -o pipefail
|
||||||
```
|
```
|
||||||
|
|
||||||
Before writing shell code, check `functions.sh` for existing helper functions (e.g. `can_run`, `append_path_try`, `source_try`) and prefer them over raw commands.
|
Before writing shell code, check `functions.sh` for existing helper functions and prefer them over raw commands.
|
||||||
|
|
||||||
RC files MUST guard slow tools and check availability:
|
RC files MUST guard slow tools and check availability:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
if is_slow_init && can_run pyenv; then
|
if is_slow_init && can_run pyenv; then
|
||||||
# initialize pyenv
|
# initialize pyenv
|
||||||
@@ -30,4 +46,11 @@ fi
|
|||||||
|
|
||||||
Installers (`install/*.sh`) MUST be idempotent — check before installing.
|
Installers (`install/*.sh`) MUST be idempotent — check before installing.
|
||||||
|
|
||||||
Env vars: `LWS_FAST=1` skips slow tools, `LWS_DEBUG=1` enables debug logging.
|
## Environment Variables
|
||||||
|
|
||||||
|
- `LWS_FAST=1` — skips slow tools during shell init
|
||||||
|
- `LWS_DEBUG=1` — enables debug logging via `xlog`
|
||||||
|
|
||||||
|
## Commits
|
||||||
|
|
||||||
|
Do not add `Co-Authored-By` lines to commit messages.
|
||||||
|
|||||||
7
bashrc
7
bashrc
@@ -7,13 +7,16 @@ export LWS=$WORKSPACE
|
|||||||
LWS_DEBUG=${LWS_DEBUG:-0}
|
LWS_DEBUG=${LWS_DEBUG:-0}
|
||||||
LWS_FAST=${LWS_FAST:-0}
|
LWS_FAST=${LWS_FAST:-0}
|
||||||
|
|
||||||
echo "Linux Workspace initialization"
|
source "$LWS/functions.sh"
|
||||||
|
|
||||||
if [[ -z $LC_ALL ]]; then
|
if [[ -z $LC_ALL ]]; then
|
||||||
export LC_ALL=en_US.UTF-8
|
export LC_ALL=en_US.UTF-8
|
||||||
fi
|
fi
|
||||||
|
|
||||||
source "$LWS/functions.sh"
|
if is_interactive_shell; then
|
||||||
|
echo "Linux Workspace initialization"
|
||||||
|
fi
|
||||||
|
|
||||||
source_directory_sh "$LWS/rc"
|
source_directory_sh "$LWS/rc"
|
||||||
source_directory_sh "$HOME/.bashrc.d"
|
source_directory_sh "$HOME/.bashrc.d"
|
||||||
|
|
||||||
|
|||||||
10
functions.sh
10
functions.sh
@@ -5,6 +5,10 @@ if [ -z "$time_ms" ]; then
|
|||||||
readonly time_ms='date +%s%3N'
|
readonly time_ms='date +%s%3N'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
function is_interactive_shell() {
|
||||||
|
[[ $- == *i* ]]
|
||||||
|
}
|
||||||
|
|
||||||
function is_fast_init() {
|
function is_fast_init() {
|
||||||
(( $LWS_FAST ))
|
(( $LWS_FAST ))
|
||||||
}
|
}
|
||||||
@@ -14,7 +18,7 @@ function is_slow_init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function xlog() {
|
function xlog() {
|
||||||
if true_false "$LWS_DEBUG"; then
|
if is_interactive_shell && true_false "$LWS_DEBUG"; then
|
||||||
echo "$@"
|
echo "$@"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
@@ -100,11 +104,11 @@ function source_directory() {
|
|||||||
xlog "LOAD DIR $mask"
|
xlog "LOAD DIR $mask"
|
||||||
for file in $mask; do
|
for file in $mask; do
|
||||||
if [[ -e "$file" ]]; then
|
if [[ -e "$file" ]]; then
|
||||||
if (( $LWS_DEBUG )); then
|
if is_interactive_shell && (( $LWS_DEBUG )); then
|
||||||
start=$($time_ms)
|
start=$($time_ms)
|
||||||
source "$file"
|
source "$file"
|
||||||
stop=$($time_ms)
|
stop=$($time_ms)
|
||||||
echo "LOAD FILE $file $((stop - start)) ms"
|
xlog "LOAD FILE $file $((stop - start)) ms"
|
||||||
else
|
else
|
||||||
source "$file"
|
source "$file"
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user