Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
bradymadden97 committed Jun 25, 2024
1 parent b28a1ce commit 2b6f8b4
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 1 deletion.
143 changes: 143 additions & 0 deletions pkgs/formatter/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{ pkgs, clang }:
pkgs.writeShellApplication {
name = "parse-formatter-options";
text = ''
#!/bin/bash
# Initialize variables
apply="false"
file=""
insertFinalNewline="false"
insertSpaces="false"
rangeStart=""
rangeEnd=""
stdinMode="false"
tabSize=""
trimFinalNewlines="false"
trimTrailingWhitespace="false"
# Function to print usage
print_usage() {
echo "Usage: $0 [-a|--apply] -f|--file <filename> [--stdin] [--range-start <offset>] [--range-end <offset>] [--tab-size <number>] [--insert-spaces] [--trim-trailing-whitespace] [--insert-final-newline] [--trim-final-newlines]"
echo
echo "Options:"
echo " -h, --help Show this help message and exit"
echo " -a, --apply Apply edits directly to the file (optional)"
echo " -f, --file <filename> Specify the file to be formatted (required)"
echo " --stdin Read file input from stdin (optional)"
echo " --insert-final-newline Insert a newline at the end of the file if one does not exist (optional)"
echo " --insert-spaces Prefer spaces over tabs (optional)"
echo " --range-start <offset> Specify the character offset for formatting (optional, requires --range-end)"
echo " --range-end <offset> Specify the character offset for formatting (optional, requires --range-start)"
echo " --tab-size <number> Size of a tab in spaces (optional)"
echo " --trim-final-newlines Trim all newlines after the final newline at the end of the file (optional)"
echo " --trim-trailing-whitespace Trim trailing whitespace on a line (optional)"
}
# Function to check if a value is a number
is_number() {
[[ "$1" =~ ^[0-9]+$ ]]
}
# Parse command-line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-a|--apply)
apply="true"
shift
;;
-f|--file)
file="$2"
shift 2
;;
-h|--help)
print_usage
exit 0
;;
--stdin)
stdinMode="true"
shift
;;
--range-start)
if is_number "$2"; then
rangeStart="$2"
else
echo "Error: --range-start must be a number."
print_usage
exit 1
fi
shift 2
;;
--range-end)
if is_number "$2"; then
rangeEnd="$2"
else
echo "Error: --range-end must be a number."
print_usage
exit 1
fi
shift 2
;;
--tab-size)
if is_number "$2"; then
tabSize="$2"
else
echo "Error: --tab-size must be a number."
print_usage
exit 1
fi
shift 2
;;
--insert-spaces)
insertSpaces="true"
shift
;;
--trim-trailing-whitespace)
trimTrailingWhitespace="true"
shift
;;
--insert-final-newline)
insertFinalNewline="true"
shift
;;
--trim-final-newlines)
trimFinalNewlines="true"
shift
;;
*)
echo "Unknown option: $1"
print_usage
exit 1
;;
esac
done
# Validate required arguments
if [[ -z "$file" ]]; then
echo "Error: File argument is required."
print_usage
exit 1
fi
# Further validate that both rangeStart and rangeEnd are provided together, if at all
if [[ -n "$rangeStart" && -z "$rangeEnd" ]] || [[ -z "$rangeStart" && -n "$rangeEnd" ]]; then
echo "Error: Both --range-start and --range-end must be provided together."
print_usage
exit 1
fi
# Export for use in sub-scripts
export apply
export file
export insertFinalNewline
export insertSpaces
export rangeStart
export rangeEnd
export stdinMode
export tabSize
export trimFinalNewlines
export trimTrailingWhitespace
'';
}
18 changes: 18 additions & 0 deletions pkgs/modules/go/gofmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Source the shared options parsing script
source ./base.sh "$@"

# Translate parsed arguments into gofmt options
gofmt_args=()

# Apply edit flag
if [[ "$apply" == "true" ]]; then
gofmt_args+=("-w")
fi

# Append the file path
gofmt_args+=("$file")

# Execute the command
gofmt "${gofmt_args[@]}"
52 changes: 51 additions & 1 deletion pkgs/modules/nodejs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,56 @@ let
${nodejs-wrapped}/bin/npx "$@"
'';
};

run-prettier = pkgs.writeShellApplication {
name = "run-prettier";
text = ''
#!/bin/bash
# Source the shared options parsing script
source ${pkgs.formatter}/bin/parse-formatter-options "$@"
# Translate parsed arguments into prettier options
prettier_args=()
# Apply edit flag
if [[ "$apply" == "true" ]]; then
prettier_args+=("--write")
fi
# Range options
if [[ -n "$rangeStart" && -n "$rangeEnd" ]]; then
prettier_args+=("--range-start" "$rangeStart" "--range-end" "$rangeEnd")
fi
# Tab size
if [[ -n "$tabSize" ]]; then
prettier_args+=("--tab-width" "$tabSize")
fi
# Insert spaces over tabs
if [[ "$insertSpaces" == "true" ]]; then
prettier_args+=("--use-tabs" "false")
else
prettier_args+=("--use-tabs" "true")
fi
# Read file content from stdin if stdinMode is enabled
if [[ "$stdinMode" == "true" ]]; then
prettier_args+=("--stdin-filepath")
fi
# Append the file path
prettier_args+=("$file")
# Execute the command
prettier "${prettier_args[@]}"
# Example display of constructed prettier command for debugging
echo "Command executed: prettier ${prettier_args[*]}"
'';
};

in

{
Expand Down Expand Up @@ -106,7 +156,7 @@ in
extensions = [ ".js" ".jsx" ".ts" ".tsx" ".json" ".html" ];
start = {
# Resolve to first prettier in path
args = [ "prettier" "--stdin-filepath" "$file" ];
args = [ "${run-prettier}" "--stdin-filepath" "-f" "$file" ];
};
stdin = true;
};
Expand Down

0 comments on commit 2b6f8b4

Please sign in to comment.